How are you setting the OCO ID in your code?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
OCO ID cannot bre reused. Please use a new OCO ID.
Collapse
X
-
OnState.Change()
{
if (State == State.SetDefaults)
{
BuyStopSignal = @"Buy stop order placed";
}
}
OnBarUpdate()
{
myOrder = EnterLongStopMarket(0, true, Convert.ToInt32(DefaultQuantity), buyStopLevel, BuyStopSignal.ToString());
}
OnExecutionUpdate(..................)
{
SetProfitTarget(BuyStopSignal.ToString(), CalculationMode.Price, buyProfitTarget);
SetStopLoss(BuyStopSignal.ToString(), CalculationMode.Price, buyStopLoss, false);
}
Works for about 4 trades and then I get the errors.
Comment
-
I think I managed to work around it by creating another string that gets assigned the BuyStopSignal.ToString + CurrentBar.ToSTring() on each and every order. It has made more trades now than before without any errors.
Comment
-
Comment
-
Chelsea's got a sample with ocostring
namespace NinjaTrader.NinjaScript.Strategies
{
public class UnmanagedOCOBracketExample : Strategy
{
private string ocoString;
protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
Cbi.MarketPosition marketPosition, string orderId, DateTime time)
{
// if the long entry filled, place a profit target and stop loss to protect the order
if (longStopEntry != null && execution.Order == longStopEntry)
{
// generate a new oco string for the protective stop and target
ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
// submit a protective profit target order
SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, 1, (High[0] + 20 * TickSize), 0, ocoString, "longProfitTarget");
// submit a protective stop loss order
SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, (Low[0] - 10 * TickSize), ocoString, "longStopLoss");
}
// reverse the order types and prices for a short
else if (shortStopEntry != null && execution.Order == shortStopEntry)
{
ocoString = string.Format("unmanageexitdoco{0}", DateTime.Now.ToString("hhmmssffff"));
SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Limit, 1, (Low[0] - 20 * TickSize), 0, ocoString, "shortProfitTarget");
SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.StopMarket, 1, 0, (High[0] + 10 * TickSize), ocoString, "shortStopLoss");
}
// I didn't use Order variables to track the stop loss and profit target, but I could have
// Instead, I detect the orders when the fill by their signalName
// (the execution.Name is the signalName provided with the order)
// when the long profit or stop fills, set the long entry to null to allow a new entry
else if (execution.Name == "longProfitTarget" || execution.Name == "longStopLoss" || execution.Name == "shortProfitTarget" || execution.Name == "shortStopLoss")
{
longStopEntry = null;
shortStopEntry = null;
}
}
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{
// only places orders in real time
if (State != State.Realtime || ExitOnCloseWait(marketDataUpdate.Time))
return;
// require both entry orders to be null to begin the entry bracket
// entry orders are set to null if the entry is cancelled due to oco or when the exit order exits the trade
// if the Order variables for the entries are null, no trade is in progress, place a new order in real time
if (longStopEntry == null && shortStopEntry == null)
{
// generate a unique oco string based on the time
// oco means that when one entry fills, the other entry is automatically cancelled
// in OnExecution we will protect these orders with our version of a stop loss and profit target when one of the entry orders fills
ocoString = string.Format("unmanagedentryoco{0}", DateTime.Now.ToString("hhmmssffff"));
longStopEntry = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopMarket, 1, 0, (High[0] + 15 * TickSize), ocoString, "longStopEntry");
shortStopEntry = SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.StopMarket, 1, 0, (Low[0] - 15 * TickSize), ocoString, "shortStopEntry");
}
}
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
int quantity, int filled, double averageFillPrice,
Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
{
AssignOrderToVariable(ref order);
// when both orders are cancelled set to null for a new entry
// if the exit on close fills, also reset for a new entry
if ((longStopEntry != null && longStopEntry.OrderState == OrderState.Cancelled && shortStopEntry != null && shortStopEntry.OrderState == OrderState.Cancelled) || (order.Name == "Exit on session close" && order.OrderState == OrderState.Filled))
{
longStopEntry = null;
shortStopEntry = null;
}
}
Last edited by PaulMohn; 05-10-2022, 09:47 AM.
Comment
-
Hello sprks7979,Originally posted by sprks7979 View PostI know this is an old thread but is there a way to add this to the builder. I am not exactly adept at programming and have no clue where to add the above code in my strategy after I unlock it. Any help would be appreciated. Thanks
Thank you for your note.
Coding with OCO requires an unmanaged strategy, although the Strategy Builder only uses the managed approach for order methods. With the managed approach, Profit Target, Stop Loss, and Trailing Stop orders are tied together via OCO but for other order types, OCO is not an option. Due to this limitation, you would not be able to add the above code in a strategy unlocked from the Strategy Builder.
Please let us know if we may be of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
44 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
126 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
65 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
42 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
46 views
0 likes
|
Last Post
|

Comment