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 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 gentlebenthebear, Today, 01:30 AM
          0 responses
          2 views
          0 likes
          Last Post gentlebenthebear  
          Started by samish18, Yesterday, 08:31 AM
          2 responses
          9 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by Mestor, 03-10-2023, 01:50 AM
          16 responses
          389 views
          0 likes
          Last Post z.franck  
          Started by rtwave, 04-12-2024, 09:30 AM
          4 responses
          31 views
          0 likes
          Last Post rtwave
          by rtwave
           
          Started by yertle, Yesterday, 08:38 AM
          7 responses
          30 views
          0 likes
          Last Post yertle
          by yertle
           
          Working...
          X