Trailing stop as a percentage for control
Forums › ProRealTime English forum › ProOrder support › Trailing stop as a percentage for control
- This topic has 7 replies, 3 voices, and was last updated 3 years ago by
phoentzs.
-
-
11/16/2021 at 8:53 PM #181693
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?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263//************************************************************************//trailing stop functionpercentageLong = startlpercentageShort = startspercentagestepL = steplpercentageStepS = stepstrailingStartL = (close/100)*percentageLongtrailingStartS = (close/100)*percentageShorttrailingStepL = (close/100)*percentageStepLtrailingStepS = (close/100)*percentageStepSDistance = 7 * PipSize //7//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstartL*pipsize THENnewSL = tradeprice(1)+trailingstepL*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstepL*pipsize THENnewSL = newSL+trailingstepL*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstartS*pipsize THENnewSL = tradeprice(1)-trailingstepS*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstepS*pipsize THENnewSL = newSL-trailingstepS*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENIF LongOnMarket THENIF (close - Distance) > newSL THENSELL AT newSL STOPELSIF (close + Distance) < newSL THENSELL AT newSL LIMITELSESELL AT MarketENDIFELSIF ShortOnMarket THENIF (close - Distance) > newSL THENEXITSHORT AT newSL LIMITELSIF (close + Distance) < newSL THENEXITSHORT AT newSL STOPELSEEXITSHORT AT MarketENDIFENDIFendif//************************************************************************11/16/2021 at 9:10 PM #181695The 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:
1234567891011NewTrade = (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 openedIf NewTrade ThenpercentageLong = startlpercentageShort = startspercentagestepL = steplpercentageStepS = stepstrailingStartL = (TradePrice/100)*percentageLong/PipSizetrailingStartS = (TradePrice/100)*percentageShort/PipSizetrailingStepL = (TradePrice/100)*percentageStepL/PipSizetrailingStepS = (TradePrice/100)*percentageStepS/PipSizeEndif1 user thanked author for this post.
11/16/2021 at 9:13 PM #181696I 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!)
11/16/2021 at 9:19 PM #18169811/16/2021 at 9:24 PM #181699no, 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.
11/16/2021 at 9:34 PM #18170111/16/2021 at 9:45 PM #181703you might also experiment with:
1234567891011121314151617181920212223//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND high-tradeprice(1)>=trailingstartL*pipsize THENnewSL = tradeprice(1)+trailingstepL*pipsizeENDIF//next movesIF newSL>0 AND high-newSL>=trailingstepL*pipsize THENnewSL = newSL+trailingstepL*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-low>=trailingstartS*pipsize THENnewSL = tradeprice(1)-trailingstepS*pipsizeENDIF//next movesIF newSL>0 AND newSL-low>=trailingstepS*pipsize THENnewSL = newSL-trailingstepS*pipsizeENDIFENDIFuses 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.
3 users thanked author for this post.
11/16/2021 at 10:02 PM #181704 -
AuthorPosts
Find exclusive trading pro-tools on