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

Bars where trades occur in a back test are not the same as in Playback Market Replay

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

    Bars where trades occur in a back test are not the same as in Playback Market Replay

    I know we have discussed the discrepancies between a back test using OHLC and Playback using market replay data.

    I was running a simple strategy test to become familiar with the various components and have a strategy that goes long on three consecutive Up mars where the Hog is greater then the open, and exits on the first down bar, close less then the open. attached is the strategy builder code. In addition the first screen shot is the trades painted on the screen using historical data. The second ws the fills during a tick replay. As you can see for entry the historical fill occurs on the open of the bar following the third Up bar. In market replay the fill occurs three or 4 bars later.

    In particular I'm talking about the correct Sell @ 38120 to establish the short position after the third up bar the Sell price on the market replay
    is @ 38080 which the back test show as a $200 profit
    Same is true of the exits, the look correct in the historical trade but lagging for some reason in market replay.

    Thank you.

    glen
    Attached Files
    Last edited by demarcog; 02-11-2021, 05:24 PM.

    #2
    Hello demarcog,

    Thank you for your post.

    You should expect that a strategy running real-time (live brokerage account, live market simulation, Market Replay/Playback connection 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 or Renko) than others due to their inherent nature in bar formation.

    When using Historical data, Strategies can only be processed at the close of each bar. The fill price of an order is based on the OHLC of a bar and the price of the order itself since that is the only information that is known during a backtest.

    When using real-time market data or Market Replay/Playback you have a choice to run a strategy tick by tick (Calculate set to 'On Each Tick') which could produce different results. The fill price is based on incoming market data and volume, and you may receive better or worse fill prices depending on where the bid or ask price is and what volume is available at this market price. During real-time live brokerage trading, orders are filled according to market dynamics.

    This is why you may see orders not fill in real-time that you may otherwise expect to see filled based on your backtesting results.

    Here is a NinjaTrader Help Guide link with information about discrepancies between realtime and historical data — https://ninjatrader.com/support/help...ime_vs_bac.htm

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      HI BrandonH,

      Thank for the response, I can understand the prices on the same bar as an historical OHCL bar but this was a market order, I can see a price difference in filled price for the same bar. Wouldn't a market order have gotten filled perhaps at a worse price but some price at the bar. If I go back and replay I'm sire for every bar there is a bid and offer so shouldn't in that case the order have been filled on the same price bar?

      BTW I there anyway of having the order execute on the last tick of bar[0] rather then the first tick of bar[1].?


      thank you.

      glen

      Comment


        #4
        Hello demarcog,

        Thank you for your note.

        When running a backtest strategies are only able to process at the close of each bar. Orders are filled based on the OHLC of a bar since that is the only information known when running a backtest. Orders cannot be filled based on the Bid/Ask price when running a backtest since that information is unknown.

        When using Market Replay/Playback the fill price is based on incoming market data and volume, and you may receive better or worse fill prices depending on where the Bid or Ask price is and what volume is available at this market price.

        Since backtests fill orders based on the OHLC and Market Replay/Playback fills orders based on the Bid/Ask price and Volume, you may not see orders placed real-time (Market Replay/Playback) that you would otherwise expect to see filled based on your backtesting results regardless of the order type.

        Let us know if we may assist further
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          HI Brandon,

          Thanks for the reply understood. But that would assume there were no bid/offers for the prior three bars which I find highly unlikely.

          The other question is is there anyway of sending an order that will execute on the last tick of bar[0] rather then the first tick of the next bar bar[1].

          thank you.

          glen

          Comment


            #6
            Hello demarcog,

            Thank you for your reply.

            During a backtest the Close price of the evaluated bar is substituted for the Bid/Ask price. This is why the Bid/Ask values are unknown and orders are not filled based on the Bid/Ask price when running a backtest on a strategy. See the notes section in the help guide links below.

            GetCurrentBid - https://ninjatrader.com/support/help...currentbid.htm
            GetCurrentAsk - https://ninjatrader.com/support/help...currentask.htm

            Running a strategy real-time (Live, Market Replay/Playback connection, etc.) will fill orders based on the Bid/Ask prices and Volume. Since orders will be filled using the Close price during a backtest and orders will fill using the Bid/Ask prices when running a strategy real-time, you will see a difference in trades placed when comparing the two.

            Using a Calculate mode of OnBarClose will process your strategy at the close of a bar. However, you may see the order marker placed at the beginning of the next bar. There is no documented or supported means for having the order marker show at the close of the bar the order is filled vs the next bar.

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by terofs, Yesterday, 04:18 PM
            1 response
            21 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by CommonWhale, Today, 09:55 AM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by Gerik, Today, 09:40 AM
            2 responses
            7 views
            0 likes
            Last Post Gerik
            by Gerik
             
            Started by RookieTrader, Today, 09:37 AM
            2 responses
            12 views
            0 likes
            Last Post RookieTrader  
            Started by alifarahani, Today, 09:40 AM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X