I have here a simple script, under unmanaged approach, which seeks to performance a simple "ExitOnClose". This is just a draft:
protected override void OnBarUpdate()
{
if ( (ToTime(Time[0]) >= 165900 && ToTime(Time[0]) <= 171458) )
{
if (sEntryOrder != null )
{
CancelOrder(sEntryOrder); // if short order alive, cancel it
}
if (lEntryOrder != null )
{
CancelOrder(lEntryOrder); // if long order alive, cancel it
}
if (Position.MarketPosition == MarketPosition.Long && Position.Quantity >0 )
{
if (sEntryOrder == null && lEntryOrder == null )
{
sEntryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "", "EXIT LONG ON CLOSE");
}
}
else if (Position.MarketPosition == MarketPosition.Short && Position.Quantity >0 )
{
if (sEntryOrder == null && lEntryOrder == null )
{
lEntryOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Market, Position.Quantity, 0, 0, "", "EXIT SHORT ON CLOSE");
}
}
else
{;
lEntryOrder = null;
slEntryOrder = null;
sEntryOrder = null;
ssEntryOrder = null;
return;
}
lEntryOrder = null;
slEntryOrder = null;
sEntryOrder = null;
ssEntryOrder = null;
return;
}
.... otherwise ( trade normally )
Question:
- Why does this happen, cause the loop is faster than the processing of the order?
- Is it because the Return instruction that breaks the flow?
Suggestions for solution would be great and highly appreciated
Thanks in advance

Comment