Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtest - OnExecutionUpdate - DIFFERENT Pricing depending on direction?

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

    Backtest - OnExecutionUpdate - DIFFERENT Pricing depending on direction?

    Hi,
    I'm playing around in NT8 with a NinjaScript strategy entering a new position immediately after a previous trade is closed, sometimes even in the same direction.
    Re-enter logic is positioned in "OnExecutionUpdate" and fires off immediately after a TakeProfit or StopLoss is executed and it may or may not reverse the direction of the next trade based on user input or some other logic.
    Code Snippets (example):
    if ( order.Name == "Profit target" && order.OrderState == OrderState.Filled && order.OrderAction == OrderAction.Sell )
    {EnterLong (0, Convert.ToInt32(MyQuantity),"MySignalName");}
    or ...
    if ( order.Name == "StopLoss" && order.OrderState == OrderState.Filled && order.OrderAction == OrderAction.Sell )
    {EnterShort (0, Convert.ToInt32(MyQuantity),"MyOtherSignalName");}

    Interestingly, in a backtest, the price at which the next trader is entered is DIFFERENT depending on the direction of the subsequent trade.
    Whenever the direction doesn't change, I get the SAME price for the subsequent trade (closing price of previous trade = opening price of next trade).
    However, when the direction changes, the subsequent trade is sometimes priced completely different in a backtest although it get's the exact same time stamp as the closing of the previous trade (see Trade 3, 5, 10, 14 in the attached).
    Is that expected behaviour? If Yes, how to avoid this in a backtest? I know, I can always run all backtests on ticks, but that is extremely resource consuming, particularly if coupled with complex optimizations. Is there really no other fix?
    With an ATM Strategy, one can handle the reverse in ONE order, but to my knowledge, ATM strategies don't work in backtests.
    Thus, what is the best approach to handle TURNING the position in ONE(!) order in a backtest or to ensure SAME price otherwise?
    Thanks for pointing me in the right direction.
    NT-Roland
    Attached Files

    #2
    Hello NT-Roland,

    If you have several orders being placed in succession without intra-bar granularity these are likely all going to fill at the open of the bar after being submitted.

    Below is a link to the help guide on the fill algorithm.


    I think this script will need intra-bar granularity for accurate fill prices.
    Below is a link to a forum post on intra-bar granularity.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      [QUOTE=NinjaTrader_ChelseaB;n1065559]Hello NT-Roland,

      If you have several orders being placed in succession without intra-bar granularity these are likely all going to fill at the open of the bar after being submitted.

      Hello Chelsea,
      I'm not sure this is true. Please have a look at the attached screenshot. For as long as you continue in OnExecutionUpdate in the SAME direction, you get the same price (not the open). If you turn the direction, you get a different price (again most often not the open). I would understand that missing intra-bar granularity, which I will explore further, thank you for the links, impacts BOTH trades, but not only of the them.
      NT-Roland


      Attached Files

      Comment


        #4
        Hello NT-Roland,

        If orders are chained through order events such as OnExecutionUpdate()/OnOrderUpdate() and not data events such as OnBarUpdate(), they will be processed immediately on the same bar and will fill using the information from that bar.

        Attached is an example that demonstrates several orders placed in succession from order events. Please note the fill prices of the orders placed on the same bar.


        The screenshot you have provided appears to match the expected behavior.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello Chelsea,
          The example provided shows that you get the same price if you IMMEDIATELY turn positions in OnExecutionUpdate with no further logic (except for the cap of turns via counter).
          It would be most appreciated, if you could provide an example demonstrating that you get the SAME price for the subsequent trade no matter
          1) if the previous trade was closed by Take Profit or Stop Loss, and(!)
          2) if the subsequent trade is in the same or opposite direction.
          I still don't get it from a CONCEPTUAL point of view, why the price at which a subsequent trade is filled should depend on how the previous trade was closed.
          I hold the price for the subsequent trade should ALWAYS be the same no matter if i) the previous trade was closed via Take Profit or Stop Loss and(!) ii) if the subsequent trade is in the same or opposite direction, particularly since the "information from the bar" used for the next trade cannot be any different.
          NT-Roland

          Comment


            #6
            Hello NT-Roland,

            The test script was to show market orders fill on the same bar when placing orders outside of data driven methods and fill at the same price.

            I am modifying the script and testing using set methods and I will let you know what I find. (An export with a reduced script with only the bare code necessary to reproduce the behavior can be very helpful)

            I appreciate your patience.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello NT-Roland,

              I was able to reproduce the changing fill prices when using Set methods.
              I'm looking further into this with our development.

              Again I appreciate your patience while we work on this.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hello NT-Roland,

                I've gotten some information back from our development.

                This is expected with the backfill logic.

                2. Second Pass
                b. if it's a Market Buy order and the bar moved up first, set the fill price to the lesser of the "next high price" or the bar High
                i. If the bar moved down first, set the fill price to the lesser of the "next high price" or the bar Low
                c. If it's a Market Sell order and the bar moved up first, set the fill price to the greater of the "next high price" or the bar High
                i. If the bar moved down first, set the fill price to the greater of the "next high price" or the bar Low

                3. Third Pass
                b. If it's a Market Buy order and the bar moved up first, set the fill price to the lesser of the "next high price" or the bar Low
                i. If the bar moved down first, set the fill price to the lesser of the "next high price" or the bar High
                c. If it's a Market Sell order and the bar moved up first, set the fill price to the greater of the "next high price" or the bar Low
                i. If the bar moved down first, set the fill price to the greater of the "next high price" or the bar High



                Throughout these three passes, prices are temporarily stored in two variables: a "next high price" and a "next low price." These are used to approximate the price that would be hit on the next tick, for the purpose of setting the fill price.

                So yes, as orders fill, that price is saved and may be used for the next fill on that bar.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea,
                  Can you please explain how NinjaTrader determines if "the bar moved up first" in a backtest?
                  What's the basis for such assessment? To my understanding, bars (e.g. 5 min) only carry O,H,L,C,V info.
                  Thx.
                  NT-Roland

                  Comment


                    #10
                    Hello NT-Roland,

                    If the open is closer to the high, NinjaTrader considers that as moving up first. If the open is closer to the low, NinjaTrader considers that as moving down first.



                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by CortexZenUSA, Today, 12:53 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post CortexZenUSA  
                    Started by CortexZenUSA, Today, 12:46 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post CortexZenUSA  
                    Started by usazencortex, Today, 12:43 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post usazencortex  
                    Started by sidlercom80, 10-28-2023, 08:49 AM
                    168 responses
                    2,265 views
                    0 likes
                    Last Post sidlercom80  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    3 responses
                    12 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X