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 Taddypole, 04-26-2024, 02:47 PM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Eduardo  
    Started by futtrader, 04-21-2024, 01:50 AM
    6 responses
    58 views
    0 likes
    Last Post futtrader  
    Started by sgordet, Today, 11:48 AM
    0 responses
    4 views
    0 likes
    Last Post sgordet
    by sgordet
     
    Started by Trader146, Today, 11:41 AM
    0 responses
    5 views
    0 likes
    Last Post Trader146  
    Started by jpapa, 04-23-2024, 07:22 AM
    2 responses
    21 views
    0 likes
    Last Post rene69851  
    Working...
    X