Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stop & Stop Loss

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

    #16
    1. Yes, you will want to make sure you are resetting the stop loss when you're flat, otherwise it will try to use the last price from the last trade, which is likely going to be completely invalid. The reset will ensure it is X number of ticks away from the entry price, and then from there you can make changes to the price dynamically.

    2. This depends under what condition you want to move the stop loss. In the example code, the stop less set set to the AvgPrice once the Close is greater than AvgPrice by 10 ticks. If you reused my example instead of what is in the sample originally, it would move it to your break even,and then continue to trail the last traded price by 10 ticks.
    MatthewNinjaTrader Product Management

    Comment


      #17
      Some more code for the esteemed panel

      Hi all,

      I'm bumped into this -somewhat older- thread and it already solved a part of my problems, but sadly not all. I hope someone is still watching

      I have a strategy that has 8 different 'trades', each with its own set of opening conditions. I am now hoping to have 3 different definitions for StopLoss for every trade:
      • I would like to set a SL of a predefined value (called 'Stop') when the position is opened
      • I would like to move that SL to BreakEven once price crosses a certain level in of Profit is achieved (defined as 'BE')
      • And then I would want the SL to act as a trailing Stop, as mentioned in the older posts here, again with a defined value, 'Tr'

      This is my code so far:


      Code:
      //Trailing Stop on Open for SRA
                  if (trailSRA == true && Position.MarketPosition == MarketPosition.Short)    
                      {
                      SetStopLoss("SRA",CalculationMode.Ticks,Stop,false);  }    
                  // Breakeven for SRA
                  if (trailSRA == true && Position.MarketPosition == MarketPosition.Short && Position.AvgPrice == (OpSRA + (BE * TickSize)))
                      {
                          SetStopLoss("SRA",CalculationMode.Ticks,OpSRA, false);   }
                 //Trailing Stop during for SRA
                  if (trailSRA == true && Position.MarketPosition == MarketPosition.Short)    
                      {
                      SetStopLoss("SRA",CalculationMode.Ticks,Close[0]+(Tr*TickSize),false); }
      Notes:
      • 'OpSRA' is the value of Open[0] as defined for this particular trade, called 'SRA'
      • I would like to run this strategy with COBC = false, since I'm using Renko Bars



      Sadly, it does not run

      Can someone point me in the right direction please ?

      Thanks in advance !!
      Last edited by Spinn; 05-13-2016, 09:43 AM.

      Comment


        #18
        Hello Spinn,

        Thanks for your post.

        With reference to your code. If the variable Stop is specified in Ticks then the first SetStopLoss statement should be fine.

        For the BE stop, the logic check of "Position.AvgPrice == (OpSRA + (BE * TickSize)" is a fixed comparison as neither side changes. If I may suggest you would want something like Close[0] <= Position.AvgPrice - BE * TickSize as this then compares the current price to the entry - the BE value (which I assume is ticks). You would subtract as I am assuming in this short direction you want your BE to be slightly profitable. In addition you would want to change the calculation mode from ticks to price because you are now referencing price, example:
        SetStopLoss("SRA",CalculationMode.Price,Position.A vgPrice - BE * TickSize, false);


        For the trailing stop, I would again change the mode to Price as you are using the Close[0] price. I suspect that the (Tr*TickSize) is meant to be subtracted from the entry price as we are going short (it is added).

        In addition you will need to add some bool logic so that the initial stop is set once, then the BE is set once and after those two are done to then only use the trail stop. In the following example I will use two bools called initialset and beSet note that these will need to be declared in your variables section and set to false. When you are flat you will need to set these bool back to false for the next time you have a short entry.

        Here is an example of what I've discussed above:

        Code:
        	//Trailing Stop on Open for SRA
            if (trailSRA == true && Position.MarketPosition == MarketPosition.Short && !initialSet)    
            {
             SetStopLoss("SRA",CalculationMode.Ticks,Stop,false);  
        	 initialSet = true; // set initial stop only once
        	}    
                    
            // Breakeven for SRA
            if (initialSet && !beSet && trailSRA == true && Position.MarketPosition == MarketPosition.Short 
        	    && Close[0] <= Position.AvgPrice - BE * TickSize)
            {
              SetStopLoss("SRA",CalculationMode.Price, Position.AvgPrice - BE * TickSize, false); 
              beSet = true;	  // set BE stop once
        	}
                   
            //Trailing Stop during for SRA
            if (beSet && trailSRA == true && Position.MarketPosition == MarketPosition.Short)    
            {
              SetStopLoss("SRA",CalculationMode.Price ,Close[0]+(Tr*TickSize),false); 
        	}
        	[COLOR="Red"]//NOTE: Further work will be needed on the trail stop, see notes below[/COLOR]
        For clarity of the bool logic added in the above. In the first stop, the bool initialSet is checked to make sure it is false, it is is then the initial stop will be set and the bool initialStop will be set true so that the initial stop statement cannot be set again.

        For the BE stop, InitialSet must be true and beSet must be false and price must be less than or equal to the entry price - BE * TickSize. If those are true then the BE stop is set and beSet is made true

        For the trail stop, beSet must be true.

        Please observe that you have no logic that defines when, after the BE stop is set to begin trailing the stop. Also you have no logic that would prevent your trailing stop from going backwards. In this you will need to invest some time to code what you need. Basically you would need to set a starting trail level and compare current price and adjust in one direction only.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        633 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        364 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        567 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X