I'm trying to code a strategy which enters in either long or short at a certain point. When a condition is true it submits a buy limit order with EnterLongLimit and a short limit order with EnterShortLimit. Whichever fills, the other is cancelled (in the OnExecution method).
I also use a stoploss, by using the ExitLongstop/ExitShortStop functions in the OnExecution method.
The code in brief:
private IOrder stopOrder = null;
private IOrder shortLimit = null;
private IOrder longLimit = null;
...
protected override void OnBarUpdate()
{
..../* defining the limit prices */
if(/*some condition here */ && shortLimit == null && longLimit == null)
{
longLimit = EnterLongLimit(0,true,1,longLimitPrice,Name+" EnterLong");
shortLimit = EnterShortLimit(0,true,1,shortLimitPrice,Name+" EnterShort");
}
}
protected override void OnExecution(IExecution exec)
{
base.OnExecution(exec);
IOrder ord = exec.Order;
if(ord.OrderAction == OrderAction.Buy)
{
if(shortLimit != null)
{
CancelOrder(shortLimit);
shortLimit = null;
}
stopOrder = ExitLongStop(0,true,ord.Quantity,exec.Price - stoptav*TickSize,"StopLoss",Name+ " EnterLong");
}
else if(ord.OrderAction == OrderAction.SellShort)
{
if(longLimit != null)
{
CancelOrder(longLimit);
longLimit = null;
}
stopOrder = ExitShortStop(0,true,ord.Quantity,exec.Price + stoptav*TickSize,"StopLoss",Name+ " EnterShort");
}
else
{
if(stopOrder != null && stopOrder.OrderState != OrderState.Filled)
{
CancelOrder(stopOrder);
}
stopOrder = null;
if(shortLimit != null && shortLimit.OrderState != OrderState.Filled)
{
CancelOrder(shortLimit);
}
shortLimit = null;
if(longLimit != null && longLimit.OrderState != OrderState.Filled)
{
CancelOrder(longLimit);
}
longLimit = null;
}
}
One of my EnterLimit orders is getting ignored due to the internal order handling rules., therefore it can only enter into one direction. The only way to get around this is using unmanaged orders? I also would like to use Trailing stops, and that takes a lot to code with unmanaged orders, so I'd be happy if you could provide a way to implement my strategy with managed orders.
The second issue:
The limit order (which was not ignored) gets filled always on the limit price, even if the last price wasn't near of that. See picture attached: The limit price is at the blue line, but as you can see the bars, it is impossible to get filled there.
I'm open to solutions to these.
Thanks!
I'm using NT7b22

Comment