Advertisement
Guest User

Daily levels

a guest
Jan 20th, 2022
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.60 KB | None | 0 0
  1. //@version=5
  2. indicator(title='Daily levels', shorttitle='Daily Levels', precision=2, overlay=true)
  3.  
  4. // Create Level Function {
  5. // -----------------------------------------------------------------------------
  6. f_create_level(_type, _start_condition, _active_condition, _global_level_array, _color, _line_width, _line_ext, _line_style, _label_size, _title, _iter, _is_enabled) =>
  7. var float _price = na
  8. var int _start_time = na
  9. var float _hh = na
  10. var float _ll = na
  11. var line _price_line = line.new(x1 = na, y1 = na, x2 = na, y2 = na, xloc = xloc.bar_time, color = _color, width = _line_width, style = _line_style)
  12. var label _price_label = label.new(x = na, y = na, xloc = xloc.bar_time, style = label.style_label_left, color = #00000000, size = _label_size, textcolor = _color)
  13.  
  14. _end_time = int(time + _line_ext * ta.change(time))
  15.  
  16. if _type == "open"
  17. if _start_condition
  18. _price := open
  19. _start_time := time
  20. else if _type == "high"
  21. if _start_condition
  22. _price := high
  23. _start_time := time
  24. else if _active_condition
  25. _price := math.max(_price, high)
  26. else if _type == "low"
  27. if _start_condition
  28. _price := low
  29. _start_time := time
  30. else if _active_condition
  31. _price := math.min(_price, low)
  32. else if _type == "close"
  33. if _start_condition
  34. _price := close
  35. _start_time := time
  36. else if _active_condition
  37. _price := close
  38.  
  39. float _price_val = _iter == 0 ? _price : ta.valuewhen(_start_condition, _price[1], _iter - 1)
  40. int _start_time_val = _iter == 0 ? _start_time : ta.valuewhen(_start_condition, _start_time[1], _iter - 1)
  41.  
  42. _found_existing = array.indexof(_global_level_array, _price_val) > -1
  43.  
  44. if _is_enabled
  45. if _found_existing
  46. line.set_xy1(_price_line, x = na, y = na)
  47. line.set_xy2(_price_line, x = na, y = na)
  48. label.set_xy(_price_label, x = na, y = na)
  49. else
  50. array.push(_global_level_array, _price_val)
  51. line.set_xy1(_price_line, x = _start_time_val, y = _price_val)
  52. line.set_xy2(_price_line, x = _end_time, y = _price_val)
  53. label.set_text(_price_label, text = _title + " : " + str.tostring(_price_val))
  54. label.set_xy(_price_label, x = _end_time, y = _price_val)
  55.  
  56.  
  57. // Global arrays used to detect duplicate levels.
  58. float[] global_high_array = array.new_float()
  59. float[] global_low_array = array.new_float()
  60. float[] global_eq_array = array.new_float()
  61.  
  62. new_H4 = ta.change(time("240")) != 0
  63. new_day = ta.change(time("D")) != 0
  64. new_week = ta.change(time("W")) != 0
  65. new_month = ta.change(time("M")) != 0
  66. new_quarter = ta.change(time("3M")) != 0
  67. new_year = ta.change(time("12M")) != 0
  68. is_monday = dayofweek == dayofweek.monday
  69.  
  70. // Global settings {
  71. // -----------------------------------------------------------------------------
  72.  
  73. inp_open_line_style = input.string("Dotted", options = ["Solid", "Dotted", "Dashed"], title = "Open Line Style", group = "Global Settings")
  74. inp_high_line_style = input.string("Solid", options = ["Solid", "Dotted", "Dashed"], title = "High Line Style", group = "Global Settings")
  75. inp_low_line_style = input.string("Solid", options = ["Solid", "Dotted", "Dashed"], title = "Low Line Style", group = "Global Settings")
  76. inp_eq_line_style = input.string("Dashed", options = ["Solid", "Dotted", "Dashed"], title = "EQ Line Style", group = "Global Settings")
  77.  
  78. inp_text_size = input.string("Small", options = ["Small", "Normal", "Large"], title = "Text Size", group = "Global Settings")
  79. inp_ext = input.int(10, title = "Line Extension", group = "Global Settings")
  80.  
  81. text_size = inp_text_size == "Small" ? size.small : inp_text_size == "Normal" ? size.normal : size.large
  82. open_line_style = inp_open_line_style == "Solid" ? line.style_solid : inp_open_line_style == "Dotted" ? line.style_dotted : line.style_dashed
  83. high_line_style = inp_high_line_style == "Solid" ? line.style_solid : inp_high_line_style == "Dotted" ? line.style_dotted : line.style_dashed
  84. low_line_style = inp_low_line_style == "Solid" ? line.style_solid : inp_low_line_style == "Dotted" ? line.style_dotted : line.style_dashed
  85. eq_line_style = inp_eq_line_style == "Solid" ? line.style_solid : inp_eq_line_style == "Dotted" ? line.style_dotted : line.style_dashed
  86.  
  87.  
  88. // Yearly
  89. // -----------------------------------------------------------------------------
  90. inp_show_yearly_high = input.bool(true, title = "HIGH", group = "Yearly Levels", inline = "1")
  91. inp_show_yearly_low = input.bool(true, title = "LOW", group = "Yearly Levels", inline = "1")
  92. inp_yearly_col = input.color(color.red, title = "color", group = "Yearly Levels", inline = "2")
  93. inp_yearly_line_width = input.int(2, title = "Line width", minval = 1, group = "Yearly Levels", inline = "2")
  94.  
  95. yearly_ok = timeframe.isintraday or timeframe.isdaily or timeframe.isweekly or (timeframe.ismonthly and timeframe.multiplier < 12)
  96.  
  97. f_create_level("high", new_year, not new_year, global_high_array, inp_yearly_col, inp_yearly_line_width, inp_ext, high_line_style, text_size, "Yearly HIGH", 0, inp_show_yearly_high and yearly_ok)
  98. f_create_level("low", new_year, not new_year, global_low_array, inp_yearly_col, inp_yearly_line_width, inp_ext, low_line_style, text_size, "Yearly LOW", 0, inp_show_yearly_low and yearly_ok)
  99.  
  100. // Quarterly
  101. // -----------------------------------------------------------------------------
  102. inp_show_quarterly_high = input.bool(true, title = "HIGH", group = "Quarterly Levels", inline = "1")
  103. inp_show_quarterly_low = input.bool(true, title = "LOW", group = "Quarterly Levels", inline = "1")
  104. inp_quarterly_col = input.color(color.orange, title = "color", group = "Quarterly Levels", inline = "2")
  105. inp_quarterly_line_width = input.int(2, title = "Line width", minval = 1, group = "Quarterly Levels", inline = "2")
  106.  
  107. quarterly_ok = timeframe.isintraday or timeframe.isdaily or timeframe.isweekly
  108.  
  109. f_create_level("high", new_quarter, not new_quarter, global_high_array, inp_quarterly_col, inp_quarterly_line_width, inp_ext, high_line_style, text_size, "Quarterly HIGH", 0, inp_show_quarterly_high and quarterly_ok)
  110. f_create_level("low", new_quarter, not new_quarter, global_low_array, inp_quarterly_col, inp_quarterly_line_width, inp_ext, low_line_style, text_size, "Quarterly LOW", 0, inp_show_quarterly_low and quarterly_ok)
  111.  
  112. // Monthly
  113. // -----------------------------------------------------------------------------
  114. inp_show_monthly_high = input.bool(true, title = "HIGH", group = "Monthly Levels", inline = "1")
  115. inp_show_monthly_low = input.bool(true, title = "LOW", group = "Monthly Levels", inline = "1")
  116. inp_monthly_col = input.color(color.lime, title = "color", group = "Monthly Levels", inline = "2")
  117. inp_monthly_line_width = input.int(2, title = "Line width", minval = 1, group = "Monthly Levels", inline = "2")
  118.  
  119. monthly_ok = timeframe.isintraday or timeframe.isdaily
  120.  
  121. f_create_level("high", new_month, not new_month, global_high_array, inp_monthly_col, inp_monthly_line_width, inp_ext, high_line_style, text_size, "Monthly HIGH", 0, inp_show_monthly_high and monthly_ok)
  122. f_create_level("low", new_month, not new_month, global_low_array, inp_monthly_col, inp_monthly_line_width, inp_ext, low_line_style, text_size, "Monthly LOW", 0, inp_show_monthly_low and monthly_ok)
  123.  
  124. // Weekly
  125. // -----------------------------------------------------------------------------
  126. inp_show_weekly_high = input.bool(true, title = "HIGH", group = "Weekly Levels", inline = "1")
  127. inp_show_weekly_low = input.bool(true, title = "LOW", group = "Weekly Levels", inline = "1")
  128. inp_weekly_col = input.color(color.aqua, title = "color", group = "Weekly Levels", inline = "2")
  129. inp_weekly_line_width = input.int(2, title = "Line width", minval = 1, group = "Weekly Levels", inline = "2")
  130.  
  131. weekly_ok = timeframe.isintraday or timeframe.isdaily
  132.  
  133. f_create_level("high", new_week, not new_week, global_high_array, inp_weekly_col, inp_weekly_line_width, inp_ext, high_line_style, text_size, "Weekly HIGH", 0, inp_show_weekly_high and weekly_ok)
  134. f_create_level("low", new_week, not new_week, global_low_array, inp_weekly_col, inp_weekly_line_width, inp_ext, low_line_style, text_size, "Weekly LOW", 0, inp_show_weekly_low and weekly_ok)
  135.  
  136. // Daily and Prev Daily
  137. // -----------------------------------------------------------------------------
  138. inp_show_daily_open = input.bool(true, title = "OPEN", group = "Daily Levels", inline = "1")
  139. inp_show_daily_high = input.bool(true, title = "HIGH", group = "Daily Levels", inline = "1")
  140. inp_show_daily_low = input.bool(true, title = "LOW", group = "Daily Levels", inline = "1")
  141. inp_daily_col = input.color(color.white, title = "color", group = "Daily Levels", inline = "2")
  142. inp_daily_line_width = input.int(1, title = "Line width", minval = 1, group = "Daily Levels", inline = "2")
  143.  
  144. daily_ok = timeframe.isintraday
  145.  
  146. f_create_level("high", new_day, not new_day, global_high_array, inp_daily_col, inp_daily_line_width, inp_ext, high_line_style, text_size, "Daily HIGH", 0, inp_show_daily_high and daily_ok)
  147. f_create_level("low", new_day, not new_day, global_low_array, inp_daily_col, inp_daily_line_width, inp_ext, low_line_style, text_size, "Daily LOW", 0, inp_show_daily_low and daily_ok)
  148.  
  149. inp_show_prev_daily_open = input.bool(true, title = "OPEN", group = "Prev Daily Levels", inline = "1")
  150. inp_show_prev_daily_high = input.bool(true, title = "HIGH", group = "Prev Daily Levels", inline = "1")
  151. inp_show_prev_daily_low = input.bool(true, title = "LOW", group = "Prev Daily Levels", inline = "1")
  152. inp_show_prev_daily_close = input.bool(true, title = "CLOSE", group = "Prev Daily Levels", inline = "1")
  153. inp_prev_daily_col = input.color(color.gray, title = "color", group = "Prev Daily Levels", inline = "2")
  154. inp_prev_daily_line_width = input.int(1, title = "Line width", minval = 1, group = "Prev Daily Levels", inline = "2")
  155.  
  156. f_create_level("high", new_day, not new_day, global_high_array, inp_prev_daily_col, inp_prev_daily_line_width, inp_ext, high_line_style, text_size, "Prev Daily HIGH", 1, inp_show_prev_daily_high and daily_ok)
  157. f_create_level("low", new_day, not new_day, global_low_array, inp_prev_daily_col, inp_prev_daily_line_width, inp_ext, low_line_style, text_size, "Prev Daily LOW", 1, inp_show_prev_daily_low and daily_ok)
  158. f_create_level("close", new_day, not new_day, global_eq_array, inp_prev_daily_col, inp_prev_daily_line_width, inp_ext, eq_line_style, text_size, "Prev Daily CLOSE", 1, inp_show_prev_daily_close and daily_ok)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement