Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Wrong execution time stamp

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

    Wrong execution time stamp

    Hello Ninja addicts !!

    I get a problem when running a simple backtest.
    This stratégie buy after 11h30, and exits after 2 times the initial risk (30 points), on FCE-CAC40 future contract.
    With CalculateOnBarClose = true
    I need to test when the strategie is flat.
    I use Print fonction to get this information, in OnBarUpdate() and in OnExecution() method.
    Prints are different, as OnExecution() returns flat information 1 bar before price is reached and trade is closed.
    Could anyone explain me this strange action ??
    This stratégie has been tested on Ninja 6.5 and Ninja 7, same result.


    Here is the code:


    public class Turtle1_Long_DEBUG : Strategy
    {
    #region Variables

    double NewStop=1;
    double FixedStop;
    double Risk;
    int OneTrade;

    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    CalculateOnBarClose = true;

    TraceOrders = true;
    EntriesPerDirection=1;
    EntryHandling=EntryHandling.UniqueEntries;
    ExitOnClose=true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (ToDay(Time[0]) != ToDay(Time[1]))
    {
    OneTrade = 0;
    }

    if (Position.MarketPosition==MarketPosition.Flat
    && BarsInProgress==1
    && ToTime(Time[0]) >= 113000
    && OneTrade == 0
    )

    {
    EnterLong(3,"Turtle Long");
    NewStop=1;
    OneTrade = 1;
    }


    if (Position.MarketPosition==MarketPosition.Long)
    {
    Risk=Position.AvgPrice-NewStop;
    FixedStop=Position.AvgPrice-30;

    if (FixedStop>NewStop)
    {NewStop=FixedStop;}

    ExitLongLimit(3,Position.AvgPrice+2*Risk,"Exit at X times Risk","");
    }


    Print("OnBarUpdate");
    Print("Date : " +ToDay(Time[0])+" Time : " +ToTime(Time[0]));
    Print("Risk= "+Risk);
    Print("Situation= "+Position.MarketPosition);
    Print ("Position.Quantity :" +Position.Quantity);
    Print("");

    }



    protected override void OnExecution(IExecution execution)
    {
    Print("OnExecution");
    Print("Date : " +ToDay(Time[0])+" Time : " +ToTime(Time[0]));
    Print("Risk= "+Risk);
    Print("Situation= "+Position.MarketPosition);
    Print ("Position.Quantity :" +Position.Quantity);
    Print("");
    }


    #region Properties
    #endregion
    }




    Here is the TraceOrders:

    21/01/2009 15:50:00 Entered internal PlaceOrder() method at 21/01/2009 15:50:00: Action=Sell OrderType=Limit Quantity=3 LimitPrice=2935,0 StopPrice=0 SignalName='Exit at X times Risk' FromEntrySignal=''
    21/01/2009 15:50:00 Ignore order amendment: Action=Sell OrderType=Limit Quantity=3 LimitPrice=2935,0 StopPrice=0 SignalName=Exit at X times Risk' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
    OnBarUpdate
    Date : 20090121 Time : 155000
    Risk= 30
    Situation= Long
    Position.Quantity :3

    OnExecution
    Date : 20090121 Time : 155000
    Risk= 30
    Situation= Flat
    Position.Quantity :0





    Thanks a lot, have a nice day.
    Arnaud
    Attached Files

    #2
    OnExecution will trigger the moment your executions start coming in. This will be ahead of OnBarUpdate and offers you the earliest time for processing further trade logic.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hello Josh,

      I understand well OnExecution() method. In real-time, everything works perfect.
      But in backtest, this is wrong. So how can I test flat position, to reset orders for example ?

      I repeat the problem described before.
      The OnExecution() method returns flat position and quantity=0 at 15h50, ONE bar before trade is closed and even before price has been touched.
      The order cannot be executed as the price has not been touched.

      I tried this backtest on ES 12-09, yesterday the 11th, with an exit on close.
      ES closes at 23h30, everything perfect on graph, trades and Executions tabs.
      But print instruction in OnExecution() method returns flat at 23h25.

      Just try, you will see !!

      Have a nive day
      Arnaud

      Comment


        #4
        Arnaud,

        Once you received OnBarUpdate for a bar, that bar is complete. You are not on that bar anymore. You are onto the next bar and that is where you get your OnExecution() prints.
        Josh P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        605 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        351 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        560 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        561 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X