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.

      JesseNinjaTrader Customer Service

      Comment


        #4
        Brilliant - that worked!!!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by RT001, Yesterday, 03:47 PM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_Jim  
        Started by johnMoss, Yesterday, 10:40 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by torch2k, Today, 06:24 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by WalterSkinner, Yesterday, 02:37 PM
        4 responses
        22 views
        0 likes
        Last Post WalterSkinner  
        Started by Fer123, Yesterday, 04:57 PM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Jim  
        Working...
        X