Advertisement
xmd79

Pivot Low & High reversals + Alerts

Sep 20th, 2023
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Klytonug
  3.  
  4. //@version=5
  5. indicator("Pivot Low & High reversals + Alerts", overlay=true, max_labels_count=500, max_lines_count=500, max_bars_back=500)
  6. pivot_length = input(70,'Pivot Length')
  7.  
  8. Apply_Regz = input.bool(true,'Regular Pivots',inline='inline1')
  9. Apply_Missed = input.bool(true,'Missed Pivots',inline='inline2')
  10. PHColor = input.color(#ef5350,'High',inline='inline1')
  11. PLColor = input.color(#26a69a,'Low',inline='inline1')
  12.  
  13. //---------------------------------------------------------------
  14. // Setup alert condition
  15. //---------------------------------------------------------------
  16. allow_alerts = input.bool(true, "Allow alerts")
  17.  
  18. var alert_triggered_reg_high = false
  19. var alert_triggered_reg_low = false
  20. var alert_triggered_missed_low = false
  21. var alert_triggered_missed_high = false
  22. var current_signal = "none"
  23.  
  24.  
  25.  
  26. text_color = input.color(color.white,'Text Label Color')
  27. //------------------------------------------------------------------------------
  28. var line structure_line = na
  29. var line inv_level = na
  30. var max = 0.,var min = 0.
  31. var max_xline1 = 0,var min_xline1 = 0
  32. var use_max = 0.,var use_max_xline1 = 0
  33. var use_min = 0.,var use_min_xline1 = 0
  34. var os = 0,var py1 = 0.,var px1 = 0
  35. //------------------------------------------------------------------------------
  36.  
  37. n = bar_index
  38. pivot_high = ta.pivothigh(pivot_length,pivot_length)
  39. pivot_low = ta.pivotlow(pivot_length,pivot_length)
  40.  
  41. max := math.max(high[pivot_length],max)
  42. min := math.min(low[pivot_length],min)
  43. use_max := math.max(high[pivot_length],use_max)
  44. use_min := math.min(low[pivot_length],use_min)
  45.  
  46. if max > max[1]
  47. max_xline1 := n-pivot_length
  48. use_min := low[pivot_length]
  49. if min < min[1]
  50. min_xline1 := n-pivot_length
  51. use_max := high[pivot_length]
  52.  
  53. if use_min < use_min[1]
  54. use_min_xline1 := n-pivot_length
  55. if use_max > use_max[1]
  56. use_max_xline1 := n-pivot_length
  57.  
  58. //------------------------------------------------------------------------------
  59. line.set_x2(inv_level[1],n)
  60.  
  61. //---------------------------------------------------------------------------
  62. // Calculate Possible Reversal + Sell Highs
  63. //---------------------------------------------------------------------------
  64. if pivot_low
  65. if Apply_Missed
  66. if os[1] == 0
  67. label.new(max_xline1,max,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
  68. tooltip=str.tostring(max,'#.####'))
  69.  
  70. structure_line := line.new(px1,py1,max_xline1,max,color=PLColor,style=line.style_solid)
  71. px1 := max_xline1,py1 := max
  72.  
  73. line.set_x2(inv_level[1],px1)
  74. inv_level := line.new(px1,py1,px1,py1,color=color.new(PHColor,50),width=2)
  75.  
  76. // Set the alert condition for missed pivot
  77. alert_triggered_missed_low := true
  78. if current_signal == "Buy" and allow_alerts
  79. alert("Sell")
  80. current_signal := "Sell"
  81.  
  82. else if pivot_low > min
  83. label.new(use_max_xline1,use_max,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
  84. tooltip=str.tostring(max,'#.####'))
  85. label.new(min_xline1,min,'Sell',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
  86. tooltip=str.tostring(min,'#.####'))
  87.  
  88. structure_line := line.new(px1,py1,min_xline1,min,color=PHColor,style=line.style_solid)
  89. px1 := min_xline1,py1 := min
  90. line.set_x2(inv_level[1],px1)
  91. inv_level := line.new(px1,py1,px1,py1,color=color.new(PLColor,50),width=2)
  92.  
  93. structure_line := line.new(px1,py1,use_max_xline1,use_max,color=PLColor,style=line.style_solid)
  94. px1 := use_max_xline1,py1 := use_max
  95. line.set_x2(inv_level,px1)
  96. inv_level := line.new(px1,py1,px1,py1,color=color.new(PHColor,50),width=2)
  97.  
  98. // Set the alert condition for regular pivot low
  99. alert_triggered_reg_low := true
  100. if current_signal == "Buy" and allow_alerts
  101. alert("Sell")
  102. current_signal := "Sell"
  103.  
  104. if Apply_Regz
  105. label.new(n-pivot_length,pivot_low,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
  106. tooltip=str.tostring(pivot_low,'#.####'))
  107. structure_line := line.new(px1,py1,n-pivot_length,pivot_low,color=PHColor,style=pivot_low > min or os[1] == 0 ? line.style_solid : line.style_solid)
  108.  
  109. // Set the alert condition for regular pivot high
  110. alert_triggered_reg_high := true
  111. if current_signal == "Sell" and allow_alerts
  112. alert("Buy")
  113. current_signal := "Buy"
  114.  
  115. py1 := pivot_low,px1 := n-pivot_length,os := 0,max := pivot_low,min := pivot_low
  116. //------------------------------------------------------------------------------
  117. // Calculate Possible Reversal + Buy lows
  118. //------------------------------------------------------------------------------
  119. if pivot_high
  120. if Apply_Missed
  121. if os[1] == 1
  122. label.new(min_xline1,min,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
  123. tooltip=str.tostring(min,'#.####'))
  124.  
  125. structure_line := line.new(px1,py1,min_xline1,min,color=PHColor,style=line.style_solid)
  126. px1 := min_xline1,py1 := min
  127.  
  128. line.set_x2(inv_level[1],px1)
  129. inv_level := line.new(px1,py1,px1,py1,color=color.new(PLColor,50),width=2)
  130.  
  131. // Set the alert condition for missed pivot
  132. alert_triggered_missed_low := true
  133. if current_signal == "Sell" and allow_alerts
  134. alert("Buy")
  135. current_signal := "Buy"
  136.  
  137. else if pivot_high < max
  138. label.new(max_xline1,max,'Buy',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
  139. tooltip=str.tostring(max,'#.####'))
  140. label.new(use_min_xline1,use_min,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
  141. tooltip=str.tostring(min,'#.####'))
  142.  
  143. structure_line := line.new(px1,py1,max_xline1,max,color=PLColor,style=line.style_solid)
  144. px1 := max_xline1,py1 := max
  145. line.set_x2(inv_level[1],px1)
  146. inv_level := line.new(px1,py1,px1,py1,color=color.new(PHColor,50),width=2)
  147.  
  148. structure_line := line.new(px1,py1,use_min_xline1,use_min,color=PHColor,style=line.style_solid)
  149. px1 := use_min_xline1,py1 := use_min
  150. line.set_x2(inv_level,px1)
  151. inv_level := line.new(px1,py1,px1,py1,color=color.new(PLColor,50),width=2)
  152.  
  153. // Set the alert condition for regular pivot low
  154. alert_triggered_reg_high := true
  155. if current_signal == "Sell" and allow_alerts
  156. alert("Buy")
  157. current_signal := "Buy"
  158.  
  159. if Apply_Regz
  160. label.new(n-pivot_length,pivot_high,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
  161. tooltip=str.tostring(pivot_high,'#.####'))
  162. structure_line := line.new(px1,py1,n-pivot_length,pivot_high,color=PLColor,style=pivot_high < max or os[1] == 1 ? line.style_solid : line.style_solid)
  163.  
  164. // Set the alert condition for regular pivot high
  165. alert_triggered_reg_high := true
  166. if current_signal == "Buy" and allow_alerts
  167. alert("Sell")
  168. current_signal := "Sell"
  169.  
  170. py1 := pivot_high,px1 := n-pivot_length,os := 1,max := pivot_high,min := pivot_high
  171. //------------------------------------------------------------------------------
  172.  
  173. var label lblz = na
  174. if barstate.islast
  175. x = 0,y = 0.
  176.  
  177. price_levels = array.new_float(0)
  178. price_levels_x = array.new_int(0)
  179.  
  180. for i = 0 to n-px1-1
  181. array.push(price_levels,os==1?low[i]:high[i])
  182. array.push(price_levels_x,n-i)
  183.  
  184. label.delete(lblz[1])
  185.  
  186. if os == 1
  187. y := array.min(price_levels)
  188. x := array.get(price_levels_x,array.indexof(price_levels,y))
  189.  
  190. if Apply_Missed
  191. lblz := label.new(x,y,'Buy',textcolor=text_color,color=PLColor,style=label.style_label_up,size=size.normal,
  192. tooltip=str.tostring(y,'#.####'))
  193. else
  194. y := array.max(price_levels)
  195. x := array.get(price_levels_x,array.indexof(price_levels,y))
  196.  
  197. if Apply_Missed
  198. lblz := label.new(x,y,'Sell',textcolor=text_color,color=PHColor,style=label.style_label_down,size=size.normal,
  199. tooltip=str.tostring(y,'#.####'))
  200.  
  201. if Apply_Missed
  202. line.delete(line.new(px1,py1,x,y,color=os == 1 ? PHColor : PLColor,style=line.style_solid)[1])
  203.  
  204. line.delete(line.new(x,y,n,y,color = color.new(os == 1 ? PHColor : PLColor,50),width=2)[1])
  205.  
  206. // Alert conditions
  207. alertcondition(alert_triggered_reg_high, title="Regular Pivot High", message="Regular pivot high alert triggered!")
  208. alertcondition(alert_triggered_reg_low, title="Regular Pivot Low", message="Regular pivot low alert triggered!")
  209. alertcondition(alert_triggered_missed_high, title="Missed High Pivot", message="Missed pivot alert triggered!")
  210. alertcondition(alert_triggered_missed_low, title="Missed Low Pivot", message="Missed pivot alert triggered!")
  211.  
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement