Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
OCO Error
Collapse
X
-
OCO Error
The order my strategy placed live this morning was rejected with the message that the OCO number can't be reused. Why would this be? Aren't the OCO numbers generated internally (or, at least not by the strategy/ user). How do I fix this?Tags: None
-
Hello spottysallrite,
Are you using a strategy builder script or the managed approach in manual coding? This error can happen if you are using unmanaged and generating your own OCO ids. If you are using the managed approach and seeing this we would need more specific details about what code was used and what situation you see this happening to further assist.
-
Using unmanaged. Looking closer at my code. Perhaps my iteration isn't working correctly. Wasn't a problem when backtesting.
Comment
-
Thanks for clarifying, yes please check where you make the OCO string first as that would relate to this message. Either of the following should work for generating an OCO, I have also linked a unmanaged sample that uses OCO.
Code:string ocoString = GetAtmStrategyUniqueId(); string ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
https://ninjatrader.com/support/help...gyuniqueid.htm
https://ninjatrader.com/support/foru...elp#post770579
Comment
-
My iteration is to simply count the trades (lacking a better idea then). Looks like the update to this didn't occur when the new trade was entered. mmm ....
Comment
-
Ok, I ran a bktst using the above suggestion of "string ocoString = GetAtmStrategyUniqueId();" and received the following error message, "
Strategy 'LAA13b/-1': Error on calling 'OnExecutionUpdate' method on bar 2421: 'GetAtmStrategyUniqueId' method can only be called in state 'Realtime' ". So this isn't useful.
Comment
-
hmm.. Ok, where is this best updated? within "OnOrderUpdate", or "OnExecutionUpdate", or "OnBarUpdate", or someplace else ?
Comment
-
Well, I had been producing it from within "OnOrderUpdate", and on the buy entry "order.OrderState == OrderState.Filled". The only place TradesCtTtl is incremented is here. This has worked fine when doing backtests, but not in live trading.
For example (all trades are long), for entering the first trade (ie, buy) and "order.OrderState == OrderState.Filled" TradesCtTtl = 1. Somehow the incrementing of TradesCtTtl was triggered on the sell order (TradesCtTtl now equal to 2), and was not updated when that next buy order was filled (TradesCtTtl still equal to 2). How could this be?
Comment
-
Hello spottysallrite,
As long as the OCO is being generated right before each set of paired orders and you are ensuring that whatever method to generate the ID makes a unique ID that should work.
If you have your OCO string generation in some other part of the script which is happening out of sync with the orders then that will cause issues.
An OCO is just a unique string that both orders share so that when one order fills the other gets cancelled. If the platform sees old orders with that OCO id it will cause an error because that means it won't know which orders to pair up to make sure the opposite is cancelled.
Comment
-
Well, my OCO is an integer that is incremented when the filled state occurs (ie, "order.OrderState == OrderState.Filled), and that from within "OnOrderUpdate()". That OCO number is then provided to the exit orders when they are created from within "OnExecutionUpdate / MktPosn == MarketPosition.Long.
What's odd is that the OCO numbers shown in Control Center/ Orders are not consistent with the OCO numbers that are printed on the Output window. How could that be?
Comment
-
Hello spottysallrite,
I would not suggest using a simple integer for the OCO but rather use something like either of the samples that I provided. If your script has an int that starts at 0 and the strategy submits 10 OCO orders live there will be 10 existing OCO id's used. If you restart that strategy and your int starts at 0 again it will generate errors if any of those ids are re used for a live order.
An always unique string like the DateTime example is suggested so that you never repeat the ids no matter how many times you may restart it.
If you are using a live account and a broker that supports native oco then the control center may show a different ID for the active orders. You still need to submit a valid unique OCO when you initially create the orders to avoid having errors.
Comment
-
Looks like I've got this fixed. I had the same variable for the OCO in the buy order as in the sell orders. I took that out it seems to be working fine. Gads. (Seems like this would have been flagged in the backtesting.)
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
62 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
134 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
75 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
45 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
50 views
0 likes
|
Last Post
|

Comment