Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close on open bar misleading backtesting

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

    Close on open bar misleading backtesting

    I'm using unmanaged trading and I submit and order which get's filled on the following bar, I also have a OnExecution which after that order is filled I place the stop and limit orders. The problem is that the close order is being filled on the same bar as the open in some cases. For example

    Long buy:
    fills at 100.00
    stop entered at 99, limit at 101
    limit filled at 101 on the same bar as the position opened.

    In some cases this is fine since the bar open and was filled near the lower range of the bar and hit 100 before the price moved to 101 and closed above moved through 101 as the bar was being created.

    But in a case where the bar opened at 101.5, moved down and closed at 99.5 both the sell and the buy orders were shown to be triggered even though the price never saw 101 after it hit 100.

    I understand this is a problem with backtesting in a strategy, since it trades correctly in Market Reply. And Strategy Analyzer doesn't know the price history, just the OHLC.

    My question is, what change can I make to make the test results in the Strategy Analyzer to more accurately reflect the actual trade?

    #2
    Hello,

    Thank you for your post.

    I wrote a short strategy to test this but I am not able to reproduce your results.

    If the stop or limit is not within the bar I am not getting a fill.

    May I confirm the issue is that you are getting orders filled outside of the bar?

    Attached is the test strategy I created to test this.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      No the fill is in the bar, it just isn't possible in real time because the sequence that the buy/sell occurred and the direction the price moved. If the open price is hit first in real time and the price towards the close real time, what it represents is what really could happen, but if the price moves through the close price before it moves through the open price on the same bar, it still shows the bar with a filled open/close and completes the trade on the same bar. See attached image
      Attached Files

      Comment


        #4
        Originally posted by dynoweb View Post
        No the fill is in the bar, it just isn't possible in real time because the sequence that the buy/sell occurred and the direction the price moved. If the open price is hit first in real time and the price towards the close real time, what it represents is what really could happen, but if the price moves through the close price before it moves through the open price on the same bar, it still shows the bar with a filled open/close and completes the trade on the same bar. See attached image
        That is because NT, correctly IMHO, is using the worst-case scenario. If you want to see the intrabar action, you will have to use a multi-timeframe script, with a smaller time frame gating your executions.

        Comment


          #5
          Hi dynoweb,

          kognam is correct.

          With only OHLC information the order will fill if it is within the bar. No intra-bar information is there to say when the order would fill.

          You would need to add intra-bar granularity to the script so that the script will use tick data to find the price for the fill.

          Below is a link to an official reference sample that demonstrates how to add intra-bar granularity.
          You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


          Also, here is a link to the differences on real-time vs backtest (historical).


          As well as a link to the help guide on the Add() method.


          A link to the help guide on BarsInProgress.


          And a link to the help guide on Multi-Time Frame & Instruments. Please see the section 'How Bar Data is Referenced', and 'Accessing the Price Data in a Multi-Bars NinjaScript'.
          Last edited by NinjaTrader_ChelseaB; 01-05-2017, 04:38 PM.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          638 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          366 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          107 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          569 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          572 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X