Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trail Triggered, but not Exit

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

    Trail Triggered, but not Exit

    A number of times I have seen issues with trailing stop or move to breakeven being triggered on price set for target exit, where the trail (or move to breakeven), moves the stoploss up but the position is not exited. Originally I started seeing this with manual trading using ATM, but now with ninjascript, so I will use a specific example. The Strategy runs every tick, and the example is historical (not live), running OnEachTick.

    It enters a long trade (trading NQ), and has a target exit at 28 ticks. It also has a trailing stop which activates at 28 ticks and moves the SL to 27, or 1 tick above entry price. Usually it works as designed, but in some cases in which price goes only 28 ticks and reverses, the trailing move of the stoploss happens, but not the exit.

    ​​​

    The blue lines shows the 28 tick target, at 20833. You can see it moved the SL to breakeven+1, where both contracts are exited.

    What I want to happen it that half the trade exits on the target, and the other half trails by 27 ticks. So as soon as it reaches 20833, half closes and the SL is moved to 20826.25. Logging info shows that the trade is placed as expected as well and the initial SL and TP (Target 1 & 2).

    Here is the code I use to trigger the trailing stop.

    Code:
    if (UseTrailingStop  &&  Position.MarketPosition != MarketPosition.Flat)
    {
        if (!trailTriggered)
        {
            double curPrice = (Position.MarketPosition == MarketPosition.Long) ? GetCurrentBid() : GetCurrentAsk();
            if (Position.MarketPosition == MarketPosition.Long  &&  curPrice >= AvePrice() + LngActivationTicks*TickSize)
            {
                Log("                                   TRAILING STOP TRIGGERED cause Bid (" + curPrice + ") >= Entry Price (" + AvePrice() + ") + activation ticks (" + LngActivationTicks + ")");
                trailTriggered = true;
            }
            else if (Position.MarketPosition == MarketPosition.Short  &&  curPrice <= AvePrice() - ShtActivationTicks*TickSize)
            {
                Log("                                   TRAILING STOP TRIGGERED cause Ask (" + curPrice + ") <= Entry Price (" + AvePrice() + ") - activation ticks (" + ShtActivationTicks + ")");
                trailTriggered = true;
            }
        }
        if (trailTriggered)
        {
            /// Trail when first leg closes -usually, but not nec; we may need to move both stops
            double newSL = GetNewTrailPrice(Position.MarketPosition.ToString());
            if (newSL != 0)
            {
                ModifyStopLoss(newSL);
                if (slOrder1 != null)
                    ModifyStopLoss(newSL, 1);
            }
        }
    }
    Here is the line that was logged from the Log() call in the above code:

    Strategy - NQ 12-24 - [OnBarUpdate] - 2024-11-25 09:25:18 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    TRAILING STOP TRIGGERED cause Bid (20833) >= Entry Price (20826) + activation ticks (28)​

    In order to see what really occurred, I loaded a tick chart to see the order of events. This is a buy trade, so I should be concerned with Bid price to close the position. I opened a chart with both Bid and Ask lines, with the Bid line in a brighter color, and here is the relevant section of the chart:



    Each half of the trade is its own entry with its own SL & TP. I'm not an expert on how the historical NT8 engine works, but when the Bid reached 20833, I would think it would close the first part of the trade, and trigger the trail. But here I saw that Bid only reached a high of 20832.75. Yet the line from the logging says it hit 20833, whereupon the trail moved the stop to BE+1.

    I was tracking what I thought was a problem with NT historical not exiting a trade when it should have; as I mentioned, a problem I have seen more than once.

    But according to the tick chart, I should be wondering why GetCurentBid() returned 20833 when it never got that high.​​

    #2
    A number of times I have seen issues with trailing stop or move to breakeven being triggered on price set for target exit, where the trail (or move to breakeven), moves the stoploss up but the position is not exited. Originally I started seeing this with manual trading using ATM, but now with ninjascript, so I will use a specific example. The Strategy runs every tick, and the example is historical (not live), running OnEachTick.

    It enters a long trade (trading NQ), and has a target exit at 28 ticks. It also has a trailing stop which activates at 28 ticks and moves the SL to 27, or 1 tick above entry price. Usually it works as designed, but in some cases in which price goes only 28 ticks and reverses, the trailing move of the stoploss happens, but not the exit.

    ​​​

    The blue lines shows the 28 tick target, at 20833. You can see it moved the SL to breakeven+1, where both contracts are exited.

    What I want to happen it that half the trade exits on the target, and the other half trails by 27 ticks. So as soon as it reaches 20833, half closes and the SL is moved to 20826.25. Logging info shows that the trade is placed as expected as well and the initial SL and TP (Target 1 & 2).

    Here is the code I use to trigger the trailing stop.

    Code:
    if (UseTrailingStop  &&  Position.MarketPosition != MarketPosition.Flat)
    {
        if (!trailTriggered)
        {
            double curPrice = (Position.MarketPosition == MarketPosition.Long) ? GetCurrentBid() : GetCurrentAsk();
            if (Position.MarketPosition == MarketPosition.Long  &&  curPrice >= AvePrice() + LngActivationTicks*TickSize)
            {
                Log("                                   TRAILING STOP TRIGGERED cause Bid (" + curPrice + ") >= Entry Price (" + AvePrice() + ") + activation ticks (" + LngActivationTicks + ")");
                trailTriggered = true;
            }
            else if (Position.MarketPosition == MarketPosition.Short  &&  curPrice <= AvePrice() - ShtActivationTicks*TickSize)
            {
                Log("                                   TRAILING STOP TRIGGERED cause Ask (" + curPrice + ") <= Entry Price (" + AvePrice() + ") - activation ticks (" + ShtActivationTicks + ")");
                trailTriggered = true;
            }
        }
        if (trailTriggered)
        {
            /// Trail when first leg closes -usually, but not nec; we may need to move both stops
            double newSL = GetNewTrailPrice(Position.MarketPosition.ToString());
            if (newSL != 0)
            {
                ModifyStopLoss(newSL);
                if (slOrder1 != null)
                    ModifyStopLoss(newSL, 1);
            }
        }
    }
    Here is the line that was logged from the Log() call in the above code:

    Strategy - NQ 12-24 - [OnBarUpdate] - 2024-11-25 09:25:18 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    TRAILING STOP TRIGGERED cause Bid (20833) >= Entry Price (20826) + activation ticks (28)​

    In order to see what really occurred, I loaded a tick chart to see the order of events. This is a buy trade, so I should be concerned with Bid price to close the position. I opened a chart with both Bid and Ask lines, with the Bid line in a brighter color, and here is the relevant section of the chart:



    Each half of the trade is its own entry with its own SL & TP. I'm not an expert on how the historical NT8 engine works, but when the Bid reached 20833, I would think it would close the first part of the trade, and trigger the trail. But here I saw that Bid only reached a high of 20832.75. Yet the line from the logging says it hit 20833, whereupon the trail moved the stop to BE+1.

    I was tracking what I thought was a problem with NT historical not exiting a trade when it should have; as I mentioned, a problem I have seen more than once.

    But according to the tick chart, I should be wondering why GetCurentBid() returned 20833 when it never got that high.​​

    Comment


      #3
      Hello DerkWehler,

      If you are also seeing that in manual trading or with ATM's then it likely does not relate to your code but rather with how the platform is filling the orders. It sounds like your script is matching the expectation from using ATM's so that is very likely expected in that situation.

      Comment


        #4
        The post was meant to have screen shots and not be posted twice. There is something wrong with this web site. If I click "Edit" or "Preview", it says "working..." at the top and never returns.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        55 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        132 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        73 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        49 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X