Sometimes the order does cancel, but more often than not the strategy attempts to cancel the order but fails, and generates this error in the log:
"Received unknown error/warning: OrderID 181848 that needs to be cancelled cannot be cancelled, state: Cancelled."
With whichever OrderId there is for that order. Everything works no problem in a backtest. It occurs whether I'm running the strategy on 500 instruments simultaneously or a single instrument. All of the orders seem normal in the Orders tab, with expected oco, OrderId, names, etc.
Here is a watered down version with what I think would be relevant:
public class CancelErrors : Strategy
{
private Order longOrder = null;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "BCAfterhours";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 60;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
OrderFillResolutionType = BarsPeriodType.Minute;
OrderFillResolutionValue = 1;
Slippage = 2;
IsAdoptAccountPositionAware = true;
StartBehavior = StartBehavior.AdoptAccountPosition;
IsUnmanaged = true;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
StopTargetHandling = StopTargetHandling.ByStrategyPosition;
BarsRequiredToTrade = 20;
IncludeCommission = true;
IgnoreOverfill = true;
IsInstantiatedOnEachOptimizationIteration = true;
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress == 0)
{
if (State == State.Historical)
return;
if (CurrentBars[0] < 30)
return;
if ((ToTime(Time[0]) < 174000))
{
if ((Position.MarketPosition == MarketPosition.Flat)
&& (longOrder != null)
&& (ToTime(Time[0]) > 71900)
)
{
CancelOrder(longOrder);
}
if ((Position.MarketPosition == MarketPosition.Flat)
&& (shortOrder != null)
&& (ToTime(Time[0]) > 71900)
)
{
CancelOrder(shortOrder);
}
}
if (ToTime(Time[0]) >= 21000 && ToTime(Time[0]) < 71900)
{
if ((longOrder == null)
&& (Position.MarketPosition == MarketPosition.Flat)
&& ((((Account.Get(AccountItem.NetLiquidation, Currency.UsDollar) * .95) - Account.Get(AccountItem.MaintenanceMargin, Currency.UsDollar)) > (Close[0] * CurrentShares)))
&& (Ready == 0)
)
{
SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Limit, CurrentShares, (MAX1[0] * (1 - ((NATR1[0] * LongEntryFx) / 100))), 0, GetAtmStrategyUniqueId() + "LongEntry", "longOrder");
}
if ((shortOrder == null)
&& (Position.MarketPosition == MarketPosition.Flat)
&& ((((Account.Get(AccountItem.NetLiquidation, Currency.UsDollar) * .95) - Account.Get(AccountItem.MaintenanceMargin, Currency.UsDollar)) > (Close[0] * CurrentShares)))
&& (Ready == 0)
)
{
SubmitOrderUnmanaged(0, OrderAction.SellShort, OrderType.Limit, CurrentShares, (MIN1[0] * (1 + ((NATR1[0] * LongEntryFx) / 100))), 0, GetAtmStrategyUniqueId() + "ShortEntry", "shortOrder");
}
}
However, if the strategy has created an entry limit order, but has not filled it, and then the time is > 71900, it frequently attempts to cancel the order as directed but fails to do so and gives me that error.
Any ideas?? Thanks!
Comment