Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Discrepancy in historical simulation wrt date/time

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

    Discrepancy in historical simulation wrt date/time

    This is an odd one. Please see the image below. I'm running a strategy simulation on historical data. I am running my chart and data on days. I'm stepping through the data with Microsoft VisualStudio's debugger, which I'm very familiar with. I've set a break for the bar of 7/29/2021 immediately after IsFirstTickOfBar(). That's the green bar just to the left of the big red bar with the Long Entry trade. At that time I have an analyzer specify buy or sell prices to get into the market based on the previous day's (7/28) bar. I check the prices of the day before and they are as expected. I place a buy order and it is filled and the fill date states 7/29/2021 12:20 pm. So far so good. Within OnExecutionUpdate() I see the order filled and set a stop. I don't expect to see that stop filled on that day because the stop is placed below the previous bar, which is way below the green bar at 7/29. But to my surprise it does get filled which I see in OnOrderUpdate(). The date in the execution order still says 7/29. When I see the results I see as in the image below that the order was placed and filled on the 7/30 bar. Indeed, the buy and stop prices that are illustrated fit that bar rather than the one before. I see this behavior is consistently true throughout the simulation. Is this a feature of IsFirstTIckOfBar() when using historical data? It acts like it's actually at the end of day for that bar. Can someone please explain what I'm seeing and why?

    Click image for larger version

Name:	Picture1.png
Views:	149
Size:	36.5 KB
ID:	1208350

    #2
    Hello dweems,

    Thank you for your post.

    You should expect that a strategy running real-time (live brokerage account, live market simulation, Market Replay etc...) will produce different results than the performance results generated during a backtest using historical data. This difference may be more easily seen on certain Bars types (e.g. Point and Figure) than others due to their inherent nature in bar formation.

    During a backtest you can select conservative or liberal fill algorithms which will produce different results. Fills are determined based on 4 data points, OHLC of a bar since that is the only information that is known during a backtest and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known, and IsFirstTickOfBar will always return true.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick'.

    Also, here is a link to the differences on real-time vs backtest (historical).
    http://ninjatrader.com/support/helpG...ime_vs_bac.htm

    Adding intra-bar granularity can help with this.

    Intra-bar granularity adds a second data series such as a 1 tick series so that the strategy has finer granularity in the historical data in between the OHLC of the primary series. This allows for more accurate trades by supplying the correct price at the correct time for the order to fill with.

    In NinjaTrader 8, there have been two new enhancements so that programmers do not have to manually add this secondary series and code the script to for high accuracy fills (Order Fill Resolution) and for intra-bar actions (TickReplay).

    Here is a link to our forums that goes into depth on using Order Fill Resolution and Tick Replay to ensure your backtests are as close to real time results as possible:

    https://ninjatrader.com/support/foru...mance?t=102504

    High Fill Order Resolution and TickReplay cannot be used together. If it is necessary to have both, it is still possible to add intra-bar granularity to a script in the code itself for order fill accuracy and use TickReplay to update indicators with Calculate set to OnPriceChange or OnEachTick historically.

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Thanks NinjaTrader_Kate. I'll look into the adding the second data series. That sounds like it would be much clearer.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      59 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      74 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X