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

ArgumentOutofRangeException using Close

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

    ArgumentOutofRangeException using Close

    Ok I have been struggling with this code long enough, time to ask for help. ;-)

    I am simply trying to copy all the close values to the new Vector and I keep getting an ArgumentOutOfRange exception on the Bolded call to that indexer.

    What am I missing here?

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

    if (CurrentBar < 14)
    return;

    try
    {
    yValues = Vector.Create<double>(Close.Count);

    for (int closeCount = 0; closeCount < Close.Count - 1; closeCount++)
    {
    yValues[closeCount] = Close.IsValidDataPoint(closeCount) ? Close[closeCount] : 0;
    }


    }
    catch (Exception ex)
    {
    Print("Exception - " + ex.Message);
    }

    }

    #2
    Hello seanhughes16,

    Thanks for your post.

    Using Vectors would go beyond the support we would be able to provide you since it is a C# specific topic and not NinjaScript-specific code from the help guide. When testing your code I am seeing multiple compile errors stating that "Vector does not contain a definition for 'Create'" and an error stating that you "Cannot apply indexing with [ ] to an expression of type System.Windows.Vector".

    To find information about how to use a Vector in C#, you could do a quick Google search for something like 'Vectors C#' or 'Vector C# MSDN'.

    That said, it seems that you are wanting to access the Close price for each bar on a chart which could be done by using the Close PriceSeries collection in NinjaScript. This holds a collection of historical bar Close prices.

    See this help guide page for more information about the Close PriceSeries: https://ninjatrader.com/support/help.../nt8/close.htm

    If you are wanting to save those historical Close prices to a custom Series, you could certainly do so by creating a custom Series<double> variable and assigning the Close[0] price to the custom series.

    Review this help guide page for more information about custom Series<T> variables and sample code: https://ninjatrader.com/support/help...t8/seriest.htm

    Please let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response Brandon.

      There is no issues with the Vectors and I understand you guys wont support that, what I am expecting is that in the code above the Close.Count property is returning 9834 for example and in the loop just trying to access these values it throws and index exception on row 12 on the BOLDED call which is clearly in the realm of support for NT.

      What is the root cause of this indexer throwing this exception when the count clearly says it is a valid index value?

      Comment


        #4
        Hello seanhughes16,

        Thanks for your note.

        The error is likely due to using Close.Count. When using Close.Count, you may need to subtract 1 or 2 from the count depending on the Calculate setting you are using.

        I'd suggest that you do not use a loop and let the OnBarUpdate() method process through time and to do your logic 1 bar at a time. Doing a loop over the bar count for every bar is going to cause the script to take a while to load data. For example, if you have 15000 bars loaded on the chart then on bar 0 that loop is 15000 bars, then on bar 1 it is 15000 bars again, meaning the 15000*15000 iterations have to process.

        Ultimately, you would need to use add Prints to the script to make sure that whatever index you are using is a valid index at the time you are accessing it. Below is a link to a forum post that demonstrates how to use prints to understand behavior.

        https://ninjatrader.com/support/foru...121#post791121

        Indexing errors can also occur when using an invalid index with arrays or collections. Below are public links to 3rd party educational sites on arrays and index out of range errors.
        Create and loop over a string array. Access array Length and get elements at indexes.


        Let me know if I may assist further.​
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Sorry Brandon, I just cant let this go.

          I stripped everything down to the bare minimum (attached modified Indicator below) and this code blows up consistently on i = 13.

          ​I adding this to an NQ chart , 15 min, 1 day loaded to keep the bars count small for this test. Very basic setup.

          We need to have a developer load that in the debugger and find out why the close[i] is blowing up on row 13 every time.

          This has nothing to do with my code, there is something wrong with the stability of the close series.
          Attached Files

          Comment


            #6
            Hello seanhughes16,

            Thanks for your note.

            I have added prints to the script within the loop that prints out the exact index being used ('i') and the CurrentBar value.

            When adding the test script to a 15-minute NQ 03-23 chart using Calculate.OnBarClose and noting the Output window, I see that the index being used ('i') is greater than the CurrentBar value. The Output window shows an index of 183 and the CurrentBars value is 182 meaning that the index being used is not in range. See the attached image.

            If we scroll up in the Output window till we see the previous loop end and the new loop begin, 'i' is 183 and the CurrentBar value is 181, meaning the index being used is out of range.

            If we scroll up in the Output window to when CurrentBar is 0, we can see the index used goes up to '13' which is out of range for the CurrentBar on the chart. See the attached image.

            Note that the maximum index you are able to use is the same value as the CurrentBar. Say the CurrentBar value is 182, you would only be able to use an index up to 182. Say the CurrentBar value is 10, you would only be able to use a maximum index of 10.

            Instead of looping through all bars for every bar, you could consider using CurrentBar instead. The loop would then start to get larger and larger for each bar that forms on the chart instead of looping through all bars for every bar. This also means that the index being used is within a valid range according to CurrentBar.

            Code:
            for (int i = 0; i < CurrentBar; i++)
            {
                Print("i " + i + " CurrentBar: " + CurrentBar);
                BigList.Add(closes[i]);
            }
            Note that Close.IsValidDataPoint(closeCount) does not need to be used because Close should always be valid. IsValidDataPoint is used when you use the Reset function.

            When checking a Bar or PriceSeries, IsValidDataPoint() returns true as long as the barIndex value falls between 0 and the total count for that series. So it should always be true for your use case.​

            IsValidDataPoint: https://ninjatrader.com/support/help...ddatapoint.htm
            Reset: https://ninjatrader.com/support/help.../nt8/reset.htm

            If you could provide me with a brief description of what exactly you are trying to accomplish I may be able to provide further insight.

            Please let me know if I may further assist.
            Attached Files
            Last edited by NinjaTrader_BrandonH; 03-01-2023, 04:53 PM.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Right, so the answer is that you cannot use the collections' count to iterate over said collection!

              The third party library that I am using requires the entire data model (price series closes) to be loaded and called on every added bar, I have tried other tricks to just use OnBarUpdate to incrementally add just the latest close to the end of the model and refit and this does not work. That is why you see me trying to go get all the closes every Bar Update.

              I do appreciate your work around of using CurrentBar as the indexer count, that is the magic answer!~

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by lightsun47, Today, 03:51 PM
              0 responses
              4 views
              0 likes
              Last Post lightsun47  
              Started by 00nevest, Today, 02:27 PM
              1 response
              8 views
              0 likes
              Last Post 00nevest  
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              44 views
              0 likes
              Last Post futtrader  
              Started by Option Whisperer, Today, 09:55 AM
              1 response
              13 views
              0 likes
              Last Post bltdavid  
              Started by port119, Today, 02:43 PM
              0 responses
              9 views
              0 likes
              Last Post port119
              by port119
               
              Working...
              X