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

Why backtest orders cannot apply to current close of a bar or next open bar ?

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

    Why backtest orders cannot apply to current close of a bar or next open bar ?

    Good Day to the support team. I hope you are all doing great.

    I am proramming a strategy which uses a specific session (trading hours) from 8 AM to 7 PM.
    My strategie is based on daily (modified with the new trading hours) indicators but I want to backtest it on minute bars.
    So I added the minute chart to my daily bars (minutes / 1440).

    At 7 PM for each day, I know exactly what I will need to do on the next day.

    Buy the high of the current day on stop
    Sell the low of the current day on stop

    So at 7 PM (of the secondary bar), I send an unmanaged order such as:

    SubmitOrderUnmanaged(1, OrderAction.Buy, OrderType.StopMarket, positionSize, 0, StopPrice, orderString, nameOrder)

    Unfortunately, This does not work as I would have expected ...

    if the StopPrice is included in the current bar then the order is not accepted.

    I would have loved to create the order on the first bar of the next day, but then the order cannot be filled on the opening if the condition is met (stop should be triggered) since the order only starts on the next bar...

    Could you help me understand why the backtesting does not allow me to execute in the current bar or does not accept an order which is for the next bar ?

    When we send an unmanaged order, are we pre open, post open but pre close, post close ?

    Thanks


    #2
    I would like to upvote this. I place order in on position update. It detects a short or long position fill and places a stop order immediately. This should be legal. Can we please put in a feature request to take into consideration stoploss on the same bar as entry even though the property is on bar close. THis would enable the async calls like on position update on order update etc. Thank you.

    Comment


      #3
      Hello guilhem and sgkcfc,

      Thank you for your posts.

      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. 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 (such as placing a stop) 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.

      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'. This is expected.

      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. One way you can do this is by submitting your orders to a secondary, more granular series by specifically coding that logic. Here's an example from our help guide that shows how to submit orders to a secondary series with the Managed Approach. This could be adapted to the Unmanaged Approach as well:



      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.
      Kate W.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by llanqui, Today, 03:53 AM
      0 responses
      6 views
      0 likes
      Last Post llanqui
      by llanqui
       
      Started by burtoninlondon, Today, 12:38 AM
      0 responses
      10 views
      0 likes
      Last Post burtoninlondon  
      Started by AaronKoRn, Yesterday, 09:49 PM
      0 responses
      15 views
      0 likes
      Last Post AaronKoRn  
      Started by carnitron, Yesterday, 08:42 PM
      0 responses
      11 views
      0 likes
      Last Post carnitron  
      Started by strategist007, Yesterday, 07:51 PM
      0 responses
      14 views
      0 likes
      Last Post strategist007  
      Working...
      X