Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Trail Stop not exiting where I would expect

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

    Trail Stop not exiting where I would expect

    Can you help me understand what is happening here? In summary, my strategy is entering a trade at the open, then exiting the trade on a trailing stop at the open price when I would have expected the stop exit to be at a different price.

    Here is my strategy code:

    Code:
            protected override void OnBarUpdate()
            {
                if (CurrentBar < BarsRequiredToTrade) { return; }
    
                SetTrailStop(CalculationMode.Ticks, 80);
    
                if (Close[0] > Close[1]
                    && (SystemPerformance.AllTrades.Count == 0 || BarsSinceExitExecution() > 10))
                {
                    EnterLong();
                }
            }
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Name = "StopLoss/ProfitTarget Test";
                }
                else if (State == State.DataLoaded)
                {
                    SetTrailStop(CalculationMode.Ticks, 80);
                }
            }
    And here is one of the bars in question after running the strategy on ES daily bars:

    Click image for larger version  Name:	Capture.PNG Views:	1 Size:	8.3 KB ID:	1057218

    I'm familiar with historical fill processing (https://ninjatrader.com/support/help...ical_fill_.htm). I'm using standard order fill resolution.

    But it doesn't seem to explain this for me.

    I have slippage set to zero.

    As I understand this we entered the long position at the open ($2666.50). Then we simulate the intrabar movement by assuming the price moves first to the closest of low or high which is, in this case, the low. The low is $2657.75. Since my stoploss is set at 80 tick (or $20), that's not a big enough drop to trigger the stoploss. Then we assume the price moves to the opposite extreme, in this case to the high, which is $2695.75. And then we assume it moves back down to the close which is not more than $20. So I would not have expected historical fill processing to have exited on this bar.

    If we made some other assumptions about how price moved in the bar, like suppose it goes from the open to the high first. Well, in that case, I would have expected it to exit at $20 below the high, or $2675.75.

    But instead, the exit price is all the way back down to the opening price...

    Can you help me make sense of this?

    By the way, most of the trades look exactly like I would expect. Even many that enter and exit in the same bar at different prices. But there are several that appear like the above example.

    Thanks,



    Last edited by BarzTrading; 05-13-2019, 11:42 AM.

    #2
    Hello BarzTrading,

    Thanks for your post.

    We'll have a look into this and update you when we have further information.
    Thanks in advance for your patience.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello BarzTrading,

      Thanks for your post.

      Focusing on April 26th, the high is 2695.75. The order is at 2666.50. 2695.75 - 2666.50 is 29.25 points which is 117 ticks. This bar has enough distance to trigger a fill on a stop that has a 80 tick distance.

      The virtual bars described mimic the market movement and triggers the order fill simulation, but since we are not adding intrabar granularity for order submissions, we do not have exact price movement and we have to use the OHLC values to simulate where the order fill occured.

      To achieve more accurate fills, I would suggest adding a single tick data series to the script to submit orders against. This will allow the orders to be filled intrabar at more expected levels.

      You will need to modify the strategy then to use its own logic for trail stops as you would then be using ExitLongStopMarket() for the stop loss order instead of using SetTrailStop().

      I would suggest modelling your strategy after the SampleIntrabarBacktest strategy that shows adding intrabar granularity. The Sample OnOrderUpdate strategy demonstrates a breakeven using ExitLongStopMarket, which could be modified to work as a Trail Stop. Combining these strategy examples should give you a strategy that can give you backtest results that you expect.

      SampleOnOrderUpdate -https://ninjatrader.com/support/help...and_onexec.htm

      SampleIntrabarBacktest - https://ninjatrader.com/support/help...ipt_strate.htm

      Please let us know if there is anything else we can do to help.
      JimNinjaTrader Customer Service

      Comment


        #4
        The virtual bars described mimic the market movement and triggers the order fill simulation, but since we are not adding intrabar granularity for order submissions, we do not have exact price movement and we have to use the OHLC values to simulate where the order fill occured.
        How do the virtual bars trigger the stop?

        This scenario is not documented here: https://ninjatrader.com/support/help...ical_fill_.htm

        For the bar in question, this image shows how the virtual bars would be formed:

        Click image for larger version

Name:	Capture.PNG
Views:	167
Size:	4.3 KB
ID:	1057425

        1. So, we assume the price moves from the open to the low. (2666.50 to 2657.75)

        2. Then from the low to the high. (2657.75 to 2695.75)

        3. Then from the high to the close. (2695.75 to 2692.75)

        Since my trailing stop loss was 80 ticks ($20), we can see that the first virtual bar is a drop of only 8.75, not enough to trigger the stop.

        The second virtual bar moves up $38. This is more than the size of my stop so I assume you mean that this virtual bar "triggers" the stop by virtue of the fact price _could_ have moved down $20 during this bar.

        The drop in the third virtual bar is $3, so also not enough to trigger the stop there.


        I'm guessing that there is some undocumented logic that says, if one of our virtual bars is bigger than the stop loss size, that we'll go ahead and exit.

        And the algorithm seems to conclude that exiting at the open price is the best approach?!

        I take your point that I should use more granular bars because standard fill processing will always have limitations. But just out of curiosity, I'd like to know how the algorithm arrives at using the open price as the stop's exit price.

        Is there a reason you didn't suggest using "Historical Fill Processing - High" with say, minute bars, instead of placing intrabar orders on tick data?

        My original bars are daily bars, so it seems tick data would be unnecessary for what I'm doing.

        Thanks,










        Comment


          #5
          Thanks for the message.

          I am keeping this ticket open and will reply back with a more indepth break down after I have been able to prepare those resources.

          Thanks for your patience.
          JimNinjaTrader Customer Service

          Comment


            #6
            Thanks again for your patience.

            We are on the same page here.

            The middle virtual bar triggers this order fill. While in your case we are looking at an up bar, the order fill engine does not know any actual price action that determines the bar only went up. The order fill engine just sees the data points and assumes that price action had moved throughout the bar. For all the engine knows, the price could have gone up and and down that bar several times.

            The Open is used because that price point is closest to where the exit would be predicted given the 4 data points.

            The bottom line here is that Standard fill only estimates order fills with OHLC points with no actual price movement underneath. I had mentioned submitting orders to a single tick data series as this would be the most accurate way to simulate order fills when backtesting. (We cannot have High Order Fill Resolution set to Ticks with a Daily series.) You could use a minute series like you mention, but I would suggest to also changing the trading hours template so the minute data does not get filtered out. (Daily bars are set by your provider and cannot filtered by wider/tighter sessions in a Trading Hours template.)

            Please let me know if there is anything else I can do to help.
            JimNinjaTrader Customer Service

            Comment


              #7
              Thanks, Jim. I appreciate the clarification.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rhyminkevin, Today, 04:58 PM
              3 responses
              47 views
              0 likes
              Last Post Anfedport  
              Started by iceman2018, Today, 05:07 PM
              0 responses
              5 views
              0 likes
              Last Post iceman2018  
              Started by lightsun47, Today, 03:51 PM
              0 responses
              7 views
              0 likes
              Last Post lightsun47  
              Started by 00nevest, Today, 02:27 PM
              1 response
              14 views
              0 likes
              Last Post 00nevest  
              Started by futtrader, 04-21-2024, 01:50 AM
              4 responses
              50 views
              0 likes
              Last Post futtrader  
              Working...
              X