• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Opening Range Backtesting

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

    Opening Range Backtesting

    Hello,

    I am working on an opening range breakout strategy that defines the High and Low of the first 5' bar as the "range". Once price crosses above or below that range, I want to enter long and short respectively. In trying to backtest this, I am having trouble getting the script to enter on the break of the range. Instead, it is currently entering on the open of the next bar after the range is broken. (I have only coded the long portion so far)

    See the attached pictures for a snippet of my code and the problem that is happening. I want to enter at the blue circle, but instead it enters me at the white circle. Does anyone know how to fix this?

    Thank you.

    #2
    Hello Captain_Qwark,

    Thanks for your post and welcome to the NinjaTrader forums!

    When backtesting, in the strategy analyzer, or looking at historical trades run on a chart, the historical data is presented as complete bars meaning the only values provided are the bars Open, High, Low and close values, the bar does not contain the ticks that make up the bar. Your strategy code, regardless of its Calculate setting, can only run once per bar, at the end of the bar. If an entry decision by your strategy is made, the order can only be filled in the next bar.

    In order for your strategy to fill immediately, in the strategy analyzer, you would need to modify your code by adding a 1 tick series to help with order fills and enable "Tick Replay". Your strategy would also need to run with Calculate.OnPriceChange or Calculate.OnEachTick in order to use the tick replay to execute your code once per price change or per tick. Adding a 1 tick series and using tick replay means that you would also need to have historical tick data for the instrument you are testing for the duration of the analysis period. You will also find that running with a 1 tick series will considerably slow the strategy analyzer. The combination of the Calculate mode, using TickReplay and single tick added series should allow the strategy to execute as you wish in the Strategy Analyzer.

    Alternatively, without modifications, you can run your strategy on a chart using the Playback with market replay data. This will provide the tick by tick performance needed to accomplish your goal, however, playback will be slower. You can still generate a performance report of the strategy by right mouse click on the chart and select strategy performance>real time (at the end of the market replay run).

    In the code you are showing, you are setting the stop and Profit target after the EnterLong(). The Set methods retain the last price they were set to, when the entry order fills they will immediately use the last price they were set to which could lead to a rejected order. The best practice would be to simply place the EnterLong() code after the Set methods as this ensures they are set to values you expect in that situation before placing the actual entry order.

    References:
    https://ninjatrader.com/support/help...ick_replay.htm
    https://ninjatrader.com/support/help...connection.htm
    https://ninjatrader.com/support/help...tegyFromAChart

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,

      Thank you for the thorough reply. I have gone ahead and added Tick Replay to the options and included it in the strategy analyzer. However, I am unsure of what you mean that I would need to add a 1 tick series to help with order fills. Can you please explain that a bit more or show me an example line of code? If it helps, I am looking to analyze this strategy with stocks in the S&P. Would I need to download the historical tick data from somewhere? Appreciate the help, I am fairly new to NT

      I have adjusted my code based on your suggestion with EnterLong(). Thank you.

      Comment


        #4
        Hello Captain_Qwark,

        Thanks for your reply.

        You can see a similar example in this example strategy: https://ninjatrader.com/support/helpGuides/nt8/?backtesting_ninjascript_strate.htm

        Yes, you would need a source for historical tick data. This would not be available from our servers as they are limited to Futures and Forex instruments.
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by jhontorres, Today, 12:16 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by KarstenKafl, 07-16-2020, 12:38 AM
        8 responses
        42 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by DieSlower, Yesterday, 11:02 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by TomTownsend, Today, 11:35 AM
        3 responses
        15 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by frankduc, Today, 11:17 AM
        2 responses
        17 views
        0 likes
        Last Post frankduc  
        Working...
        X