Dynamic Stop Loss – ATR Based

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #66027 quote
    GraHal
    Participant
    Master

    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
    GraHal

    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 then
     buy 1 lot at market
     //first stoploss:
     SL = close-ATRsl
     set stop ploss SL
     dynamicSL = SL
    endif
     
    //dynamicSL
    if longonmarket then 
     if(ATRsl>dynamicSL) then 
      dynamicSL=ATRsl
     endif
     sell at dynamicSL stop
    endif
     
    GRAPH dynamicSL as "dyn"
    #66035 quote
    verdi55
    Participant
    Veteran

    line 13 should read :

    sell at SL stop

    because SL is a large number, in the vicinity of close.

     

    In total, for shorts, maybe like this ?

    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 then
    sellshort 1 lot at market
    //first stoploss:
    SL = close+ATRsl
    exitshort at SL stop
    dynamicSL = SL
    endif
     
    //dynamicSL
    if shortonmarket then
    if(ATRsl<dynamicSL) then
    dynamicSL=ATRsl
    endif
    exitshort at dynamicSL stop
    endif
     
    GRAPH dynamicSL as "dyn"
    
    #66047 quote
    BC
    Participant
    Master

    Hi Verdi

    I think line 13 should be

    SL = ATRsl-Close
    #66059 quote
    Nicolas
    Keymaster
    Master
    set stop ploss SL

    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.

    #66084 quote
    verdi55
    Participant
    Veteran

    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 ?

    #66090 quote
    BC
    Participant
    Master

    Hi Verdi

    U are right! 👍

    #66093 quote
    verdi55
    Participant
    Veteran

    The 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,

    if(ATRsl>dynamicSL) then

    works 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. 

     

    [attachment file=66094]

    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.

    GraHal thanked this post
    DAX-1-Minute-1.png DAX-1-Minute-1.png
    #66096 quote
    GraHal
    Participant
    Master

    Thank 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
    GraHal

    PS 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 !? 🙂

    Dynamic-SL.jpg Dynamic-SL.jpg MANicsStop-5M-DJI.itf
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Dynamic Stop Loss – ATR Based


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 7 replies,
has 4 voices, and was last updated by GraHal
7 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/21/2018
Status: Active
Attachments: 3 files
Logo Logo
Loading...