Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop loss that changes as the market moves in my favor

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

    Stop loss that changes as the market moves in my favor

    Hi Ninjatrader programmers,

    I took a screenshot of my IDE to make it easier to read. I'm also going to paste the code below in case someone wants to copy and paste it to an IDE to try to figure it out. What I'm trying to do is create a stop-loss that is set to:

    -20 at the beginning of trade
    Breakeven when the market has moved in my favor by 20 pips
    +20 when the market has moved in my favor by +40 pips
    +40 when the market has moved in my favor by +60 pips
    Trailing stop loss of -10 once price has moved in my favor by 70 pips.

    This code below looks like it should work. If you would like to get more information about my backtest results, please let me know. This code is simple, I just don't know why I can't get it to work. Thanks a million.





    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Resets stop-loss when market becomes flat.
    SetStopLoss(CalculationMode.Ticks, stoplossticks);

    } else if (Position.MarketPosition == MarketPosition.Long){
    // Once the price is greater than entry price+20 ticks, set stop loss to breakeven
    if (Close[0] > Position.AvgPrice + 20 * TickSize && Close[0] < Position.AvgPrice + 40 * TickSize){
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);

    }
    } else if (Position.MarketPosition == MarketPosition.Long){
    // Once the price is greater than entry price+40 ticks, set stop loss to 20 ticks from entry
    if(Close[0] > Position.AvgPrice + 40 * TickSize && Close[0] < Position.AvgPrice + 60 * TickSize){
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 20 * TickSize);

    }
    } else if (Position.MarketPosition == MarketPosition.Long) {
    // Once the price is greater than entry price+60 ticks, set stop loss to 40 ticks from entry
    if(Close[0] > Position.AvgPrice + 60 * TickSize && Close[0] < Position.AvgPrice + 70 * TickSize) {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 40 * TickSize);

    }

    } else if ( Position.MarketPosition == MarketPosition.Long) {
    // Once the price is greater than entry price+70 ticks, set trailing stop loss to 10 ticks.
    if(Close[0] > Position.AvgPrice + 70 * TickSize) {
    SetTrailStop(CalculationMode.Ticks, 10);

    }
    }

    #2
    In case it is helpful, I wrote that code under OnBarUpdate()

    Comment


      #3
      Hello BernWillChris,

      Thanks for your post and welcome to the Forum!

      Thank-you for sharing your code.

      When you post code snippets, if you use the "go advanced" you can then use code tags to make it all fit and be easier to read, for example:

      Code:
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      // Resets stop-loss when market becomes flat.
      SetStopLoss(CalculationMode.Ticks, stoplossticks);
      
      } else if (Position.MarketPosition == MarketPosition.Long){
      // Once the price is greater than entry price+20 ticks, set stop loss to breakeven
      if (Close[0] > Position.AvgPrice + 20 * TickSize && Close[0] < Position.AvgPrice + 40 * TickSize){
      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      
      }
      } else if (Position.MarketPosition == MarketPosition.Long){
      // Once the price is greater than entry price+40 ticks, set stop loss to 20 ticks from entry
      if(Close[0] > Position.AvgPrice + 40 * TickSize && Close[0] < Position.AvgPrice + 60 * TickSize){
      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 20 * TickSize);
      
      }
      } else if (Position.MarketPosition == MarketPosition.Long) {
      // Once the price is greater than entry price+60 ticks, set stop loss to 40 ticks from entry
      if(Close[0] > Position.AvgPrice + 60 * TickSize && Close[0] < Position.AvgPrice + 70 * TickSize) {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 40 * TickSize); 
      
      }
      
      } else if ( Position.MarketPosition == MarketPosition.Long) {
      // Once the price is greater than entry price+70 ticks, set trailing stop loss to 10 ticks.
      if(Close[0] > Position.AvgPrice + 70 * TickSize) {
      SetTrailStop(CalculationMode.Ticks, 10);
      
      }
      }

      From a coding standpoint you are using SetTrailStop and regrettably you can only use SetTrailStop() or SetStopLoss(). When both are used, the SetStopLoss will override (unless they are applied to different entry signals).

      Here is the helpguide reference: http://ninjatrader.com/support/helpG...ttrailstop.htm Please observe the first line in the "Tips" section.

      Comment


        #4
        Originally posted by BernWillChris View Post
        Hi Ninjatrader programmers,

        I took a screenshot of my IDE to make it easier to read. I'm also going to paste the code below in case someone wants to copy and paste it to an IDE to try to figure it out. What I'm trying to do is create a stop-loss that is set to:

        -20 at the beginning of trade
        Breakeven when the market has moved in my favor by 20 pips
        +20 when the market has moved in my favor by +40 pips
        +40 when the market has moved in my favor by +60 pips
        Trailing stop loss of -10 once price has moved in my favor by 70 pips.

        This code below looks like it should work. If you would like to get more information about my backtest results, please let me know. This code is simple, I just don't know why I can't get it to work. Thanks a million.





        if (Position.MarketPosition == MarketPosition.Flat)
        {
        // Resets stop-loss when market becomes flat.
        SetStopLoss(CalculationMode.Ticks, stoplossticks);

        } else if (Position.MarketPosition == MarketPosition.Long){
        // Once the price is greater than entry price+20 ticks, set stop loss to breakeven
        if (Close[0] > Position.AvgPrice + 20 * TickSize && Close[0] < Position.AvgPrice + 40 * TickSize){
        SetStopLoss(CalculationMode.Price, Position.AvgPrice);

        }
        } else if (Position.MarketPosition == MarketPosition.Long){
        // Once the price is greater than entry price+40 ticks, set stop loss to 20 ticks from entry
        if(Close[0] > Position.AvgPrice + 40 * TickSize && Close[0] < Position.AvgPrice + 60 * TickSize){
        SetStopLoss(CalculationMode.Price, Position.AvgPrice + 20 * TickSize);

        }
        } else if (Position.MarketPosition == MarketPosition.Long) {
        // Once the price is greater than entry price+60 ticks, set stop loss to 40 ticks from entry
        if(Close[0] > Position.AvgPrice + 60 * TickSize && Close[0] < Position.AvgPrice + 70 * TickSize) {
        SetStopLoss(CalculationMode.Price, Position.AvgPrice + 40 * TickSize);

        }

        } else if ( Position.MarketPosition == MarketPosition.Long) {
        // Once the price is greater than entry price+70 ticks, set trailing stop loss to 10 ticks.
        if(Close[0] > Position.AvgPrice + 70 * TickSize) {
        SetTrailStop(CalculationMode.Ticks, 10);

        }
        }
        Without looking at any other considerations, you are processing your code in the wrong order. You must process from highest value filter to lowest, otherwise you will always satisfy the lowest filter, precisely because it is the lowest. A value that is bigger than 40 is necessarily bigger than 20, so will get processed by the >20 filter, as it is the first in order.

        You will also have to create a manual trailing stop, as you cannot use both SetStop() and SetTrailStop() in the same class file. There is an example in the subforum that lists reference sample code.

        ref: http://ninjatrader.com/support/forum...ead.php?t=3222

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X