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

Plot limited to 256 bars, infinite plot is not possible

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

    Plot limited to 256 bars, infinite plot is not possible

    Hi all,

    I have created following script, that works fine for me, but only if I use MaximumBarsLookBack.TwoHundredFiftySix and not MaximumBarsLookBack.Infinite.

    There are following steps in the script:

    1. read the data from an external DB
    2. store the date in myDateTimeDataSeries
    3. store the close in myValueDataSeries
    4. check for every bar the date in myDateTimeDataSeries
    5. if there is a match for the date plot the value from myDateTimeDataSeries

    After changing the Maximum Bars Look Back setting from 256 to infinite, I got the following error under the referral to line 107, where I assign the value close to myValueDataSeries.

    It would be great if you could help me, to solve the error.

    Thanks and best regards
    Andi


    28.08.2023 20:00:00 System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    at System.ThrowHelper.ThrowArgumentOutOfRangeExceptio n(ExceptionArgument argument, ExceptionResource resource)
    at NinjaTrader.NinjaScript.Series`1.set_Item(Int32 barsAgo, T value)
    at NinjaTrader.NinjaScript.Indicators.AA.AFMYSQL3.OnS tateChange() in C:\Users\AF\Documents\NinjaTrader 8\bin\Custom\Indicators\AA\AFMYSQL3.cs:line 107

    Indicator 'AFMYSQL3': 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.
    Attached Files

    #2
    Hello andifor,

    Thank you for your post and welcome to the NinjaTrader forum community!

    More information regarding MaximumBarsLookBack may be found in the help guide here:


    We also have a tip page about making sure there are enough bars in the data series you are accessing and prevent this type of error:


    I suggest keeping those tips in mind as well as adding print statements to help narrow down the cause and debug your script. For more details on using prints to debug your scripts:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,

      I have already looked at the help pages and unfortunately have not found a solution.

      Maybe someone can take a look at the script and has a possible solution to this issue?​

      Thanks and best regards,
      Andi

      Comment


        #4
        Originally posted by andifor View Post
        Hi Emily,

        I have already looked at the help pages and unfortunately have not found a solution.

        Maybe someone can take a look at the script and has a possible solution to this issue?​

        Thanks and best regards,
        Andi
        Have you added debugging prints? If so, what prints have you tried and what was the output from the added prints? Rather than using try-catch blocks, I suggest creating a copy of your script that removes the try-catch blocks and contains a print every few lines of code. Monitor the NinjaScript Output window to see which print is the last to appear before the error, indicating that the error is caused by logic below the location of that print statement in your script. This can help to first narrow down the line of code causing the error, and then you can try printing each value used in that portion of code to see which item is causing the error and from there you could test adding a check for the number of items in the series prior to attempting to access that index.

        Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one-on-one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

        That said, through email or on the forum we are happy to answer any questions you may have about NinjaScript if you decide to debug this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior. Please try the suggesting debugging steps and let us know the results. You can save the output from the NinjaScript Output window by right-clicking and selecting Save as to create a .txt file that may be attached to your reply.

        Please let us know if we may be of further assistance.​
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi Emily,

          thanks a lot for your support!

          I was able to narrow down the search for the error to this code:

          for (int index = 0; reader.Read() && index < Bars.Count; index++)

          {

          DateTime date = Convert.ToDateTime(reader["Date"]);
          double close = Convert.ToDouble(reader["close"]);

          myDateTimeDataSeries[index] = date;
          myValueDataSeries[index] = close;

          Print("index: " + index + " " + "date: " + date + "close: " + close);

          }

          Print(myDateTimeDataSeries[0] + " " + myValueDataSeries[0]);
          Print(myDateTimeDataSeries[1] + " " + myValueDataSeries[1]);
          Print(myDateTimeDataSeries[2] + " " + myValueDataSeries[2]);
          Print(myDateTimeDataSeries[3] + " " + myValueDataSeries[3]);


          After the execution of the indicator with the setting Maximum bars look back = 256 everything is fine and I got the output 1 in the attachement.

          After the execution of the indicator with the setting Maximum bars look back = Infinite, script runs in to an error --> see output 2 in the attachement.

          As you can see, only the index is incremented to create the data series correctly. In the second case, there is unfortunately an error that I cannot fix.

          Thanks and best regards,
          Andi
          Attached Files

          Comment


            #6
            Hello andifor,

            Thank you for your reply.

            What value are you using to print the "Bars on chart" in the output? What are the results if you print CurrentBar as well? Based on your output, it seems as though the prints were successful using an index of 0 barsAgo. Then when the loop incremented the index and tried to access values from 1 bar ago, the error occurred. If this happens on the first bar of the chart, that makes sense because values from 1 bar ago would not exist yet. I understand you are using Bars.Count which would give you the number of bars. CurrentBar starts at 0 and Count starts at 1, so these values are not the same. Adding CurrentBar to your prints should help as an additional debugging tool. You may need to revise your use of Bars.Count in the loop to determine the index value.

            Please let us know if we may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Hi Emily,

              first of all I would like to mention that I use the code that I sent you (yesterday and today) within the State.DataLoaded.
              The error occurs in this state. The idea is to create the custom data series in State.DataLoaded​ and later within OnBarUpdate() I would like to access to this custom series in order to plot the values to the chart.

              I am using Bars.Count to get the value for "Bars on chart":
              Print ("Bars on Chart: " + Bars.Count);

              I've added to the script:
              Print ("CurrentBar: " + CurrentBar);

              After the execution of the indicator with the setting Maximum bars look back = 256 everything is fine and I got the output 1 in the attachement.

              After the execution of the indicator with the setting Maximum bars look back = Infinite, script runs in to an error --> see output 2 in the attachement.

              Thanks and best regards,
              Andi
              Attached Files
              Last edited by andifor; 12-08-2023, 09:53 AM.

              Comment


                #8
                Hello andifor,

                Thank you for your reply.

                To clarify, are you using your for loop in State.DataLoaded? DataLoaded is only called once after all data series have been loaded. I suspect that when you are using MaximumBarsLookBack.TwoHundredFiftySix, since the last 256 values from the series are stored in memory you potentially have access to previous indexes without errors. If you are using MaximumBarsLookBack.Infinite you have access to the full series, starting from bar 0 all the way on the left. Please add current bar to the print with index in it so you may compare how many bars back you are trying to access vs. the CurrentBar value:
                Print("CurrentBar: " + CurrentBar + " index: " + index + " " + "date: " + date + "close: " + close);

                You should not assign values to your series in OnStateChange() and instead should assign them in OnBarUpdate(). I am a little confused about what logic you are using when state is DataLoaded vs. what you are doing in OnBarUpdate()​. My understanding was that the logic you shared in post #5 was being called in OnBarUpdate().

                Please let me know the results after adding CurrentBar to the print with the index inside of the loop.

                I look forward to your reply.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Emily,

                  finally I found a solution, where I can read the data from a MySQL database and store the data in a list in the in State.DataLoaded​.

                  while (reader.Read() && i < Bars.Count)
                  {
                  DateTime date = Convert.ToDateTime(reader["Date"]);
                  double close = Convert.ToDouble(reader["close"]);

                  myFetchedValues_date.Add(date);
                  myFetchedValues_close.Add(close);
                  i++;
                  }​

                  Later within OnBarUpdate() I can access to this list in order to plot the values to the chart.​

                  if(CurrentBars[0] < 1) return;
                  int i = 0;
                  while (i < myFetchedValues_date.Count)
                  {
                  DateTime dbDate = myFetchedValues_date[i];
                  DateTime barDate = Time[0].Date;
                  if (barDate.Date == dbDate.Date)
                  {
                  double positive = myFetchedValues_close[i];

                  Values[0][0] = positive;

                  break;
                  }
                  i++;
                  }​

                  Best regards
                  Andi

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Haiasi, 04-25-2024, 06:53 PM
                  2 responses
                  17 views
                  0 likes
                  Last Post Massinisa  
                  Started by Creamers, Today, 05:32 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post Creamers  
                  Started by Segwin, 05-07-2018, 02:15 PM
                  12 responses
                  1,786 views
                  0 likes
                  Last Post Leafcutter  
                  Started by poplagelu, Today, 05:00 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post poplagelu  
                  Started by fx.practic, 10-15-2013, 12:53 AM
                  5 responses
                  5,408 views
                  0 likes
                  Last Post Bidder
                  by Bidder
                   
                  Working...
                  X