Advertisement
xmd79

ICT Premium/Discount

Dec 4th, 2023
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.17 KB | None | 0 0
  1. // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Giovanni-1-
  3.  
  4. //@version=5
  5. indicator('ICT Premium/Discount' , overlay=true , max_bars_back=500 , max_lines_count = 500)
  6.  
  7. Timezone = 'America/New_York'
  8.  
  9. DOM_240 = (timeframe.multiplier >= 240)
  10. DOM240 = (timeframe.multiplier <= 240)
  11. DOM60 = (timeframe.multiplier <= 60)
  12. DOM40 = (timeframe.multiplier <= 40)
  13. DOM30 = (timeframe.multiplier <= 30)
  14. DOM15 = (timeframe.multiplier <= 15)
  15. DOM5 = (timeframe.multiplier <= 5)
  16. DOM1 = (timeframe.multiplier <= 1)
  17.  
  18. //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
  19. // Start Range
  20. //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
  21.  
  22. //-------------------------------------------------------------
  23. // -- Functions
  24. backtestt(int _start) =>
  25. backtestRange = time >= _start and time <= timestamp(year, month, dayofmonth, 23, 59) ? true : false
  26. backtestRange
  27. //-------------------------------------------------------------
  28.  
  29. start_test = input.time(defval = timestamp('01 Dec 2023 00:00 +0000') , title = 'Start Date     ' , inline = '0' , group = 'Start')
  30. col_start_test = input.color(defval = color.rgb(141, 122, 122, 73) , title = '' , inline = '0' , group = 'Start')
  31.  
  32. show_range = input.bool(defval = true , title = 'Show High - Low Range' , inline = '1' , group = 'Start')
  33. show_txt_HL = input.bool(defval = true , title = 'Price' , inline = '1' , group = 'Start')
  34. BG_range = input.bool(defval = false , title = 'BG' , inline = '1' , group = 'Start')
  35. col_Premium = input.color(defval = color.red , title = 'Premium / Discount' , inline = '2' , group = 'Start')
  36. col_Discoun = input.color(defval = color.green , title = '' , inline = '2' , group = 'Start')
  37. style_PD = input.string(defval = line.style_solid , title = '' , options = [line.style_solid , line.style_dashed, line.style_dotted] , inline = '2' , group = 'Start')
  38. size_ln_PD = input(defval = 2 , title = ' Width' , inline = '2' , group = 'Start')
  39.  
  40. show_Midlin = input(defval = true , title = 'Show Midline' , inline = '3' , group = 'Start')
  41. col_Midline = input.color(defval = color.blue , title = '' , inline = '3' , group = 'Start')
  42. style_Mid = input.string(defval = line.style_solid , title = '' , options = [line.style_solid , line.style_dashed, line.style_dotted] , inline = '3' , group = 'Start')
  43. size_ln_Mid = input(defval = 1 , title = 'Width' , inline = '3' , group = 'Start')
  44. show_txt_Mid = input.bool(defval = true , title = 'Price' , inline = '3' , group = 'Start')
  45.  
  46. show_Quarti = input(defval = false , title = 'Show 75/25 ' , inline = '4' , group = 'Start')
  47. col_Quartil = input.color(defval = color.black , title = '' , inline = '4' , group = 'Start')
  48. style_Quart = input.string(defval = line.style_dotted , title = '' , options = [line.style_solid , line.style_dashed, line.style_dotted] , inline = '4' , group = 'Start')
  49. size_ln_Qua = input(defval = 1 , title = 'Width' , inline = '4' , group = 'Start')
  50. show_txt_Qua = input.bool(defval = false , title = 'Price' , inline = '4' , group = 'Start')
  51.  
  52. //-------------------------------------------------------------
  53. is_line_start = year == year(start_test) and month == month(start_test) and dayofmonth == dayofmonth(start_test) and hour == hour(start_test) and minute == minute(start_test)
  54. var line start_line = na
  55. if is_line_start
  56. start_line := line.new(bar_index , bar_index , bar_index , bar_index + 1 , extend = extend.both , color = col_start_test , style = line.style_dotted)
  57. plotshape(series = is_line_start , title = '' , style = shape.circle , location = location.bottom , color = color.rgb(120, 123, 134, 100) , text = 'Start Line' , textcolor = color.new(col_start_test , 80) , size = size.small)
  58. //-------------------------------------------------------------
  59.  
  60. //-------------------------------------------------------------
  61. start_range = backtestt(start_test)
  62. start_day = time('D') == time('D')[1]
  63. start_track = start_range
  64.  
  65. reset_range = start_range and not(start_range[1])
  66. reset_day = time('D') != time('D')[1]
  67. reset_res = reset_range
  68.  
  69. var _hh = float(na)
  70. _hh := reset_res ? high : start_track? high > _hh ? high : nz(_hh[1]) : na
  71. var _ll = float(na)
  72. _ll := reset_res ? low : start_track? low < _ll ? low : nz(_ll[1]) : na
  73.  
  74. var line lineVarHighs = line(na)
  75. var line lineVarLows = line(na)
  76. var label Price_high = label(na)
  77. var label Price_low = label(na)
  78. var line lineMid = line(na)
  79. var label Price_5 = label(na)
  80. var line line_75 = line(na)
  81. var line line_25 = line(na)
  82. var label Price_75 = label(na)
  83. var label Price_25 = label(na)
  84.  
  85. if show_range
  86. if reset_res
  87. lineVarHighs := line.new(bar_index[1] , _hh , bar_index , _hh , xloc.bar_index , extend = extend.none , color = col_Premium , width = size_ln_PD , style = style_PD)
  88. lineVarLows := line.new(bar_index[1] , _ll , bar_index , _ll , xloc.bar_index , extend = extend.none , color = col_Discoun , width = size_ln_PD , style = style_PD)
  89. if show_txt_HL
  90. Price_high := label.new(x = last_bar_index , y = _hh , text = show_txt_HL ? str.tostring(_hh) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Premium, textalign = text.align_right , tooltip = 'Premium High' )
  91. Price_low := label.new(x = last_bar_index , y = _ll , text = show_txt_HL ? str.tostring(_ll) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_center , textcolor = col_Discoun , textalign = text.align_right , tooltip = 'Discount Low')
  92.  
  93.  
  94. if show_Midlin
  95. midValue = (_hh + _ll) / 2
  96. lineMid := line.new(bar_index[1] , midValue , bar_index , midValue , xloc.bar_index , extend = extend.none , color = col_Midline , width = size_ln_Mid , style = style_Mid)
  97. if show_txt_Mid
  98. Price_5 := label.new(x = last_bar_index , y = midValue , text = show_txt_HL ? str.tostring(midValue) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Midline , textalign = text.align_right , tooltip = 'Midline High' )
  99.  
  100.  
  101. if show_Quarti
  102. Value25 = _hh - 0.25 * (_hh - _ll)
  103. line_25 := line.new(bar_index[1] , Value25, bar_index , Value25 , xloc.bar_index , extend = extend.none , color = col_Quartil , width = size_ln_Qua , style = style_Quart)
  104.  
  105. Value75 = _hh - 0.75 * (_hh - _ll)
  106. line_75 := line.new(bar_index[1] , Value75, bar_index , Value75 , xloc.bar_index , extend = extend.none , color = col_Quartil , width = size_ln_Qua , style = style_Quart)
  107. if show_txt_Qua
  108. Price_75 := label.new(x = last_bar_index , y = Value75 , text = show_txt_HL ? str.tostring(Value75) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Quartil , textalign = text.align_right)
  109. Price_25 := label.new(x = last_bar_index , y = Value25 , text = show_txt_HL ? str.tostring(Value25) : na , xloc = xloc.bar_index , yloc = yloc.price , color = na , style = label.style_label_down , textcolor = col_Quartil , textalign = text.align_right)
  110.  
  111.  
  112. if BG_range
  113. linefill.new(lineVarHighs , line_25 , color = color.new(col_Premium,90))
  114. linefill.new(line_25 , lineMid , color = color.new(col_Premium,95))
  115. linefill.new(lineVarLows , line_75 , color = color.new(col_Discoun,90))
  116. linefill.new(line_75 , lineMid , color = color.new(col_Discoun,95))
  117.  
  118. if start_track
  119. line.set_x2(lineVarHighs, bar_index)
  120. line.set_y1(lineVarHighs, _hh)
  121. line.set_y2(lineVarHighs, _hh)
  122.  
  123. line.set_x2(lineVarLows, bar_index)
  124. line.set_y1(lineVarLows, _ll)
  125. line.set_y2(lineVarLows, _ll)
  126.  
  127. line.set_x2(lineMid, bar_index)
  128. line.set_y1(lineMid, (_hh + _ll) / 2)
  129. line.set_y2(lineMid, (_hh + _ll) / 2)
  130.  
  131. line.set_x2(line_25, bar_index)
  132. line.set_y1(line_25, _hh - 0.25 * (_hh - _ll))
  133. line.set_y2(line_25, _hh - 0.25 * (_hh - _ll))
  134.  
  135. line.set_x2(line_75, bar_index)
  136. line.set_y1(line_75, _hh - 0.75 * (_hh - _ll))
  137. line.set_y2(line_75, _hh - 0.75 * (_hh - _ll))
  138.  
  139. label.set_x(Price_high, bar_index+2)
  140. label.set_y(Price_high, _hh)
  141. label.set_x(Price_low, bar_index+2)
  142. label.set_y(Price_low, _ll)
  143.  
  144. label.set_x(Price_5, bar_index+2)
  145. label.set_y(Price_5, (_hh + _ll) / 2)
  146.  
  147. label.set_x(Price_75, bar_index+2)
  148. label.set_y(Price_75, _hh - 0.75 * (_hh - _ll))
  149. label.set_x(Price_25, bar_index+2)
  150. label.set_y(Price_25, _hh - 0.25 * (_hh - _ll))
  151. //-------------------------------------------------------------
  152.  
  153. //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
  154. // FVG
  155. //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
  156.  
  157. type Settings
  158. bool CE_show
  159. string CE_style
  160. color CE_color
  161. bool fra_show
  162. string fra_style
  163. color fra_color
  164. bool Border_show
  165. bool mitigated_show
  166. string mitigated_type
  167. color mitigated_color
  168. bool mitigated_CE_show
  169. color mitigated_CE_color
  170. string mitigated_CE_style
  171. bool mitigated_fra_show
  172. color mitigated_fra_color
  173. string mitigated_fra_style
  174. bool ltf_hide
  175. bool label_show
  176. color label_color
  177. color label_bgcolor
  178. string label_size
  179. int padding
  180. int buffer
  181.  
  182. type Imbalance_Settings
  183. bool show
  184. string htf
  185. color color_bull
  186. color color_bear
  187. int max_count
  188.  
  189. type Imbalance
  190. int open_time
  191. int close_time
  192. float open
  193. float middle
  194. float mid_25
  195. float mid_75
  196. float close_up_75
  197. float close_dn_75
  198. float close
  199. bool mitigated
  200. int mitigated_time
  201. line line_middle
  202. line line_25
  203. line line_75
  204. label lbl
  205. box box
  206.  
  207. type ImbalanceStructure
  208. Imbalance[] imbalance
  209. Imbalance_Settings settings
  210.  
  211. type Helper
  212. string name = 'Helper'
  213.  
  214. //+------------------------------------------------------------------------------------------------------------+//
  215. //+--- Settings ---+//
  216. //+------------------------------------------------------------------------------------------------------------+//
  217.  
  218. Settings settings = Settings.new()
  219. Imbalance_Settings HTF_1_Settings = Imbalance_Settings.new()
  220. Imbalance_Settings HTF_2_Settings = Imbalance_Settings.new()
  221. Imbalance_Settings HTF_3_Settings = Imbalance_Settings.new()
  222. Imbalance_Settings HTF_4_Settings = Imbalance_Settings.new()
  223. Imbalance_Settings HTF_5_Settings = Imbalance_Settings.new()
  224. Imbalance_Settings HTF_6_Settings = Imbalance_Settings.new()
  225.  
  226. string tooltip1 = 'HTF FVG Settings:\n\tShow/Hide timeframe\n\tTimeframe to display\n\tBullish FVG Color\n\tBearish FVG Color\n\tMaximum number of FVGs to display'
  227. string tooltip2 = 'Mitigated FVG Settings:\n\tShow/Hide mitigated (Applies to all).\n\tBullish FVG Color\n\tBearish FVG Color\n\tWhen to mark it as mitigated (Based on HTF timeframe, not current timeframe)'
  228.  
  229. settings.ltf_hide := input.bool(defval = false , title = 'Hide Lower Timeframes' , inline = '0' , group = 'FVG')
  230. settings.Border_show := input.bool(defval = false , title = 'Show Border' , inline = '0' , group = 'FVG')
  231.  
  232. HTF_1_Settings.show := input.bool(defval = true , title = '' , inline = 'htf1', group = 'FVG')
  233. htf_1 = input.timeframe(defval = '' , title = '', inline='htf1' , group = 'FVG')
  234. HTF_1_Settings.htf := htf_1
  235. HTF_1_Settings.color_bull := input.color(defval = color.new(#089981 , 80) , title = '', inline = 'htf1' , group = 'FVG')
  236. HTF_1_Settings.color_bear := input.color(defval = color.new(color.white , 80) , title = '', inline = 'htf1' , group = 'FVG')
  237. HTF_1_Settings.max_count := input.int(defval = 5 , title = '' , inline = 'htf1' , group = 'FVG' , tooltip = tooltip1)
  238.  
  239. HTF_2_Settings.show := input.bool(defval = false , title = '', inline = 'htf2' , group = 'FVG')
  240. htf_2 = input.timeframe(defval = '60' , title = '' , inline = 'htf2' , group = 'FVG')
  241. HTF_2_Settings.htf := htf_2
  242. HTF_2_Settings.color_bull := input.color(defval = color.new(#089981,90) , title = '', inline='htf2' , group = 'FVG')
  243. HTF_2_Settings.color_bear := input.color(defval = color.new(color.white,90) , title = '', inline='htf2' , group = 'FVG')
  244. HTF_2_Settings.max_count := input.int(defval = 5 , title = '', inline = 'htf2' , group = 'FVG' , tooltip = tooltip1)
  245.  
  246. HTF_3_Settings.show := input.bool(defval = false , title = '', inline = 'htf3' , group = 'FVG')
  247. htf_3 = input.timeframe(defval = '240' , title = '' , inline = 'htf3' , group = 'FVG')
  248. HTF_3_Settings.htf := htf_3
  249. HTF_3_Settings.color_bull := input.color(defval = color.new(#089981,90) , title = '', inline='htf3' , group = 'FVG')
  250. HTF_3_Settings.color_bear := input.color(defval = color.new(color.white,90) , title = '', inline='htf3' , group = 'FVG')
  251. HTF_3_Settings.max_count := input.int(defval = 5 , title = '', inline = 'htf3' , group = 'FVG' , tooltip = tooltip1)
  252.  
  253. settings.CE_show := input.bool(defval = true , title = 'Show C.E.' , inline='3' , group = 'FVG')
  254. settings.CE_color := input.color(defval = color.new(color.white , 0) , title = '' , inline = '3' , group='FVG')
  255. settings.CE_style := input.string(defval = '----', title = '', options = ['⎯⎯⎯', '----', '····'] , inline='3' , group = 'FVG')
  256. settings.fra_show := input.bool(defval = true , title = 'Show 75-25%' , inline = '4' , group = 'FVG')
  257. settings.fra_color := input.color(defval = color.new(color.black,60) , title = '' , inline = '4' , group = 'FVG')
  258. settings.fra_style := input.string(defval = '····' , title = '' , options = ['⎯⎯⎯', '----', '····'] , inline = '4' , group = 'FVG')
  259.  
  260. settings.mitigated_show := input.bool(defval = true , title = 'Show Inversion FVG' , inline = '0', group = 'Inversion')
  261. settings.mitigated_color := input.color(defval = color.new(color.orange , 90) , title = '' , inline = '0' , group = 'Inversion')
  262. settings.mitigated_CE_show := input.bool(defval = true , title = 'C.E.' , inline = '1' , group = 'Inversion')
  263. settings.mitigated_CE_color := input.color(defval = color.new(color.orange,0) , title = '' , inline = '1' , group = 'Inversion')
  264. settings.mitigated_CE_style := input.string(defval = '⎯⎯⎯', title = '', options = ['⎯⎯⎯', '----', '····'] , inline = '1' , group = 'Inversion')
  265. settings.mitigated_fra_show := input.bool(defval = false , title = '75-25%' , inline = '2' , group = 'Inversion')
  266. settings.mitigated_fra_color := input.color(defval = color.new(color.black,60) , title = '' , inline = '2' , group = 'Inversion')
  267. settings.mitigated_fra_style := input.string(defval = '····' , title = '' , options = ['⎯⎯⎯', '----', '····'] , inline = '2' , group = 'Inversion')
  268. settings.mitigated_type := 'Body filled'
  269.  
  270. settings.label_show := input.bool(defval = true , title = 'Show Label' , inline = '0' , group = 'Label')
  271. settings.label_color := input.color(defval = color.new(color.black, 10) , title = '' , inline = '0' , group = 'Label')
  272. settings.label_bgcolor := input.color(defval = color.new(color.white, 100) , title = '' , inline = '0' , group = 'Label')
  273. settings.label_size := input.string(defval = size.small , title = '' , options = [size.tiny , size.small , size.normal , size.large , size.huge] , inline = '0' , group = 'Label')
  274. settings.padding := input.int(defval = 0 , title = 'Distance from current candle' , minval = 0 , inline = '1' , group = 'Label')
  275. settings.buffer := input.int(defval = 1 , title = 'Spacing between timeframes' , minval = 0 , inline = '2' , group = 'Label')
  276.  
  277. //+------------------------------------------------------------------------------------------------------------+//
  278. //+--- Variables ---+//
  279. //+------------------------------------------------------------------------------------------------------------+//
  280. color color_transparent = #ffffff00
  281.  
  282. Helper helper = Helper.new()
  283.  
  284. var ImbalanceStructure FVG_1 = ImbalanceStructure.new()
  285. var ImbalanceStructure FVG_2 = ImbalanceStructure.new()
  286. var ImbalanceStructure FVG_3 = ImbalanceStructure.new()
  287.  
  288. var Imbalance[] FVGs_1 = array.new<Imbalance>()
  289. var Imbalance[] FVGs_2 = array.new<Imbalance>()
  290. var Imbalance[] FVGs_3 = array.new<Imbalance>()
  291.  
  292. FVG_1.imbalance := FVGs_1
  293. FVG_1.settings := HTF_1_Settings
  294. FVG_2.imbalance := FVGs_2
  295. FVG_2.settings := HTF_2_Settings
  296. FVG_3.imbalance := FVGs_3
  297. FVG_3.settings := HTF_3_Settings
  298.  
  299. //Used internally for padding
  300. var int TF_1 = 0
  301. var int TF_2 = 0
  302. var int TF_3 = 0
  303.  
  304. //+------------------------------------------------------------------------------------------------------------+//
  305. //+--- Methods ---+//
  306. //+------------------------------------------------------------------------------------------------------------+//
  307. method LineStyle(Helper helper, string style) =>
  308. helper.name := style
  309. out = switch style
  310. '----' => line.style_dashed
  311. '····' => line.style_dotted
  312. => line.style_solid
  313. out
  314.  
  315. method Gethtftext(Helper helper, string htf) =>
  316. helper.name := htf
  317. formatted = htf
  318. seconds = timeframe.in_seconds(htf)
  319.  
  320. if seconds < 60
  321. formatted := str.tostring(seconds) + 's'
  322. else if (seconds / 60) < 60
  323. formatted := str.tostring((seconds/60)) + 'm'
  324. else if (seconds/60/60) < 24
  325. formatted := str.tostring((seconds/60/60)) + 'H'
  326. formatted
  327.  
  328. method Validtimeframe(Helper helper, tf) =>
  329. helper.name := tf
  330. n1 = timeframe.in_seconds()
  331. n2 = timeframe.in_seconds(tf)
  332. n1 <= n2
  333.  
  334. //+------------------------------------------------------------------------------------------------------------+//
  335. //+--- Imbalances Methods ---+//
  336. //+------------------------------------------------------------------------------------------------------------+//
  337. // AddZone is used to display and manage imbalance related boxes
  338. method AddZone(ImbalanceStructure IS, Imbalance imb) =>
  339. if IS.settings.show and time >= start_test
  340. int buffer = time+((time-time[1])*(settings.padding+1+(settings.buffer)))
  341. if na(imb.box)
  342. imb.box := box.new(imb.open_time, imb.open, buffer, imb.close, settings.Border_show ? imb.open < imb.close ? color.new(IS.settings.color_bull, color.t(IS.settings.color_bull)/ 3) : color.new(IS.settings.color_bear, color.t(IS.settings.color_bear)/ 3) : color_transparent, settings.Border_show ? 1 : 0, bgcolor = imb.open < imb.close ? IS.settings.color_bull : IS.settings.color_bear, xloc=xloc.bar_time)
  343. if settings.label_show
  344. imb.lbl := label.new(buffer, imb.middle, text=helper.Gethtftext(IS.settings.htf), xloc=xloc.bar_time, textcolor=settings.label_color, style=label.style_label_left, color=settings.label_bgcolor, size = settings.label_size)
  345. if settings.CE_show
  346. imb.line_middle := line.new(imb.open_time, imb.middle, buffer, imb.middle, xloc=xloc.bar_time, style=helper.LineStyle(settings.CE_style), color=settings.CE_color)
  347. if settings.fra_show
  348. imb.line_25 := line.new(imb.open_time, imb.mid_25, buffer, imb.mid_25, xloc=xloc.bar_time, style=helper.LineStyle(settings.fra_style), color=settings.fra_color)
  349. imb.line_75 := line.new(imb.open_time, imb.mid_75, buffer, imb.mid_75, xloc=xloc.bar_time, style=helper.LineStyle(settings.fra_style), color=settings.fra_color)
  350. else
  351. //if mitigated
  352. box.set_right(imb.box, imb.mitigated ? imb.mitigated_time : buffer)
  353. box.set_bgcolor(imb.box, imb.open < imb.close ? imb.mitigated ? settings.mitigated_color : IS.settings.color_bull : imb.mitigated ? settings.mitigated_color : IS.settings.color_bear)
  354. box.set_border_color(imb.box, settings.Border_show ? imb.open < imb.close ? color.new(settings.mitigated_color, color.t(settings.mitigated_color)/ 3) : color.new(settings.mitigated_color, color.t(settings.mitigated_color)/ 3) : color_transparent)
  355. label.delete(imb.lbl)
  356. if settings.CE_show
  357. line.set_color(imb.line_middle , color.new(settings.mitigated_color , 100))
  358. if settings.mitigated_CE_show
  359. line.set_x2(imb.line_middle, imb.mitigated ? imb.mitigated_time : buffer)
  360. line.set_color(imb.line_middle , settings.mitigated_CE_color)
  361. line.set_style(imb.line_middle , helper.LineStyle(settings.mitigated_CE_style))
  362. if settings.fra_show
  363. line.set_color(imb.line_75 , color.new(settings.mitigated_color , 100))
  364. line.set_color(imb.line_25 , color.new(settings.mitigated_color , 100))
  365. if settings.mitigated_fra_show
  366. line.set_x2(imb.line_25, imb.mitigated ? imb.mitigated_time : buffer)
  367. line.set_x2(imb.line_75, imb.mitigated ? imb.mitigated_time : buffer)
  368. line.set_color(imb.line_25 , settings.mitigated_fra_color)
  369. line.set_color(imb.line_75 , settings.mitigated_fra_color)
  370.  
  371. if imb.mitigated and time >= start_test and not settings.mitigated_show
  372. box.delete(imb.box)
  373. line.delete(imb.line_middle)
  374. line.delete(imb.line_25)
  375. line.delete(imb.line_75)
  376. label.delete(imb.lbl)
  377. IS
  378.  
  379. // AddImbalance adds a newly discovered imbalance. this applies for both FVG and Volume Imbalance
  380. method AddImbalance(ImbalanceStructure IS, float o, float c, int o_time) =>
  381. Imbalance imb = Imbalance.new()
  382. imb.open_time := o_time
  383. imb.open := o
  384. imb.middle := (o+c)/2
  385. imb.mid_25 := c+(o-c)*0.25
  386. imb.mid_75 := c+(o-c)*0.75
  387. imb.close := c
  388.  
  389. IS.imbalance.unshift(imb)
  390. IS.AddZone(imb)
  391.  
  392. if time >= start_test and IS.imbalance.size() > IS.settings.max_count
  393. temp = IS.imbalance.pop()
  394. box.delete(temp.box)
  395. line.delete(temp.line_middle)
  396. line.delete(temp.line_25)
  397. line.delete(temp.line_75)
  398. label.delete(temp.lbl)
  399. IS
  400.  
  401. // CheckMitigated checks if the imbalance has been mitigated based on the settings
  402. method CheckMitigated(ImbalanceStructure IS, o, h, l, c) =>
  403. if IS.imbalance.size() > 0
  404. for i = IS.imbalance.size() - 1 to 0
  405. imb = IS.imbalance.get(i)
  406.  
  407. if not imb.mitigated
  408. switch settings.mitigated_type
  409. 'Body filled' =>
  410. imb.mitigated := imb.open < imb.close ? math.min(o, c) <= imb.open : math.max(o, c) >= imb.open
  411.  
  412. if imb.mitigated
  413. if not settings.mitigated_show
  414. box.delete(imb.box)
  415. line.delete(imb.line_middle)
  416. line.delete(imb.line_25)
  417. line.delete(imb.line_75)
  418. label.delete(imb.lbl)
  419. IS.imbalance.remove(i)
  420. else
  421. if IS.settings.max_count
  422. imb.mitigated_time := time
  423. IS.AddZone(imb)
  424. IS
  425.  
  426. method AdjustMargins(ImbalanceStructure IS, int step) =>
  427. if time >= start_test and IS.imbalance.size() > 0
  428. int buffer = time+((time-time[1])*(settings.padding+1+(settings.buffer*(step-1))))
  429. for i = IS.imbalance.size() - 1 to 0
  430. imb = IS.imbalance.get(i)
  431. box.set_right(imb.box, buffer)
  432. label.set_x(imb.lbl, buffer)
  433. if settings.CE_show
  434. line.set_x2(imb.line_middle, buffer)
  435. if settings.fra_show
  436. line.set_x2(imb.line_25, buffer)
  437. line.set_x2(imb.line_75, buffer)
  438. IS
  439. // FindImbalance looks for imbalances and, if found, adds it to the list
  440. method FindImbalance(ImbalanceStructure IS, o, h, l, c, t, o1, h1, l1, c1, t1, o2, h2, l2, c2, t2) =>
  441. if time >= start_test and IS.settings.show and (h < l2 or l > h2)
  442. o = h < l2 ? l2 : h2
  443. c = h < l2 ? h : l
  444. IS.AddImbalance(o, c, t2)
  445. IS
  446.  
  447. method Process(ImbalanceStructure IS, float o, float h, float l, float c, int t, float o1, float h1, float l1, float c1, int t1, float o2, float h2, float l2, float c2, int t2, bool confirmed) =>
  448. var int visible = 0
  449. if time >= start_test and IS.settings.show and confirmed
  450. if not settings.ltf_hide or (settings.ltf_hide and helper.Validtimeframe(IS.settings.htf))
  451. if ta.change(time(IS.settings.htf)) and IS.settings.show
  452. IS.FindImbalance(o, h, l, c, t, o1, h1, l1, c1, t1, o2, h2, l2, c2, t2)
  453. visible := 1
  454. IS.CheckMitigated(o, h, l, c)
  455. visible
  456.  
  457. //+------------------------------------------------------------------------------------------------------------+//
  458. //+--- Main call to start the process ---+//
  459. //+------------------------------------------------------------------------------------------------------------+//
  460. [o_1, h_1, l_1, c_1, t_1, confirmed] = request.security(syminfo.tickerid, htf_1, [open, high, low, close, time, barstate.isconfirmed])
  461. [o1_1, h1_1, l1_1, c1_1, t1_1] = request.security(syminfo.tickerid, htf_1, [open[1], high[1], low[1], close[1], time[1]])
  462. [o2_1, h2_1, l2_1, c2_1, t2_1] = request.security(syminfo.tickerid, htf_1, [open[2], high[2], low[2], close[2], time[2]])
  463. TF_1 := FVG_1.Process(o_1, h_1, l_1, c_1, t_1, o1_1, h1_1, l1_1, c1_1, t1_1, o2_1, h2_1, l2_1, c2_1, t2_1, confirmed)
  464. if barstate.isconfirmed and time >= start_test
  465. FVG_1.AdjustMargins(TF_1)
  466.  
  467. [o_2, h_2, l_2, c_2, t_2, confirmed_2] = request.security(syminfo.tickerid, htf_2, [open, high, low, close, time, barstate.isconfirmed])
  468. [o1_2, h1_2, l1_2, c1_2, t1_2] = request.security(syminfo.tickerid, htf_2, [open[1], high[1], low[1], close[1], time[1]])
  469. [o2_2, h2_2, l2_2, c2_2, t2_2] = request.security(syminfo.tickerid, htf_2, [open[2], high[2], low[2], close[2], time[2]])
  470. TF_2 := TF_1 + FVG_2.Process(o_2, h_2, l_2, c_2, t_2, o1_2, h1_2, l1_2, c1_2, t1_2, o2_2, h2_2, l2_2, c2_2, t2_2, confirmed_2)
  471. if barstate.isconfirmed and time >= start_test
  472. FVG_2.AdjustMargins(TF_2)
  473.  
  474. [o_3, h_3, l_3, c_3, t_3, confirmed_3] = request.security(syminfo.tickerid, htf_3, [open, high, low, close, time, barstate.isconfirmed])
  475. [o1_3, h1_3, l1_3, c1_3, t1_3] = request.security(syminfo.tickerid, htf_3, [open[1], high[1], low[1], close[1], time[1]])
  476. [o2_3, h2_3, l2_3, c2_3, t2_3] = request.security(syminfo.tickerid, htf_3, [open[2], high[2], low[2], close[2], time[2]])
  477. TF_3 := TF_1 + FVG_3.Process(o_3, h_3, l_3, c_3, t_3, o1_3, h1_3, l1_3, c1_3, t1_3, o2_3, h2_3, l2_3, c2_3, t2_3, confirmed_3)
  478. if barstate.isconfirmed and time >= start_test
  479. FVG_3.AdjustMargins(TF_3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement