xmd79

Price Change Alerts

Dec 24th, 2022
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 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. // © domsito
  3.  
  4. // To enable alerts do this:
  5. // - Click on alerts clock icon in top right corner of the screen
  6. // - Click on "Create alert button"
  7. // - In Condition dropdown select "alerts"
  8. // - In the next dropdown select "Any alert function() call" and click on "Create" button
  9.  
  10. // Script uses repainting on purpose to display daily percentage changes since the previous day's closing
  11.  
  12. //@version=5
  13. indicator("Price Change Alerts", "alerts", overlay = true)
  14. max_bars_back(time, 5000)
  15.  
  16. var bool showDailyChange = input.bool(true, "Show change since previous day close", group = "General")
  17.  
  18. var bool showPrevDayLine = input.bool(true, "Prevous day's closing line", group = "line", inline = "0")
  19. var lineColor = input(color.blue, "", inline = "0", group = "line")
  20.  
  21. var bool showPrevWeekCloseLine = input.bool(true, "Prevous week's closing line", group = "line", inline = "1")
  22. var lineColor2 = input(color.lime, "", inline = "1", group = "line")
  23.  
  24. var float alertDelta = input.float(5.0, "Alert delta", 0.01, 10000, tooltip = "Alerts will be triggered only after price changes by 'delta' amount from the last alert price")
  25. var int minSeconds = input.int(60, "Minimum number of seconds between alerts", 10, 10000)
  26.  
  27. varip float lastAlertValue = 0.0
  28. varip int lastAlertTime = 0
  29.  
  30. var lbl = label.new(na, na, na, style = label.style_label_left)
  31. var lineDay = line.new(na, na, na, na, color = lineColor, style = line.style_solid, width = 2)
  32. var lineWeek = line.new(na, na, na, na, color = lineColor2, style = line.style_solid, width = 2)
  33.  
  34. varip float change = 0.0
  35. varip float changeSign = na
  36.  
  37. delta(float v1, float v2) =>
  38. (((v2 / v1)-1) * 100.0)
  39.  
  40.  
  41. var float prevDayClose = na
  42. var int bar_time_prev_day_close = 0
  43. var int bar_time_prev_week_close = 0
  44. var float prevWeekClose = 0
  45.  
  46. if (ta.change(dayofmonth))
  47. prevDayClose := close[2] //previous bar close is previous day's closing price, since we just entered after hours
  48. bar_time_prev_day_close := bar_index[2]
  49. if (dayofweek == dayofweek.monday)
  50. bar_time_prev_week_close := bar_index[2]
  51. prevWeekClose := close[2]
  52. if barstate.islast
  53. change := delta(prevDayClose, close)
  54. backColor = change < 0.0 ? color.red : color.green
  55. if showPrevDayLine
  56. line.set_x1(lineDay, bar_time_prev_day_close )
  57. line.set_x2(lineDay, bar_index)
  58. line.set_y1(lineDay, prevDayClose)
  59. line.set_y2(lineDay, prevDayClose)
  60. if showPrevWeekCloseLine
  61. line.set_x1(lineWeek, bar_time_prev_week_close)
  62. line.set_x2(lineWeek, bar_index)
  63. line.set_y1(lineWeek, prevWeekClose)
  64. line.set_y2(lineWeek, prevWeekClose)
  65. if showDailyChange
  66. label.set_text(lbl, str.tostring(change, "0.00") + "% ")
  67. label.set_color(lbl, backColor)
  68. label.set_x(lbl, bar_index+1)
  69. label.set_y(lbl, close)
  70.  
  71. if (barstate.islastconfirmedhistory)
  72. change := delta(prevDayClose, close)
  73. changeSign := math.sign(change)
  74. lastAlertValue := math.round(close / alertDelta) * alertDelta
  75.  
  76. if (math.abs(close-lastAlertValue) >= alertDelta) and ((time - lastAlertTime)/1000.0 >= minSeconds)
  77. alert(syminfo.ticker + ((close-lastAlertValue) > 0 ? " UP" : " DOWN") + " to " + str.tostring(close, "0.00"), alert.freq_all)
  78. //lastAlertValue:= close
  79. lastAlertValue := math.round(close / alertDelta) * alertDelta
  80. lastAlertTime := time
  81.  
  82.  
  83. plot(lastAlertValue, "last alert value", display = display.data_window)
  84. plot((time - lastAlertTime)/1000.0, "Seconds since last alert", display = display.data_window)
  85.  
Advertisement
Add Comment
Please, Sign In to add comment