Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Understanding historical backfill logic of trailing stops

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

    Understanding historical backfill logic of trailing stops

    Hi!

    I'm studying NinjaScript and working with trailing stop losses today. I submit a trailing stop of 20 points for every long trade:

    Code:
    // Manage stops for open positions
    if (Position.MarketPosition == MarketPosition.Long)
    {
        // Trail stop of 20 points
        SetTrailStop(CalculationMode.Ticks, 20 * (1 / TickSize));
    }
    The instrument is the E-mini S&P 500 future. It works fine here:

    (first attached image)

    But there are also some cases that look very odd on historical data:

    (second attached image)

    Here the trade fills on the open, but is also closed at the open. That happens because the open is more than 20 points away from the bar's high.

    But of course that high happened later in time than the open did. So why does the trail stop fill at the open?

    I read the historical order backfill (https://ninjatrader.com/support/help...fill_logic.htm) page, but my comprehension of that page doesn't help me understand this situation.

    Thanks for any insights!



    #2
    Hi J_o_s, thanks for your note.

    I wanted to re-work my response to this. The SetTrailStop does depend on the Calculate setting of the strategy, so if the strategy runs Calculate.OnBarClose the trailing stop will only update once at the close of the bar, this could be the problem you are seeing. I created an example that updates the price on each tick. It adds a 1 tick series and updates the trail stop on each tick. You must also have Tick Replay enabled on the Data Series.

    Please let me know if I can assist any further.
    Attached Files
    Last edited by NinjaTrader_ChrisL; 04-05-2020, 02:46 PM.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply and the example code. Appreciated!

      Unfortunately I don't have tick data for the instrument I'm backtesting.

      Does that mean that, in this case, `SetTrailStop()` is not a good method to use? (Else I'd still get the, for lack of a better word, 'weird' behaviour I mentioned above.)

      Comment


        #4
        Hi J_o_s, thanks for your reply.

        If you do not have the tick data then the price movement can not be replayed as it was in real time. So any method you use to set your trail stop will not be accurate without the historical tick data, unfortunately. All SetTrailStop is doing behind the scenes is placing a stop market order at the requested price, so if you don't have the high resolution tick information the strategy will use the Open,High,Low,Close of the bar.

        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks for the clarification Chris, I understand it now. :-)

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by MatthewLesko, Today, 11:13 PM
          0 responses
          2 views
          0 likes
          Last Post MatthewLesko  
          Started by Lance El Camino, Today, 04:14 PM
          1 response
          13 views
          0 likes
          Last Post s.kinra
          by s.kinra
           
          Started by jtrading82, Today, 04:56 PM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_ShawnB  
          Started by dmking, Today, 06:00 PM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_EricB  
          Started by WHICKED, Today, 03:23 PM
          1 response
          15 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X