Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stop Not Fixed

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

    Trailing Stop Not Fixed

    Hi,

    Please find the trailing stop code I've been testing below. The code works fine expect that the trailing stop doesn't stay fixed. I can see that as the price changes the code will adjust the trailing stoploss but I'm not sure how to fix the stoploss so that as a limit is hit the stop moves and stays fixed until the next target is hit.

    I'm not a programmer, I know the issue just not sure how to fix it so the code behaves this way.

    Any help would be appreciated.

    // Resets stoploss to original value when all positions closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 10);
    }
    // If a short position is open all for stoploss modification
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    //Prices hit 5 ticks setstop loss to breakeven
    if (Low[0] <= Position.AveragePrice + -10 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -2 * TickSize);
    }
    //Prices hit 8 ticks setstop loss to 2 ticks
    if (Low[0] <= Position.AveragePrice + -16 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -4 * TickSize);
    }


    Thanks.

    #2
    Hello Frankriso,

    Thanks for your post.

    What you would need to do is use a bool variable that can be set either true or false to help control.

    For example, if you create a bool named DoItOnce and set it to true initially, in your first 5 tick condition you would check if the bool DoItOnce is true as well as the existing price check. In the action section you would then set the bool DoItOnce false. This would prevent the condition from being used again until the bool is once again set true.

    You can reset the bool to true in the actions of the condition where you are checking for a flat position.

    For example:

    // Resets stoploss to original value when all positions closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 10);
    DoItOnce = true; // reset bool when flat
    }
    // If a short position is open all for stoploss modification
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    //Prices hit 5 ticks setstop loss to breakeven
    if (Low[0] <= Position.AveragePrice + -10 * TickSize && DoItOnce)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -2 * TickSize);
    DoItOnce = false; // prevent setting more than once
    }
    //Prices hit 8 ticks setstop loss to 2 ticks
    if (Low[0] <= Position.AveragePrice + -16 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + -4 * TickSize);
    }


    Note: you will want to create the bool above the OnStateChange() method, for example:

    private bool DoItOnce = true; // create and initialize

    Comment


      #3
      Thanks Paul has saved me hours trying to figure that one out.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      62 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 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
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X