AnthonyAlexander

Head & Shoulders Pattern Completed

Jan 1st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. //@version=3
  2. study(title="Head & Shoulders Pattern",shorttitle="Head & Shoulders",overlay=true)
  3. len=20
  4.  
  5. phi=pivothigh(high,len,len)
  6. plo=pivotlow(low,len,len)
  7. hilo = 0
  8. hilo := phi ? 1 : plo ? -1 : hilo[1]
  9. tgl = (phi and hilo[1] == -1) or (plo and hilo[1] == 1)
  10. hi = close
  11. hi := tgl ? high[len] : high[len] > hi[1] ? high[len] : hi[1]
  12. lo = close
  13. lo := tgl ? low[len] : low[len] < lo[1] ? low[len] : lo[1]
  14. p1 = close
  15. p2 = close
  16. p3 = close
  17. p4 = close
  18. p5 = close
  19. p6 = close
  20. p1 := phi ? hi: plo ? lo : p1[1]
  21. p2 := tgl ? p1[1] : p2[1]
  22. p3 := tgl ? p2[1] : p3[1]
  23. p4 := tgl ? p3[1] : p4[1]
  24. p5 := tgl ? p4[1] : p5[1]
  25. p6 := tgl ? p5[1] : p6[1]
  26.  
  27. c6 = p1 > p6 and p2 > p6 and p3 > p6 and p4 > p6 and p5 > p6
  28. c5 = p5 > p2 and p3 > p5 and p5 > p4
  29. c4 = p3 > p4 and p1 > p4
  30. c3 = p3 > p2 and p3 > p1
  31. c2 = p1 > p2
  32. c1 = (p3 - p2) > (p1 - p2)*1.3 and (p3 - p2) > (p5 - p4)*1.3
  33.  
  34. pre_has = c1 and c2 and c3 and c4 and c5 and c6 and tgl
  35. trg_p2 = close
  36. trg_p2 := pre_has ? p2 : trg_p2[1]
  37. trg_p3 = close
  38. trg_p3 := pre_has ? p3 : trg_p3[1]
  39. state = 0
  40. if state[1] == 0
  41. state := pre_has ? 1 : 0
  42. else
  43. if state[1] == 1
  44. ok = trg_p2 > close
  45. ng = high > trg_p3 or tgl
  46. state := ng ? 2 : ok ? 0 : 1
  47. else
  48. if state[1] == 2
  49. state := 0
  50.  
  51. cb6 = p1 < p6 and p2 < p6 and p3 < p6 and p4 < p6 and p5 < p6
  52. cb5 = p5 < p2 and p3 < p5 and p5 < p4
  53. cb4 = p3 < p4 and p1 < p2
  54. cb3 = p3 < p2 and p3 < p1
  55. cb2 = p1 < p2
  56. cb1 = (p3 - p2) < (p1 - p2)*1.3 and (p3 - p2) < (p5 - p4)*1.3 and tgl
  57.  
  58. pre_hasb = cb1 and cb2 and cb3 and cb4 and cb5 and cb6
  59. trg_pb2 = close
  60. trg_pb2 := pre_hasb ? p2 : trg_pb2[1]
  61. trg_pb3 = close
  62. trg_pb3 := pre_hasb ? p3 : trg_pb3[1]
  63. stateb = 0
  64. if stateb[1] == 0
  65. stateb := pre_hasb ? 1 : 0
  66. else
  67. if stateb[1] == 1
  68. okb = trg_pb2 < close
  69. ngb = low < trg_pb3 or tgl
  70. stateb := ngb ? 2 : okb ? 0 : 1
  71. else
  72. if stateb[1] == 2
  73. stateb := 0
  74.  
  75.  
  76. has = state == 0 and state[1] == 1
  77. hasb = stateb == 0 and stateb[1] == 1
  78.  
  79. ///////////
  80.  
  81. plotshape(hasb, text='Large Head & \n Shoulders', style=shape.labelup,title="Bullish Head & Shoulders", location=location.belowbar, color=green, textcolor=white, offset=0)
  82. plotshape(has, text='Large Head & \n Shoulders', style=shape.labeldown,title="Bearish Head & Shoulders", location=location.abovebar, color=red, textcolor=white, offset=0)
  83.  
  84. //////////
  85.  
  86. len2=4
  87.  
  88. phi2=pivothigh(high,len2,len2)
  89. plo2=pivotlow(low,len2,len2)
  90. hilo2 = 0
  91. hilo2 := phi2 ? 1 : plo2 ? -1 : hilo2[1]
  92. tgl2 = (phi2 and hilo2[1] == -1) or (plo2 and hilo2[1] == 1)
  93. hi2 = close
  94. hi2 := tgl2 ? high[len2] : high[len2] > hi2[1] ? high[len2] : hi2[1]
  95. lo2 = close
  96. lo2 := tgl2 ? low[len2] : low[len2] < lo2[1] ? low[len2] : lo2[1]
  97. p12 = close
  98. p22 = close
  99. p32 = close
  100. p42 = close
  101. p52 = close
  102. p62 = close
  103. p12 := phi2 ? hi2: plo2 ? lo2 : p12[1]
  104. p22 := tgl2 ? p12[1] : p22[1]
  105. p32 := tgl2 ? p22[1] : p32[1]
  106. p42 := tgl2 ? p32[1] : p42[1]
  107. p52 := tgl2 ? p42[1] : p52[1]
  108. p62 := tgl2 ? p52[1] : p62[1]
  109.  
  110. c62 = p12 > p62 and p22 > p62 and p32 > p62 and p42 > p62 and p52 > p62
  111. c52 = p52 > p22 and p32 > p52 and p52 > p42
  112. c42 = p32 > p42 and p12 > p42
  113. c32 = p32 > p22 and p32 > p12
  114. c22 = p12 > p22
  115. c12 = (p32 - p22) > (p12 - p22)*1.3 and (p32 - p22) > (p52 - p42)*1.3
  116.  
  117. pre_has2 = c12 and c22 and c32 and c42 and c52 and c62 and tgl2
  118. trg_p22 = close
  119. trg_p22 := pre_has2 ? p22 : trg_p22[1]
  120. trg_p32 = close
  121. trg_p32 := pre_has2 ? p32 : trg_p32[1]
  122. state2 = 0
  123. if state2[1] == 0
  124. state2 := pre_has2 ? 1 : 0
  125. else
  126. if state2[1] == 1
  127. ok2 = trg_p22 > close
  128. ng2 = high > trg_p32 or tgl2
  129. state2 := ng2 ? 2 : ok2 ? 0 : 1
  130. else
  131. if state2[1] == 2
  132. state2 := 0
  133.  
  134. cb62 = p12 < p62 and p22 < p62 and p32 < p62 and p42 < p62 and p52 < p62
  135. cb52 = p52 < p22 and p32 < p52 and p52 < p42
  136. cb42 = p32 < p42 and p12 < p22
  137. cb32 = p32 < p22 and p32 < p12
  138. cb22 = p12 < p22
  139. cb12 = (p32 - p22) < (p12 - p22)*1.3 and (p32 - p22) < (p52 - p42)*1.3 and tgl2
  140.  
  141. pre_hasb2 = cb12 and cb22 and cb32 and cb42 and cb52 and cb62
  142. trg_pb22 = close
  143. trg_pb22 := pre_hasb2 ? p22 : trg_pb22[1]
  144. trg_pb32 = close
  145. trg_pb32 := pre_hasb2 ? p32 : trg_pb32[1]
  146. stateb2 = 0
  147. if stateb2[1] == 0
  148. stateb2 := pre_hasb2 ? 1 : 0
  149. else
  150. if stateb2[1] == 1
  151. okb2 = trg_pb22 < close
  152. ngb2 = low < trg_pb32 or tgl2
  153. stateb2 := ngb2 ? 2 : okb2 ? 0 : 1
  154. else
  155. if stateb2[1] == 2
  156. stateb2 := 0
  157.  
  158.  
  159. has2 = state2 == 0 and state2[1] == 1
  160. hasb2 = stateb2 == 0 and stateb2[1] == 1
  161.  
  162. ///////////
  163.  
  164. plotshape(hasb2, text='Head & \n Shoulders', style=shape.labelup,title="Bullish Head & Shoulders", location=location.belowbar, color=green, textcolor=white, offset=0)
  165. plotshape(has2, text='Head & \n Shoulders', style=shape.labeldown,title="Bearish Head & Shoulders", location=location.abovebar, color=red, textcolor=white, offset=0)
  166.  
  167.  
  168.  
  169.  
  170.  
  171. ////
  172. alertcondition(hasb or hasb2, title='Bullish Head & Shoulders Pattern', message='Bullish Head & Shoulders Pattern Formed')
  173. alertcondition(has or has2, title='Bearish Head & Shoulders Pattern', message='Bearish Head & Shoulders Pattern')
Add Comment
Please, Sign In to add comment