Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsPeriods.BarsPeriodType in Backtesting

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

    BarsPeriods.BarsPeriodType in Backtesting

    Hi,

    So in NT7 I used to have a lot of multi-series indicators which worked on both day and minute charts. To do this without hardcoding them as either day or minute indicators, I would use the following so that the indicator would automatically derive the secondary series from the primary series of the chart/strategy:

    Code:
    Add("XYZ", BarsPeriods[0].Id, BarsPeriods[0].Value);
    I take it that this has now been renamed, and the NT8 equivalent is:

    Code:
    AddDataSeries("XYZ", BarsPeriods[0].BarsPeriodType, BarsPeriods[0].Value);
    However, when I have used this in an indicator which is then used within a strategy using AddChartIndicator(), I have encountered some problems. Note that these problems are found when the strategy is backtested in Strategy Analyzer. I am backtesting a daily bar strategy, and have the following line of code within my indicator:

    Code:
    Print (CurrentBars[0] + " / " + CurrentBars[1] + " : " + CurrentBars[2]);
    This then prints the following, which makes me think that the indicator is adding 1 minute bars, despite the primary bar series being daily bars:
    1 / 777 : 689
    2 / 1166 : 1034
    3 / 1555 : 1379
    4 / 1932 : 1724

    When I change the code in the indicator to the following, I get the following as expected:

    Code:
    AddDataSeries("XYZ", BarsPeriodType.Day, 1);
    1 / 1 : 1
    2 / 2 : 2
    3 / 3 : 3
    4 / 4 : 4
    5 / 5 : 5
    6 / 6 : 6

    #2
    Hello wuileng,

    Thank you for your post.

    BarsPeriod can run into this issue in both NinjaTrader 7 and 8. It is not designed to run in Initialize() (NT 7) or SetDefaults (NT 8) as the bars objects may not have loaded yet.

    While I have had luck running this in Initialize() in NinjaTrader 7 it does not always work and in most cases does not work on any ones PC that I provide the code to. This is due to the fact the bars have not loaded.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CaptainJack, 05-29-2026, 05:09 AM
    0 responses
    51 views
    0 likes
    Last Post CaptainJack  
    Started by CaptainJack, 05-29-2026, 12:02 AM
    0 responses
    30 views
    0 likes
    Last Post CaptainJack  
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    99 views
    0 likes
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    177 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    170 views
    0 likes
    Last Post CarlTrading  
    Working...
    X