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

Enter a start of new candle

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

    Enter a start of new candle

    Hi,

    I have programmed my strategy which should check some conditions on bar closed (Minute). If they are verified, it should enter the market (EnterLong or EnterShort) at the first Tick of the new minute.
    When I try the strategy on backtesting data everything works fine.

    For example:
    - Check conditions at the close of the minute: 04:57:59
    - Enter to market (EnterLong): 04:58:00
    - Exit to market (ExitLong): 04:59:00

    When I try the strategy on realtime data ( or playback data), it is not good.
    - Check conditions at the close of the minute: 04:57:59
    - Enter to market (EnterLong): 04:58:01
    - Exit to market (ExitLong): 04:59:01

    I have read the guide on "Discrepancies: Real-Time vs Backtest" and I have tried to introduce bars of different granularity (Second or Tick), I have tried to calculate at each Tick, but nothing seems to work.

    There is a solution so that I can enter/exit at market at the first available Tick of the first second of the new minute?

    Thanks Albert

    PS: I attach the program
    Attached Files

    #2
    Hello Apo84,

    Thanks for your post.

    Your strategy is running with Calculate.OnBarClose and that means that when the bar closes (last tick of the bar) your strategy code is run. If the entry condition permit, the EnterLong() order is placed and can only be filled sometime in the next (currently forming) bar. Note: When using Calculate.OnBarClose (or running on historical data) IsFirstTickOfBar will be true when the bar closes.

    If you are trying to get it filled on the first tick on real-time data, that likely would not be possible given that it takes physical time for your order to be transmitted, received, placed, and filled by the exchange. The market order will, of course, be filled, but at the price convenience of the market. The best you can get here would be to submit the order on the first tick of the bar.

    "When I try the strategy on realtime data ( or playback data), it is not good.
    - Check conditions at the close of the minute: 04:57:59
    - Enter to market (EnterLong): 04:58:01
    - Exit to market (ExitLong): 04:59:01"


    Based on those values, it looks like you are getting your order filled within the first second of the bar. The actual entry time could be better as you are presenting with 1-second resolution.

    On Real time data, regarding this block of code:

    if ( IsFirstTickOfBar
    && Close[1]-Open[1]<0 && Close[2]-Open[2]<0 && Open[2]-Close[1]>=10
    && Close[0]-Open[0]>0 && Close[0]-Open[0]>15
    && control==0
    )


    I would suggest using Calculate.OnEachTick and then shift your bars ago references. The bars ago of [0], in OnEachTick, will point to the currently forming bar (which has just 1 tick in it) so you will need to shift back your bars ago references by 1 bar in order for your entry conditions to work as expected. (InOnBarClose mode AND on historical data, the bars ago of [0] points to the just closed bar so it is important to understand this difference)
    Reference: https://ninjatrader.com/support/help...?calculate.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the quick answer.

      Comparing the Enter-Exit values with those calculated in Matlab I believe that the different values are due to the time needed to enter the order.
      The discrepancies are on the order of half a second. For me they are acceptable.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by andrewtrades, Today, 04:57 PM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by chbruno, Today, 04:10 PM
      0 responses
      3 views
      0 likes
      Last Post chbruno
      by chbruno
       
      Started by josh18955, 03-25-2023, 11:16 AM
      6 responses
      436 views
      0 likes
      Last Post Delerium  
      Started by FAQtrader, Today, 03:35 PM
      0 responses
      6 views
      0 likes
      Last Post FAQtrader  
      Started by rocketman7, Today, 09:41 AM
      5 responses
      19 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X