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

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 Rapine Heihei, Today, 08:19 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 08:25 PM
        0 responses
        5 views
        0 likes
        Last Post Rapine Heihei  
        Started by f.saeidi, Today, 08:01 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 07:51 PM
        0 responses
        6 views
        0 likes
        Last Post Rapine Heihei  
        Started by frslvr, 04-11-2024, 07:26 AM
        5 responses
        98 views
        1 like
        Last Post caryc123  
        Working...
        X