Updated version of trailing SL
Forums › ProRealTime English forum › ProOrder support › Updated version of trailing SL
- This topic has 19 replies, 3 voices, and was last updated 7 months ago by Acivi90.
-
-
08/18/2017 at 11:44 AM #44175
Hi,
I tried to post a comment here in another thread (https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/) but could add code, so I’ll post it here instead.
This has been in the back of my mind for quite some time, and I finally got around to work on it.
I have changed the way that the SL is updated after first being initialized. Instead of moving in increments of TrailingStep, the newSL is now based on the close of the highest candle since the trade was entered.
So imagine if a trade goes our way, with 5 30 pip candles (!) after the SL was first initialized. Then, to use TrailingStep of 5 (as above), the SL would move 5*5 (=25) pips in the original version. If we instead base the SL on the previously closed candle and we use the TrailingStart of 20 as above, we lock in 130 out of the 150 pips.
Since I’m working on pretty short time frames, this suits me better. Hopefully someone else can make use of it as well.
I had to introduce the variable CAND to find the highest/lowest close from the time the trade was entered, instead of simply use higher/lower close since there can be spikes and dips. And, like I mentioned before – TrailingStart is now the difference between the highest/lowest close and the SL (aka how much leg room you’ll allow price) and the TrailingStep is how much you’d like to lock in when price have gone TrailingStart in your way.
No offence, Nicolas – I’m just trying to improve it!
Comments are more than welcome!
Tobias
12345678910111213141516171819202122232425262728293031323334353637383940IF TSL = 1 THEN//reset the stoploss valueIF NOT ONMARKET THENnewSL = 0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingStart*PipSize THENnewSL = TRADEPRICE(1) + TrailingStep*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] > HIGHEST[CAND](CLOSE) THENnewSL = CLOSE[1] - TrailingStart*PipSizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingStart*PipSize THENnewSL = TRADEPRICE(1) - TrailingStep*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] < LOWEST[CAND](CLOSE) THENnewSL = CLOSE[1] + TrailingStart*PipSizeENDIFENDIF//stop order to exit the positionsIF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFSET STOP pLOSS SLENDIF6 users thanked author for this post.
08/18/2017 at 2:59 PM #4418808/21/2017 at 1:01 PM #44305Here’s an slightly updated version:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748SL = 20 // Initial SLTP = 30TSL = 1 // Use TSL?TrailingDistance = 20 // Distance from close to TSLTrailingStep = 3 // Pips locked at start of TSL//************************************************************************IF TSL = 1 THEN//reset the stoploss valueIF NOT ONMARKET THENnewSL = 0CAND = 0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL = 0 AND CLOSE - TRADEPRICE(1) >= TrailingDistance*PipSize THENnewSL = TRADEPRICE(1) + TrailingStep*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] >= HIGHEST[CAND](CLOSE) THENnewSL = CLOSE[1] - TrailingDistance*PipSizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL = 0 AND TRADEPRICE(1) - CLOSE[1] >= TrailingDistance*PipSize THENnewSL = TRADEPRICE(1) - TrailingStep*PipSizeENDIF//next movesCAND = BarIndex - TradeIndexIF newSL > 0 AND CLOSE[1] <= LOWEST[CAND](CLOSE) THENnewSL = CLOSE[1] + TrailingDistance*PipSizeENDIFENDIF//stop order to exit the positionsIF newSL > 0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFSET STOP pLOSS SLENDIFEnjoy!
5 users thanked author for this post.
12/05/2017 at 11:24 PM #5483412/07/2017 at 12:58 PM #5497912/09/2017 at 1:29 PM #55214Hi,
I understand that this type of trailing does not work in a 4h candle, if all the action occurs within those four hours. I am backtesting and seeing that big candles do not read trailing information if everything happens in one candle.
I understand that the code reads the changes at the CLOSE, so if everything happens in just one candle nothing happens.
EXAMPLE: trailingstart = 20, trailingstep=10, TF:H4, LONG candle, open 1,1310, close 1,1329, maximum 1,1360…..nothing happens, the operation continue in the next candle and goes wrong…
Can anyone confirm this???
Thanks,
Juan
01/06/2018 at 10:40 PM #57547Hi Juan. I also have similar problems: I understand that the important value of the candle is the low value (for long trades). That means that, if in some moment of the candle (4H or day candle, as I use) the price touches the already activated newSL, the trade should stop immediately because this is the given instruction code: IF xxx SELL AT newSL STOP.
But also in my case, there are many candles where the trailing stop doesn’t work properly, and the problem is that I don’t know the reason.
Could anyone help us?
01/07/2018 at 12:20 AM #57551A general remark how I set my trailing stops (as a highspeed scalper I do not allow ANY kind of retracements):
Base: a long trend is defined as higher highs and higher lows.
Going long: first a security stop (1 Min. DAX 10 points).
Second step: Break even, if in profit (caculated with 2*spread+2*slippage expressed in Euro).
Third step: Set absolute stop at low of previous candle-(spread+slippage).
Repeat each minute until getting stopped out.
A violation of the previous bars low means no longer a long trend by definition, hard but simple.
I understand that for a H4 or daily that would be sometimes a lot of points – that’s why I am no longtermer – but with this I do not have to play with such things as “after how many points to start for how many points”. It just trails each bar with the low of the previous bar. // Remark: during PRT using time I did not robot, but I do with MT.
04/20/2018 at 10:51 PM #6889105/22/2019 at 10:55 AM #9914504/15/2024 at 8:56 PM #23155804/16/2024 at 6:41 PM #231591Hi,
You can change these lines in the trailing stop…
(There are a number of errors in this trailing stop)
IF newSL > 0 and Close[1] >= Highest[CAND](Close) THEN
newSL = Close[1] – (0.006*Close[1])
IF newSL > 0 and Close[1] <= Lowest[CAND](Close) THEN
newSL = Close[1] + (0.006*Close[1])
04/17/2024 at 8:49 PM #231633Thanks for your answer, one question more. Iam not that good att this codeing.
If i want a trailing stopp that start when i am 0.1% profit and it should trailing 0.3% from tradingprice, this will work as i want for a long position?
TrailingPercent = 0.1
Trailingdistans = 0.003
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100)
endif//Reset the trailing value
IF NOT ONMARKET THEN
newSL=0
CAND = 0
ENDIF//Manage long positions
IF LONGONMARKET THEN
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = Close[1] – (trailingdistans*Close[1])
ENDIF
//next moves
CAND = BarIndex – TradeIndex
IF newSL > 0 and Close[1] >= Highest[CAND](Close) THEN
newSL = Close[1] – (trailingdistans*Close[1])
ENDIF
ENDIFIF newSL>0 THEN
SELL AT newSL STOP
ENDIF04/17/2024 at 9:12 PM #23163404/17/2024 at 9:30 PM #231635Yes this was just a exampel on the percentage, i am going to look into what value i will use when the code is done.
I was doing a research on a algo with backtest and useing “SET STOP %TRAILING” but when i as going to start on demo that code wasnt okey to use.
The code i write above had a hole, if the trade goes up 0.05% i have now stop so now i am rewrite it to below.
And a again, really Appreciate your time into this.
// Stops and targets
SET STOP %LOSS 0.6
SET TARGET %PROFIT 1.2
//%trailing stop function
trailingPercent = 0.1
Trailingdistans = 0.005
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
endif//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
CAND = 0
ENDIF//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = Close[1] – (trailingdistans*Close[1])
ENDIF
//next moves
CAND = BarIndex – TradeIndex
IF newSL > 0 and Close[1] >= Highest[CAND](Close) THEN
newSL = Close[1] – (trailingdistans*Close[1])
ENDIF
ENDIF//stop order to exit the positions
IF newSL>0 AND CLOSE < NEWSL THEN
SELL AT MARKET
ENDIF -
AuthorPosts
Find exclusive trading pro-tools on