Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnExecution firing on previous bar before actual event

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    OnExecution firing on previous bar before actual event

    Hi,

    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.

    #2
    Never mind, I'm an idiot. Of course when the execution occurs on bar 1:57, we have to assume the execution itself doesn't yet know the final details of the current bar, so all references point to the previous one.

    Sorry, been staring at pages of error output for too long....

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by mgco4you, Today, 09:46 PM
    1 response
    3 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by wzgy0920, Today, 09:53 PM
    0 responses
    3 views
    0 likes
    Last Post wzgy0920  
    Started by Rapine Heihei, Today, 08:19 PM
    1 response
    8 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by Rapine Heihei, Today, 08:25 PM
    0 responses
    6 views
    0 likes
    Last Post Rapine Heihei  
    Started by f.saeidi, Today, 08:01 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X