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();
                  }
              }


      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.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Mindset, 04-21-2026, 06:46 AM
          0 responses
          88 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by M4ndoo, 04-20-2026, 05:21 PM
          0 responses
          135 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by M4ndoo, 04-19-2026, 05:54 PM
          0 responses
          68 views
          0 likes
          Last Post M4ndoo
          by M4ndoo
           
          Started by cmoran13, 04-16-2026, 01:02 PM
          0 responses
          119 views
          0 likes
          Last Post cmoran13  
          Started by PaulMohn, 04-10-2026, 11:11 AM
          0 responses
          69 views
          0 likes
          Last Post PaulMohn  
          Working...
          X