is 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 :
defparam preloadbars = 150
defparam cumulateorders = false
mm = average[100](close)
ATRsl = mm + highest[50](averagetruerange[100](close)*2)
condition = close crosses under mm
if condition and (not shortonmarket) then
sellshort 1 lot at market
//first stoploss:
exitshort at ATRsl stop
currentdynamicstopprice = ATRsl
endif
//dynamicstopprice
if shortonmarket then
if (ATRsl < currentdynamicstopprice) then
currentdynamicstopprice = ATRsl
endif
exitshort at currentdynamicstopprice stop
endif
GRAPH currentdynamicstopprice as "dynamic stop loss"
The long version then also needs a revision :
defparam preloadbars = 150
defparam cumulateorders = false
mm = average[100](close)
ATRsl = mm – highest[50](averagetruerange[100](close)*2)
condition = close crosses over mm
if condition and (not longonmarket) then
buy 1 lot at market
//first stoploss:
sell at ATRsl stop
currentdynamicstopprice = ATRsl
endif
//dynamicstopprice
if longonmarket then
if (ATRsl > currentdynamicstopprice) then
currentdynamicstopprice = ATRsl
endif
sell at currentdynamicstopprice stop
endif
GRAPH currentdynamicstopprice as "dynamic stop loss"
Phew ! Quite a lot of hard thinking. Why complex, when it can be done easily ?
Everybody agree ?