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

Multi Time Frame order query

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

    Multi Time Frame order query

    Hi NT Support,
    I need some assistance developing a MTF strategy.

    For simplicity assume my primary bar series is 60 min and my secondary bar series is 1 min. I need the strategy to check for a SMA cross over on the primary bar series and then enter a long limit order on the secondary bar series. However the limit order level will be determined by the primary bar series, meaning that Close[0], high[0] and Low[0] are the ones of the 60min bar series.

    Is the code below correct? yes/no

    Code:
         protected override void Initialize()
         {
    			Add(PeriodType.Minute, 1)
    }
    protected override void OnBarUpdate()
    {
    if (BarsInProgress == 0)
    		{
    			if (CrossAbove(SMA(10),SMA(50),1))
    		{
    		EnterLongLimit(1,Close[0]-0.35(High[0]-Low[0]));
    	}
    }
    }
    In particular:
    1 - I am not sure that the EnterLongLimit is coded to do as described above;
    2 - I am not sure if the EnterLongLimit will be running for only 1 minute, or for the entire 60 min during which the SMA crossover has occured on the primary bar series
    Last edited by sburtt; 05-06-2013, 02:56 AM.

    #2
    sburtt, you submit the order in primary context to the added series meaning the Close price referenced comes from your primary seires here. However it's not submitted with liveUntilCancelled set, so I would expect it to expire on the next bar update seen i.e your next 1 minute bar.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      [...]However it's not submitted with liveUntilCancelled set, so I would expect it to expire on the next bar update seen i.e your next 1 minute bar.
      Bertrand, please could you further elaborate on this. I am not sure what you mean exactly. would you be able to indicate me how to modify the script in order to have the limit order live for the entire 60min session?

      p.s. please not I am using the managed approach, I am not using IOrder method.

      Thanks for your help
      Last edited by sburtt; 05-06-2013, 03:55 AM.

      Comment


        #4
        sburtt, you would need to use the advanced approach using IOrders here, as otherwise you could not CancelOrder then as desired for your liveUntilCancelled order -





        You can also resubmit the expired order if not filled, but this would not be advantageous for your position in the queue.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          could this work in my case?
          EnterLongLimit(1, true, DefaultQuantity, Close[0] - 0.35* (High[0] - Low[0]), "");
          if Yes. At the moment in my script there is no CancelOrder(), hence how exactly will this limit order get cancelled? browsing on the NT7 help page I find this description:

          liveUntilCancelleD = The order will NOT expire at the end of a bar, but instead remain live until the CancelOrder() method is called or its time in force has been reached.

          what eaxtly does "time in force has been reached" mean in the case i've described at the beginning?

          Comment


            #6
            If you don't cancel it would be taken care of by the default end of session handling then, determined as well by which TIF setting you would use -

            BertrandNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              If you don't cancel it would be taken care of by the default end of session handling then, determined as well by which TIF setting you would use -

              http://www.ninjatrader.com/support/h...ub=TimeInforce
              so basically something like this should work if I need the order to be cancelled after 60 bars of the secondary bar series?

              Code:
              private IOrder myEntryOrder = null;
              private int barNumberOfOrder = 0;
               
              protected override void OnBarUpdate()
              {    
                 if (myEntryOrder == null && CrossAbove(SMA(10),SMA(50),1))
                      {
                       myEntryOrder = EnterLongLimit((1, true, DefaultQuantity, Close[0] - 0.35* (High[0] -   
                       Low[0]), "Long Entry");
                       barNumberOfOrder = CurrentBar;
                       }
                 if (CurrentBar > barNumberOfOrder + 59)
                       CancelOrder(myEntryOrder);
              }
              Last edited by sburtt; 05-06-2013, 06:30 AM.

              Comment


                #8
                sburtt, I would not expect that to work like you envision, as the CurrentBar count you store and access would be for the primary series. For example store the CurrentBars[1] value and then cancel order in your one minute series in BarsInProgress 1.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  sburtt, I would not expect that to work like you envision, as the CurrentBar count you store and access would be for the primary series. For example store the CurrentBars[1] value and then cancel order in your one minute series in BarsInProgress 1.
                  I think I understand what you mean, something like this maybe. However, It's way to advanced given my superficial programming skill set. Do you know if there is availabe on line some kind of library with multiple example of strategies using IOrders, in order for me to study?

                  Code:
                  private IOrder myEntryOrder = null;
                  private int barNumberOfOrder = 0;
                   
                  protected override void OnBarUpdate()
                  {    
                  if(BarsInProgress == 0)
                  {
                     if (myEntryOrder == null && CrossAbove(SMA(10),SMA(50),1))
                          {
                           myEntryOrder = EnterLongLimit((1, true, DefaultQuantity, Close[0] - 0.35* (High[0] -   
                           Low[0]), "Long Entry");
                           barNumberOfOrder = BarsArray[1].CurrentBar;
                           }
                  }
                  if(BarsInProgress ==1)
                  {
                     if (BarsArray[1].CurrentBar > barNumberOfOrder + 59)
                           CancelOrder(myEntryOrder);
                  }
                  }

                  Comment


                    #10
                    Looks good sburtt, only item which you need to in is the reset of the IOrder object when it reaches a terminal state (so would be filled or cancelled in your example). Our reference sample here would show how this is done - http://www.ninjatrader.com/support/f...ead.php?t=7499
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Bertrand,

                      I think i understand what you mean. I think this should work, however when i run the script in Strategy analyzer I end up with no trades, whilst when I run the same strategy coded for the managed approach I get many trades. this is just a test, but I am trying to figure out what I am doing wrong. Your advise is welcome:

                      Code:
                          public class aIOrder : Strategy
                          {
                              #region Variables
                           	
                      		private IOrder entryOrder = null;
                      		private int barNumberOfOrder = 0;
                      
                              #endregion
                      
                              protected override void Initialize()
                              {
                                  CalculateOnBarClose = true;
                      			ExitOnClose = false;
                      		}
                              
                              protected override void OnBarUpdate()
                              {
                      			
                      //Enter long
                      if (entryOrder == null && CrossAbove())
                       {
                          entryOrder = EnterLongLimit(0, true, DefaultQuantity, Close[0] - 0.35 * (High[0] -     
                           Low[0]), "Long Entry");
                           barNumberOfOrder = CurrentBar;
                        }
                      				
                      //Enter short
                      if (entryOrder == null && CrossBelow())
                        {
                            entryOrder = EnterShortLimit(0, true, DefaultQuantity, Close[0] + 0.35 * (High[0] -   
                             Low[0]),"Short Entry");
                      	barNumberOfOrder = CurrentBar;
                         }
                      							
                      	if(CurrentBar > barNumberOfOrder +1)
                      		{
                      			CancelOrder(entryOrder);
                      		}
                      										
                      //Exit long
                      if (Position.MarketPosition == MarketPosition.Long && CrossBelow())
                      {
                      	ExitLong(0,DefaultQuantity, "Exit Long On Cross", "Long Entry");
                       }
                      				
                      //Exit short
                      if (Position.MarketPosition == MarketPosition.Short && CrossAbove())
                       {
                      	ExitShort(0,DefaultQuantity, "Exit Short On Cross", "Short Entry");
                        }
                      				
                      		
                      protected override void OnOrderUpdate(IOrder order)
                      		{
                      			if (entryOrder != null && entryOrder == order)
                      			{
                      				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                      				{
                      					entryOrder = null;
                      				}
                      			}
                      		}
                      		        
                      protected override void OnExecution(IExecution execution)
                      		{
                      			if (entryOrder != null && entryOrder == execution.Order)
                      			{
                      				if (execution.Order.OrderState != OrderState.PartFilled)
                      					{
                      						entryOrder 	= null;
                      					}
                      			}
                      		}
                      			
                          }
                      }

                      Comment


                        #12
                        sburtt, do it matter how many bars you give it then for filling your limit order? This could be too hard to produce any fills.

                        if(CurrentBar > barNumberOfOrder +1)
                        {
                        CancelOrder(entryOrder);
                        }

                        Also it would be good printing this entry price number to see if that calculates out to what you would expect then - Close[0] - 0.35 * (High[0] - Low[0])
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          sburtt, do it matter how many bars you give it then for filling your limit order? This could be too hard to produce any fills.

                          if(CurrentBar > barNumberOfOrder +1)
                          {
                          CancelOrder(entryOrder);
                          }

                          Also it would be good printing this entry price number to see if that calculates out to what you would expect then - Close[0] - 0.35 * (High[0] - Low[0])
                          Bertrade. It actually does matter. I want the strategy to enter long/short position via limit orders ONLY on the bar the cross over actually occurs. De facto I am running this strategy on 120min charts, hence I should have 120 min to get the order filled once the cross over occurs, and that will be at a limit price given by a 35% retracement of the previous high-low range.

                          The problem i am having is that the code reported in my previous post is not working properly. i get the following error message in the Output window:

                          02/01/2013 22:00:00 Entered internal PlaceOrder() method at 02/01/2013 22:00:00: BarsInProgress=0 Action=SellShort OrderType=Limit Quantity=1 LimitPrice=115.30 StopPrice=0 SignalName='' FromEntrySignal=''
                          03/01/2013 00:00:00 Cancelled order due to end of session handling: BarsInProgress=0: Order='NT-00000/Backtest' Name='Sell short' State=Working Instrument='$EURJPY' Action=SellShort Limit price=115.3 Stop price=0 Quantity=1 Strategy='aIOrder' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='3a0394337a0242159a925dd446cde9cb' Gtd='01/12/2099 00:00:00'
                          **NT** Error on calling 'OnBarUpdate' method for strategy 'aIOrder/6d9f0221c5754494a078c5e1f30bc502': Object reference not set to an instance of an object.

                          Comment


                            #14
                            Thanks. From the error received it look like you miss a check for null when you access an IOrder object, likely this is the part that's giving you the issue - so would change to and retest :

                            if(CurrentBar > barNumberOfOrder +1 && entryOrder != null)
                            {
                            CancelOrder(entryOrder);
                            }
                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Bertrade, this actually works, no issues anymore. Thanks

                              Just 1 last query: why in the multiple examples in the NinjaScript Help page this part of code is never shown? I refer to && entryOrder != null, all examples are reported this way:

                              if(CurrentBar > barNumberOfOrder +1)
                              {
                              CancelOrder(entryOrder);
                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by geddyisodin, 04-25-2024, 05:20 AM
                              8 responses
                              60 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by jxs_xrj, 01-12-2020, 09:49 AM
                              4 responses
                              3,287 views
                              1 like
                              Last Post jgualdronc  
                              Started by Option Whisperer, Today, 09:55 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Option Whisperer  
                              Started by halgo_boulder, 04-20-2024, 08:44 AM
                              2 responses
                              22 views
                              0 likes
                              Last Post halgo_boulder  
                              Started by mishhh, 05-25-2010, 08:54 AM
                              19 responses
                              6,189 views
                              0 likes
                              Last Post rene69851  
                              Working...
                              X