Dynamic Stop Loss – ATR Based
Forums › ProRealTime English forum › ProOrder support › Dynamic Stop Loss – ATR Based
- This topic has 7 replies, 4 voices, and was last updated 6 years ago by GraHal.
-
-
03/21/2018 at 10:50 PM #66027
Can anybody see how to get this code to work for an ExistShort please? I’ve tried for an hour / several changes, but I can’t get it to work.
I need the full code for an Exit Long and an ExitShort please.
The original code came from a Nicolas post here
https://www.prorealcode.com/blog/learning/moving-stoploss-dynamic-informations-proorder/
Many Thanks
GraHal12345678910111213141516171819202122232425defparam preloadbars = 150defparam cumulateorders = falsemm = average[100](close)ATRsl = mm - highest[50](averagetruerange[100](close)*2)condition = close crosses over mmif condition thenbuy 1 lot at market//first stoploss:SL = close-ATRslset stop ploss SLdynamicSL = SLendif//dynamicSLif longonmarket thenif(ATRsl>dynamicSL) thendynamicSL=ATRslendifsell at dynamicSL stopendifGRAPH dynamicSL as "dyn"03/22/2018 at 1:45 AM #66035line 13 should read :
1sell at SL stopbecause SL is a large number, in the vicinity of close.
In total, for shorts, maybe like this ?
12345678910111213141516171819202122232425defparam preloadbars = 150defparam cumulateorders = falsemm = average[100](close)ATRsl = mm + highest[50](averagetruerange[100](close)*2)condition = close crosses under mmif condition thensellshort 1 lot at market//first stoploss:SL = close+ATRslexitshort at SL stopdynamicSL = SLendif//dynamicSLif shortonmarket thenif(ATRsl<dynamicSL) thendynamicSL=ATRslendifexitshort at dynamicSL stopendifGRAPH dynamicSL as "dyn"03/22/2018 at 9:18 AM #6604703/22/2018 at 9:53 AM #6605903/22/2018 at 1:17 PM #660841set stop ploss SLis ok as long as the instrument has a pointsize = 1 , because SL variable is calculated in price format, the best way would be to use ‘set stop loss’ to be completely compatible with any security.
Yes, that’s right. Maybe I shouldn’t code at 1:45 in the night in order to get sleepy…
I did not realize that ATRsl is an absolute price quantity in the region of the current price. Not an ATR quantity (a small stop distance).
Now, let’s recall what is intended with this code :
The absolute stop price of a position shall be ATRsl, right from the beginning. When this moves closer to positionprice, it shall be adapted in the way of a trailing stop.
So, why don’t we use ATRsl as an absolute stop (exit) price all of the time ? We then do not need the quantities dynamicSL and SL anymore (which have caused the whole confusion), we just look in every bar whether ATRsl has moved closer to positionprice or not.
We have to add “and (not shortonmarket)” in line 9, because do not want these commands to be executed onve again when a position is already open, which could reset the current dynamic stop to a higher value (we only want lower values for shorts).
Therefore, revised version for shorts :
12345678910111213141516171819202122232425defparam preloadbars = 150defparam cumulateorders = falsemm = average[100](close)ATRsl = mm + highest[50](averagetruerange[100](close)*2)condition = close crosses under mmif condition and (not shortonmarket) thensellshort 1 lot at market//first stoploss:exitshort at ATRsl stopcurrentdynamicstopprice = ATRslendif//dynamicstoppriceif shortonmarket thenif (ATRsl < currentdynamicstopprice) thencurrentdynamicstopprice = ATRslendifexitshort at currentdynamicstopprice stopendifGRAPH currentdynamicstopprice as "dynamic stop loss"The long version then also needs a revision :
123456789101112131415161718192021222324defparam preloadbars = 150defparam cumulateorders = falsemm = average[100](close)ATRsl = mm – highest[50](averagetruerange[100](close)*2)condition = close crosses over mmif condition and (not longonmarket) thenbuy 1 lot at market//first stoploss:sell at ATRsl stopcurrentdynamicstopprice = ATRslendif//dynamicstoppriceif longonmarket thenif (ATRsl > currentdynamicstopprice) thencurrentdynamicstopprice = ATRslendifsell at currentdynamicstopprice stopendifGRAPH currentdynamicstopprice as "dynamic stop loss"Phew ! Quite a lot of hard thinking. Why complex, when it can be done easily ?
Everybody agree ?
03/22/2018 at 2:14 PM #6609003/22/2018 at 3:32 PM #66093The result is about the same as with Nicolas’ original code, although line 19 is not entirely correct there. In the first bar after a position has been opened,
1if(ATRsl>dynamicSL) thenworks nevertheless most of time, because the first dynamicSL (only in this bar being a small stop distance) is always very small compared to ATRsl (which is always a large absolute price).
This is also the reason why the “GRAPH dynamicSL ” command in the last line of the original code gives a little strange looking results. There are spikes down to almost 0 each time when a new position is opened or when “condition” is true once again during the course of an open position, because then “dynamicSL” switches from an absolute price to a small stop distance and back.
In the new code, there are only “sell at xx stop” and “exitshort at xx stop” commands, which may improve comprehensiblity a little. And the graph commands show nicely the development of the trailing stops. Maybe Nicolas can amend this in his code.
1 user thanked author for this post.
03/22/2018 at 3:39 PM #66096Thank you @Verdi55, seems I wasn’t doing much wrong last night after all, it just wasn’t making much difference to my results so I assumed my code for a Short wasn’t correct. 🙂
Last night I was trying to fit the Dynamic SL into an RSI[2] Strat I am working on.
Yes I was scratching my head also to understand how it worked, your code seems much simpler.
This morning, using the complex version (before I saw the easier version) I got a Long and Short working using the simple MA condition. I may as well attach results and an .itf file in case anybody wants to take it further?
For the hell of it, I will now fit Verdi55 simpler version into the same simple condition Strat and see what the difference is if any! 🙂
Cheers
GraHalPS It’s only good over 10k bars, but I am starting to think the market changed so much from early Feb is there any point going back further and thus ending up with a strategy that is not reactive enough for today’s very short up & down zig & zags !? 🙂
-
AuthorPosts
Find exclusive trading pro-tools on