Hello, I know there are already a lot of topics about trailing stops. Only some of them are equipped with extra functions that unfortunately I don’t really understand the code. I’m not a programmer …
Based on Nicolas Code, I separated this into long and short with the help of Roberto and also built in a distance. This is to prevent the trailing from working below the minimum distance of the broker or exiting beforehand.
Now I have tried to change this code from points to percent myself.
Can an experienced programmer have a look over here? Does the code reliably do what it should? Without surprises?
//************************************************************************
//trailing stop function
percentageLong = startl
percentageShort = starts
percentagestepL = stepl
percentageStepS = steps
trailingStartL = (close/100)*percentageLong
trailingStartS = (close/100)*percentageShort
trailingStepL = (close/100)*percentageStepL
trailingStepS = (close/100)*percentageStepS
Distance = 7 * PipSize //7
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstartL*pipsize THEN
newSL = tradeprice(1)+trailingstepL*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstepL*pipsize THEN
newSL = newSL+trailingstepL*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstartS*pipsize THEN
newSL = tradeprice(1)-trailingstepS*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstepS*pipsize THEN
newSL = newSL-trailingstepS*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
IF LongOnMarket THEN
IF (close - Distance) > newSL THEN
SELL AT newSL STOP
ELSIF (close + Distance) < newSL THEN
SELL AT newSL LIMIT
ELSE
SELL AT Market
ENDIF
ELSIF ShortOnMarket THEN
IF (close - Distance) > newSL THEN
EXITSHORT AT newSL LIMIT
ELSIF (close + Distance) < newSL THEN
EXITSHORT AT newSL STOP
ELSE
EXITSHORT AT Market
ENDIF
ENDIF
endif
//************************************************************************
The way it is written now, it always changes values each bar, which must not happen.
Moreover I suggest that TRADEPRICE be used, instead of close, to calculate percentages.
In addition you have to add PIPSIZE to calculatations.
Replace lines 3-10 with these ones:
NewTrade = (OnMarket AND Not OnMarket[1]) OR (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket) //True when there's been a change in the direction (likely to be due to a Stop & Reverse) or a new trade has been just opened
If NewTrade Then
percentageLong = startl
percentageShort = starts
percentagestepL = stepl
percentageStepS = steps
trailingStartL = (TradePrice/100)*percentageLong/PipSize
trailingStartS = (TradePrice/100)*percentageShort/PipSize
trailingStepL = (TradePrice/100)*percentageStepL/PipSize
trailingStepS = (TradePrice/100)*percentageStepS/PipSize
Endif
I think lines 7 – 10 need to be (tradeprice/100) or (positionprice/100) rather than (close/100)
otherwise, surely it’s going to read each new close and start measuring from there. the trail will only start if it gets to the trailingstart distance in one candle.
(Roberto got in before me!)
Thanks Roberto, I’ll try.
@nonethless: Doesn’t every trailing stop do that? I also like their trailing stop, but I miss the function of distance, i.e. the minimum distance.
no, it should measure from the entry price (tradeprice or positionprice) which doesn’t change. positionprice will change if you use cumulative orders, but the trail then measures from the aggregate.
the minimum distance can also be resolved by ticking the box that says ‘Readjust stops’ when you launch the code. Then PRT automatically keeps your trail and SL at the min distance.
I tried that. It works on the live account. But it often doesn’t work on the demo account. At least for me. I do not know why. That’s why I wanted a second solution.
you might also experiment with:
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND high-tradeprice(1)>=trailingstartL*pipsize THEN
newSL = tradeprice(1)+trailingstepL*pipsize
ENDIF
//next moves
IF newSL>0 AND high-newSL>=trailingstepL*pipsize THEN
newSL = newSL+trailingstepL*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-low>=trailingstartS*pipsize THEN
newSL = tradeprice(1)-trailingstepS*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-low>=trailingstepS*pipsize THEN
newSL = newSL-trailingstepS*pipsize
ENDIF
ENDIF
uses high and low as a trigger instead of close, so it goes to breakeven more quickly. ‘close’ sometimes produces more profit whereas using ‘high,low’ is more protective, higher % win – depends on which you prioritise.
In a quick direct comparison, your trailing stop is worlds better and takes a lot more profits. I have to check that with a little more time to find out why. Unfortunately I don’t have any more time today.