• 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

OnBarUpdate Error when enabling strategy in replay

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

    OnBarUpdate Error when enabling strategy in replay

    Hello,

    I am creating a strategy using a 3rd party (open source code) Heiken Ashi indicator while trying to use AddRenko as well for the data series, in an attempt to create a LinReg crossover entry type strategy. When trying to test this on replay, I am receiving this error message:

    Strategy 'RenkoCrossAuto': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
    I am new to accessing a data series not offered within the builder, as well as using a 3rd party indicator within a strategy, so I am not 100% if I have coded it all right.

    Any help on this issue would be much appreciated.

    Thank you,
    Chris

    #2
    Posting some minimalist sample code that causes the error would be a start.

    Comment


      #3
      Thank for the reply Sledge,

      Here is some code from OnStateChange. I assume the error has to do with either the Heiken Ashi or renko.
      else if (State == State.Configure)
      {
      AddRenko("ES 03-18", 2, MarketDataType.Last);
      SetProfitTarget(@" Target1", CalculationMode.Ticks, Target1);
      SetProfitTarget(@" Target2", CalculationMode.Ticks, Target2);
      SetProfitTarget(@" Target3", CalculationMode.Ticks, Target3);
      SetStopLoss("", CalculationMode.Ticks, StopLoss, true);
      }
      else if (State == State.DataLoaded)
      {
      HeikenAshi81 = HeikenAshi8(Close);
      HeikenAshi81.Plots[0].Brush = Brushes.Gray;
      HeikenAshi81.Plots[1].Brush = Brushes.Gray;
      HeikenAshi81.Plots[2].Brush = Brushes.Gray;
      HeikenAshi81.Plots[3].Brush = Brushes.Gray;
      AddChartIndicator(HeikenAshi81);
      AddChartIndicator(LinReg1);
      LinReg1 =LinReg(Close, Convert.ToInt32(LinRegMA));
      LinReg1.Plots[0].Brush = Brushes.Goldenrod;
      }
      Last edited by chrisca; 01-07-2018, 12:30 AM.

      Comment


        #4
        Originally posted by chrisca View Post
        Thank for the reply Sledge,

        Here is some code from OnStateChange. I assume the error has to do with either the Heiken Ashi or renko.
        Code:
        else if (State == State.Configure)
        {
        AddRenko("ES 03-18", 2, MarketDataType.Last);
        Print ("config1");
        SetProfitTarget(@" Target1", CalculationMode.Ticks, Target1);
        Print ("config2");
        SetProfitTarget(@" Target2", CalculationMode.Ticks, Target2);
        Print ("config3");
        SetProfitTarget(@" Target3", CalculationMode.Ticks, Target3);
        Print ("config4");
        SetStopLoss("", CalculationMode.Ticks, StopLoss, true);
        Print ("config5");
        }
        else if (State == State.DataLoaded)
        {
        HeikenAshi81 = HeikenAshi8(Close);
        Print ("DataLoaded1");
        HeikenAshi81.Plots[0].Brush = Brushes.Gray;
        Print ("DataLoaded2");
        HeikenAshi81.Plots[1].Brush = Brushes.Gray;
        Print ("DataLoaded3");
        HeikenAshi81.Plots[2].Brush = Brushes.Gray;
        Print ("DataLoaded4");
        HeikenAshi81.Plots[3].Brush = Brushes.Gray;
        Print ("DataLoaded5");
        AddChartIndicator(HeikenAshi81);
        Print ("DataLoaded6");
        AddChartIndicator(LinReg1);
        Print ("DataLoaded7");
        LinReg1 =LinReg(Close, Convert.ToInt32(LinRegMA));
        Print ("DataLoaded8");
        LinReg1.Plots[0].Brush = Brushes.Goldenrod;
        Print ("DataLoaded9");
        }

        If the Print doesn't show up, you won't see it. Now we'll know which line is the problem.

        Comment


          #5
          Thanks for the idea sledge,

          So after running with the prints added, the OnStateChange error popped up after DataLoaded6. The following line
          AddChartIndicator(LinReg1);
          is apparently where the snag is being hit.

          I took DataLoaded lines 7,8 and 9 out since those are mainly used for adding the indicator to the chart and received an
          OnBarUpdate method on bar 0
          error.
          Last edited by chrisca; 01-07-2018, 01:09 PM.

          Comment


            #6
            Hello chrisca,

            In the code you have posted, it appears that LinReg1 is attempted to be added to the chart before it is initialized.

            LinReg1 =LinReg(Close, Convert.ToInt32(LinRegMA));

            Above is where the indicator is initialize and assigned to LinReg1.

            AddChartIndicator(LinReg1);

            And this line adds it to the chart.

            Create it first, then add it to the chart.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks Chelsea,

              So I moved that line and ttied it again, but am now getting this error instead:

              Strategy 'RenkoCrossAuto': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
              I have Bars Required To Trade set to 1 and my BarsInProgress == 1 as well and let replay run for at least 25 bars. Would this have something to do with me using AddRenko data series?

              Comment


                #8
                Hello chrisca,

                This message indicates an invalid index was used.

                For example, calling Close[1] on the very first bar will cause this error.

                The reason for this is because there will not be a bar ago, essentially there is not enough data at bar 0 to calculate to look a bar ago.

                The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling each bar for OnBarUpdate();

                What you would nee to do instead is wait for after the first bar to be built from both data series and then run your if check for closes, to ensure that you have enough data.

                http://ninjatrader.com/support/helpG...currentbar.htm
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Chelsea,

                  So I got it to work and it was running fine, but when I turned it off and reset ninjascript, I get another error when trying to enable it again.

                  Strategy 'RenkoCrossAuto': Error on calling 'OnBarUpdate' method on bar -1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                  I didn't change any settings.

                  Comment


                    #10
                    Hello chrisca,

                    The script adds a secondary series.

                    Both series need data.

                    Are you checking that both series have at least one bar?

                    If so, what code have you added to ensure that both CurrentBars[0] and CurrentBars[1] are both greater than at least 1 or any indexes called?

                    Below is a link to the help guide on CurrentBars.
                    https://ninjatrader.com/support/help...urrentbars.htm
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      So here is what I just added, but I am pretty sure it is wrong since I am a little confused as to verify that both series have at least 1 bar:

                      protected override void OnBarUpdate()
                      {
                      if (BarsInProgress != 1)
                      return;

                      if (CurrentBars[0] != 1)
                      return;

                      if (CurrentBars[1] != 1)
                      return;

                      Comment


                        #12
                        https://ninjatrader.com/support/foru...ht=CurrentBars

                        Comment


                          #13
                          Hello chrisca,

                          The code you have checks that CurrentBars[0] and CurrentBars[1] are not equal to one not that they are greater than one to continue (or if they are less than one to return). Also, the script can only process when BarsInProgress is 1, meaning it will only process for the secondary series and will not continue if the primary series is processing.

                          The > symbol is the greater than symbol, the < is the less than symbol.

                          if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
                          return;

                          Below I am providing a link to a forum post with helpful information about getting started with NinjaScript.
                          http://ninjatrader.com/support/forum...609#post503609
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Thanks Chelsea,

                            I was able to get it to work. As for order management, a long entry will be placed as once price closes above the LinReg as planned, but another will immediately be submitted again since the conditions are technically still true. Is there a way to keep another order from being submitted until another event (short entry) has been triggered?

                            Thank you very much for the help and I greatly appreciate it.
                            Chris

                            Comment


                              #15
                              Hello Chris,

                              You can use a bool as a trigger.

                              When the first action happens, set the bool to true. Require the bool to be true for the second condition to trigger. When the second condition triggers set the bool back to false.

                              Below is a link to a 3rd party educational site on bool.
                              https://www.dotnetperls.com/bool
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by XimoTrader, Today, 12:57 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_PatrickG  
                              Started by FxInception, Today, 12:27 PM
                              2 responses
                              10 views
                              0 likes
                              Last Post FxInception  
                              Started by siroya, Today, 10:34 AM
                              1 response
                              20 views
                              0 likes
                              Last Post NinjaTrader_ShawnB  
                              Started by Leeroy_Jenkins, Today, 11:22 AM
                              2 responses
                              12 views
                              0 likes
                              Last Post Leeroy_Jenkins  
                              Started by RedDuke, Today, 10:27 AM
                              2 responses
                              15 views
                              0 likes
                              Last Post RedDuke
                              by RedDuke
                               
                              Working...
                              X