MazParticipant
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
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.
MazParticipant
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)
ALEModerator
Master
Hello Maz
could be possible to change the code in a breakout strategy on the neck level?
Hello Maz,
is it possible to transform the system to a simple screener?
Regards,
Nicolas
HuwParticipant
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?
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.
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.
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!? 🙂
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