Is there any reason the following shouldn’t work with a moving average value (or any other defined indicator) as the trigger for a trailing stop?
Seems to work as expected in initial tests (where HULLa is defined further up the page)
once trailingstop = 1
if trailingstop then
trailingpercentlong = tsl // %
trailingpercentshort = tss // %
stepPercentlong = stl
stepPercentshort = sts
if onmarket then
trailingstartlong = tradeprice(1)*(trailingpercentlong/100) //trailing will start @trailingstart points profit
trailingstartshort = tradeprice(1)*(trailingpercentshort/100) //trailing will start @trailingstart points profit
trailingsteplong = tradeprice(1)*(stepPercentlong/100) //% step to move the stoploss
trailingstepshort = tradeprice(1)*(stepPercentshort/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartlong THEN
newSL = tradeprice(1)+trailingsteplong
ENDIF
//next moves
IF newSL>0 AND HULLa-newSL>trailingsteplong THEN
newSL = newSL+trailingsteplong
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartshort THEN
newSL = tradeprice(1)-trailingstepshort
ENDIF
//next moves
IF newSL>0 AND newSL-HULLa>trailingstepshort THEN
newSL = newSL-trailingstepshort
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
endif
tradeprice(1)-HULLa
Because at line 23 and the others you make a difference with inverted operators, write it as above.
Do you mean that line 23 should be
IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THEN
isn’t that effectively the same as line 35 for short positions?
Yes, in line 23 and the other ones those two operators need to be swapped.
Like so?
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THEN
newSL = tradeprice(1)+trailingsteplong
ENDIF
//next moves
IF newSL>0 AND newSL-HULLa>trailingsteplong THEN
newSL = newSL+trailingsteplong
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartshort THEN
newSL = tradeprice(1)-trailingstepshort
ENDIF
//next moves
IF newSL>0 AND HULLa-newSL>trailingstepshort THEN
newSL = newSL-trailingstepshort
ENDIF
ENDIF
or
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THEN
newSL = tradeprice(1)+trailingsteplong
ENDIF
//next moves
IF newSL>0 AND HULLa-newSL>trailingsteplong THEN
newSL = newSL+trailingsteplong
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartshort THEN
newSL = tradeprice(1)-trailingstepshort
ENDIF
//next moves
IF newSL>0 AND newSL-HULLa>trailingstepshort THEN
newSL = newSL-trailingstepshort
ENDIF
ENDIF
Yes, but I did not spot it before, you have to also replace TRADEPRICE(1) with CLOSE.
This is the original Nicolas’code and the modified version for you:
Close-tradeprice(1) //original
Close-Hulla //modified for Hull
the opposite for Short trades.
Roberto, the original TS that I had been using was like this:
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstartlong THEN
newSL = tradeprice(1)+trailingsteplong
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingsteplong THEN
newSL = newSL+trailingsteplong
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstartshort THEN
newSL = tradeprice(1)-trailingstepshort
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstepshort THEN
newSL = newSL-trailingstepshort
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
endif
Then without changing anything else, I exchanged ‘close’ for ‘typicalprice’. This also works fine, but then I thought, why have the TS triggered by the performance of just one candle? why not use an MA value?
It is precisely the use of ‘close’ that I am trying to get away from.
I don’t understand why ‘close’ shouldn’t simply be replaced by ‘HULLa’ as in my first post.
Tradeprice(1) needs to be replaced by HULLa.
If profitable CLOSE needs to be above the MA for Long trades and below for Short trades.
Ok, I’ll try that, thanks!