I have a piece of code that reverses my position at the end of a bar.
Orders for exit from the existing position and entry into the new position are sent together at the same time. The code is below.
This code works MOST of the time as expected. For example, if I am 30K short, I see ONE order to buy 60K that puts me in a 30K long position.
But sometimes, ONLY the exit order is placed and not the entry. I don't see any rejected or cancelled orders or error messages. I am at a complete loss in debugging this issue. I do not see any pattern to identify why this is happening. Does anyone have any idea how I can find and address this issue?
Here is the code that sends the orders:
Code: Select all
protected override void Create()
{
Output.Clear();
//entry orders, when bars switch direction
m_LongEntryOrder = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));
m_ShortEntryOrder = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.SellShort));
//protect and profit orders when we have a LONG entry
m_ProtectOrderSell = OrderCreator.Stop(new SOrderParameters(Contracts.UserSpecified, "P-Sell", EOrderAction.Sell, OrderExit.Total));
m_LockOrderSell = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "L-Sell", EOrderAction.Sell, OrderExit.Total));
//protect and profit orders when we have a SHORT entry
m_ProtectOrderBuy = OrderCreator.Stop(new SOrderParameters(Contracts.UserSpecified, "P-Buy", EOrderAction.BuyToCover, OrderExit.Total));
m_LockOrderBuy = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "L-Buy", EOrderAction.BuyToCover, OrderExit.Total));
//Exit orders when bars switch direction
m_LongExitOrder = OrderCreator.MarketThisBar(new SOrderParameters(EOrderAction.Sell, "LongExit"));
m_ShortExitOrder = OrderCreator.MarketThisBar(new SOrderParameters(EOrderAction.BuyToCover, "ShortExit"));
}
protected void SendEntryOrder(EOrderAction EntryAction)
{
switch (EntryAction)
{
case EOrderAction.Buy:
m_ShortExitOrder.Send(CurrentPos);
m_LongEntryOrder.Send(EntryTradeSize);
m_LockOrderSell.Send(getProfitTarget(), LockInTradeSize);
m_ProtectOrderSell.Send(getStopTarget(), EntryTradeSize);
break;
case EOrderAction.SellShort:
m_LongExitOrder.Send(CurrentPos);
m_ShortEntryOrder.Send(EntryTradeSize);
m_LockOrderBuy.Send(getProfitTarget(), LockInTradeSize);
m_ProtectOrderBuy.Send(getStopTarget(), EntryTradeSize);
break;
default:
break;
}
}
Erol