AddDataSeries("SPY",BarsPeriodType.Day,1,22);
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Setting a number of days in AddDataSeries
Collapse
X
-
Setting a number of days in AddDataSeries
I am trying to get the value of a 22 Period Daily Moving average. I am using the following to add the series:
I understand that to be adding 22 Daily bars. Is this not the correct way? I am getting an error that these are not the correct parameters. I just need to calc the 22 period average of those one time to set a fixed value that the indicator will use. Any help with the MA would be a bonus. ThanksCode:Tags: None
-
Hello swcookie,
The error would be correct, there is no overload pattern like this. You can find the accepted patterns in the help guide here: https://ninjatrader.com/support/help...=adddataseries
The closest to what you are asking for would be the overload that includes a number of bars to load. You could calculate the number of bars you want that would equate to 22 days and then add that. For a daily series you would just need to use 22 for 22 Bars of daily data.
Alternatively, you would add just the daily series using the normal overload set,Code:AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)
and then use 22 days to load in the chart or where the strategy is applied.Code:AddDataSeries(string instrumentName, BarsPeriodType periodType, int period)
I look forward to being of further assistance.
-
I am having a little trouble with the overload pattern. This is what I am using:
Still not able to compile. I am not seeing any real life examples on the forum or in the docs, of what this looks like when its overloaded this way. Can you assist?Code:AddDataSeries("SPY",BarsPeriodType.Day,22,"Use instrument settings",true);
Comment
-
Hello swcooke,
Thanks for the reply.
The method is expecting you to make a new BarPeriod object and enter the name of the session template you want to use:
AddDataSeries("SPY", new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, 22, "US Equities RTH", true);
To find the default trading hours template an instrument is using, go to Tools>Instruments>Search the instrument>Open the instrument configuration menu>look at the Trading Hours field.
Please let me know if I can assist further.
Comment
-
Thanks for that. I am trying to calculate the 22 period MA 1 time at the beginning of the script so that the script can use its' value. Here is what I have but it does not match the 22 period MA that I add to a chart as just a basic indicator:
Code:else if (State == State.Configure) { AddDataSeries("SPY", new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, 22, "US Equities RTH", true); } else if (State == State.DataLoaded) { Print("Value is " + SMA(BarsArray[1], 22)[0]); //this does not match what an SMA Indicator is returning for the last bar }
Comment
-
Hello swcooke,
Thank you for the reply.
In this case, you need to move your logic out of OnStateChange to OnBarUpdate. You cannot access price or indicator values in DataLoaded.
Additionally, if you access the indicator on the first bar or once, that would either be before the BarsRequiredToPlot or would just be the first value for your indicator. The SMA changes over time so to get the same values you see on the chart, you would have to check its value for each OnBarUpdate call.
I look forward to being of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
579 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
334 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
551 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment