Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Comparing current close to close 365 days ago

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

    Comparing current close to close 365 days ago

    HI all,

    This is just an exercise where I'm trying to figure how to work with timings between bars, in both backtesting and live, and I'm stuck on backtesting.

    I'd like to understand how to evaluate daily bar closes between 2 periods, for instance:

    if ( Close(365) > Close(0) )

    When I run a backtest on ES 09-23, I get an index out of range error.

    So I tried loading another data set.with 400 bars of history (I think)...

    else if (State == State.Configure) { AddDataSeries("ES 09-23",new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 },400,"US Equities RTH" , null); }

    Then with this evaluation

    if ( Close[0]>Closes[1][365] )

    still getting the index out of range on the backtest.

    How should this be implemented, any help greatly appreciated.

    Regards

    ​​

    #2
    Hello boxing123,

    Thank you for your post and welcome to the NinjaTrader forum community!

    A new series does not need to be added in this case. I suggest reviewing the following page in the help guide about making sure you have enough bars in the data series you are accessing:You should be able to prevent this error by doing a CurrentBar check. For example, if you want to access the Close price from 365 bars ago and compare that to the current bar's Close price, you could use a check such as the following:

    Code:
    if (CurrentBar < 365)
    return;
    
    if (Close[365] > Close[0])
    // do something
    Please let us know if we may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    47 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    23 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    33 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    51 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Working...
    X