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

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:




    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 wzgy0920, 04-20-2024, 06:09 PM
        2 responses
        26 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, 02-22-2024, 01:11 AM
        5 responses
        32 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, Yesterday, 09:53 PM
        2 responses
        49 views
        0 likes
        Last Post wzgy0920  
        Started by Kensonprib, 04-28-2021, 10:11 AM
        5 responses
        192 views
        0 likes
        Last Post Hasadafa  
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,234 views
        0 likes
        Last Post xiinteractive  
        Working...
        X