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 NullPointStrategies, Today, 05:17 AM
    0 responses
    53 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    49 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X