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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        43 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        21 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        30 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        50 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        40 views
        0 likes
        Last Post CarlTrading  
        Working...
        X