Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
OnExecution error
Collapse
X
-
Thanks Bertrand, but it won't work at all. In fact it will crash because you refer twice to a null object - execution.Order and execution.Order.Name.Originally posted by NinjaTrader_Bertrand View PostDave, perhaps that attached example helps to show how to get the reference needed.
That is the whole point I am making.
regards
Dave
Comment
-
Yes, I am. Try this :
Gives:Code:protected override void Initialize() { ExitOnClose = true; ExitOnCloseSeconds = 600; } protected override void OnBarUpdate() { if (Time[0].Hour == 10 && Position.MarketPosition == MarketPosition.Flat) EnterLong(); } protected override void OnExecution(IExecution e) { try { Print("EE "+e.ToString()); if (e.Order == null) Print("About to crash"); Print("OO "+e.Order.ToString()); } catch (Exception ee) { Print("Ex: "+ee.ToString()); } }
Code:OO Order='NT-00014/Backtest' Name='Buy' State=Filled Instrument='ES ##-##' Action=Buy Limit price=0 Stop price=0 Quantity=1 Strategy='test2' Type=Market Tif=Gtc Oco='' Filled=1 Fill price=1275.5 Token='0c65ad0b2e0c4b36a6b398112562fa57' Gtd='01/12/2099 00:00:00' EE Execution='NT-00015' Instrument='ES ##-##' Account='Backtest' Name='Exit on close' Exchange=Default Price=1281.5 Quantity=1 Market position=Short Commission=2.5 Order='NT-00015' Time='24/06/2011 00:00:00' About to crash Ex: System.NullReferenceException: Object reference not set to an instance of an object. at NinjaTrader.Strategy.test2.OnExecution(IExecution e)
Comment
-
In that case, it looks like you might be better keying off the name of the order then.Originally posted by dave1992 View PostCome on Josh, of course it's a proper order. It has an order ID and everything. I need to detect Exit on Close in OnExecution since I run an unmanaged strategy.
See order update logging below.
31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=PendingSubmit Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quan
31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=Accepted Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quantity=
31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=Working Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quantity=1
31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=Filled Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quantity=1
Comment
-
Originally posted by dave1992 View PostThe name of the order doesn't exist in OnExec because the order is null. That's the whole point.The order name does exist at OnOrderUpdate(). It would appear that this is an NT internally generated order that has no associated, exposed, IOrder object, much like we have with the SetStopLoss() etc statements for OCO control.31/05/2011 09:49:00 Order Update = Order='NT-00018/Backtest' Name='Exit on close' State=Filled Instrument='FGBS ##-##' Action=Sell Limit price=0 Stop price=0 Quantity=1Last edited by koganam; 07-07-2011, 11:53 AM.
Comment
-
I agree with you, and will call it a design oversight rather than a bug or limitation. At the very least it shows an inconsistency in order processing, but that seems to be just the way it is.Originally posted by dave1992 View PostOh dear. If there is no IOrder object, what gets passed to OnOrderUpdate?
Sometimes we must perforce code with what we have: it is just one of the constraints of using a closed framework.
Comment
-
Dave, they would simply not expose it, very much like SetStopLoss / SetProfitTarget, thus we have to capture it : http://www.ninjatrader.com/support/f...ead.php?t=5790
Comment
-
No! SetStopLoss order is exposed in OnExec exactly like you'd expect it to be. Please stop just guessing the explanation to this.Originally posted by NinjaTrader_Bertrand View PostDave, they would simply not expose it, very much like SetStopLoss / SetProfitTarget, thus we have to capture it : http://www.ninjatrader.com/support/f...ead.php?t=5790
You know, often the simplest answer is the truth. Instead of continually making up clever internal reasons that simple users like me should just accept, the simplest answer is that it's a bug and there's a line of code somewhere where someone forgot to assign the order to Execution.order. I'm not saying there's not a more complex internal reason, but then that's still very bad design. If an order is executed, I expect access to it when it's executed, like EVERY other order in the system,
Comment
-
Dave,
To clarify, there are no references for orders from Set() methods just like ExitOnClose. If you want them you need to make them. That is what Bertrand is saying and this is 100% accurate. There is no "guessing" going on in this regard. Set() method orders and ExitOnClose orders both have no way for you to assign an IOrder object for them during order submission. That is what is meant by "no reference" for them. You can't do something like "myIOrder = SetStopLoss(CalculationMode.Price, 100);". There is no reference unless you "capture" it and make your own as demonstrated by the reference sample linked earlier. Hopefully that makes it clear what we are talking about.
Now back to the question at hand, I am unfortunately not sure what you are referring to exactly. The code Bertrand posted shows execution.Order is not null for ExitOnClose in OnExecution(). Furthermore there are several ways you can identify the order from OnExecution() being outlined in the code. You can do execution.Order.Name == "Exit on close" or you can do execution.Name == "Exit on close". Both of which would provide the same positive ID.
Here is the output tagged with where the events came from:
The printouts here show the order as positively IDed from OnOrderUpdate(), generically printed from OnExecution() showing that execution.Order is not null, then positively IDed from OnExecution().Code:ONORDERUPDATE: Order='NT-00265/Backtest' Name='Exit on close' State=PendingSubmit Instrument='ES 09-11' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='TrackExitOnClose' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='99dfc9b7367243c889ba0d831312a734' Gtd='12/1/2099 12:00:00 AM' ONORDERUPDATE: Order='NT-00265/Backtest' Name='Exit on close' State=Accepted Instrument='ES 09-11' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='TrackExitOnClose' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='99dfc9b7367243c889ba0d831312a734' Gtd='12/1/2099 12:00:00 AM' ONORDERUPDATE: Order='NT-00265/Backtest' Name='Exit on close' State=Working Instrument='ES 09-11' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='TrackExitOnClose' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='99dfc9b7367243c889ba0d831312a734' Gtd='12/1/2099 12:00:00 AM' ONORDERUPDATE: Order='NT-00265/Backtest' Name='Exit on close' State=Filled Instrument='ES 09-11' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='TrackExitOnClose' Type=Market Tif=Gtc Oco='' Filled=1 Fill price=1351.75 Token='99dfc9b7367243c889ba0d831312a734' Gtd='12/1/2099 12:00:00 AM' ONEXECUTION: Order='NT-00265/Backtest' Name='Exit on close' State=Filled Instrument='ES 09-11' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='TrackExitOnClose' Type=Market Tif=Gtc Oco='' Filled=1 Fill price=1351.75 Token='99dfc9b7367243c889ba0d831312a734' Gtd='12/1/2099 12:00:00 AM' execution.Name: Exit on close execution.Order.Name: Exit on close
Also, copying your OnExecution() code snippet did not print anything out for e.Order == null because it is not actually null on our end. So the question going forward is what is different between your testing environment versus ours. We are using 7.0.1000.6 in the Strategy Analyzer. How are you testing on your end?Attached FilesJosh P.NinjaTrader Customer Service
Comment
-
You do not need to make them You can just refer to them like Execution.order. Let me say it explicitly. The IOrder object for SetStopLoss is available in OnExec - if Bertrand is saying it's fine for Execution.order to be null for this type of order (and he has said/implied that a few times in this thread) then he is wrong that they are treated the same.Originally posted by NinjaTrader_Josh View PostDave,
To clarify, there are no references for orders from Set() methods just like ExitOnClose. If you want them you need to make them. That is what Bertrand is saying and this is 100% accurate.
Either decide that my null is a problem, and deal with that, or decide that null is OK for Exit on Close and deal with that, but don't mix up both which is what has been happening on this thread. I mean how do you think it looks to me when I say it's null and I get a suggestion to just check Execution.order, and then I get a comment saying it's OK that it's null, you just have to check == null, then I get you have to check OnOrderUpdate and what possible purpose could I have for checking the order in the execution object in the first place.
This wouldn't have even been a misunderstanding if you guys had read what I wrote. Clearly assigning IOrder doesn't come into it because there is no method to generate an ExitOnClose!! There's nothing for me to call. I've NEVER talked about generating an order. I'm only talking about the IOrder object in the Execution object.There is no "guessing" going on in this regard. Set() method orders and ExitOnClose orders both have no way for you to assign an IOrder object for them during order submission. That is what is meant by "no reference" for them. You can't do something like "myIOrder = SetStopLoss(CalculationMode.Price, 100);". There is no reference unless you "capture" it and make your own as demonstrated by the reference sample linked earlier. Hopefully that makes it clear what we are talking about.
How can you be unsure?? I can't say it more plainly. Look at what I said in post 8Now back to the question at hand, I am unfortunately not sure what you are referring to exactly. The code Bertrand posted shows execution.Order is not null for ExitOnClose in OnExecution(). Furthermore there are several ways you can identify the order from OnExecution() being outlined in the code. You can do execution.Order.Name == "Exit on close" or you can do execution.Name == "Exit on close". Both of which would provide the same positive ID.
How can you be unsure what I'm referring too? Same thing that is referred to in at least 9 other posts in this thread.I'm getting execution.order = null every time for exit on close
It was null for another user too. It's not just me who is a crazy user. I've been coding long enough to know that if an object is sometimes null where you wouldn't expect it to be, it's really likely to be a bug. If it was null for everyone, it could be a feature or poor design right? I'm using 7.0.1000.6 as I said. Testing with strat analyzer.
thanks
Dave
Comment
-
Dave,
Are you using OnOrderUpdate() in your code? Please ensure you have this method even if you are leaving it blank.
Please follow the example either I posted or what Bertrand has posted. You will not find any issues when following the framework outlined there.Josh P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
652 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 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
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
577 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment