That makes 'Out of range errors' so much stranger, then. Please see my other post below, similar with the errors I am talking about in my first post on this thread. How can I possibly get 'Out of range errors' when I have 5 days of data on my chart?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy triggering before BarsRequiredToTrade
Collapse
X
-
Right... I think I get where the confusion originates: I thought the 9 bars I set up are the number of bars since I activated the strategy, but from your post it looks like they are the first historical bars from the start of the chart?
That makes 'Out of range errors' so much stranger, then. Please see my other post below, similar with the errors I am talking about in my first post on this thread. How can I possibly get 'Out of range errors' when I have 5 days of data on my chart?
-
Hello itrader46,
Correct your script will process historical data first. 9 bars will be starting from bar 0 of historical data, the error you were seeing is happening farther into the processing past that point.I thought the 9 bars I set up are the number of bars since I activated the strategy, but from your post it looks like they are the first historical bars from the start of the chart?
Regarding the other post and errors, I believe it would be good to clarify some of the information there as the error is the same as this post.
You have two different errors listed, the first error with EventHandlerBarsUpdate is not something we have a documented solution or case for. This error will require debugging the script further to isolate what code is causing that to happen so we can figure out how to prevent that.
The other error relating to BarsAgo is a generic error that is produced any time an error occurs based on data usage, this could just be a side effect of the first error. We will need to reduce the test case and find a situation to explore these errors.
One part I want to clarify in the other post:
I am not certain this is completely accurate toward your case. If you do not have a "EventHandlerBarsUpdate" method or property specifically coded in your script than this information would not apply. You need to debug your code to isolate the issue however the error is likely being displayed as a result of the situation and syntax used so we would need to know what syntax and situation to further explore this.This would mean that you are referencing an object that is null. This is not the same thing as an index out of range error which would be seen if you are referencing some BarsAgo value before the script processes that many bars.
I suggest debugging the code in your EventHandlerBarsUpdate method by adding prints so you can find out which specific line is creating the "Object reference not set to an instance of an object" error. (The output is hinting that this error is being thrown in your EventHandlerBarsUpdate method.)
I understand that debugging the script can be tedious however you are currently in the best position to see the problem. It would be best if you can try to further isolate a specific test case where we can explore these errors in more detail. The steps to explore the problem are important here because you noted this is pretty random. If you are able to isolate any steps where we can see this even a couple of times that would be helpful to explore the errors further.
I look forward to being of further asisstance.
Comment
-
Having said that, I would have expected the code below to print the first bar on chart (Bar 0), but it doesn't, as you can see below the code
PrintsCode:protected override void OnBarUpdate() { // Make sure this strategy does not execute against historical data if (State == State.Historical) return; if (!strategyStart) { Print ("Strategy start bar: " + CurrentBar + " - " + Time[0]); strategyStart = true; }
" Enabling NinjaScript strategy 'ATM2/178978059'
Strategy start bar: 4480 - 21/10/2019 05:01:00 "
Whereas this code produces the prints below,
PrintsCode:protected override void OnBarUpdate() { if (!strategyStart) { Print ("Strategy start bar: " + CurrentBar + " - " + Time[0]); strategyStart = true; } // Make sure this strategy does not execute against historical data if (State == State.Historical) return;
" Strategy start bar: 0 - 15/10/2019 23:01:00
Enabling NinjaScript strategy 'ATM2/178978060'
----------
Short condition at: 53.74 - Current Bid: 53.74 - R = 1 - Open: 53.76 - Current Bar: 4483 @ 21/10/2019 05:05:00
Short ATM triggered at: 53.74 Open: 53.76 - Current Bar: 4483 Short order bar = 4483 - 21/10/2019 05:05:00 "
So basically, it looks like the strategy doesn't execute before the " if (State == State.Historical) return; ", just as I expected.
Does that mean that, when you say
that I should never ever get Out of Range exception, unless I set an indicator with a period of over 5000?Correct your script will process historical data first. 9 bars will be starting from bar 0 of historical data, the error you were seeing is happening farther into the processing past that point.
Does the script actually process historical data, coz I thought it wasn't with ATM strategies?
In that case, the error message is completely ...erroneous to begin with and doesn't offer the slightest bit of hint as to what the issue is.
Regarding those errors, since I added this code (OnBarUpdate), I've never had them anymore. Do you think that would have anything to do with it, or they are just not coming now and they might return again?
Code:else if (State == State.DataLoaded) { T31b0 = T3(Closes[0], 5, 3, 0.7); T32b0 = T3(Closes[0], 8, 3, 0.7); T31b1 = T3(Closes[1], 5, 3, 0.7); T32b1 = T3(Closes[1], 8, 3, 0.7); T31b2 = T3(Closes[2], 5, 3, 0.7); T32b2 = T3(Closes[2], 8, 3, 0.7); T31b0.Plots[0].Brush = Brushes.Transparent; T32b0.Plots[0].Brush = Brushes.Transparent; T31b1.Plots[0].Brush = Brushes.Transparent; T32b1.Plots[0].Brush = Brushes.Transparent; T31b2.Plots[0].Brush = Brushes.Transparent; T32b2.Plots[0].Brush = Brushes.Transparent; AddChartIndicator(T31b0); AddChartIndicator(T32b0); AddChartIndicator(T31b1); AddChartIndicator(T32b1); AddChartIndicator(T31b2); AddChartIndicator(T32b2); protected override void OnBarUpdate() { if (CurrentBars[0] < 9 || CurrentBars[1] < 9 || CurrentBars[2] < 9 /*|| CurrentBars[3] < 9*/) return; if (T31b0 == null || T32b0 == null || T31b1 == null || T32b1 == null || T31b2 == null || T32b2 == null) { T31b0 = T3(Closes[0], 5, 3, 0.7); T32b0 = T3(Closes[0], 8, 3, 0.7); T31b1 = T3(Closes[1], 5, 3, 0.7); T32b1 = T3(Closes[1], 8, 3, 0.7); T31b2 = T3(Closes[2], 5, 3, 0.7); T32b2 = T3(Closes[2], 8, 3, 0.7); }Last edited by itrader46; 11-14-2019, 12:35 PM.
Comment
-
Hello itrader46,
You are using a return statement for historical, this should only print in realtime and would not likely be the first bar unless there was no data present. It could be a high number depending on the amount of historical data you skipped over or a low number if limited data is present.Having said that, I would have expected the code below to print the first bar on chart (Bar 0), but it doesn't, as you can see below the code
Code:
protected override void OnBarUpdate()
{
// Make sure this strategy does not execute against historical data
if (State == State.Historical)
return;
if (!strategyStart)
{
Print ("Strategy start bar: " + CurrentBar + " - " + Time[0]);
strategyStart = true;
}
Correct because you put the print before the return now. Return will prevent code execution beyond that point or returns out of the method.Whereas this code produces the prints below,
In general a script will normally process from bar 0 which is what your script does also. You are additionally returning for historical in your conditions. If realtime happens to be larger than 9 bars the remainder of your conditions checking for data wouldn't apply because they are after the return. If there is limited data and you had less than 9 bars entering realtime for one of the series you may see your later conditions being used checking for data.So basically, it looks like the strategy doesn't execute before the " if (State == State.Historical) return; ", just as I expected.
Does that mean that, when you say
Correct your script will process historical data first. 9 bars will be starting from bar 0 of historical data, the error you were seeing is happening farther into the processing past that point.
that I should never ever get Out of Range exception, unless I set an indicator with a period of over 5000?
In your specific error it was over bar 5000 for that series so it looks like that was farther into processing however that could be just entering realtime after processing historical. I have no context of the test you did to understand the print in contrast to the logic.This is a situation where debugging and finding the combination of steps used and syntax would be needed to further understand the error better.
Yes your script will still have its OnBarUpdate called normally, only managed strategy properties won't apply to ATM's. An ATM works outside your strategy so your strategy can only communicate with it or initiate it, other Managed or Unmanaged strategy properties (position, pnl, performance etc) do not apply to ATMs.Does the script actually process historical data, coz I thought it wasn't with ATM strategies?
I couldn't say as I don't have the full context of the original problem, if that is what you have identified as the problem and that has solved it we could likely conclude the case. If you wanted to explore that in more detail I would suggest extracting the code required and making it into a specific sample that you can attach so that we could explore that directly.Regarding those errors, since I added this code (OnBarUpdate), I've never had them anymore. Do you think that would have anything to do with it, or they are just not coming now and they might return again?
Please let me know if I may be of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
54 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
130 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
72 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment