Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Struggling to implement trailing stop loss

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

    Struggling to implement trailing stop loss

    Hello everyone,

    I've been working on my algo for some weeks now, and I'm struggling to get trailing stop loss working.
    I already tried a lot of different things, but always encounter issues or not the expected behavior.

    The idea is to have a fixed SL of 20 points from my entry until price is reaching 20 points in profits. From this moment, I take one contract off and I would like my stop loss to trail price only if it's doing higher highs (15 points trail stop) / or higher price since my trade has been opened.

    Here what my current code is looking like regarding this specific part. It currently sets my SL at Breakeven when 20 points in profit is reached and then take the 2nd contract off at 40 points.

    Code:
    if (Position.MarketPosition == MarketPosition.Long)
    
    {
    
    //Keep Stop order where it is until TP1 is reached
    if (Position.Quantity == Contracts && !halfPositionClosed)
    
    {
    stopLossOrder = ExitLongStopMarket(Contracts, Position.AveragePrice - SL);
    double limitPrice1 = Position.AveragePrice + TP1;
    ExitLongLimit(Convert.ToInt32(Contracts)/2, limitPrice1, "ExitHalfLong", "Long");
    }
    
    // Another part of the code set halfPositionClosed to true
    
    if (halfPositionClosed)
    {
    [COLOR=#e74c3c]double breakevenPrice = Position.AveragePrice + (1); // Add one tick to cover commissions[/COLOR]
    stopLossOrder = ExitLongStopMarket(Position.Quantity, breakevenPrice);
    Print("current SL long: " + stopLossOrder);
    
    // TP2
    double limitPrice2 = Position.AveragePrice + TP2;
    ExitLongLimit(Convert.ToInt32(Contracts)/2, limitPrice2, "ExitFullLong", "Long");
      }
    }
    ​
    When I switch the line in red for this : double breakevenPrice = High[0] - (15);
    ​My Stop Loss is following price, whatever it does (even if it's going down or if not doing higher highs).

    Could someone help me on this topic?

    Thanks in advance for the help.

    #2
    Hello kick71,

    I have some examples of custom trailing logic you find helpful.
    Code:
    https://ninjatrader.com/support/forum/forum/ninjatrader-8/platform-technical-support-aa/97419-priceos-cs-expecting?p=802269#post802269
    The stop and or limit price is saved to a variable and supplied as a parameter to the order method.
    Once the current price reaches the order price plus or minus the trailing trigger distance (StopLossDistance) the stop price variable is updated to that price.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the link, it looks a bit advanced for me but I'm gonna try to understand how to implement some parts into my own code.
      Do you have any tips with what I've already posted?

      Thank you.

      Comment


        #4
        Hello kick71,

        Assign orders to order objects in OnOrderUpdate(), declare the variables storing the stop / limit price and next trail price within the scope of the class (so they persist through every method call).
        Use a condition comparing the saved price with the next trail price, then update the stop / limit price with the new price and set the next trail price.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks to your examples in the other topic, I found a solution to implement something in my code and it looks like it's working fine. Here is the updated version of the code:
          Code:
          if (Position.MarketPosition == MarketPosition.Long)
          
          {
          //Keep Stop order where it is until TP1 is reached
          if (Position.Quantity == Contracts && !halfPositionClosed)
          
          {
          
          stopLossOrder = ExitLongStopMarket(Contracts, Position.AveragePrice - SL); // Value in points
          double limitPrice1 = Position.AveragePrice + TP1;
          ExitLongLimit(Convert.ToInt32(Contracts)/2, limitPrice1, "ExitHalfLong", "Long");
          
          }
          
          // Trailing stop
          if (halfPositionClosed)
          
          {
          
          // Code for Trailing SL after TP1
          double updatedTrailSL = High[0] - (TrailDistance);
          
          // Check if the high - 15pts is higher than the current stopLossOrder price
          if (updatedTrailSL > stopLossOrder.StopPrice)
          
          {
          
          // Update the stopLossOrder with the new SL price
          double TrailSL = High[0] - (TrailDistance);
          stopLossOrder = ExitLongStopMarket(Position.Quantity, updatedTrailSL);
          
          }
          
          else
          
          {
          
          TrailSL = stopLossOrder.StopPrice;
          stopLossOrder = ExitLongStopMarket(Position.Quantity, TrailSL);
          
          }
          }
          }

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello kick71,

            Assign orders to order objects in OnOrderUpdate(), declare the variables storing the stop / limit price and next trail price within the scope of the class (so they persist through every method call).
            Use a condition comparing the saved price with the next trail price, then update the stop / limit price with the new price and set the next trail price.
            Does OnOrderUpdate run on each tick even though on bar update can run on bar close?

            Comment


              #7
              Hello Trader17,

              OnOrderUpdate() is not a data driven method. This runs at the time an order changes states (like being submitted, becoming working, filling, or being cancelled).
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              50 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              126 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              69 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              42 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X