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?
I guess the last trade to open did not achieve the starting level of the trailing start and so therefore this non-started trailing stop was applied (i.e. not applied) to all cumulated trades?
If 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.
Thanks 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 ?
I 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.
This is Nicolas’code updated to accomodate for cumulative positions (the attached pics highlights changes, apart from
PositionPrice):
//*********************************************************************************
// https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
//
// trailing stop function
//-------------------------------------------------------------
//reset the stoploss value
IF NOT ONMARKET THEN
NewSL = 0
MyPositionPrice = 0
ENDIF
//-------------------------------------------------------------
// Update locked in profit, if any, after comulating positions
PositionCount = abs(CountOfPosition)
IF NewSL > 0 THEN
IF PositionCount > PositionCount[1] THEN
IF LongOnMarket THEN
NewSL = max(NewSL,PositionPrice * NewSL / MyPositionPrice)
ELSE
NewSL = min(NewSL,PositionPrice * NewSL / MyPositionPrice)
ENDIF
ENDIF
ENDIF
//-------------------------------------------------------------
trailingstart = 20 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-PositionPrice>=trailingstart*pipsize THEN
newSL = PositionPrice+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND PositionPrice-close>=trailingstart*pipsize THEN
newSL = PositionPrice-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
MyPositionPrice = PositionPrice
//*********************************************************************************
that’s great – thanks Roberto
Link to Nicolas Code above for Cum TS added as Log 253 here …
Snippet Link Library
Is it better to replace lines 50 and 51 with the new Set Stop Price X function ?
Yes, 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:
Set Stop Price NewSL
Yes, 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:
Is it possible to use set stop price for exit from a position but with 2 different level and of course different qantity ?
Set Stop Price PriceA // 10% of position at level A
Set Stop Price PriceB // 90% of position at level B
You could set up a Test Algo on your Demo Account …
If barindex > 0 Then
Buy 10 Contracts at Market
Endif
If LongonMarket then
Sell 1 Contracts at TradePrice - 25*pipsize Stop
Sell 9 Contracts at YradePrice - 75*pipsize Stop
Endif
It works, but doesn’t drop to 0 positions … thats for you to figure out! 😉
DEFPARAM CUMULATEORDERS = True
If Time = 080000 Then
Buy 10 Contracts at Market
Endif
If LongonMarket then
Sell 1 Contracts at TradePrice - 25*pipsize Stop
Sell 9 Contracts at TradePrice - 50*pipsize Stop
Endif
Sorted!
It’s more fun trying and just doing it?
DEFPARAM CUMULATEORDERS = True
If Time = 080000 Then
Buy 10 Contracts at Market
Endif
If LongonMarket then
Sell 1 Contracts at Average[20](close) - 25*pipsize Stop
Sell 9 Contracts at Average[20](close) - 50*pipsize Stop
Endif
It’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! 😉