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 Strategy - No results when backtesting?

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

    Opening Range Strategy - No results when backtesting?

    Hi all,

    I'm trying to backtest a simple opening range breakout strategy but I get no trade results when backtesting. I have included the code below. Any assistance would be appreciated

    One thing that does confuse me is why the script renames my indicator from "HighLowByTimeRange" to "HighLowByTimeRange1". Why the additional "1"?



    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class OpeningRange : Strategy
    {
    private HighLowByTimeRange HighLowByTimeRange1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "OpeningRange";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    SetProfitTarget(@"LongMarket", CalculationMode.Ticks, 8);
    SetStopLoss(@"LongMarket", CalculationMode.Ticks, 8, false);
    }
    else if (State == State.DataLoaded)
    {
    HighLowByTimeRange1 = HighLowByTimeRange(8, 30, 8, 45);
    HighLowByTimeRange1.Plots[0].Brush = new SolidColorBrush(Colors.Green);
    HighLowByTimeRange1.Plots[1].Brush = new SolidColorBrush(Colors.Red);
    AddChartIndicator(HighLowByTimeRange1);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 1
    || HighLowByTimeRange1.CurrentBar < 1)
    return;

    // Set
    if ((Time[0].TimeOfDay > new TimeSpan(8, 45, 28))
    && (Time[0].TimeOfDay < new TimeSpan(10, 30, 8))
    && ((Close[0] + (2 * TickSize)) >= HighLowByTimeRange1.HighestHigh[0]))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"LongMarket");
    }

    }
    }
    }

    #2
    Hello user_456, and thank you for your questions.

    First, your variable was renamed so that your instance and class names would be different.

    Please study the attached indicator and strategy. The direct answer to your question, is that you should not be testing your indicator's CurrentBar in from your strategy in this fashion. Instead, I recommend testing that one bar beyond the minimum is checked for, to guarantee your indicator's data series has caught up to your strategy's.

    The attached strategy and indicator will place trades in the strategy analyzer.

    Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the information. I'm trying to do this in NT8. The examples you sent won't import into NT8.

      Also would you please be so kind as to rephrase your earlier comments with more specifics? I don't understand... "the direct answer to your question, is that you should not be testing your indicator's CurrentBar in from your strategy in this fashion. Instead, I recommend testing that one bar beyond the minimum is checked for, to guarantee your indicator's data series has caught up to your strategy's."

      I don't know what you mean by minimum or current bar. I'm trying to get the high and low from my timed range indicator and go long two ticks above the high of the indicator. The indicator looks fine on my chart.

      Thanks very much

      Comment


        #4
        Hello user456,

        The files I attached were written in NT8. One is an indicator and one is a strategy. However, importing them directly will not be necessary. The following changes to your code will cause it to compile, and to be able to generate trades.

        Code:
        [FONT=Courier New]
                else if (State == State.Configure)
                {
                    SetProfitTarget(@"LongMarket", CalculationMode.Ticks, 8);
                    SetStopLoss(@"LongMarket", CalculationMode.Ticks, 8, false);
                    /* JDP 25JUL16 18:49 GMT
                    
                    Please see http://ninjatrader.com/support/helpGuides/nt8/en-us/?addchartindicator.htm 
                }
                else if (State == State.DataLoaded)
                {
                    */
                    HighLowByTimeRange1             = HighLowByTimeRange(8, 30, 8, 45);
                    HighLowByTimeRange1.Plots[0].Brush = new SolidColorBrush(Colors.Green);[/FONT]
        Here, this works around an error you will encounter when trying to call AddChartIndicator outside State == State.Configure.

        Code:
        [FONT=Courier New]        if (CurrentBars[0] < 1/* || HighLowByTimeRange1.CurrentBar < 1*/)
                    return;[/FONT]
        This is the code that was preventing you from being able to place trades in the Strategy Analyzer, and this is what I was referring to in accessing your Indicator's CurrentBar from the Strategy.

        Please let us know if there are any other ways we may help.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Thanks very much for the additional information. I created this strategy in Strategy Builder and was not receiving any compiling errors that I know of.

          Whatever mistake that I made in Strategy Builder... Is there not a way that I could fix it there? I have a screen shot of the conditions window. Very kind of you to supply correcting code but I'd like to learn from my mistake rather than just paste code in the unlocked strategy.
          Attached Files

          Comment


            #6
            Hello user_456,

            Could you use the freely and publicly available software Jing, available here https://www.techsmith.com/jing.html , or any other screencasting software, to record the creation of a similar strategy? If it is possible for the strategy builder to create code that does not compile, as was the case with your code, we will need to investigate and repair the strategy builder. Thank you in advance for any help you can give us reproducing what you saw on your end.

            To answer your question directly, whatever you did in the strategy builder to examine the indicator's CurrentBar, is what prevented your script from placing trades.
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Hello user_456,


              This behavior (the strategy builder adding secondary data series information to State.DataLoaded) was confirmed on our end, and is scheduled to be repaired in Beta 13. Please keep an eye on the NinjaTrader 8 Release Notes page for updates and bugfixes.






              If you continue observing this behavior in Beta 13, or if there are any other ways we can help, please do not hesitate to reach out and we will assist further.
              Jessica P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by geddyisodin, Today, 05:20 AM
              0 responses
              1 view
              0 likes
              Last Post geddyisodin  
              Started by JonesJoker, 04-22-2024, 12:23 PM
              6 responses
              32 views
              0 likes
              Last Post JonesJoker  
              Started by GussJ, 03-04-2020, 03:11 PM
              12 responses
              3,239 views
              0 likes
              Last Post Leafcutter  
              Started by AveryFlynn, Today, 04:57 AM
              0 responses
              5 views
              0 likes
              Last Post AveryFlynn  
              Started by RubenCazorla, 08-30-2022, 06:36 AM
              3 responses
              79 views
              0 likes
              Last Post PaulMohn  
              Working...
              X