Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to EnterLong at market open (BackTest)

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

    How to EnterLong at market open (BackTest)

    Hi,
    I'm trying to submit an order at the first price that comes in at market opening:

    if (Times[0][0].TimeOfDay >= StartTime.TimeOfDay)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Entry");
    }

    However during the backtest this opens the trade one minute after the opening (at the beginning of the 2nd 1 minute bar),

    Is it not possible in the context of backtest to enter the trade at exactly my defined StartTime time (or at least 1 second later) ?

    Derjan

    #2
    Hi Derjan,
    Welcome to the forum.
    To my understanding, this is not possible. "OnBarUpdate" runs once per candle (only), both in backtest and live trading.
    If you want a faster execution, you have to change the primary data series to Ticks.
    NT still "only" fires at the NEXT tick. However, the delay is much shorter in absolute terms.
    To note that using ticks has a knock on effect on performance in complex scripts because the number of calcs to perform is significantly higher.
    NT-Roland

    Comment


      #3
      Hello Derjan, thanks for your post and welcome to the NinjaTrader forum.

      To submit an order at the very beginning of a session, change your strategy to run OnEachTick and check for the first bar of the session and the first tick of that bar

      e.g.

      Code:
      protected override void OnBarUpdate()
              {
                  if(Bars.IsFirstBarOfSession && IsFirstTickOfBar)
                  {
                      EnterLong();
                  }
      
                  if(Bars.IsLastBarOfSession)
                  {
                      if(Position.MarketPosition == MarketPosition.Long)
                          ExitLong();
                  }
              }


      Chris L.NinjaTrader Customer Service

      Comment


        #4
        Thanks Roland & Chris.
        @Chris: But for BackTesting I can't use this (based on ticks), as the smallest Resolution is one minute ?

        Comment


          #5
          Hello Derjan,

          Thanks for your message.

          In order to backtest with Calculate.OnEachTick or Calculate.OnPriceChange, Tick Replay must be used.

          Please note that order fill will still use the OHLC points of the data series that the order is submitted to and there will not be any intrabar movement to simulate the order fills even though Tick Replay is being used. In order to have the strategy process Calculate.OnEachTick/Calculate.OnPriceChange with Tick Replay and have intrabar fills, the strategy must submit orders to a single tick data series. Please see example below.

          Backtesting with intrabar granularity - https://ninjatrader.com/support/help...ipt_strate.htm

          Developing for Tick Replay - https://ninjatrader.com/support/help...ick_replay.htm

          Please let us know if we can be of further assistance.
          JimNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ljohnson91, Today, 10:38 AM
          1 response
          2 views
          0 likes
          Last Post NinjaTrader_ChristopherJ  
          Started by tonynt, Today, 10:15 AM
          0 responses
          10 views
          0 likes
          Last Post tonynt
          by tonynt
           
          Started by darthy, 08-21-2020, 03:39 PM
          2 responses
          25 views
          0 likes
          Last Post darthy
          by darthy
           
          Started by ballboy11, Today, 09:43 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by rene69851, 08-17-2020, 07:30 PM
          7 responses
          46 views
          0 likes
          Last Post NinjaTrader_Heath  
          Working...
          X