Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy start behavior
Collapse
X
-
Strategy start behavior
I have several bots that are stuck in a position due to a large gap in price when the globex opened on Sunday. This strategy places a bracket order on both sides of the position and since the current market price is still within these brackets the strategy will not take any new positions until the price hits one of the bracket orders. I have tested all 4 of the options for the start behavior and none of them allow the user to just start the strategy from the current bar and since the minimum days to load is 1 and that gap is within the most recent day I don't see any way around this other than changing the time frame trading hours to something that doesn't include that gap. Is there a way to just run a strategy starting from the current bar without using any historical data on start-up?Tags: None
-
Hello gordongekko,
Thanks for your post.
You could force your strategy to ignore all historical data and process its logic only once you are on real-time date by adding this line to the top of your strategies OnBarUpdate() logic.
Code:if(State==State.Historical)return;
Josh G.NinjaTrader Customer Service
-
Thanks for the quick reply. Generally I would prefer to leave have it start live when it the last historical trade finishes (when flat) but in situations like this I need an override. I'll add that as bool property option for these situations.
Comment
-
Will this affect any indicators or variables used by the strategy that require historical data? For example if the strategy uses the 12 26 9 macd will it have to wait at least 26 bars after being enabled to generate a trigger assuming nothing else requires more data or will it be able to fire on bar 1?
Comment
-
Thanks for clarifying this. So for this particular bot I'm going to run it with no historical data but use a time filter so the indicators will have their data but it wont be allowed to trigger until the time window is open.
Comment
-
Originally posted by NinjaTrader_JoshG View PostYes, this would effect anything that requires historical data.
That sounds different from my NT7 experience.
Just because the strategy ignores historical data doesn't mean the indicators used by the strategy also ignore historical data -- how could they?
When "used by the strategy" I mean indicator references that are created in Initialize() or OnStartUp() -- before OnBarUpdate() is ever called.
Comment
-
Originally posted by gordongekko View PostWill this affect any indicators or variables used by the strategy that require historical data? For example if the strategy uses the 12 26 9 macd will it have to wait at least 26 bars after being enabled to generate a trigger assuming nothing else requires more data or will it be able to fire on bar 1?
That is, this line of code,
Code:if(State==State.Historical)return;
Comment
-
Some confusion may exist around how you use the indicators.
For example, say you access MACD(12,26,9) directly, with full arguments, and you don't access the indicator any other way ... for example,
Code:if(State==State.Historical)return; if(MACD(12,26,9) <test against condition ABC>) EnterLong(); else if (MACD(12,26,9) <test against condition XYZ>) EnterShort();
EDIT:
I'm not strictly sure if what I said above is true, but it's the working model I have in my head. NinjaTrader support, could you please confirm everything in this posting?
But you can get around this 'limitation' by making the instantiation happen sooner.
How? Create a reference to MACD(12,26,9) before calling OnBarUpdate.
How? Easy, by adding MACD(12,26,9) to the chart.
If you use AddChartIndicator(MACD(12,26,9)), the MACD indicator will process all historical data in its own OnBarUpdate, despite the 5 lines of code above.
Make sense?
It all depends where in your strategy it first creates the reference to MACD(12,26,9).
The use of AddChartIndicator provides this very subtle and rarely talked about feature -- it allows the indicator to be instantiated in time such that it is guaranteed to process all historical bars in its own OnBarUpdate.
If you don't use AddChartIndicator, or create a reference to the indicator via some other means, then yes, the 5 lines of code above mean that on the first call to MACD(12,26,9) inside of OnBarUpdate, MACD will start processing with realtime data only (but all historical data is available to it, it just didn't process any of it inside its own OnBarUpdate).
But, the story gets more complicated:
Now we bump up against NinjaTrader's indicator caching code in the auto generated NinjaScript code at the bottom of every indicator. If some where else in NinjaTrader, such as in a chart or in another strategy, the MACD(12,26,9) has already been referenced -- then your strategy gets a copy of that previous created reference from the indicator cache -- and that copy will have most likely already processed all the historical data that your strategy is trying to skip over -- which is fine and expected and a good thing for you.
Bottom line:
Just use AddChartIndicator() and you'll have no issues.
Comment
-
bltdavid,
Couple of things here.
If you use the line if(State==State.Historical)return; in your strategies OnBarUpdate() than it will not process its logic until you reach State.Realtime. If your indicator does not have that same line in its code, it will still process historical data. Bottom line is no entry logic will be processed on historical data.
Also, AddChartIndicator() is simply used for displaying an indicator on a chart from within a strategy. It would not be used to load more historical data to process your logic on.
Help Guide - AddChartIndicator()
Please let me know if this doesn't clear everything up.Josh G.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_JoshG View Postbltdavid,
Couple of things here.
If you use the line if(State==State.Historical)return; in your strategies OnBarUpdate() than it will not process its logic until you reach State.Realtime. If your indicator does not have that same line in its code, it will still process historical data. Bottom line is no entry logic will be processed on historical data.
Also, AddChartIndicator() is simply used for displaying an indicator on a chart from within a strategy. It would not be used to load more historical data to process your logic on.
Help Guide - AddChartIndicator()
Please let me know if this doesn't clear everything up.
If your strategy has this in its OnBarUpdate(),
Code:if(State==State.Historical)return; if(MACD(12,26,9) <test against condition ABC>) EnterLong(); else if (MACD(12,26,9) <test against condition XYZ>) EnterShort();
I mean, clearly, the MACD(12,26,9) is not being called with historical data, so that brings into question your statement in red below,
Originally posted by NinjaTrader_JoshG View PostIf you use the line if(State==State.Historical)return; in your strategies OnBarUpdate() than it will not process its logic until you reach State.Realtime. If your indicator does not have that same line in its code, it will still process historical data.
So, yes, we all know MACD is capable of processing historical data, but does it actually process historical data in this case above or not? If yes, then how?
Comment
-
Originally posted by NinjaTrader_JoshG View PostAlso, AddChartIndicator() is simply used for displaying an indicator on a chart from within a strategy. It would not be used to load more historical data to process your logic on.
My point is not more historical data, it's whether the indicator will see any historical data in its own OnBarUpdate.
I know what AddChartIndicator() does.
But you failed to respond directly to my comment that the reference passed to AddChartIndicator() is of any importance.
I'm saying that that indicator reference is important.
That act of creating that reference to pass to AddChartIndicator() happens to provide a very subtle service to the strategy, which is (see my previous post): "it allows the indicator to be instantiated in time such that it is guaranteed to process all historical bars in its own OnBarUpdate."
Is that statement true or false?
Comment
-
In the situation you are describing the indicator can see its own historical data. The strategy can see that historical data as well, but will not process its logic until it's in State.Realtime
But you failed to respond directly to my comment that the reference passed to AddChartIndicator() is of any importance.
I'm saying that that indicator reference is important.
That act of creating that reference to pass to AddChartIndicator() happens to provide a very subtle service to the strategy, which is (see my previous post): "it allows the indicator to be instantiated in time such that it is guaranteed to process all historical bars in its own OnBarUpdate."
Is that statement true or false?
Are you implying that if you used the indicator elsewhere and do not pass a reference into AddChartIndicator that it would not be guaranteed to process all historical bars in its own OnBarUpdate?Josh G.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_JoshG View PostIn the situation you are describing the indicator can see its own historical data. The strategy can see that historical data as well, but will not process its logic until it's in State.Realtime
Originally posted by NinjaTrader_JoshG View PostThat would not help the indicator to process more or less historical data in its own OnBarUpdate(). I believe it is more efficient than defining that indicator each time, but it will not boost your historical data loading times.
Originally posted by NinjaTrader_JoshG View PostAre you implying that if you used the indicator elsewhere and do not pass a reference into AddChartIndicator that it would not be guaranteed to process all historical bars in its own OnBarUpdate?
At least, that's what I keep asking you for confirmation.
If this is the only code in the strategy where MACD(12,26,9) is called,
Code:protected override void OnBarUpdate() { if(State==State.Historical)return; if(MACD(12,26,9) <test against condition ABC>) EnterLong(); else if (MACD(12,26,9) <test against condition XYZ>) EnterShort(); }
We all know MACD is capable of processing historical data, but in the above case, what happens? Does the MACD(12,26,9) process any historical data or not?
If you assert that the MACD(12,26,9) will process the same amount of historical data that the strategy processes (the strategy effectively ignores all its historical data, but it still sees all of it and processes all of it by returning early), then please explain how MACD(12,26,9) is able to see those same historical bars?
For example, using the OnBarUpdate above, if the first realtime bar is CurrentBar=5000, does the MACD(12,26,9) process historical bars 0 - 4999 or not?
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by samish18, Today, 10:13 AM
|
0 responses
2 views
0 likes
|
Last Post
by samish18
Today, 10:13 AM
|
||
Started by kenz987, Yesterday, 10:20 AM
|
2 responses
12 views
0 likes
|
Last Post
by kenz987
Today, 10:13 AM
|
||
Started by nicthe, 08-23-2023, 07:53 AM
|
7 responses
196 views
0 likes
|
Last Post
by nicthe
Today, 09:26 AM
|
||
Started by nicthe, Today, 09:24 AM
|
0 responses
5 views
0 likes
|
Last Post
by nicthe
Today, 09:24 AM
|
||
Started by stalt, 12-28-2015, 01:36 PM
|
6 responses
1,536 views
0 likes
|
Last Post
by giulyko00
Today, 09:16 AM
|
Comment