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 graph:



    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
    Arnaud, have you compared this to the Executions tab for this day? What do you get there?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hello Bertrand,

      in Executions Tab, I get the correct hour and price level for the trade.

      But the problem is still there.
      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
        Hi Arnaud, ok thanks for clarifying - will attempt to reproduce here.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Arnaud, can you please post the full code sample? The copy + paste you provided doesn't take any trades when compiled into a strategy. Thank you.
          AustinNinjaTrader Customer Service

          Comment


            #6
            Here is the complete code

            #region Using declarations
            using System;
            using System.ComponentModel;
            using System.Diagnostics;
            using System.Drawing;
            using System.Drawing.Drawing2D;
            using System.Xml.Serialization;
            using NinjaTrader.Cbi;
            using NinjaTrader.Data;
            using NinjaTrader.Indicator;
            using NinjaTrader.Gui.Chart;
            using NinjaTrader.Strategy;
            #endregion

            // This namespace holds all strategies and is required. Do not change it.
            namespace NinjaTrader.Strategy
            {
            /// <summary>
            /// Enter the description of your strategy here
            /// </summary>
            [Description("Enter the description of your strategy here")]
            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
            && 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
            }
            }

            Comment


              #7
              Printing Time[0] in OnExecution() is not the time of the execution. That is the time of the last processed bar which is historical. Once you finish processing OnBarUpdate() that Time[0] is no longer relevant as it is in the past.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Thanks for your reply Josh,

                but I still do not understand why OnExecution() prints flat 1 bar before being really executed. Only on backtest.
                Try on SP500 5 minutes bars, exit on close, OnExecution() prints "flat" at 23h25, instead of 23h30.
                Strange isn't it ??

                Thanks for your help.
                Arnaud

                Comment


                  #9
                  Arnaud, you can't compare the Time prints from the OnBarUpdate() with the ones you get from the OnExecution(), as when you call OnExecution() there's no corresponding bar event aligning - the result you got.

                  Please compare OnBarUpdate's Time with IExecution's Time property -

                  BertrandNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geotrades1, Today, 10:02 AM
                  1 response
                  4 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by ender_wiggum, Today, 09:50 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by rajendrasubedi2023, Today, 09:50 AM
                  1 response
                  11 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by bmartz, Today, 09:30 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by geddyisodin, Today, 05:20 AM
                  3 responses
                  26 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Working...
                  X