Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stuck with MIN function in ninjascript

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

    Stuck with MIN function in ninjascript

    I am looking to define a variable which is the minimum of all bars, except the current one (over a lookback period)

    int Lookback = 10
    double minvalue = MIN(Low, Lookback)[1];

    but this throws an error

    System.ArgumentOutOfRangeException
    HResult=0x80131502
    Message=Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    Source=mscorlib
    StackTrace:
    at System.ThrowHelper.ThrowArgumentOutOfRangeExceptio n(ExceptionArgument argument, ExceptionResource resource)
    at NinjaTrader.NinjaScript.Series`1.get_Item(Int32 barsAgo)
    at NinjaTrader.NinjaScript.NinjaScriptBase.get_Item(I nt32 barsAgo)
    ...
    at NinjaTrader.NinjaScript.NinjaScriptBase.Update(Int 32 idx, Int32 bip)

    I checked and only Low[0] and Low[1] exist, Low[2] hasnt been formed when this error occurs.

    But Low.Count is 1040 (although in the Watch Window, under Low, Count actually says zero)

    How can I check myself if the data has enough values to use this method?

    #2
    Just tested the same code on an empty chart and it worked just fine. I think it has to do with another indicator that was working with multiple time frames. Not sure how to fix it yet

    Comment


      #3

      Hello crystalet,

      Thank you for the post.

      You could use CurrentBar as the Lookback if you wanted all bars, CurrentBar is the processing bars index. If you are on bar 2 CurrentBar will equal 2 and the MIN would see that as the MIN of the 2 bars.

      Code:
      double minvalue = MIN(Low, CurrentBar)[1];
      The [1] BarsAgo would be correct to get the 1 BarsAgo value from now or minus the current bar. The CurrentBar increments for each new bar. You would very likely need to add a condition if(CurrentBar < 1) return; because you used 1 BarsAgo here

      Alternatively to check that enough bars are available for the Lookback you would do:

      Code:
      int Lookback = 10;
      if(CurrentBar < Lookback) return;
      double minvalue = MIN(Low, Lookback)[1];

      I look forward to being of further assistance.

      Comment


        #4
        Brilliant - that worked!!!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        646 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        367 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X