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

MTF + out range index value + BarsRequiredToTrade : problem

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

    MTF + out range index value + BarsRequiredToTrade : problem

    Hello Everyone,

    I am having troubles understanding the behavior of my script.

    After working on a 4 secondary series MTF strategy it started to stop working due to the index value wich is out of range.

    The MTF strategy has been backtested numerous times without such issue, with no recent excessive back in time request in the calculation.
    I checked the help guide about CurrentBars and BarsRequiredToTrade, and i hope our example now will help clarify some of it.

    I tried to elevate the default number of BarsRequiredToTrade in the if (State == State.SetDefaults) section. no result

    I also found the sentence that causes the out of range index issue and isolated it.
    The main curiosity here is that : this exact same sentence is used in a bool wich has been backtested and works fine, still now... in the same strategy...

    I am providing attached what, from the code, seemed related to this issue.

    Would anyone care to tell us what I am missing about CurrentBars ?



    Attached Files

    #2
    Hey Everyone,

    Starting fresh on the copy of a previous backtested script of this same strategy, after copying pieces of the logic from the last non working script, and after a few tries from the new copy :

    - the new copy works fine in the analyzer but not on a chart : it shows in the control center in white and the enabling checkbox does not react. (the strategy was added from the chart, this is a primary 400days chart of tick data : lot more than 250 bars).

    - The sentence from the .txt example from last post is definitely what causes the bar index value issue, wether it is in the bool or isolated.
    (I add here that this condition had previously been backtested on a chart and analyzer numerous times, the fact that it cannot anymore is extremely confusing,)

    Would anyone please explain to us the fundamental principle about CurrentBars that is totally disregarded in our example, and would allow a proper adaptation ?


    Comment


      #3
      hey All,

      250 bars of series3 = 25 bars of series4.
      the strategy works on the chart when the maximum count (apparently on any series) is met :

      By setting CurrentBars[3] < 250, it works.
      By setting CurrentBars[4] < 25, it works.
      apparently we do not need to make sure all series have met the max requirement of the oldest calculation. Although there must be at least one series that looks this far back.
      Can someone please confirm the mechanics described is good ? and this is how we SHOULD adapt our Bars requirement ?


      looking at our previous example below, following this logic , setting the biggest series (placed last) to the biggest requirement seems to make the strategy calculate.
      Code:
      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Tick, 90);
      AddDataSeries(Data.BarsPeriodType.Tick, 270);
      AddDataSeries(Data.BarsPeriodType.Tick, 540);
      AddDataSeries(Data.BarsPeriodType.Tick, 5400);
      }
      
      protected override void OnBarUpdate()
      {
      if (BarsInProgress == 0)
      {}
      
      if (CurrentBars[0] < BarsRequiredToTrade
      || CurrentBars[1] < BarsRequiredToTrade
      || CurrentBars[2] < BarsRequiredToTrade
      || CurrentBars[3] < BarsRequiredToTrade      // strategy is working if we set this line to 250 too (it actually worked at 245 too... why would it work at 245 which is less than the requirement ?
      || CurrentBars[4] < 25 )
      return;
      
      // here we go with the famous IntrabarSample
      if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1)
      return;
      
      if (BarsInProgress == 1)
      {
      
      // Set A Example Bool
      if (
      (...)
      && ((CrossAbove(myIndicA3, myOtherIndic3, 250) == true) // here the number after the name of the indic relates to its series, like usual
      || (CrossAbove(myIndicB3, myOtherIndic3, 250) == true)
      || (CrossAbove(myIndicA2, myOtherIndic2, 100) == true) )
      )

      Something yet remains to be clarified :
      If the CurrentBars requirement should be managed the way we just did in this post, how come it was possible to backtest this script before doing this very operation ?
      This script (including this very sentence causing the error) must have had a minimum of 40 to 60 analyzer backtests, and less but many ninjascript chart reloads as well, before we found the need to change BarsRequiredToTrade to an int.
      Last edited by Amedeus; 06-12-2021, 12:53 PM.

      Comment


        #4
        Hello Amedeus,

        For each index used, CurrentBar should be greater than the index for that bar series.

        If using Closes[1][100], then CurrentBars[1] should be greater than 100 to prevent an invalid index.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea, thank you very much,

          What you say first seemed like this could not have been put clearer and simpler,

          "If using Closes[1][100], then CurrentBars[1] should be greater than 100 to prevent an invalid index."
          To the common folk like some of us and I, this also means : if using closes[3][250], then we should write in our example : (...|| CurrentBars[3] > 300...), then any index3 error would be prevented, because the maximum lookBack in this index is 250.
          this is why i did set the CurrentBars of our example script like this below, following your explaination,
          Code:
          if (CurrentBars[0] > 300
          || CurrentBars[1] > 300
          || CurrentBars[2] > 300
          || CurrentBars[3] > 300
          || CurrentBars[4] > 300)
          return;
          
          // MTFIntrabarSample CurrentBars check
          if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1) return;
          
          if (BarsInProgress == 1)
          { // Set A Example Bool if  (...)}
          All CurrentBars are greater than what our logic asks from their index, like Chelsea told me they should be, great !
          BUT the strategy doesnt start and the index error shows up in the log...




          allright, now let's take a risk and assume that (CurrentBars[3] > 300) combined with (CurrentBars[3] < 1) is an impossible combination for a calculation program, like if it were some type of a mathematical nonsense.
          If also we can agree that there is no point asking a trading program to only find bars that are older than the ones our logic wants to use,

          we can still find sense out of your explaination and set the CurrentBars like below, with a CurrenBars that is lesser than a bigger number than what we need from the index to look back.
          This way CurrentBar can be both less than 1 and less than the max too.
          Code:
          if (CurrentBars[0] < 300
          || CurrentBars[1] < 300
          || CurrentBars[2] < 300
          || CurrentBars[3] < 300
          || CurrentBars[4] < 30)
          return;
          
          // MTFIntrabarSample CurrentBars check
          if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1 || CurrentBars[4] < 1) return;
          then the strategy starts.

          I'll assume here that we all get it.
          Basically , (CurrentBars[3] < N) just translates to "the program will look back N much on series3"....
          This is after another look at the help guide, what it tells us. It's just not all obvious for everybody.



          Thanks again Chelsea, this was very educational,
          and indeed could not be simpler and clearer.
          Last edited by Amedeus; 06-13-2021, 06:13 PM.

          Comment


            #6
            the fact that the strategy calculated this condition :
            Code:
            && ((CrossAbove(myIndicA3, myOtherIndic3, 250) == true)
            || (CrossAbove(myIndicB3, myOtherIndic3, 250) == true)
            || (CrossAbove(myIndicA2, myOtherIndic2, 100) == true) )
            while BarsRequiredToTrade default was set like this :
            Code:
            if (State == State.SetDefaults) { BarsRequiredToTrade = 20;}
            and CurrentBars like this :
            Code:
            
            protected override void OnBarUpdate()
            {
            if (BarsInProgress == 0) {}
            
            if (CurrentBars[0] < BarsRequiredToTrade
            || CurrentBars[1] < BarsRequiredToTrade
            || CurrentBars[2] < BarsRequiredToTrade
            || CurrentBars[3] < BarsRequiredToTrade
            || CurrentBars[4] < BarsRequiredToTrade) return;
            
            if (CurrentBars[0] < 1
            || CurrentBars[1] < 1
            || CurrentBars[2] < 1
            || CurrentBars[3] < 1
            || CurrentBars[4] < 1) return;
            
            if (BarsInProgress == 1)
            {
            for just one bar period, remains a mistery to me.
            Even more now than before.
            Last edited by Amedeus; 06-13-2021, 06:13 PM.

            Comment


              #7
              Hello Amedeus,

              MaximumBarsLookBack should be set to .Infinite any time indexes larger than 256 are used.


              If you are experiencing an indexing error, I would recommend reducing the code (or commenting out code) and using prints to identify the specific index causing the error.


              What is the specific index causing the error?
              What is the collection/array/series object that does not have that index?
              What is the CurrentBar value for that series or the .Count() of the collection/array?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hey Chelsea , thank you,

                the strategy seems to work now, after configuring each index CurrentBars like in post5. I thaught we were done for now. We could be.

                But I hear prints are the bomb... Sometime ago even, someone around offered me to discuss outputs and prints, that's how much they must be.

                Also the mystery post6 led me to try one of your prints on a copy of this strategy as it was before operation post5.
                meaning : SetDefault BarsRequiredToTrade = 20, and all indexes CurrenBars<BarsRequiredToTrade in OnBarUpdate.
                The mystery deepened.

                previously the error happened on the chart, not in the analyzer. (previously to that, it did not happened at all !)
                now it bugs in the analyzer while the strategy applies on the chart...

                this is the print at its place, that you can recognize from the other day, to which I added CurrentBar and Count.
                Code:
                protected override void OnBarUpdate()
                {
                Print(string.Format("{0} | BarsInProgress: {1}, {2} {3} {4} {5} {6} ", Time[0], BarsInProgress, CurrentBar, Count , Instrument.FullName, BarsPeriod.Value, BarsPeriod.BarsPeriodType));
                and attached is the Output
                I take from it that the bar you were asking me to find is on the index 0. Something we should expect, since we remember that we are in the configuration post6. Should we not ?
                Do I take it right ?

                Why is it working on the chart now ? when it's a 90 tick chart like series1, or when primary is the same period as series2 and series3 too.
                All sensitive conditions are enabled.
                Attached Files
                Last edited by Amedeus; 06-14-2021, 06:52 PM.

                Comment


                  #9
                  Hello Amedeus,

                  Indexes will have brackets around them.

                  With Close[5], the [5] is an index for 5 bars ago with a series object. Price series are a collection of double values for every bar on the chart access with the bars ago index of that bar.

                  If CurrentBar is 2, and there are not 6 bars, Close[5] will be an invalid index.

                  When is the error occurring?

                  What line of code is causing the error?

                  What index is being used on a collection? What is the size of that collection?

                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10

                    Hey Chelsea, thank you,

                    This line
                    Code:
                    // Set MyBoolLong
                    if (((CrossAbove(MyIndic3.Default, MyInputa , 200) == true )
                    && (CrossAbove(MyIndic3.Default, MyInputb , 80) == true ) )
                    || ((CrossAbove(MyIndic3.Default, MyInputb , 200) == true )
                    && (CrossAbove(MyIndic3.Default, MyInputc , 80) == true ) )
                    && (MyIndic3.Default[0] >= MyInput ))
                    { MyBoolLong = true; }
                    is causing this error
                    Code:
                    ...
                    08/01/2020 01:28:58 | BarsInProgress: 1, 1181 247814 MGC 08-21 90 Tick
                    08/01/2020 01:29:12 | BarsInProgress: 0, 1182 247814 MGC 08-21 90 Tick
                    08/01/2020 01:29:12 | BarsInProgress: 1, 1182 247814 MGC 08-21 90 Tick
                    Strategy 'aaaaaaaaaaaaaaaaaarrrrrrrrrrrrreeee': Error on calling 'OnBarUpdate' method on bar 1182: 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.

                    this is what i guess we try to deduce something from
                    Code:
                    Error on calling 'OnBarUpdate' method on bar 1182
                    08/01/2020 01:29:12 | BarsInProgress: 0, 1182 247814 MGC 08-21 90 Tick
                    08/01/2020 01:29:12 | BarsInProgress: 1, 1182 247814 MGC 08-21 90 Tick
                    it tells us the error is occuring WHEN bar 1182 from Collection/series0 is CurrentBar.
                    We ask index [200] of series3 Collection to tell us something.
                    I guess we need hand math to know the size of the collection at that time : (1182 x 90ticks)/540ticks = 197 bars of series3.
                    -> double [200] (exceptionnaly has no brackets because it is used with CrossAbove ?) is not completed yet.

                    this is why it works if we make the script check for minimum the max LookBack :
                    ...|| CurrentBars[3] < 200 makes it work in both the analyzer and the chart.


                    should it not bug when applied on the chart with this : OnBarUpdate {CurrentBars[3] < BarsRequiredToTrade}. It did not.

                    Comment


                      #11
                      Hello Amedeus,

                      Keep reducing the specific index.

                      Does Print(CrossAbove(MyIndic3.Default, MyInputa , 200)) cause this error?

                      How is MyIndic3 instantiated?
                      Is this using BarsArray[3] as the input series?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hey Chelsea, thanks

                        (CrossAbove(MyIndic3.Default, MyInputa , 200)) causes the error.

                        The input series of MyIndic3 is Closes[3].

                        How do I know what instantiates MyIndic3 ?
                        NinjaTrader 8 <- I can see here the special line regarding instantiation, but there is nothing of the sort in this script.

                        Comment


                          #13
                          Hi Chelsea,

                          Some precisions about MyIndic, just so we can confirm nothing is missed.
                          Here is all I know about it and where it is placed before OnBarUpdate :
                          Code:
                          public class aaaaaaaaaaaaaaaaaarrrrrrrrrrrrreeee : Strategy
                          {
                          private MyIndic MyIndic3;
                          
                          else if (State == State.DataLoaded)
                          { MyIndic3  = MyIndic(Closes[3], MyPeriod);

                          Comment


                            #14
                            Hey Chelsea,

                            I am in front of the Using Bars Objects as Input to Indicator Methods sub section of the NinjaTrader 8 MTF guide.

                            I think this is what you want me to read.

                            I remember reading it but concluded we need to use the BarsArray syntax, kinda only if we want to compare different series in the same line.
                            Our line does not, we compare values from the same index each time...
                            but we do it if BarsInProgress == 1

                            are we touching the why it does not behave the same in the analyzer and chart ?
                            shoud we add some BarsArray syntax in our line ? even if MyIndic3 has an input that is closes[3] ?


                            Comment


                              #15
                              Hello Amedeus,

                              Closes[3] is fine to use as the input series.

                              What is the value of CurrentBars[3] when the error is produced?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by LawrenHom, Today, 10:45 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post LawrenHom  
                              Started by love2code2trade, Yesterday, 01:45 PM
                              4 responses
                              28 views
                              0 likes
                              Last Post love2code2trade  
                              Started by funk10101, Today, 09:43 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post funk10101  
                              Started by pkefal, 04-11-2024, 07:39 AM
                              11 responses
                              37 views
                              0 likes
                              Last Post jeronymite  
                              Started by bill2023, Yesterday, 08:51 AM
                              8 responses
                              44 views
                              0 likes
                              Last Post bill2023  
                              Working...
                              X