trailing stop with cumulative orders
Forums › ProRealTime English forum › ProOrder support › trailing stop with cumulative orders
- This topic has 16 replies, 4 voices, and was last updated 1 year ago by ZeroCafeine.
Tagged: cumulate, cumulative, POSITIONPRICE, stop, tradeprice, trailing, trailing stop
-
-
10/09/2020 at 8:27 PM #146926
I tested a new system today in 2 versions with cumulative orders true and false. The latter opened one position which closed in profit as the price retraced to the trailing stop.
The one with cumulative orders opened multiple positions, but none of them closed out as the price fell — ie the trailing stop had no effect at all.
Is there any way to make a trailing stop work with cumulative orders?
10/09/2020 at 9:23 PM #14693210/09/2020 at 11:24 PM #146945If you are using a code snippet as a trailing stop, replace TRADEPRICE with POSITIONPRICE, which is the average price used by ProOrder with multiple cumulated positions.
This should make things better, but previously saved profits would require to be updated after accumulating positions, to fully make it work as expected.
10/10/2020 at 12:18 AM #146947Thanks Roberto, that seems to have made a big difference. What do you mean though when you say, previously saved profits would require to be updated after accumulating positions ?
10/10/2020 at 12:49 AM #146950I mean that if you buy (say 1 lot) at 1000 with a trailing start at 20 and a trailing step of 10, after +32 points you’ll have:
Current Price 1032 PositionPrice (former TradePrice) is 1000
Profits locked in 10 (you will exit at 1010 in case price retraces)
after cumulating 1 more lot at, say 1034, PositionPrice is 1017 ((1000 + 1034) / 2):
Profits previously locked in 10 (you will still exit at 1010 in case price retraces, but you will lose because PositionPrice is now 1017)
update profits to be locked in 1027 1017 (current PositionPrice) + 10 pips (first step), so you will be able to exit profitaby at 1027.
It’s not that straightforward and needs some extra coding, but it’s worth doing it.
10/10/2020 at 1:30 AM #146951This is Nicolas’code updated to accomodate for cumulative positions (the attached pics highlights changes, apart from PositionPrice):
Nicolas'trailing stop123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354//*********************************************************************************// https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function///// trailing stop function//-------------------------------------------------------------//reset the stoploss valueIF NOT ONMARKET THENNewSL = 0MyPositionPrice = 0ENDIF//-------------------------------------------------------------// Update locked in profit, if any, after comulating positionsPositionCount = abs(CountOfPosition)IF NewSL > 0 THENIF PositionCount > PositionCount[1] THENIF LongOnMarket THENNewSL = max(NewSL,PositionPrice * NewSL / MyPositionPrice)ELSENewSL = min(NewSL,PositionPrice * NewSL / MyPositionPrice)ENDIFENDIFENDIF//-------------------------------------------------------------trailingstart = 20 //trailing will start @trailinstart points profittrailingstep = 5 //trailing step to move the "stoploss"//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-PositionPrice>=trailingstart*pipsize THENnewSL = PositionPrice+trailingstep*pipsizeENDIF//next movesIF newSL>0 AND close-newSL>=trailingstep*pipsize THENnewSL = newSL+trailingstep*pipsizeENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND PositionPrice-close>=trailingstart*pipsize THENnewSL = PositionPrice-trailingstep*pipsizeENDIF//next movesIF newSL>0 AND newSL-close>=trailingstep*pipsize THENnewSL = newSL-trailingstep*pipsizeENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFMyPositionPrice = PositionPrice//*********************************************************************************2 users thanked author for this post.
10/10/2020 at 1:49 AM #146956that’s great – thanks Roberto
10/11/2020 at 11:21 AM #147037Link to Nicolas Code above for Cum TS added as Log 253 here …
3 users thanked author for this post.
04/21/2023 at 5:34 PM #213589Is it better to replace lines 50 and 51 with the new Set Stop Price X function ?
04/22/2023 at 5:45 PM #213611Yes, it’s a code snippet written years before those new instructions were added.
It’s easy to replace them, just replace both of those lines with this one:
1Set Stop Price NewSL1 user thanked author for this post.
04/29/2023 at 4:17 PM #213850Yes, it’s a code snippet written years before those new instructions were added.
It’s easy to replace them, just replace both of those lines with this one:
1Set Stop Price NewSLIs it possible to use set stop price for exit from a position but with 2 different level and of course different qantity ?
123Set Stop Price PriceA // 10% of position at level ASet Stop Price PriceB // 90% of position at level B04/29/2023 at 4:48 PM #213852You could set up a Test Algo on your Demo Account …
12345678If barindex > 0 ThenBuy 10 Contracts at MarketEndifIf LongonMarket thenSell 1 Contracts at TradePrice - 25*pipsize StopSell 9 Contracts at YradePrice - 75*pipsize StopEndif04/29/2023 at 5:00 PM #213853It works, but doesn’t drop to 0 positions … thats for you to figure out! 😉
123456789DEFPARAM CUMULATEORDERS = TrueIf Time = 080000 ThenBuy 10 Contracts at MarketEndifIf LongonMarket thenSell 1 Contracts at TradePrice - 25*pipsize StopSell 9 Contracts at TradePrice - 50*pipsize StopEndif1 user thanked author for this post.
04/29/2023 at 5:05 PM #213855Sorted!
It’s more fun trying and just doing it?
123456789DEFPARAM CUMULATEORDERS = TrueIf Time = 080000 ThenBuy 10 Contracts at MarketEndifIf LongonMarket thenSell 1 Contracts at Average[20](close) - 25*pipsize StopSell 9 Contracts at Average[20](close) - 50*pipsize StopEndif1 user thanked author for this post.
04/29/2023 at 9:38 PM #213859It’s more fun trying and just doing it?
I was only jesting! 🙂
Coding Algos can be frustrating and very time consuming. We should all try to help each other more, even though at times it feels like the blind leading the blind! 😉
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on