if (CurrentBars[1] < BarsRequired) { return; }
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Days to Load for Multi Time Series Strategy
Collapse
X
-
Days to Load for Multi Time Series Strategy
I am running a strategy that consists of two time series, the main time series is one minute bars and the second is one day bars. When running the strategy and accessing data on the secondary day bar time series I am presented with the error “You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.” Changing the value of “Days to load” to something ridiculously high seems to work but I shouldn’t need that much data when I am only going back as far as 5 days. All the threads I’ve read simply suggest adding the following code at the start of OnBarUpdate() to make sure there are enough bars generated before accessing the data .
Code: -
Hello cory360,
Thank you for writing in.
The issue is your text is trying to draw 10 bars back, however you need to check to ensure you have at least 10 bars on the chart. You should add the following to your code under OnBarUpdate and it should work as you expect. if(CurrentBar<10) return;
An Example:
protected override void OnBarUpdate()
{
if(CurrentBars[0 ] < 3 || CurrentBars[1] <3) return;
if(Close[0]>Close[3])
{
DrawText("test","Text to draw",10,2000,Color.Blue);
}
Reference Example: Make sure you have enough bars in the data series you are accessing
Please let us know if you need further assistance.Alan P.NinjaTrader Customer Service
-
"CurrentBar" is only used for the main time series correct? If I want to check to ensure I have enough bars on my secondary time series I need to use "CurrentBars[1]". I have tried implementing a check for both and the one checking for the main time series works just fine but the secondary never gets past 10 or whatever number I put in. Because of this it just continues to "return;" without ever reaching the rest of the code.
Comment
-
Hello Cory360,
Yes, CurrentBar would only apply to the main time series.
What is the secondary time frame you are using?
Who are you connected to?
If you go to Control Center>Tools>Historical Data Manager is the data of that secondary time series listed?
Can you open a chart with the same data you are using as part of your strategy?
I look forward to your reply.Alan P.NinjaTrader Customer Service
Comment
-
The secondary time frame I am using is day bars.
I am connected to InteractiveBrokers through their TradersWorkStation platform.
There is data for the secondary time frame in the Historical Data Manager and I am able to access it fine when running a backtest from the Strategy Analyzer. It is only when I attempt to run the strategy from the Strategies tab on the Control Centre that I run into this problem.
Lastly, I am able to open a chart with the secondary time frame just fine.
Comment
-
Hello Cory360,
Without the code I’m unable to test on my end. If you’d like to share a copy of the code so I can see if anything pops out, please do so.
I have provided a reference example using a secondary data series which might help you troubleshoot the issue.
See Synchronizing a DataSeries object to a secondary time frame.
Note: In NinjaTrader 8 It is no longer needed to use an indicator to sync a secondary series. This can be done directly from the Series<T> (https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?seriest.htm) constructor. This post is left for historical purposes. Series objects are useful for
Please let us know if you need further assistance.Alan P.NinjaTrader Customer Service
Comment
-
I wrote just a simple strategy that yields the same results. It only needs to find data for 2 days back.
Code:protected override void Initialize() { CalculateOnBarClose = true; Add(PeriodType.Day, 1); } protected override void OnBarUpdate() { if (CurrentBars[0] < 5 || CurrentBars[1] < 5) { return; } if (Closes[1][1] > Closes[1][2]) { Print("Condition is true"); } else { Print("Condition is false"); } }
Next I try modifying the code a little to print the values of "CurrentBars[0]" and "CurrentBars[1]" before the code reaches "return;".
Code:protected override void Initialize() { CalculateOnBarClose = true; Add(PeriodType.Day, 1); } protected override void OnBarUpdate() { if (CurrentBars[0] < 5 || CurrentBars[1] < 5) { Print("Minute Bars Loaded: " + CurrentBars[0]); Print("Day Bars Loaded: " + CurrentBars[1]); return; } if (Closes[1][1] > Closes[1][2]) { Print("Condition is true"); } else { Print("Condition is false"); } }
Comment
-
Hello Cory360,
Yes, that code will print 0’s for day bars.
When applying the strategy, you have Min Bars Required set to 20. See Attachment.
If you change this value to 1 and enable the strategy you should see prints for day bars loaded starting at a value of 1.
Please let us know if you need further assistance.Alan P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by strategist007, Today, 07:51 PM
|
0 responses
2 views
0 likes
|
Last Post Today, 07:51 PM | ||
Started by StockTrader88, 03-06-2021, 08:58 AM
|
44 responses
3,967 views
3 likes
|
Last Post
by jhudas88
Today, 06:59 PM
|
||
Started by rbeckmann05, Today, 06:48 PM
|
0 responses
4 views
0 likes
|
Last Post
by rbeckmann05
Today, 06:48 PM
|
||
Started by rhyminkevin, Today, 04:58 PM
|
4 responses
55 views
0 likes
|
Last Post
by dp8282
Today, 06:13 PM
|
||
Started by iceman2018, Today, 05:07 PM
|
0 responses
6 views
0 likes
|
Last Post
by iceman2018
Today, 05:07 PM
|
Comment