So until now i have been using the MFE/TSL thats on the forum, this is has been working OK, but i see major flaws with this..
Lets use an example:
trailingstop = 20
StartTrailingStopValue = 40
This means that the trailing stop will activate when 40 pip profit is made, and the trailing stop is then 20 pips
Now, to move the trailing stop i would like 1 pip more in profit = 1 pip more in stop loss, but with this stop loss that im using today, i need another 40 pips of profit before the stop loss moves up 20.
I need a stop loss that moves +1 for every pip i make, after 40 pips of profit.
I think that i have seen such a code on this forum? but i cant seem to find it, or i might have misunderstood what i read then.
So if i have 100 pips of profit, my stop loss is only 40, but i want it to be @ 60 pips. I want the stop loss to start @ 40pips profit, and just continue 1 pip up for every pip i make in profit.
Can someone help me? 🙂
I realise that what i wrote might have been confusing to read, sorry and let me rephrase so its more understandable.
As it is now, the trailing stop loss im using moves the stop loss 20 pips up, for every 40 pips of profit i make.
What i would like is a stop loss that works like this:
When the trade starts, my target is 200 pips and stop loss is 90 pips.
If my trade gets 40 pips in profit i want a trailing stop loss to start, with 40 pips as trailing stop.
In other words, when i get 40 pips profit, my new stop loss is 0 pips profit.
If my trade goes to 60 pips profit, my new stop loss is at 20 pips profit. (60 pips profit in trade- 40 pip trailing stop loss = 20 pips profit)
if my trade goes to 100 pips profit, my new stop loss is at 60 pips. ( 100 pips profit in trade – 40 pip trailing stop loss = 60 pips profit)
I realize PRT systems can only move the stop loss after each candle(?), and thats fine, gotta live with that. Unless it can work with the equity curve, calculating my highest high in EQ curve and calculate new stop loss as it moves up or down in profit? But anyways, if the candle moves 5 or 20 pips up, before it closes, i want the stop loss moved every time my profit is peaking above 40+ pips.
I hope this was more clear.
This a combination of Nicolas‘ Breakeven & Trailing Stop code (https://www.prorealcode.com/blog/learning/breakeven-code-automated-trading-strategy/ & https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/).
I did not test it much, nor did I use GRAPH to debug it. Give me some feedback.
You may insert this code in your strategy provided you first set the three variables:
- trailingstep = number of pips to add to your profit, after breakeven, while on the rise, as a safeguard in case the market reverses
- startBreakeven = number of pips to wait before setting your SL to breakeven
- PointsToKeep = number of pips to add to breakeven so that it’s not so even (thus less painful if hit)
//******************************************************************************************************
//
// trailing stop function
trailingstep = 5 //5 trailing step to move the "stoploss" after BreakEven
startBreakeven = 5 //5 pips in gain to activate the breakeven function
PointsToKeep = 3 //3 pips to keep in profit above/below entry price when the breakeven is activated
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
breakevenLevel = 0
ENDIF
//******************************************************************************************************
// ----- BREAKEVEN code
//
//reset the breakevenLevel when no trade are on market
// --- LONG side
IF LONGONMARKET AND (close - tradeprice(1)) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THEN
breakevenLevel = tradeprice(1) + (PointsToKeep * pipsize) //calculate the breakevenLevel
ENDIF
// --- SHORT side
IF SHORTONMARKET AND (tradeprice(1) - close) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THEN
breakevenLevel = tradeprice(1) + (PointsToKeep * pipsize) //calculate the breakevenLevel
ENDIF
//Set new Stop Loss
IF breakevenLevel > 0 THEN
newSL = BreakEvenLevel
ENDIF
//******************************************************************************************************
// ----- TRAILING STOP code
//
//manage long positions
IF LONGONMARKET AND BreakEvenLevel THEN
//next moves after BreakEven
IF newSL > 0 AND ((close - newSL) >= (trailingstep * pipsize)) THEN
newSL = newSL + (trailingstep * pipsize)
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET AND BreakEvenLevel THEN
//next moves after BreakEven
IF newSL > 0 AND ((newSL - close) >= (trailingstep * pipsize)) THEN
newSL = newSL - (trailingstep * pipsize)
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//******************************************************************************************************
Sorry, I attached the same code twice!
Hi Robert, thanks for the reply, sadly that does not help me as much.
I need the stop loss to be activated at 40 pips profit, and then just constantly work as a normal trailing stop loss. So if my trade goes to 55 pips profit, my stop loss should be at 15 pips profit.
Am i reading the code wrong?
Hi Robert, thanks for the reply, sadly that does not help me as much. I need the stop loss to be activated at 40 pips profit, and then just constantly work as a normal trailing stop loss. So if my trade goes to 55 pips profit, my stop loss should be at 15 pips profit. Am i reading the code wrong?
Unless there’s some logic error in the code, you should set:
- startBreakeven = 40
- set trailingstep = 15
and it should do. Did you test it?
As for PointsToKeep , it only affectes the way you consider BreakEven, if ZERO it is a true BreakEven, any other value is BreakEven +- some pips, but it does not (well… it should not, I did not test it).
Hey, ive tried with the settings u said, but same results.
Added photo to show what i mean
Oh i think i have read the MFE trailing wrong, cus it seems to do what i need to do.
Ill start testing with this and see if its right 🙂
///trailing stop
trailingstop = 40
StartTrailingStopValue = 40
//resetting variables when no trades are on market
if not onmarket then
MAXPRICE = 0
priceexit = 0
endif
//case LONG order
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
if close-tradeprice(1) > StartTrailingStopValue*pointsize then
if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level
endif
endif
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
Line 27 of tje code I posted should be replaced by
newSL = BreakEvenLevel - (PointsToKeep * pipsize)
so that the SL is not affected by the value in PointsToKeep.
As for the rest I tested it on DAX 5min and it works finely!
My previous post (#62105) should be ignored, PointsToKeep will always affect NewSL, unless you do some math to remeber, for the second setting of NewSL, to subtract PointsToKeep pips that were added the previous setting. This could be done, but it complicates a bit the code.
So if you set:
- trailingstep = 10
- startBreakeven = 30
- PointsToKeep = 2
BreakEven will be activated when your profit reaches 30+ pips (at closing time) and the SL would be set at a 2 pip-profit (BreakEven + PointsToKeep).
Then the code will wait till SL + trailingstep (at closing time) is reached to update it to 12. And so on…
Hi again Robert, so im wondering: if the candle HIGH is above 30, but the close is below 30, will the stop loss be activated then?
What i dont like about that code is that it will only move the stop loss for every 30 pip i reach, right?
Så it moves the stop loss when i go above 30, but it will not move the stop loss if i reach 50 pips in profit, but at 60 pips profit it will move the stop loss again, am i right?
What i would like is something that moves the stop loss all the time, regardless if im 40 pips up, or 50 or 80. it will always be 40 pips below my profit-peak.
i think the MFE trailing stop loss that i copy/pasted there is doing just that ^
Hi again Robert, so im wondering: if the candle HIGH is above 30, but the close is below 30, will the stop loss be activated then? What i dont like about that code is that it will only move the stop loss for every 30 pip i reach, right? Så it moves the stop loss when i go above 30, but it will not move the stop loss if i reach 50 pips in profit, but at 60 pips profit it will move the stop loss again, am i right? What i would like is something that moves the stop loss all the time, regardless if im 40 pips up, or 50 or 80. it will always be 40 pips below my profit-peak. i think the MFE trailing stop loss that i copy/pasted there is doing just that ^
1 – if the candle HIGH is above 30, but the close is below 30, will the stop loss be activated then? NO, it will not
2 – What i would like is something that moves the stop loss all the time, regardless if im 40 pips up, or 50 or 80. Set startBreakeven=xx and trailingstep=1
This is not a real Trailing Stop as a native trailing stop. It’s just sort of…. written by Nicolas to overcome some limitations by IG/PRT native trailing stop support. We all have to wait for a better one when the new version is available.
Can you combine this code with a SL? If the market goes wrong from start, you will not get out of position.
You can add a SET STOP LOSS instuction if you want, for sure.