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
    Paul H.NinjaTrader Customer Service

    Comment


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

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by vantojo, Today, 06:46 AM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by sidlercom80, Today, 02:40 AM
      2 responses
      14 views
      0 likes
      Last Post sidlercom80  
      Started by b16_aln, Today, 01:52 AM
      0 responses
      14 views
      0 likes
      Last Post b16_aln
      by b16_aln
       
      Started by junkone, Yesterday, 08:55 PM
      2 responses
      13 views
      0 likes
      Last Post junkone
      by junkone
       
      Started by adaeiqendqw, 01-16-2021, 02:56 AM
      1 response
      17 views
      0 likes
      Last Post NinjaTrader_Jason  
      Working...
      X