I am having a problem that occurs intermittently while trading live with interactive brokers using managed orders. Basically, my entry will trigger perfectly at the right time with the correct quantity. The associated stop loss and profit target (assigned OnExecution) are placed at the correct price, but sometimes with the wrong quantity. For example, upon entering the trade I end up Long 10 contracts as desired, but my stop loss and profit target are both only for 4 contracts.
If I follow the log, in the On Order the quantity is listed correctly as 10, then on execution the quantity changes to 4, and no error is generated. This has occured multiple times, and googling has lead me to things like Race conditions, etc etc. It probably occurs about 1 our of ten times if i run the strategy continuously.
I have tried it with both By Strategy Position and Per Entry Execution selected, although I hear IB forces it to strategy position anyway.
I am able to change the quantity manually after the fact, but I'd really love to be able to let this money maker run without me babysitting it.
I have attached the log window from the most recent time it happened.
Here is the related code for this specific entry.
OnBarUpdate()
// Long Entry
if (((MarketType == 0) || (MarketType == 1))
&& (other conditions)
)
{
EnterLong(Contracts, @"breaklongOrder");
}
OnOrderUpdate()
if (order.Name == "breaklongOrder")
{
breaklongOrder = order;
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
breaklongOrder = null;
}
OnExecutionUpdate()
//Long
if (breaklongOrder != null && breaklongOrder == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
SetProfitTarget(@"breaklongOrder", CalculationMode.Ticks, BreakTarget, true);
SetStopLoss(@"breaklongOrder", CalculationMode.Ticks, BreakStop, false);
if (execution.Order.OrderState != OrderState.PartFilled)
breaklongOrder = null;
}
}
Any ideas? Can I prevent this? Or if not, can I use ChangeOrder to change the quantity of the stop and target orders to equal my actual position quantity?
Could I do something like....
if (stopOrder.Quantity != longOrder.Quantity)
{
ChangeOrder(stopOrder, longOrder.Quantity, 0, Position.AveragePrice - (10 * TickSize));
}
For example?
Thanks so much for your help!
Comment