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

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 James650, Today, 08:25 AM
          1 response
          2 views
          0 likes
          Last Post NinjaTrader_ChristopherJ  
          Started by Rogers101, 05-05-2024, 11:30 AM
          14 responses
          40 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by MSerag, Yesterday, 11:52 PM
          2 responses
          21 views
          0 likes
          Last Post MSerag
          by MSerag
           
          Started by ETFVoyageur, 04-30-2024, 06:05 PM
          11 responses
          80 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by dcriador, Yesterday, 01:43 AM
          4 responses
          25 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X