Need a trailing stop loss that moves more dynamic
Forums › ProRealTime English forum › ProOrder support › Need a trailing stop loss that moves more dynamic
- This topic has 18 replies, 4 voices, and was last updated 7 years ago by
tob107.
-
-
02/08/2018 at 3:48 PM #62034
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 = 40This 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? 🙂
02/08/2018 at 5:12 PM #62055I 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.
02/09/2018 at 12:11 AM #62084This 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)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051//******************************************************************************************************//// trailing stop functiontrailingstep = 5 //5 trailing step to move the "stoploss" after BreakEvenstartBreakeven = 5 //5 pips in gain to activate the breakeven functionPointsToKeep = 3 //3 pips to keep in profit above/below entry price when the breakeven is activated////reset the stoploss valueIF NOT ONMARKET THENnewSL = 0breakevenLevel = 0ENDIF//******************************************************************************************************// ----- BREAKEVEN code////reset the breakevenLevel when no trade are on market// --- LONG sideIF LONGONMARKET AND (close - tradeprice(1)) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THENbreakevenLevel = tradeprice(1) + (PointsToKeep * pipsize) //calculate the breakevenLevelENDIF// --- SHORT sideIF SHORTONMARKET AND (tradeprice(1) - close) >= (startBreakeven * pipsize) AND breakevenlevel = 0 THENbreakevenLevel = tradeprice(1) + (PointsToKeep * pipsize) //calculate the breakevenLevelENDIF//Set new Stop LossIF breakevenLevel > 0 THENnewSL = BreakEvenLevelENDIF//******************************************************************************************************// ----- TRAILING STOP code////manage long positionsIF LONGONMARKET AND BreakEvenLevel THEN//next moves after BreakEvenIF newSL > 0 AND ((close - newSL) >= (trailingstep * pipsize)) THENnewSL = newSL + (trailingstep * pipsize)ENDIFENDIF//manage short positionsIF SHORTONMARKET AND BreakEvenLevel THEN//next moves after BreakEvenIF newSL > 0 AND ((newSL - close) >= (trailingstep * pipsize)) THENnewSL = newSL - (trailingstep * pipsize)ENDIFENDIF//stop order to exit the positionsIF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//******************************************************************************************************02/09/2018 at 12:17 AM #62087Sorry, I attached the same code twice!
02/09/2018 at 9:50 AM #62097Hi 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?
02/09/2018 at 10:03 AM #62099Hi 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).02/09/2018 at 10:12 AM #6210002/09/2018 at 10:29 AM #62103Oh 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 🙂
123456789101112131415161718192021222324252627///trailing stoptrailingstop = 40StartTrailingStopValue = 40//resetting variables when no trades are on marketif not onmarket thenMAXPRICE = 0priceexit = 0endif//case LONG orderif longonmarket thenMAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current tradeif close-tradeprice(1) > StartTrailingStopValue*pointsize thenif MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop thenpriceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price levelendifendifendif//exit on trailing stop price levelsif onmarket and priceexit>0 thenEXITSHORT AT priceexit STOPSELL AT priceexit STOPendif02/09/2018 at 10:58 AM #62105Line 27 of tje code I posted should be replaced by
1newSL = 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!
02/09/2018 at 11:28 AM #62108My 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…
02/09/2018 at 2:09 PM #62124Hi 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 ^
02/09/2018 at 2:46 PM #62131Hi 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.
02/20/2018 at 9:32 PM #6332202/22/2018 at 11:02 AM #6347702/22/2018 at 12:18 PM #63510You can add a SET STOP LOSS instuction if you want, for sure.
-
AuthorPosts
Find exclusive trading pro-tools on