I'm seeing some odd results when backtesting using a strategy that submits limit orders in response to OnExecution Events (1m bars, FXCM, NT7), using the unmanaged approach.
Specifically, although the chart tab and executions tab show the actual execution happening on the correct bar at the correct price and time (say 01:57:00), the actual OnExecution code is running at the end of (or against) the previous bar (01:56:00). E.g:
protected override void OnBarUpdate()
{
goShort = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, units, newUpLevel, 0, "1" + Time[0].ToString(), "NewSell " + Time[0].ToString());
}
protected override void OnExecution(IExecution execution)
{
if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
{
if (execution.Order.Name.Substring(0,7) == "NewSell")
{
Print(execution.Time.ToString() + ":" + execution.Price.ToString() + " @ " + Time[0].ToString());
goLong = SubmitOrder(0, OrderAction.Buy, OrderType.Limit,units * 2, nextDownLevel, 0, "2" + Time[0].ToString(), "OldBuy " + Time[0].ToString());
}
}
}
Using this code, goShort may be a limit order set at say 1.5300. Suppose at bar ending 01:56:00 the High is 1.5290 and the limit order hasn't triggered yet. At 01:57:00 the High is 1.5310, and the limit order should be triggered.This is indeed what happens on the chart and on the executions tab. However, goLong will place an order on bar ending 01:56:00 (as shown on the orders tab), and similarly Time[0].ToString for that order will show 01:56:00. Also the OHLC[0] as referenced in OnExecution will all reference the bar ending at 01:56:00. However, price never actually reaches the target value until the following bar.
This line (when run from OnExecution):
Print(execution.Time.ToString() + ":" + execution.Price.ToString() + " @ " + Time[0].ToString());
Produces this:
01/01/2016 01:57:00:1.53000 @ 01/01/2016 01:56:00
Even though the price of 1.5300 was never reached on bar 01:56:00. The execution price and time are correct, but they are in the future as the code is running on bar 01:56:00.
Comment