Using MA to trigger a trailing stop
Forums › ProRealTime English forum › ProOrder support › Using MA to trigger a trailing stop
- This topic has 9 replies, 2 voices, and was last updated 5 years ago by
nonetheless.
-
-
11/10/2020 at 3:51 PM #150043
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)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849once trailingstop = 1if trailingstop thentrailingpercentlong = tsl // %trailingpercentshort = tss // %stepPercentlong = stlstepPercentshort = stsif onmarket thentrailingstartlong = tradeprice(1)*(trailingpercentlong/100) //trailing will start @trailingstart points profittrailingstartshort = tradeprice(1)*(trailingpercentshort/100) //trailing will start @trailingstart points profittrailingsteplong = tradeprice(1)*(stepPercentlong/100) //% step to move the stoplosstrailingstepshort = tradeprice(1)*(stepPercentshort/100) //% step to move the stoplossendif//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartlong THENnewSL = tradeprice(1)+trailingsteplongENDIF//next movesIF newSL>0 AND HULLa-newSL>trailingsteplong THENnewSL = newSL+trailingsteplongENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartshort THENnewSL = tradeprice(1)-trailingstepshortENDIF//next movesIF newSL>0 AND newSL-HULLa>trailingstepshort THENnewSL = newSL-trailingstepshortENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFendif2 users thanked author for this post.
11/10/2020 at 5:05 PM #1500451tradeprice(1)-HULLaBecause at line 23 and the others you make a difference with inverted operators, write it as above.
11/10/2020 at 5:20 PM #150046Do you mean that line 23 should be
1IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THENisn’t that effectively the same as line 35 for short positions?
1 user thanked author for this post.
11/10/2020 at 6:23 PM #150050Yes, in line 23 and the other ones those two operators need to be swapped.
11/10/2020 at 6:30 PM #150051Like so?
1234567891011121314151617181920212223//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THENnewSL = tradeprice(1)+trailingsteplongENDIF//next movesIF newSL>0 AND newSL-HULLa>trailingsteplong THENnewSL = newSL+trailingsteplongENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartshort THENnewSL = tradeprice(1)-trailingstepshortENDIF//next movesIF newSL>0 AND HULLa-newSL>trailingstepshort THENnewSL = newSL-trailingstepshortENDIFENDIFor
1234567891011121314151617181920212223//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-HULLa>=trailingstartlong THENnewSL = tradeprice(1)+trailingsteplongENDIF//next movesIF newSL>0 AND HULLa-newSL>trailingsteplong THENnewSL = newSL+trailingsteplongENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND HULLa-tradeprice(1)>=trailingstartshort THENnewSL = tradeprice(1)-trailingstepshortENDIF//next movesIF newSL>0 AND newSL-HULLa>trailingstepshort THENnewSL = newSL-trailingstepshortENDIFENDIF11/10/2020 at 6:54 PM #150055Yes, but I did not spot it before, you have to also replace TRADEPRICE(1) with CLOSE.
11/10/2020 at 7:06 PM #150056This is the original Nicolas’code and the modified version for you:
12Close-tradeprice(1) //originalClose-Hulla //modified for Hullthe opposite for Short trades.
1 user thanked author for this post.
11/10/2020 at 7:14 PM #150057Roberto, the original TS that I had been using was like this:
123456789101112131415161718192021222324252627282930//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstartlong THENnewSL = tradeprice(1)+trailingsteplongENDIF//next movesIF newSL>0 AND close-newSL>trailingsteplong THENnewSL = newSL+trailingsteplongENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstartshort THENnewSL = tradeprice(1)-trailingstepshortENDIF//next movesIF newSL>0 AND newSL-close>trailingstepshort THENnewSL = newSL-trailingstepshortENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIFendifThen 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.
11/10/2020 at 11:13 PM #150078Tradeprice(1) needs to be replaced by HULLa.
If profitable CLOSE needs to be above the MA for Long trades and below for Short trades.
1 user thanked author for this post.
11/10/2020 at 11:19 PM #150079Ok, I’ll try that, thanks!
-
AuthorPosts
Find exclusive trading pro-tools on