Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Two instruments trading on same day gives an error

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

    Two instruments trading on same day gives an error

    Hi

    I have a strategy with 2 (sometimes 3) instruments. All instruments use daily bars. I execute onbarupdate only for instrument==0.

    My strategy e.g. goes long with instrument 1. Then on day X the position is closed and same day enterlong for instrument2 is called. If I do this, then I get this error:

    -------------------
    **NT** Error on calling 'OnBarUpdate' method for strategy 'mystrategy':
    c # The object reference was not set to an object instance.
    -------------

    If I close position of instrument1 on day X, and only open position of instrument2 one day later, all works fine. What must I adjust to be able to execute trades with both instruments on same day(bar)?

    #2
    Hello Beauregard,

    Thank you for your post.

    Is the strategy calculating OnBarClose?

    Would you be able to provide an example of your entry/exit logic?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      hi Kate

      I see that in the code i set CalculateOnBarClose = false, however I am backtesting. And according your manual OnBarUpdate is only called o the close of each historical bar.
      When checking for this I saw that in the backtest parameters "Entries per Direction=1". I set it now to 2, and now I have no error.
      Does this mean, that closing/opening needs two entries in the direction?

      I remember in an earlier test I set this in the code, however this did not work. How can I set this directly in the code?

      Comment


        #4
        Hello Beauregard,

        Thank you for your reply.

        I'd need an example of your entry/exit logic to tell you why you're getting this error in backtesting and why changing Entries per Direction to 2 fixes it. I'd be happy to comment on that further if you include a code snippet with your reply.

        You can set a default for EntriesPerDirection in OnStateChange when State == State.SetDefaults. It's generally set to 1, but you can specify any limit you like.

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          hi Kate
          Thanks for your feedback.
          About the first point:
          I have in "OnBarUpdate" this code
          Code:
          if ((Time[0].Month==1) && ( Time[0].Month != Time[1].Month) )
                              {
          
                                  //Exitlong Instrument1
          
                                      // Exitlong Instrument1    
                                      ExitLong(0,1,"exit instr1","instr1order");
          
                                  }
                              }
          
                              if ((Time[0].Month==1) && ( Time[0].Month != Time[1].Month) )
                              {
          
                                  // Enter Long Instrument2
          
                                  EnterOrderName="instr2order";
          
                                  if (direction==0 || direction==1)
                                  {
                                      CurrentOrder=EnterLong(1,1,EnterOrderName);
                                      CurrentOrderToken=CurrentOrder.Token;
                                  }
          
          
                              }
          
          
          
          
                              if ((Time[0].Month==8) && ( Time[0].Month != Time[1].Month) )
                              {
                                  // Exitlong Instrument2    
                                  if (Positions[1].MarketPosition == MarketPosition.Long)
                                  {
                                      ExitLong(1,1,"exit instr1","instr2order");
          
                                  }
          
          
                              }
                              if ((Time[0].Month==8) && ( Time[0].Month != Time[1].Month) )
                              {
                                  // Enter Long Instrument1
          
                                  EnterOrderName="instr1order";
          
                                  if (direction==0 || direction==1)
                                  {
                                      CurrentOrder=EnterLong(0,1,EnterOrderName);
                                      CurrentOrderToken=CurrentOrder.Token;
                                  }
          
          
                              }
          about point2: How exaclty can I set EntriesperDirection=2? As I undestand this should done in the Init Routine?

          Comment


            #6
            Hello Beauregard,

            Thanks for your reply.

            It looks like you're running into that issue because you're exiting an long order and entering a long order on the same bar. The exit order for the first long order hasn't actually executed when you try to enter the long order on the secondary instrument, so you've got two orders in the same direction at once for a second before the first order actually executes.

            To set EntriesPerDirection=2; simply add that to your Initialize Method:



            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              thanks! all clear now

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DayTradingDEMON, Today, 09:28 AM
              4 responses
              21 views
              0 likes
              Last Post DayTradingDEMON  
              Started by geddyisodin, Yesterday, 05:20 AM
              9 responses
              50 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by George21, Today, 10:07 AM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_ChristopherJ  
              Started by Stanfillirenfro, Today, 07:23 AM
              9 responses
              24 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by navyguy06, Today, 09:28 AM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X