I've been failing to get my strategy to adopt an existing account order, and am also seeing some odd behaviour. Connecting to FXCM, if there is an existing working order, I would like the strategy to begin managing it. The behaviour I think I'm seeing is that the strategy will only successfully map the order if it was generated by the strategy whilst it is currently loaded in memory.
Let's say I have a sole Limit Order active on the account, in a 'working' state. If I disable the strategy and then re-enable it in the main NT window, it successfully maps the order and begins managing it. If I right click on the chart and reload the ninjascript (F5), it succesfully maps the order and begins managing it.
However, if I do both (disable, refresh, enable), it submits a new working order to the account matching the existing one, resulting in two identical working orders on the account. Likewise, if I delete the strategy in the main NT window and then add it to the chart again, it submits a second working order.
The code below is successfully detecting the working order under all conditions, and also submits it historically on the same bar as the original with all the same details. However, NT only maps the order to the existing order in the situations mentioned above. If the strategy is deleted and re-added to the chart, then although the code identifies the order and submits it historically, NT fails to map it to the existing order and instead submits as a new one.
public class EntryTest : Strategy
{
private Order existingOrder;
private Order matchOrder;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "EntryTest";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 1;
IsInstantiatedOnEachOptimizationIteration = true;
IsAdoptAccountPositionAware = true;
StartBehavior = StartBehavior.ImmediatelySubmit;
}
else if (State == State.Configure)
{
IsUnmanaged = true;
var myAcct = Account.All.FirstOrDefault(x => x.Name == this.Account.Name); //identify the working order
foreach (Order order in myAcct.Orders)
{
if (order.Instrument.FullName == Instrument.FullName)
{
if (order.OrderState == OrderState.Working)
{
existingOrder = order;
}
}
}
}
}
protected override void OnBarUpdate() //submit the order on the same bar as the original
{
if (existingOrder != null && State == State.Historical)
{
if(CurrentBar > BarsRequiredToTrade && Time[0] <= existingOrder.Time && Time[0].Add(Time[0] - Time[1]) > existingOrder.Time)
{
Print (existingOrder.ToString());
matchOrder = SubmitOrderUnmanaged(0, existingOrder.OrderAction, existingOrder.OrderType, existingOrder.Quantity, existingOrder.LimitPrice, existingOrder.StopPrice, existingOrder.Oco, existingOrder.Name);
Print (matchOrder.ToString());
}
}
}
}
}
Comment