Head and Shoulders equity curve stop loss

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #33532 quote
    Maz
    Participant
    Veteran

    Hi,

    I put together a quick and dirty H&S pattern detection for PRT and thought to share it here, in case it’s of use to anyone, or in case someone would like to improve or contribute. I am sure you are probably aware of the head and shoulders pattern and its uses – but if not then take a look here.

    Below is the raw beginnings of a module that can be plugged into a trading system. It looks roughly for head and shoulders patterns with the equity curve when your system is onMarket. If a head and shoulders pattern is found within your equity curve, a trigger will happen (“hsDone”) and you can then perform an action (such as exit the open trade to lock in profits, or any other action).

    The code should be pretty self explanatory but it’s by no means a finished product. I don’t have time to go though all the logic cases, filters and variables at the moment but thought to share in case someone would like to pick it up and contribute. Open to all ideas of improving the H&S detection algo. Will pick up on it myself when I have more time.

     

    // -- equity curve head and shoulders detection ----
    once useHSPatterStop = 1
    
    if useHSPatterStop then
    // Variabls ------------------------
    D                     = positionPerf // data = ie usually current trade performance
    hsMinWidth            = 4            // minimum shoulder-head width in bars
    rightShoulderMinDepth = 0.5          // minimum % of left shoulder that right shoulder must retrace to for a valid setup
    //-----------------------------------
    maxD   = max(maxD, D)
    findL  = (not hsLeft) and (not hsHead) and (d >= maxD) and (d > hsLeft)
    restL  = d > hsLeft and (barIndex < hsLeftIndex+round(hsMinWidth/2) or d > hsLeft*2)
    findH  = hsLeft and (not hsRight) and d > hsLeft //and d > hsHead
    findH  = findH and d > hsHead and barIndex > hsLeftIndex+hsMinWidth
    findR  = (not hsRight) and hsHead and hsLeft and (d >= hsLeft)
    findR  = findR and ( barIndex-hsHeadIndex >= hsMinWidth )
    findR  = findR and lowest[barIndex-hsHeadIndex](d) <= hsLeft-(hsLeft-lowest[barIndex-hsLeftIndex](d))*rightShoulderMinDepth
    hsDone = hsRight and d <= hsLeft - lowest[barIndex-hsLeftIndex](d)
    graph hsDone
    
    if findL or restL then             // We found possible left shoulder
    hsLeft = d
    hsLeftIndex = barIndex
    hsHead = 0
    hsRight = 0
    elsif findH and not findR then     // We found possible head
    hsHead = d
    hsHeadIndex = barIndex
    elsif findR then                   // We found possible right shoulder 
    hsRight = d
    elsif hsDone then                  // Pattern completed below left shoulder low
    
    // INSERT YOUR H&S CONDITION HANDLER HERE
    canExitMyTrade = 1 // for example
    
    endif
    if hsDone or (not onMarket) then   // reset to clear HS pattern
    hsLeft = 0
    hsHead = 0
    hsRight = 0
    endif
    endif

    All the best,

    M

    Wing, manel and GraHal thanked this post
    head-n-shoulders-1.png head-n-shoulders-1.png
    #33537 quote
    ALE
    Moderator
    Master

    Very interesting!

    Maz thanked this post
    #33548 quote
    Nicolas
    Keymaster
    Master

    Why are you using Positionperf instead of High or Close of candlesticks? Since Positionperf is updated only once per bar, I presume the pattern found result should be the same.

    But it’s a great idea and good share, thanks a lot Maz.

    Maz thanked this post
    #33571 quote
    Maz
    Participant
    Veteran

    Yep, as an example. But you could always do something like:

    if longOnMarket then
      D = high 
    elsif shortOnMarket then
      D = low
    endif

    (or simply close)

    ALE thanked this post
    #33698 quote
    Despair
    Blocked
    Master

    Intresting!

    Maz thanked this post
    #41288 quote
    ALE
    Moderator
    Master

    Hello Maz
    could be possible to change the code in a breakout strategy on the neck level?

    #49051 quote
    Salocin
    Participant
    Average

    Hello Maz,

     

    is it possible to transform the system to a simple screener?

     

    Regards,

    Nicolas

    #49065 quote
    Huw
    Participant
    Average

    I’m thinking it could be an idea to adapt this for a reverse head and shoulders as well as double tops and double bottoms for a breakout strategy?

    #49230 quote
    jebus89
    Participant
    Master

    This is very cool, thanks Maz. Great work. Ill definitly try to adapt this into some of my strats and see if it would make a difference for my equity 🙂 Right off the bat this was fun to see! Attached screenshot of the clear difference, although in this example, holding it would actually give me a few pips extra haha.

    HS.png HS.png
    #112925 quote
    Crillezz
    Participant
    Veteran

    Hi Maz.

    Is it possible to make a head and sholders screener?

    Would like it to find the pattern before it is completed.

    When the price is at the same level as the top or bottom of the left shoulder.

    dsasfsfsfdsfsfs.png dsasfsfsfdsfsfs.png
    #112927 quote
    GraHal
    Participant
    Master

    Maz hasn’t been seen around these parts (to my knowledge?) for quite some time.

    Also you can’t strictly see a head and shoulders pattern until it is complete … but I know what you mean … get in early like!? 🙂

    #112966 quote
    Crillezz
    Participant
    Veteran

    Yes I am aware that it is not possible to see head and shplders until it is shaped.

    I am posting a new image of a Quasimodo pattern. The difference is that it does a lower low before the right shoulder.

    It would not be impossible to do a screener of such in an early stage.

    So a screener when price forms a higher high => higher kow => higher high => lower low.

    Before the right shoulder is formed.

    The idea is to go short on the top of the left shoulder when the right shoulder is formed.

    Hope you understand what I mean

    dsfdsfsdsdfsfdfsd.png dsfdsfsdsdfsfdfsd.png
Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Head and Shoulders equity curve stop loss


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Maz @eqmaz Participant
Summary

This topic contains 11 replies,
has 9 voices, and was last updated by Crillezz
6 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/26/2017
Status: Active
Attachments: 4 files
Logo Logo
Loading...