Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Ninjascript: Grab the last (oldest) bar of a data series? Or grab the bar X days ago.

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

    Ninjascript: Grab the last (oldest) bar of a data series? Or grab the bar X days ago.

    Hello,

    I am developing an indicator and I would like the calculation to start (reset) at the oldest bar of the data that is loaded.

    To keep it simple for the sake of the example, let's say the indicator just starts at zero and adds the volume of each consecutive day to a running total.

    So, for example:

    if "oldest_bar_of_series":
    CummVolume = 0;
    else:
    start adding



    Two questions:

    1 - Say I load 10, 15, 30, 45 or 100 days of data in my chart, how can access the last, oldest bar in the series so that I set the starting value to 0 based on this oldest bar?

    2 - Secondly, say I've loaded 100 days of data (could be 1m, 5m, 30m, 60, etc chart... whatever timeframe) but I wanted to "reset" the indicator on the last X days. So X being 10, the cumulative sum would reset at "day 90". How can I accomplish this via Ninjascript?

    Thank you kindly!





    #2
    Hello focus333,

    Thanks for your post.

    When you load a script, the script will always start at the "oldest" bar in the data series and will process each bar in the data series up to the current bar. You can set your variables like this: If (CurrentBar == 0) { // set variables here;}

    For your second question, considering the way Ninjatrader loads the data in sequence, I would suggest resetting after x days. All you would need then is an int type variable to act as a counter and increment the counter as each day gets loaded and then reset variables on the 90th day. For example:

    Assumptions: int variable myDayCounter previously created and x = 90;

    if (Bars.IsFirstBarOfSession)
    {
    myDayCounter += 1; // increment day counter
    }

    if (myDayCounter == x)
    {
    // reset variables here
    }

    Reference: https://ninjatrader.com/support/help...rofsession.htm

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    656 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    371 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    109 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    574 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    579 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X