Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using data series in loops

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

    Using data series in loops

    Hi.
    How to use OHLC data in "for" loops?

    For example, suppose you want to add up Open values in the last 50 bars (Decussate).

    I think the answer is:

    double S=0;

    for( int i=0 ;i<50 ; i++)

    if (i%2==0) S=S+Open[i];

    Value[0]=S;

    (Note that the above example has no specific logic and is just an example. You can implement any other logic in the loop.)
    -----------
    BUT!

    In any loop, you can not use expressions such as Open[i] or Bars.GetOpen(i) or Open.GetValueAt(i), etc. (None of them worked for me.)

    My question is simple. How can calculations be performed on the last few bars that cannot be performed with the default functions(Such as MAX , MIN , SUM , etc ) and require a "for" loop to write them manually?

    #2
    Hello alichi50,

    Thanks for your post.

    You can run a for loop as you wish.

    An important consideration in doing so is understanding how a script is applied to the data. When you apply a script to a chart, the code you have written will start with the very first historical bar of the data series and will execute your code as you have created it. However, if you try to access a prior bar then the script will fail with a run time error of bar indexing. Your for loop example would be doing just that.

    So if you are looping back 50 bars, then your code must wait until the script has processed the first 50 bars at least.

    The way to have your code wait is to check the systems CurrentBar which is a bar counter as the first line in your OnBarUpdate().

    For example:

    if (CurrentBar < 50) return; // do not process below this line until we are on bar 50 or greater.
    Reference: https://ninjatrader.com/support/help...currentbar.htm

    Bars.GetOpen() does not take a "bars ago" as the input, rather it uses specific bar numbers. You could do something like Bars.GetOpen(CurrentBar-i)

    The same would be true for Open.GetValueAt() as again it would need to be a specific bar number.

    I would suggest just sticking with the Open, High, Low, and Close data series, and their reference again is a bars ago.





    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    637 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    366 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    107 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
    571 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X