Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to not entry when closing?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to not entry when closing?

    How to not entry when closing?

    I think when the conditions of closing and opening are met about the same time,
    it can cause some confusion with the quantity of exiting "Position.Quantity".

    What can I do to prevent that from happening?
    What are your suggestions?
    What is the best course of action?

    Pause entry during exiting?
    or
    Pause after every entry?
    or
    Not entry when exiting?
    or
    How much time to pause?
    or
    etc?

    i.e.
    Both under protected override void OnBarUpdate()

    Opening
    Code:
    if             (
                    (Open[0]-Low[0] >= 0.00001)
                    )
                {
                    EnterLong(0,Quantity,"EnterLong");
                }
    Closing
    Code:
    if             (
                    Profit>= ProfitTarget
                    )                             
                {                        
                    ExitShort(0,Position.Quantity,"EXIT SHORT EURUSD (ProfitTarget)","");
                }

    #2
    Hello johnnybegoode,

    Thanks for your post.

    You can check that the MarketPosition is correct for your operation. For example, in the entry condition, you can check for a flat market position.

    Reference: https://ninjatrader.com/support/help...etposition.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I might have misdiagnosed the problem.

      I would like Strategy 1 (trading Account 101) and Strategy 2 (trading Account 102)
      to close all their positions when the profit of the combined Account 101 and 102 hit the profit target.

      They do most of the time, but there are times that only one of two strategies close all
      and the other strategy just does not. I would like to eliminate that happening.

      I thought that it was because the entry and exit were too close, but recent observations have shown otherwise.

      It might be because Strategy 1 closes all its positions before Strategy 2 or vice versa realizes "Profit >= ProfitTarget"
      causing the combination to be no longer "Profit >= ProfitTarget" when Strategy 2 check for it.


      Code:
                  Account a = Account.All.First(t => t.Name == "Sim101"); // Account Display Name
                  double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar);
                  double UnrealizedPnlA = a.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar); 
      
                  Account b = Account.All.First(t => t.Name == "Sim102"); 
                  double BalanceB =  b.Get(AccountItem.CashValue, Currency.UsDollar);
                  double UnrealizedPnlB = b.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
      Code:
      double Profit = 
                      (
                      UnrealizedPnlA 
                       +
                      UnrealizedPnlB
                      );
      Strategy 2 (Strategy 1 is the reverse)
      Code:
      if              (
                      Profit>= ProfitTarget
                      )                             
                  {                        
                      ExitShort(0,Position.Quantity,"EXIT SHORT EURUSD (ProfitTarget)","");
                  }

      Comment


        #4
        Hello johnnybegoode,

        Thanks for your reply.

        In that case, you would want to use print statements to debug what is happening.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          In this case, how to print statement on the closing when there is no error in log?

          Code:
          if         
          (
          Profit>= ProfitTarget
          )                             
          
          {
          ExitShort(0,Position.Quantity,"EXIT SHORT EURUSD (ProfitTarget)","");
          }

          Comment


            #6
            Hello johnnybegoode,

            Thanks for your reply.

            You do not need an error for debugging.

            You would want to use print statements in your code so that you can see why/when your strategy is not hitting the exit condition (The condition that is evaluated and determines to exit or not). You would need to, I think, add print statements in both instances to understand the sequence.

            Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              What is the sampling rate for OnBarUpdate()?

              There is a lag with NT8 when checking Account value.
              How long does it take?

              Code:
              Account a = Account.All.First(t => t.Name == "Sim101"); // Account Display Name
              double Balance = a.Get(AccountItem.CashValue, Currency.UsDollar);
              double UnrealizedPnlA = a.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
              Maybe I could execute the exit after the strategy has detected that the Account value
              by pausing for a few seconds?

              Comment


                #8
                Hello johnnybegoode,

                Thanks for your reply.

                When OnBarUpdate() is called, with live (or market replay) data, depends on what you have set for the strategy's Calculate mode.

                Calculate.OnbarClose would be once per bar and the frequency would depend on the bar type (IE a price based bar (Tick, Renko, Volume or time-based bar, 1 minute, 5 minutes, etc.).

                Calculate.OnPriceChange would be independent of bar type and would call OnbarUpdate() when the incoming tick changes the price from the previous tick(s).

                Calculate.OnEachTick would be independent of bar type and would call OnBarUpdate() on every incoming tick.

                Historical data would be calculated as if Calculate.OnBarClose was employed.
                Last edited by NinjaTrader_PaulH; 05-14-2019, 12:46 PM. Reason: Changed line spacing for readability.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  I think that "Position.Quantity" gets confused under the Executions tab
                  - (Look at the Position Column of the attached screenshot).

                  This might or might not cause the problem of closing.
                  Attached Files
                  Last edited by johnnybegoode; 05-14-2019, 04:27 PM.

                  Comment


                    #10
                    Hello johnnybegoode,

                    Thanks for your reply.

                    We recommend the use of print statements in your code to help debug.
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by zstheorist, Today, 07:52 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post zstheorist  
                    Started by pmachiraju, 11-01-2023, 04:46 AM
                    8 responses
                    150 views
                    0 likes
                    Last Post rehmans
                    by rehmans
                     
                    Started by mattbsea, Today, 05:44 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post mattbsea  
                    Started by RideMe, 04-07-2024, 04:54 PM
                    6 responses
                    33 views
                    0 likes
                    Last Post RideMe
                    by RideMe
                     
                    Started by tkaboris, Today, 05:13 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post tkaboris  
                    Working...
                    X