Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study(title="Head & Shoulders Pattern",shorttitle="Head & Shoulders",overlay=true)
- len=20
- phi=pivothigh(high,len,len)
- plo=pivotlow(low,len,len)
- hilo = 0
- hilo := phi ? 1 : plo ? -1 : hilo[1]
- tgl = (phi and hilo[1] == -1) or (plo and hilo[1] == 1)
- hi = close
- hi := tgl ? high[len] : high[len] > hi[1] ? high[len] : hi[1]
- lo = close
- lo := tgl ? low[len] : low[len] < lo[1] ? low[len] : lo[1]
- p1 = close
- p2 = close
- p3 = close
- p4 = close
- p5 = close
- p6 = close
- p1 := phi ? hi: plo ? lo : p1[1]
- p2 := tgl ? p1[1] : p2[1]
- p3 := tgl ? p2[1] : p3[1]
- p4 := tgl ? p3[1] : p4[1]
- p5 := tgl ? p4[1] : p5[1]
- p6 := tgl ? p5[1] : p6[1]
- c6 = p1 > p6 and p2 > p6 and p3 > p6 and p4 > p6 and p5 > p6
- c5 = p5 > p2 and p3 > p5 and p5 > p4
- c4 = p3 > p4 and p1 > p4
- c3 = p3 > p2 and p3 > p1
- c2 = p1 > p2
- c1 = (p3 - p2) > (p1 - p2)*1.3 and (p3 - p2) > (p5 - p4)*1.3
- pre_has = c1 and c2 and c3 and c4 and c5 and c6 and tgl
- trg_p2 = close
- trg_p2 := pre_has ? p2 : trg_p2[1]
- trg_p3 = close
- trg_p3 := pre_has ? p3 : trg_p3[1]
- state = 0
- if state[1] == 0
- state := pre_has ? 1 : 0
- else
- if state[1] == 1
- ok = trg_p2 > close
- ng = high > trg_p3 or tgl
- state := ng ? 2 : ok ? 0 : 1
- else
- if state[1] == 2
- state := 0
- cb6 = p1 < p6 and p2 < p6 and p3 < p6 and p4 < p6 and p5 < p6
- cb5 = p5 < p2 and p3 < p5 and p5 < p4
- cb4 = p3 < p4 and p1 < p2
- cb3 = p3 < p2 and p3 < p1
- cb2 = p1 < p2
- cb1 = (p3 - p2) < (p1 - p2)*1.3 and (p3 - p2) < (p5 - p4)*1.3 and tgl
- pre_hasb = cb1 and cb2 and cb3 and cb4 and cb5 and cb6
- trg_pb2 = close
- trg_pb2 := pre_hasb ? p2 : trg_pb2[1]
- trg_pb3 = close
- trg_pb3 := pre_hasb ? p3 : trg_pb3[1]
- stateb = 0
- if stateb[1] == 0
- stateb := pre_hasb ? 1 : 0
- else
- if stateb[1] == 1
- okb = trg_pb2 < close
- ngb = low < trg_pb3 or tgl
- stateb := ngb ? 2 : okb ? 0 : 1
- else
- if stateb[1] == 2
- stateb := 0
- has = state == 0 and state[1] == 1
- hasb = stateb == 0 and stateb[1] == 1
- ///////////
- plotshape(hasb, text='Large Head & \n Shoulders', style=shape.labelup,title="Bullish Head & Shoulders", location=location.belowbar, color=green, textcolor=white, offset=0)
- plotshape(has, text='Large Head & \n Shoulders', style=shape.labeldown,title="Bearish Head & Shoulders", location=location.abovebar, color=red, textcolor=white, offset=0)
- //////////
- len2=4
- phi2=pivothigh(high,len2,len2)
- plo2=pivotlow(low,len2,len2)
- hilo2 = 0
- hilo2 := phi2 ? 1 : plo2 ? -1 : hilo2[1]
- tgl2 = (phi2 and hilo2[1] == -1) or (plo2 and hilo2[1] == 1)
- hi2 = close
- hi2 := tgl2 ? high[len2] : high[len2] > hi2[1] ? high[len2] : hi2[1]
- lo2 = close
- lo2 := tgl2 ? low[len2] : low[len2] < lo2[1] ? low[len2] : lo2[1]
- p12 = close
- p22 = close
- p32 = close
- p42 = close
- p52 = close
- p62 = close
- p12 := phi2 ? hi2: plo2 ? lo2 : p12[1]
- p22 := tgl2 ? p12[1] : p22[1]
- p32 := tgl2 ? p22[1] : p32[1]
- p42 := tgl2 ? p32[1] : p42[1]
- p52 := tgl2 ? p42[1] : p52[1]
- p62 := tgl2 ? p52[1] : p62[1]
- c62 = p12 > p62 and p22 > p62 and p32 > p62 and p42 > p62 and p52 > p62
- c52 = p52 > p22 and p32 > p52 and p52 > p42
- c42 = p32 > p42 and p12 > p42
- c32 = p32 > p22 and p32 > p12
- c22 = p12 > p22
- c12 = (p32 - p22) > (p12 - p22)*1.3 and (p32 - p22) > (p52 - p42)*1.3
- pre_has2 = c12 and c22 and c32 and c42 and c52 and c62 and tgl2
- trg_p22 = close
- trg_p22 := pre_has2 ? p22 : trg_p22[1]
- trg_p32 = close
- trg_p32 := pre_has2 ? p32 : trg_p32[1]
- state2 = 0
- if state2[1] == 0
- state2 := pre_has2 ? 1 : 0
- else
- if state2[1] == 1
- ok2 = trg_p22 > close
- ng2 = high > trg_p32 or tgl2
- state2 := ng2 ? 2 : ok2 ? 0 : 1
- else
- if state2[1] == 2
- state2 := 0
- cb62 = p12 < p62 and p22 < p62 and p32 < p62 and p42 < p62 and p52 < p62
- cb52 = p52 < p22 and p32 < p52 and p52 < p42
- cb42 = p32 < p42 and p12 < p22
- cb32 = p32 < p22 and p32 < p12
- cb22 = p12 < p22
- cb12 = (p32 - p22) < (p12 - p22)*1.3 and (p32 - p22) < (p52 - p42)*1.3 and tgl2
- pre_hasb2 = cb12 and cb22 and cb32 and cb42 and cb52 and cb62
- trg_pb22 = close
- trg_pb22 := pre_hasb2 ? p22 : trg_pb22[1]
- trg_pb32 = close
- trg_pb32 := pre_hasb2 ? p32 : trg_pb32[1]
- stateb2 = 0
- if stateb2[1] == 0
- stateb2 := pre_hasb2 ? 1 : 0
- else
- if stateb2[1] == 1
- okb2 = trg_pb22 < close
- ngb2 = low < trg_pb32 or tgl2
- stateb2 := ngb2 ? 2 : okb2 ? 0 : 1
- else
- if stateb2[1] == 2
- stateb2 := 0
- has2 = state2 == 0 and state2[1] == 1
- hasb2 = stateb2 == 0 and stateb2[1] == 1
- ///////////
- plotshape(hasb2, text='Head & \n Shoulders', style=shape.labelup,title="Bullish Head & Shoulders", location=location.belowbar, color=green, textcolor=white, offset=0)
- plotshape(has2, text='Head & \n Shoulders', style=shape.labeldown,title="Bearish Head & Shoulders", location=location.abovebar, color=red, textcolor=white, offset=0)
- ////
- alertcondition(hasb or hasb2, title='Bullish Head & Shoulders Pattern', message='Bullish Head & Shoulders Pattern Formed')
- alertcondition(has or has2, title='Bearish Head & Shoulders Pattern', message='Bearish Head & Shoulders Pattern')
Add Comment
Please, Sign In to add comment