xmd79

Liquidity Hunter By TeslaKripto

Nov 6th, 2023
222
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.93 KB | None | 0 0
  1. //@version=5
  2. indicator("Liquidity Hunter By TeslaKripto", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count = 500)
  3.  
  4. // Inputs
  5. swingSizeR = input.int(10, 'Bars Right-Left', inline='brl')
  6. swingSizeL = input.int(15, '-', inline='brl')
  7. showBoxes = input.bool(true, 'Show Boxes ', inline='aa')
  8. showSwingLines = input.bool(true, 'Show Lines', inline='aa')
  9. showBubbles = input.bool(true, 'Show Labels ', inline='bb')
  10. showVol = input.bool(false, 'Show Volume', inline='bb')
  11. showOId = input.bool(false, 'Show OI Δ   ', inline='cc')
  12. extendtilfilled = input.bool(true, 'Extend Until Fill', inline='cc')
  13.  
  14. // Conditions
  15. hidefilled = input.bool(false, 'Hide Filled', group='Conditions')
  16. voltresh = input.int(0, 'Volume >', group='Conditions')
  17. oitresh = input.int(0, 'OI Δ (abs.) >', group='Conditions')
  18. pnoid = input.string('/', 'Only Swings With', options = ['Positive OI Delta', 'Negative OI Delta', '/'], group='Conditions')
  19. // Appearance inputs
  20. showhighs = input.bool(true, '', inline='sh', group='Appearance')
  21. showlows = input.bool(true, '', inline='sl', group='Appearance')
  22. sellcol = input.color(#aa2430, 'Lows (Line - Label - Box)', inline = 'sh', group='Appearance')
  23. buycol = input.color(#66bb6a, 'Highs (Line - Label - Box)', inline='sl', group='Appearance')
  24. sellcolB = input.color(#aa2430, '', inline='sh', group='Appearance')
  25. buycolB = input.color(#66bb6a, '', inline = 'sl', group='Appearance')
  26. sellboxCol = input.color(#80192231, '', inline = 'sh', group='Appearance')
  27. buyboxCol = input.color(#66bb6a31, '', inline='sl', group='Appearance')
  28. lineStyle = input.string('Dotted', 'Line Style + Width', ['Solid', 'Dashed', 'Dotted'], inline='l', group='Appearance')
  29. lineWid = input.int(1, '', inline='l', group='Appearance')
  30. boxWid = input.float(0.7, 'Box Width + Type ', step=0.1, inline='xx', group='Appearance')
  31. boxStyle = input.string('TYPE 1', '', options=['TYPE 1', 'TYPE 2'], inline='xx', group='Appearance')
  32. labelsize = input.string('Size: Tiny', 'Text Style        ', options = ['Size: Normal','Size: Large', 'Size: Small', 'Size: Tiny', 'Size: Auto' ], inline='txt', group = 'Appearance' )
  33. texthalign = input.string('Right','', options = ['Middle', 'Right', 'Left'], inline='txt', group = 'Appearance')
  34. lookback = input.bool(false, '', inline='lb')
  35. daysBack = input.float(150, 'Lookback (D)               ',inline='lb')
  36. // OI Data
  37. binance = input.bool(true, 'Binance USDT.P', inline = 'src', group = 'Open Interest')
  38. binance2 = input.bool(true, 'Binance USD.P', inline = 'src', group = 'Open Interest')
  39. binance3 = input.bool(true, 'Binance BUSD.P', inline = 'src2', group = 'Open Interest')
  40. bitmex = input.bool(true, 'BitMEX USD.P', inline = 'src2', group = 'Open Interest')
  41. bitmex2 = input.bool(true, 'BitMEX USDT.P ', inline = 'src3', group = 'Open Interest')
  42. kraken = input.bool(true, 'Kraken USD.P', inline = 'src3', group = 'Open Interest')
  43.  
  44. // Calculating inRange, used for lookback in days
  45. MSPD = 24 * 60 * 60 * 1000
  46. lastBarDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), hour(timenow), minute(timenow), second(timenow))
  47. thisBarDate = timestamp(year, month, dayofmonth, hour, minute, second)
  48. daysLeft = math.abs(math.floor((lastBarDate - thisBarDate) / MSPD))
  49. inRange = lookback ? (daysLeft < daysBack) : true
  50.  
  51. //Pivot calculations
  52. int prevHighIndex= na, int prevLowIndex= na, bool highActive= false, bool lowActive= false, bool h= false, bool l= false
  53. pivHi = ta.pivothigh(high, swingSizeL, swingSizeR)
  54. pivLo = ta.pivotlow(low, swingSizeL, swingSizeR)
  55.  
  56. if not na(pivHi)
  57. h := true
  58. prevHighIndex := bar_index - swingSizeR
  59. if not na(pivLo)
  60. l := true
  61. prevLowIndex := bar_index - swingSizeR
  62.  
  63. // Getting OI data
  64. mex = syminfo.basecurrency=='BTC' ? 'XBT' : string(syminfo.basecurrency)
  65. oid1 = nz(request.security('BINANCE' + ":" + string(syminfo.basecurrency) + 'USDT.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  66. oid2 = nz(request.security('BINANCE' + ":" + string(syminfo.basecurrency) + 'USD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  67. oid3 = nz(request.security('BINANCE' + ":" + string(syminfo.basecurrency) + 'BUSD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  68. oid4 = nz(request.security('BITMEX' + ":" + mex + 'USD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  69. oid5 = nz(request.security('BITMEX' + ":" + mex + 'USDT.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  70. oid6 = nz(request.security('KRAKEN' + ":" + string(syminfo.basecurrency) + 'USD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  71.  
  72. deltaOI = (binance ? nz(oid1,0) : 0) + (binance2 ? nz(oid2,0)/close : 0) + (binance3 ? nz(oid3,0) : 0) + (bitmex ? nz(oid4,0)/close : 0) + (bitmex2 ? nz(oid5,0)/close : 0) + (kraken ? nz(oid6,0)/close : 0)
  73.  
  74.  
  75.  
  76.  
  77. //Volume, OI, box width
  78. vol = volume[swingSizeR]
  79.  
  80. oitreshcond = oitresh > 0 ? math.abs(deltaOI[swingSizeR])>oitresh : true
  81. voltreshcond = voltresh > 0 ? vol > voltresh : true
  82. oicond = pnoid=='Positive OI Delta' ? deltaOI[swingSizeR]>0 : pnoid=='Negative OI Delta' ? deltaOI[swingSizeR]<0 : true
  83.  
  84. color CLEAR = color.rgb(0,0,0,100)
  85. boxWid1 = 0.001 * boxWid
  86.  
  87.  
  88. // Styles
  89. boxStyle(x) =>
  90. switch x
  91. 'TYPE 1' => h ? pivHi : l ? pivLo : na
  92. 'TYPE 2' => h ? pivHi * (1 - boxWid1) : l ? pivLo * (1 + boxWid1) : na
  93. lineStyle(x) =>
  94. switch x
  95. 'Solid' => line.style_solid
  96. 'Dashed' => line.style_dashed
  97. 'Dotted' => line.style_dotted
  98. switchtextsize(textsize) =>
  99. switch textsize
  100. 'Size: Normal' => size.normal
  101. 'Size: Small' => size.small
  102. 'Size: Tiny' => size.tiny
  103. 'Size: Auto' => size.auto
  104. 'Size: Large' => size.large
  105. switchhalign(texthalign) =>
  106. switch texthalign
  107. 'Middle' => text.align_center
  108. 'Right' => text.align_right
  109. 'Left' => text.align_left
  110.  
  111. //Swing level labels
  112. var levelBoxes = array.new_box(), var levelLines = array.new_line()
  113. if h and inRange and showhighs and oitreshcond and voltreshcond and oicond
  114. hBox = box.new(prevHighIndex, pivHi * (1 + boxWid1), bar_index, boxStyle(boxStyle), border_color = na, bgcolor = showBoxes ? sellboxCol : CLEAR, text= (showVol ? str.tostring(vol, format.volume) : na) +' '+ (showOId ? str.tostring(deltaOI[swingSizeR], format.volume) : ''),text_halign=switchhalign(texthalign),text_valign=text.align_center,text_color=chart.fg_color, text_size=switchtextsize(labelsize))
  115. hLine = line.new(prevHighIndex, pivHi, bar_index, pivHi, color = showSwingLines ? sellcol : CLEAR, style=lineStyle(lineStyle), width=lineWid)
  116. array.push(levelBoxes, hBox)
  117. array.push(levelLines, hLine)
  118. if l and inRange and showhighs and oitreshcond and voltreshcond and oicond
  119. lBox = box.new(prevLowIndex, pivLo * (1 - boxWid1), bar_index, boxStyle(boxStyle), border_color = na, bgcolor = showBoxes ? buyboxCol : CLEAR, text= (showVol ? str.tostring(vol, format.volume) : na) +' '+ (showOId ? str.tostring(deltaOI[swingSizeR], format.volume) : ''),text_halign=switchhalign(texthalign),text_valign=text.align_center,text_color=chart.fg_color, text_size=switchtextsize(labelsize))
  120. lLine = line.new(prevLowIndex, pivLo, bar_index, pivLo, color = showSwingLines ? buycol : CLEAR, style=lineStyle(lineStyle), width=lineWid)
  121. array.push(levelBoxes, lBox)
  122. array.push(levelLines, lLine)
  123.  
  124. // Looping over the full array of lines and updating them, and deleting them if they have been touched
  125. size = array.size(levelBoxes)
  126. if size > 0
  127. for i = 0 to size - 1
  128. j = size - 1 - i
  129. box = array.get(levelBoxes, j)
  130. line = array.get(levelLines, j)
  131. level = line.get_y2(line)
  132. filled = (high >= level and low <= level)
  133.  
  134. if filled and extendtilfilled and not hidefilled
  135. array.remove(levelLines, j)
  136. array.remove(levelBoxes, j)
  137. continue
  138.  
  139. box.set_right(box, bar_index+1)
  140. line.set_x2(line, bar_index+1)
  141. if filled and hidefilled
  142. array.remove(levelLines, j)
  143. array.remove(levelBoxes, j)
  144. line.delete(line)
  145. box.delete(box)
  146.  
  147. if not filled and not extendtilfilled
  148. array.remove(levelLines, j)
  149. array.remove(levelBoxes, j)
  150. continue
  151. box.set_right(box, bar_index[0]+4)
  152. line.set_x2(line, bar_index[0]+4)
  153.  
  154.  
  155. // Deleting the oldest lines if array is too big
  156. if array.size(levelBoxes) >= 500
  157. int i = 0
  158. while array.size(levelBoxes) >= 500
  159. box = array.get(levelBoxes, i)
  160. line = array.get(levelLines, i)
  161. box.delete(box)
  162. line.delete(line)
  163. array.remove(levelBoxes, i)
  164. array.remove(levelLines, i)
  165. i += 1
  166.  
  167. // Plotting circle labels
  168. plotshape(showhighs and showBubbles and h and oitreshcond and voltreshcond and oicond ? high[swingSizeR] : na, style=shape.circle, location = location.absolute, offset = -swingSizeR, color=sellcolB, size = size.tiny)
  169. plotshape(showlows and showBubbles and l and oitreshcond and voltreshcond and oicond ? low[swingSizeR] : na, style=shape.circle, location = location.absolute, offset = -swingSizeR, color=buycolB, size = size.tiny)
  170.  
  171. //////////////////////////////////////////////////////////////////////-------------------/////////////////////////////////////////////////
  172.  
  173. //Option to show Liquidity lines on the chart
  174. show_liquidity = input(true,"Show Liquidity? (Daily, Weekly, Monthly)",inline='Show Liquidity'
  175. , group = 'Configuration')
  176.  
  177. var gStartOffset = 0
  178. var gEndOffset = 25
  179.  
  180. color_text_prev_high_low = input.color(color.new(#f6eb54, 13), 'Color of previous day high and low'
  181. , group = 'Configuration')
  182.  
  183. english_language_selected = input(true, 'Show Labels in English'
  184. , group = 'Configuration')
  185.  
  186.  
  187.  
  188. // DEFINE COLORS of the lines displayed on the chart
  189. // --> Blue lines: Daily liquidity
  190. // --> Yellow lines: Weekly Liquidity
  191. // --> Purple lines: Monthly liquidity
  192. //daily liquidity
  193. gIsDailyEnabled = true
  194. gDailyAboveLiquidityColor = color.new(#4987d3, 13)
  195. gDailyBelowLiquidityColor = color.new(#4987d3, 13)
  196. gDailyWidth = 1
  197.  
  198. //weekly liquidity
  199. gIsWeeklyEnabled = true
  200. gWeeklyAboveLiquidityColor = color.new(#f6eb54, 13)
  201. gWeeklyBelowLiquidityColor = color.new(#f6eb54, 13)
  202. gWeeklyWidth = 1
  203.  
  204. //month liquidity
  205. gIsMonthlyEnabled = true
  206. gMonthlyAboveLiquidityColor = color.new(#f66ecf, 13)
  207. gMonthlyBelowLiquidityColor = color.new(#f66ecf, 13)
  208. gMonthlyWidth = 1
  209.  
  210. cleanedLevelColor = color.new(#ffffff, 100)
  211. cleanedLevelStyle = "Dashed"
  212.  
  213.  
  214. var highArray = array.new_float()
  215. var lowArray = array.new_float()
  216. var highLinesArray = array.new_line()
  217. var lowLinesArray = array.new_line()
  218. var purgedLinesArray = array.new_line()
  219.  
  220. [prevDayHigh, prevDayLow] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead=barmerge.lookahead_on)
  221. [prevWeekHigh, prevWeekLow] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead=barmerge.lookahead_on)
  222. [prevMonthHigh, prevMonthLow] = request.security(syminfo.tickerid, "M", [high[1], low[1]], lookahead=barmerge.lookahead_on)
  223. [prev4HHigh, prev4HLow] = request.security(syminfo.tickerid, "240", [high[1], low[1]], lookahead=barmerge.lookahead_on)
  224. [prev1HHigh, prev1HLow] = request.security(syminfo.tickerid, "60", [high[1], low[1]], lookahead=barmerge.lookahead_on)
  225.  
  226.  
  227. //Set Labels text according language selected (English or Portuguese)
  228. demand_lbl_txt = ""
  229. if (english_language_selected == false)
  230. demand_lbl_txt := "Zona de Compra"
  231. else
  232. demand_lbl_txt := "Demand/Buy Zone"
  233.  
  234. supply_lbl_txt = ""
  235. if (english_language_selected == false)
  236. supply_lbl_txt := "Zona de Venda"
  237. else
  238. supply_lbl_txt := "Supply/Sell Zone"
  239.  
  240. strong_high_lbl = ""
  241. if (english_language_selected == false)
  242. strong_high_lbl := "Alto Forte"
  243. else
  244. strong_high_lbl := "Strong High"
  245.  
  246. strong_low_lbl = ""
  247. if (english_language_selected == false)
  248. strong_low_lbl := "Baixo Forte"
  249. else
  250. strong_low_lbl := "Strong Low"
  251.  
  252.  
  253. weak_low_lbl = ""
  254. if (english_language_selected == false)
  255. weak_low_lbl := "Baixo Fraco"
  256. else
  257. weak_low_lbl := "Weak Low"
  258.  
  259. weak_high_lbl = ""
  260. if (english_language_selected == false)
  261. weak_high_lbl := "Alto Fraco"
  262. else
  263. weak_high_lbl := "Weak High"
  264.  
  265. previous_day_high_lbl = ""
  266. if (english_language_selected == false)
  267. previous_day_high_lbl := "Preço Máximo do dia Anterior"
  268. else
  269. previous_day_high_lbl := "Previous Day Highest Price"
  270.  
  271. previous_day_low_lbl = ""
  272. if (english_language_selected == false)
  273. previous_day_low_lbl := "Preço Minimo do dia Anterior"
  274. else
  275. previous_day_low_lbl := "Previous Day Lowest Price"
  276.  
  277. // Functions
  278. f_drawLine(_y, _c, _w=1) => line.new(bar_index, _y, bar_index, _y, color=_c, width=_w)
  279.  
  280. f_create(_high, _low, _upperColor, _lowerColor, _linewidth) =>
  281. array.push(highArray, _high)
  282. array.push(lowArray, _low)
  283. array.push(highLinesArray, f_drawLine(_high, _upperColor, _linewidth))
  284. array.push(lowLinesArray, f_drawLine(_low, _lowerColor, _linewidth))
  285.  
  286. f_updateStickyLevels(_levels) =>
  287. for _line in _levels
  288. line.set_x1(_line, bar_index + gStartOffset)
  289. line.set_x2(_line, bar_index + gEndOffset)
  290.  
  291. f_moveLevel(_from, _to, _level, _index) =>
  292. array.push(_to, _level)
  293. array.remove(_from, _index)
  294.  
  295. f_highlightPurgedLevel(_level) =>
  296. _style = cleanedLevelStyle == "Solid" ? line.style_solid : cleanedLevelStyle == "Dashed" ? line.style_dashed : line.style_dotted
  297. line.set_color(_level, cleanedLevelColor)
  298. line.set_style(_level, _style)
  299.  
  300. f_updateUpperLevels(_high, _highs, _levels, _purgedLevels) =>
  301. while array.min(_highs) < _high
  302. for [_index, _value] in _highs
  303. if _high > _value
  304. _line = array.get(_levels, _index)
  305. f_highlightPurgedLevel(_line)
  306. f_moveLevel(_levels, _purgedLevels, _line, _index)
  307. array.remove(_highs, _index)
  308. f_updateLowerLevels(_low, _lows, _levels, _purgedLevels) =>
  309. while array.max(_lows) > _low
  310. for [_index, _value] in _lows
  311. if _low < _value
  312. _line = array.get(_levels, _index)
  313. f_highlightPurgedLevel(_line)
  314. f_moveLevel(_levels, _purgedLevels, _line, _index)
  315. array.remove(_lows, _index)
  316.  
  317. f_clearLevels(_levels) =>
  318. while array.size(_levels) > 0
  319. for [_index, _line] in _levels
  320. line.delete(array.remove(_levels, _index))
  321.  
  322. f_isHigherTimeframe(_timeframe) => timeframe.in_seconds() <= timeframe.in_seconds(_timeframe)
  323.  
  324.  
  325. //Draw Lines if liquidity is enabled
  326. if show_liquidity
  327. if gIsDailyEnabled and f_isHigherTimeframe("D") and ta.change(time("D"))
  328. f_create(prevDayHigh, prevDayLow, gDailyAboveLiquidityColor, gDailyBelowLiquidityColor, gDailyWidth)
  329.  
  330. if gIsWeeklyEnabled and f_isHigherTimeframe("W") and ta.change(time("W"))
  331. f_create(prevWeekHigh, prevWeekLow, gWeeklyAboveLiquidityColor, gWeeklyBelowLiquidityColor, gWeeklyWidth)
  332.  
  333. if gIsMonthlyEnabled and f_isHigherTimeframe("M") and ta.change(time("M"))
  334. f_create(prevMonthHigh, prevMonthLow, gMonthlyAboveLiquidityColor, gMonthlyBelowLiquidityColor, gMonthlyWidth)
  335.  
  336.  
  337. if barstate.islast
  338. f_updateStickyLevels(highLinesArray)
  339. f_updateStickyLevels(lowLinesArray)
  340. f_updateStickyLevels(purgedLinesArray)
  341.  
  342. // Highlight the levels that got their liquidity taken
  343.  
  344. f_updateUpperLevels(high, highArray, highLinesArray, purgedLinesArray)
  345. f_updateLowerLevels(low, lowArray, lowLinesArray, purgedLinesArray)
  346.  
  347. // Clean the levels that had their liquidity taken on a daily basis
  348. if ta.change(time("D"))
  349. f_clearLevels(purgedLinesArray)
  350.  
  351. color TRANSP_COLOR = #ffffff00
  352.  
  353. //Tooltips
  354. string TOOLTIP = 'Allows to display historical Structure'
  355. string STYLE = 'color theme'
  356. string COLOR_CANDLES = 'Display additional candles'
  357.  
  358.  
  359. mode = 'Historical'
  360. style = 'Colored'
  361. show_trend = false
  362.  
  363.  
  364. //Internal Structure
  365. show_internals = false
  366. show_ibull = 'All'
  367. swing_ibull_css =#5ef8d727
  368.  
  369.  
  370. //Bear Structure
  371. show_ibear = 'All'
  372. swing_ibear_css = #c40d0d5b
  373. ifilter_confluence = false
  374.  
  375.  
  376. //Swing Structure
  377. show_Structure = true
  378.  
  379. //Bull Structure
  380. show_bull = 'All'
  381. swing_bull_css = #0dea7f63
  382.  
  383. //Bear Structure
  384. show_bear = 'All'
  385. swing_bear_css = #c40d0de8
  386.  
  387. //Swings
  388. show_swings = true
  389. length = 50
  390. show_hl_swings = true
  391.  
  392.  
  393. //Order Blocks Variables
  394.  
  395. show_iob = false
  396. iob_showlast = 5
  397. show_ob = true
  398. ob_showlast = 5
  399. ob_filter = 'Atr'
  400. ibull_ob_css = #0bf6841f
  401. ibear_ob_css = color.new(#e64444, 80)
  402. bull_ob_css = #0bf6841f
  403. bear_ob_css = color.new(#e64444, 80)
  404.  
  405.  
  406. //Imbalances / Gaps
  407.  
  408. show_fvg = true
  409. fvg_auto = true
  410. fvg_tf = ''
  411. bull_fvg_css = color.new(#b980ef4d, 70)
  412. bear_fvg_css = color.new(#b980ef4d, 70)
  413. fvg_extend = 5
  414.  
  415.  
  416. //Previous day/week high/low
  417.  
  418. //Daily
  419. show_pdhl = true
  420. pdhl_style = '····'
  421. pdhl_css = #ffffff
  422.  
  423. //Premium and Discount zones
  424.  
  425. show_sd = true
  426. premium_css = #e616169c
  427. discount_css = #0899445d
  428.  
  429. //Functions
  430. n = bar_index
  431.  
  432. atr = ta.atr(200)
  433. cmean_range = ta.cum(high - low) / n
  434.  
  435. //HL Output function
  436. hl() => [high, low]
  437.  
  438. //Get ohlc values function
  439. get_ohlc()=> [close[1], open[1], high, low, high[2], low[2]]
  440.  
  441. //Display Structure function
  442. display_Structure(x, y, txt, css, dashed, down, lbl_size)=>
  443. structure_line = line.new(x, y, n, y
  444. , color = css
  445. , style = dashed ? line.style_dashed : line.style_solid)
  446.  
  447. structure_lbl = label.new(int(math.avg(x, n)), y, txt
  448. , color = TRANSP_COLOR
  449. , textcolor = css
  450. , style = down ? label.style_label_down : label.style_label_up
  451. , size = lbl_size)
  452.  
  453. if mode == 'Present'
  454. line.delete(structure_line[1])
  455. label.delete(structure_lbl[1])
  456.  
  457. //Swings detection/measurements
  458. swings_calc(len)=>
  459. var os = 0
  460.  
  461. upper = ta.highest(len)
  462. lower = ta.lowest(len)
  463.  
  464. os := high[len] > upper ? 0 : low[len] < lower ? 1 : os[1]
  465.  
  466. top = os == 0 and os[1] != 0 ? high[len] : 0
  467. btm = os == 1 and os[1] != 1 ? low[len] : 0
  468.  
  469. [top, btm]
  470.  
  471. //Order block coordinates function
  472. ob_coord(use_max, loc, target_top, target_btm, target_left, target_type)=>
  473. min = 99999999.
  474. max = 0.
  475. idx = 1
  476.  
  477. ob_threshold = ob_filter == 'Atr' ? atr : cmean_range
  478.  
  479. //Search for highest/lowest high within the structure interval and get range
  480. if use_max
  481. for i = 1 to (n - loc)-1
  482. if (high[i] - low[i]) < ob_threshold[i] * 2
  483. max := math.max(high[i], max)
  484. min := max == high[i] ? low[i] : min
  485. idx := max == high[i] ? i : idx
  486. else
  487. for i = 1 to (n - loc)-1
  488. if (high[i] - low[i]) < ob_threshold[i] * 2
  489. min := math.min(low[i], min)
  490. max := min == low[i] ? high[i] : max
  491. idx := min == low[i] ? i : idx
  492.  
  493. array.unshift(target_top, max)
  494. array.unshift(target_btm, min)
  495. array.unshift(target_left, time[idx])
  496. array.unshift(target_type, use_max ? -1 : 1)
  497.  
  498. //Set order blocks
  499. display_ob(boxes, target_top, target_btm, target_left, target_type, show_last, swing, size)=>
  500. for i = 0 to math.min(show_last-1, size-1)
  501. get_box = array.get(boxes, i)
  502.  
  503. box.set_lefttop(get_box, array.get(target_left, i), array.get(target_top, i))
  504. box.set_rightbottom(get_box, array.get(target_left, i), array.get(target_btm, i))
  505. box.set_extend(get_box, extend.right)
  506.  
  507. color css = na
  508.  
  509. if swing
  510. if style == 'Monochrome'
  511. css := array.get(target_type, i) == 1 ? color.new(#b2b5be, 80) : color.new(#5d606b, 80)
  512. border_css = array.get(target_type, i) == 1 ? #b2b5be : #5d606b
  513. else
  514. css := array.get(target_type, i) == 1 ? bull_ob_css : bear_ob_css
  515.  
  516.  
  517. box.set_border_color(get_box, css)
  518. box.set_bgcolor(get_box, css)
  519. else
  520. if style == 'Monochrome'
  521. css := array.get(target_type, i) == 1 ? color.new(#b2b5be, 80) : color.new(#5d606b, 80)
  522. else
  523. css := array.get(target_type, i) == 1 ? ibull_ob_css : ibear_ob_css
  524.  
  525. box.set_border_color(get_box, css)
  526. box.set_bgcolor(get_box, css)
  527.  
  528. //Line Style function
  529. get_line_style(style) =>
  530. out = switch style
  531. '⎯⎯⎯' => line.style_solid
  532. '----' => line.style_dashed
  533. '····' => line.style_dotted
  534.  
  535. //Set line and labels for previous high and lows
  536. define_previous_days_prices(h, l, tf, css)=>
  537. var line high_line = line.new(na,na,na,na
  538. , xloc = xloc.bar_time
  539. , color = css
  540. , style = get_line_style(pdhl_style))
  541.  
  542. var label high_lbl = label.new(na,na
  543. , xloc = xloc.bar_time
  544. , text = str.format(previous_day_high_lbl, tf)
  545. , color = TRANSP_COLOR
  546. , textcolor = color_text_prev_high_low
  547. , size = size.small
  548. , style = label.style_label_left)
  549.  
  550. var line low_line = line.new(na,na,na,na
  551. , xloc = xloc.bar_time
  552. , color = css
  553. , style = get_line_style(pdhl_style))
  554.  
  555. var label low_lbl = label.new(na,na
  556. , xloc = xloc.bar_time
  557. , text = str.format(previous_day_low_lbl, tf)
  558. , color = TRANSP_COLOR
  559. , textcolor = color_text_prev_high_low
  560. , size = size.small
  561. , style = label.style_label_left)
  562.  
  563. hy = ta.valuewhen(h != h[1], h, 1)
  564. hx = ta.valuewhen(h == high, time, 1)
  565.  
  566. ly = ta.valuewhen(l != l[1], l, 1)
  567. lx = ta.valuewhen(l == low, time, 1)
  568.  
  569. if barstate.islast
  570. ext = time + (time - time[1])*20
  571.  
  572. //High
  573. line.set_xy1(high_line, hx, hy)
  574. line.set_xy2(high_line, ext, hy)
  575.  
  576. label.set_xy(high_lbl, ext, hy)
  577.  
  578. //Low
  579. line.set_xy1(low_line, lx, ly)
  580. line.set_xy2(low_line, ext, ly)
  581.  
  582. label.set_xy(low_lbl, ext, ly)
  583.  
  584. //-----------------------------------------------------------------------------}
  585. //Global variables
  586. //-----------------------------------------------------------------------------{
  587. var trend = 0, var itrend = 0
  588.  
  589. var top_y = 0., var top_x = 0
  590. var btm_y = 0., var btm_x = 0
  591.  
  592. var itop_y = 0., var itop_x = 0
  593. var ibtm_y = 0., var ibtm_x = 0
  594.  
  595. var trail_up = high, var trail_dn = low
  596. var trail_up_x = 0, var trail_dn_x = 0
  597.  
  598. var top_cross = true, var btm_cross = true
  599. var itop_cross = true, var ibtm_cross = true
  600.  
  601. var txt_top = '', var txt_btm = ''
  602.  
  603. //Alerts
  604. bull_choch_alert = false
  605. bull_bos_alert = false
  606.  
  607. bear_choch_alert = false
  608. bear_bos_alert = false
  609.  
  610. bull_ichoch_alert = false
  611. bull_ibos_alert = false
  612.  
  613. bear_ichoch_alert = false
  614. bear_ibos_alert = false
  615.  
  616. bull_iob_break = false
  617. bear_iob_break = false
  618.  
  619. bull_ob_break = false
  620. bear_ob_break = false
  621.  
  622. eqh_alert = false
  623. eql_alert = false
  624.  
  625. //Structure colors
  626. var bull_css = style == 'Monochrome' ? #b2b5be
  627. : swing_bull_css
  628.  
  629. var bear_css = style == 'Monochrome' ? #b2b5be
  630. : swing_bear_css
  631.  
  632. var ibull_css = style == 'Monochrome' ? #b2b5be
  633. : swing_ibull_css
  634.  
  635. var ibear_css = style == 'Monochrome' ? #b2b5be
  636. : swing_ibear_css
  637.  
  638. //Swings
  639. [top, btm] = swings_calc(length)
  640.  
  641. [itop, ibtm] = swings_calc(5)
  642.  
  643. //-----------------------------------------------------------------------------}
  644. //Pivot High
  645. //-----------------------------------------------------------------------------{
  646. var line extend_top = na
  647.  
  648. var label extend_top_lbl = label.new(na, na
  649. , color = TRANSP_COLOR
  650. , textcolor = bear_css
  651. , style = label.style_label_down
  652. , size = size.small)
  653.  
  654. if top
  655. top_cross := true
  656. txt_top := top > top_y ? 'HH' : 'LH'
  657.  
  658. if show_swings
  659. top_lbl = label.new(n-length, top, txt_top
  660. , color = TRANSP_COLOR
  661. , textcolor = bear_css
  662. , style = label.style_label_down
  663. , size = size.small)
  664.  
  665. if mode == 'Present'
  666. label.delete(top_lbl[1])
  667.  
  668. //Extend recent top to last bar
  669. line.delete(extend_top[1])
  670. extend_top := line.new(n-length, top, n, top
  671. , color = bear_css)
  672.  
  673. top_y := top
  674. top_x := n - length
  675.  
  676. trail_up := top
  677. trail_up_x := n - length
  678.  
  679. if itop
  680. itop_cross := true
  681.  
  682. itop_y := itop
  683. itop_x := n - 5
  684.  
  685. //Trailing maximum
  686. trail_up := math.max(high, trail_up)
  687. trail_up_x := trail_up == high ? n : trail_up_x
  688.  
  689. //Set top extension label/line
  690. if barstate.islast and show_hl_swings
  691. line.set_xy1(extend_top, trail_up_x, trail_up)
  692. line.set_xy2(extend_top, n + 20, trail_up)
  693.  
  694. label.set_x(extend_top_lbl, n + 20)
  695. label.set_y(extend_top_lbl, trail_up)
  696. label.set_text(extend_top_lbl, trend < 0 ? strong_high_lbl : weak_high_lbl)
  697.  
  698. //-----------------------------------------------------------------------------}
  699. //Pivot Low
  700. //-----------------------------------------------------------------------------{
  701. var line extend_btm = na
  702.  
  703. var label extend_btm_lbl = label.new(na, na
  704. , color = TRANSP_COLOR
  705. , textcolor = bull_css
  706. , style = label.style_label_up
  707. , size = size.small)
  708.  
  709. if btm
  710. btm_cross := true
  711. txt_btm := btm < btm_y ? 'LL' : 'HL'
  712.  
  713. if show_swings
  714. btm_lbl = label.new(n - length, btm, txt_btm
  715. , color = TRANSP_COLOR
  716. , textcolor = bull_css
  717. , style = label.style_label_up
  718. , size = size.small)
  719.  
  720. if mode == 'Present'
  721. label.delete(btm_lbl[1])
  722.  
  723. //Extend recent btm to last bar
  724. line.delete(extend_btm[1])
  725. extend_btm := line.new(n - length, btm, n, btm
  726. , color = bull_css)
  727.  
  728. btm_y := btm
  729. btm_x := n-length
  730.  
  731. trail_dn := btm
  732. trail_dn_x := n-length
  733.  
  734. if ibtm
  735. ibtm_cross := true
  736.  
  737. ibtm_y := ibtm
  738. ibtm_x := n - 5
  739.  
  740. //Trailing minimum
  741. trail_dn := math.min(low, trail_dn)
  742. trail_dn_x := trail_dn == low ? n : trail_dn_x
  743.  
  744. //Set btm extension label/line
  745. if barstate.islast and show_hl_swings
  746. line.set_xy1(extend_btm, trail_dn_x, trail_dn)
  747. line.set_xy2(extend_btm, n + 20, trail_dn)
  748.  
  749. label.set_x(extend_btm_lbl, n + 20)
  750. label.set_y(extend_btm_lbl, trail_dn)
  751. label.set_text(extend_btm_lbl, trend > 0 ? strong_low_lbl : weak_low_lbl)
  752.  
  753. //-----------------------------------------------------------------------------}
  754. //Order Blocks Arrays
  755. //-----------------------------------------------------------------------------{
  756. var iob_top = array.new_float(0)
  757. var iob_btm = array.new_float(0)
  758. var iob_left = array.new_int(0)
  759. var iob_type = array.new_int(0)
  760.  
  761. var ob_top = array.new_float(0)
  762. var ob_btm = array.new_float(0)
  763. var ob_left = array.new_int(0)
  764. var ob_type = array.new_int(0)
  765.  
  766. //-----------------------------------------------------------------------------}
  767. //Pivot High BOS and Choch
  768. //-----------------------------------------------------------------------------{
  769. //Filtering
  770. var bull_concordant = true
  771.  
  772. if ifilter_confluence
  773. bull_concordant := high - math.max(close, open) > math.min(close, open - low)
  774.  
  775. //Detect internal bullish Structure
  776. if ta.crossover(close, itop_y) and itop_cross and top_y != itop_y and bull_concordant
  777. bool choch = na
  778.  
  779. if itrend < 0
  780. choch := true
  781. bull_ichoch_alert := true
  782. else
  783. bull_ibos_alert := true
  784.  
  785. txt = ""
  786. if(english_language_selected == false)
  787. txt := choch ? 'Alteração de Caráter' : 'Quebra Estrutura'
  788. else
  789. txt := choch ? 'Change of Character' : 'Break of Structure'
  790.  
  791. if show_internals
  792. if show_ibull == 'All' or (show_ibull == 'BOS' and not choch) or (show_ibull == 'CHoCH' and choch)
  793. display_Structure(itop_x, itop_y, txt, ibull_css, true, true, size.tiny)
  794.  
  795. itop_cross := false
  796. itrend := 1
  797.  
  798. //Internal Order Block
  799. if show_iob
  800. ob_coord(false, itop_x, iob_top, iob_btm, iob_left, iob_type)
  801.  
  802. //Detect bullish Structure
  803. if ta.crossover(close, top_y) and top_cross
  804. bool choch = na
  805.  
  806. if trend < 0
  807. choch := true
  808. bull_choch_alert := true
  809. else
  810. bull_bos_alert := true
  811.  
  812. txt = ""
  813. if(english_language_selected == false)
  814. txt := choch ? 'Alteração de Caráter' : 'Quebra Estrutura'
  815. else
  816. txt := choch ? 'Change of Character' : 'Break of Structure'
  817. //txt = choch ? 'Alteração de Caráter' : 'Quebra Estrutura'
  818.  
  819. if show_Structure
  820. if show_bull == 'All' or (show_bull == 'BOS' and not choch) or (show_bull == 'CHoCH' and choch)
  821. display_Structure(top_x, top_y, txt, #0ddc67d0, true, true, size.small)
  822.  
  823. //Order Block
  824. if show_ob
  825. ob_coord(false, top_x, ob_top, ob_btm, ob_left, ob_type)
  826.  
  827. top_cross := false
  828. trend := 1
  829.  
  830. //-----------------------------------------------------------------------------}
  831. //Pivot Low BOS and CHoCH
  832. //-----------------------------------------------------------------------------{
  833. var bear_concordant = true
  834.  
  835. if ifilter_confluence
  836. bear_concordant := high - math.max(close, open) < math.min(close, open - low)
  837.  
  838. //Detect internal bearish Structure
  839. if ta.crossunder(close, ibtm_y) and ibtm_cross and btm_y != ibtm_y and bear_concordant
  840. bool choch = false
  841.  
  842. if itrend > 0
  843. choch := true
  844. bear_ichoch_alert := true
  845. else
  846. bear_ibos_alert := true
  847.  
  848. txt = ""
  849. if(english_language_selected == false)
  850. txt := choch ? 'Alteração de Caráter' : 'Quebra Estrutura'
  851. else
  852. txt := choch ? 'Change of Character' : 'Break of Structure'
  853. //txt = choch ? 'Alteração de Caráter' : 'QUEBRA DE ESTRUTURA'
  854.  
  855. if show_internals
  856. if show_ibear == 'All' or (show_ibear == 'BOS' and not choch) or (show_ibear == 'CHoCH' and choch)
  857. display_Structure(ibtm_x, ibtm_y, txt, ibear_css, true, false, size.small)
  858.  
  859. ibtm_cross := false
  860. itrend := -1
  861.  
  862. //Internal Order Block
  863. if show_iob
  864. ob_coord(true, ibtm_x, iob_top, iob_btm, iob_left, iob_type)
  865.  
  866. //Detect bearish Structure
  867. if ta.crossunder(close, btm_y) and btm_cross
  868. bool choch = na
  869.  
  870. if trend > 0
  871. choch := true
  872. bear_choch_alert := true
  873. else
  874. bear_bos_alert := true
  875.  
  876. txt = ""
  877. if(english_language_selected == false)
  878. txt := choch ? 'Alteração de Caráter' : 'Quebra Estrutura'
  879. else
  880. txt := choch ? 'Change of Character' : 'Break of Structure'
  881.  
  882. //txt = choch ? 'Alteração de Caráter' : 'QUEBRA DE ESTRUTURA'
  883.  
  884. if show_Structure
  885. if show_bear == 'All' or (show_bear == 'BOS' and not choch) or (show_bear == 'CHoCH' and choch)
  886. display_Structure(btm_x, btm_y, txt, #dc0d0dd0, true, false, size.small)
  887.  
  888. //Order Block
  889. if show_ob
  890. ob_coord(true, btm_x, ob_top, ob_btm, ob_left, ob_type)
  891.  
  892. btm_cross := false
  893. trend := -1
  894.  
  895. //-----------------------------------------------------------------------------}
  896. //Order Blocks
  897. //-----------------------------------------------------------------------------{
  898. //Set order blocks
  899. var iob_boxes = array.new_box(0)
  900. var ob_boxes = array.new_box(0)
  901.  
  902. //Delete internal order blocks box coordinates if top/bottom is broken
  903. for element in iob_type
  904. index = array.indexof(iob_type, element)
  905.  
  906. if close < array.get(iob_btm, index) and element == 1
  907. array.remove(iob_top, index)
  908. array.remove(iob_btm, index)
  909. array.remove(iob_left, index)
  910. array.remove(iob_type, index)
  911. bull_iob_break := true
  912.  
  913. else if close > array.get(iob_top, index) and element == -1
  914. array.remove(iob_top, index)
  915. array.remove(iob_btm, index)
  916. array.remove(iob_left, index)
  917. array.remove(iob_type, index)
  918. bear_iob_break := true
  919.  
  920. //Delete internal order blocks box coordinates if top/bottom is broken
  921. for element in ob_type
  922. index = array.indexof(ob_type, element)
  923.  
  924. if close < array.get(ob_btm, index) and element == 1
  925. array.remove(ob_top, index)
  926. array.remove(ob_btm, index)
  927. array.remove(ob_left, index)
  928. array.remove(ob_type, index)
  929. bull_ob_break := true
  930.  
  931. else if close > array.get(ob_top, index) and element == -1
  932. array.remove(ob_top, index)
  933. array.remove(ob_btm, index)
  934. array.remove(ob_left, index)
  935. array.remove(ob_type, index)
  936. bear_ob_break := true
  937.  
  938. iob_size = array.size(iob_type)
  939. ob_size = array.size(ob_type)
  940.  
  941. if barstate.isfirst
  942. if show_iob
  943. for i = 0 to iob_showlast-1
  944. array.push(iob_boxes, box.new(na,na,na,na, xloc = xloc.bar_time))
  945. if show_ob
  946. for i = 0 to ob_showlast-1
  947. array.push(ob_boxes, box.new(na,na,na,na, xloc = xloc.bar_time))
  948.  
  949. if iob_size > 0
  950. if barstate.islast
  951. display_ob(iob_boxes, iob_top, iob_btm, iob_left, iob_type, iob_showlast, false, iob_size)
  952.  
  953. if ob_size > 0
  954. if barstate.islast
  955. display_ob(ob_boxes, ob_top, ob_btm, ob_left, ob_type, ob_showlast, true, ob_size)
  956.  
  957.  
  958. //-----------------------------------------------------------------------------}
  959. //Imbalances
  960. //-----------------------------------------------------------------------------{
  961. var bullish_fvg_max = array.new_box(0)
  962. var bullish_fvg_min = array.new_box(0)
  963.  
  964. var bearish_fvg_max = array.new_box(0)
  965. var bearish_fvg_min = array.new_box(0)
  966.  
  967. float bullish_fvg_avg = na
  968. float bearish_fvg_avg = na
  969.  
  970. bullish_fvg_cnd = false
  971. bearish_fvg_cnd = false
  972.  
  973. [src_c1, src_o1, src_h, src_l, src_h2, src_l2] =
  974. request.security(syminfo.tickerid, fvg_tf, get_ohlc())
  975.  
  976. if show_fvg
  977. delta_per = (src_c1 - src_o1) / src_o1 * 100
  978.  
  979. change_tf = timeframe.change(fvg_tf)
  980.  
  981. threshold = fvg_auto ? ta.cum(math.abs(change_tf ? delta_per : 0)) / n * 2
  982. : 0
  983.  
  984. //FVG conditions
  985. bullish_fvg_cnd := src_l > src_h2
  986. and src_c1 > src_h2
  987. and delta_per > threshold
  988. and change_tf
  989.  
  990. bearish_fvg_cnd := src_h < src_l2
  991. and src_c1 < src_l2
  992. and -delta_per > threshold
  993. and change_tf
  994.  
  995. //FVG Areas
  996. if bullish_fvg_cnd
  997. array.unshift(bullish_fvg_max, box.new(n-1, src_l, n + fvg_extend, math.avg(src_l, src_h2)
  998. , border_color = bull_fvg_css
  999. , bgcolor = bull_fvg_css))
  1000.  
  1001. array.unshift(bullish_fvg_min, box.new(n-1, math.avg(src_l, src_h2), n + fvg_extend, src_h2
  1002. , border_color = bull_fvg_css
  1003. , bgcolor = bull_fvg_css))
  1004.  
  1005. if bearish_fvg_cnd
  1006. array.unshift(bearish_fvg_max, box.new(n-1, src_h, n + fvg_extend, math.avg(src_h, src_l2)
  1007. , border_color = bear_fvg_css
  1008. , bgcolor = bear_fvg_css))
  1009.  
  1010. array.unshift(bearish_fvg_min, box.new(n-1, math.avg(src_h, src_l2), n + fvg_extend, src_l2
  1011. , border_color = bear_fvg_css
  1012. , bgcolor = bear_fvg_css))
  1013.  
  1014. for bx in bullish_fvg_min
  1015. if low < box.get_bottom(bx)
  1016. box.delete(bx)
  1017. box.delete(array.get(bullish_fvg_max, array.indexof(bullish_fvg_min, bx)))
  1018.  
  1019. for bx in bearish_fvg_max
  1020. if high > box.get_top(bx)
  1021. box.delete(bx)
  1022. box.delete(array.get(bearish_fvg_min, array.indexof(bearish_fvg_max, bx)))
  1023.  
  1024. //-----------------------------------------------------------------------------}
  1025. //Previous day/week high/lows
  1026. //-----------------------------------------------------------------------------{
  1027. //Daily high/low
  1028. [pdh, pdl] = request.security(syminfo.tickerid, 'D', hl()
  1029. , lookahead = barmerge.lookahead_on)
  1030.  
  1031.  
  1032. //Display Daily
  1033. if show_pdhl
  1034. define_previous_days_prices(pdh, pdl, 'D', pdhl_css)
  1035.  
  1036.  
  1037.  
  1038. //Premium and Discount Zones
  1039.  
  1040. var premium = box.new(na, na, na, na
  1041. , bgcolor = color.new(premium_css, 80)
  1042. , border_color = na)
  1043.  
  1044. var premium_lbl = label.new(na, na
  1045. , text = supply_lbl_txt
  1046. , color = TRANSP_COLOR
  1047. , textcolor = #e43e3ee5
  1048. , style = label.style_label_down
  1049. , size = size.normal)
  1050.  
  1051. var discount = box.new(na, na, na, na
  1052. , bgcolor = color.new(discount_css, 80)
  1053. , border_color = na)
  1054.  
  1055. var discount_lbl = label.new(na, na
  1056. , text = demand_lbl_txt
  1057. , color = TRANSP_COLOR
  1058. , textcolor = #13b667
  1059. , style = label.style_label_up
  1060. , size = size.normal)
  1061.  
  1062. //Show Premium and Discount Areas
  1063. if barstate.islast and show_sd
  1064. avg = math.avg(trail_up, trail_dn)
  1065.  
  1066. box.set_lefttop(premium, math.max(top_x, btm_x), trail_up)
  1067. box.set_rightbottom(premium, n, .95 * trail_up + .05 * trail_dn)
  1068.  
  1069. label.set_xy(premium_lbl, int(math.avg(math.max(top_x, btm_x), n)), trail_up)
  1070.  
  1071. box.set_lefttop(discount, math.max(top_x, btm_x), .95 * trail_dn + .05 * trail_up)
  1072. box.set_rightbottom(discount, n, trail_dn)
  1073. label.set_xy(discount_lbl, int(math.avg(math.max(top_x, btm_x), n)), trail_dn)
  1074.  
  1075. //-----------------------------------------------------------------------------}
  1076. //Trend Definition
  1077. //-----------------------------------------------------------------------------{
  1078. var color trend_css = na
  1079.  
  1080.  
  1081.  
  1082. if style == 'Colored'
  1083. trend_css := itrend == 1 ? bull_css : bear_css
  1084. else if style == 'Monochrome'
  1085. trend_css := itrend == 1 ? #b2b5be : #5d606b
  1086.  
  1087.  
  1088.  
  1089. //-----------------------------------------------------------------------------}
  1090. // Definition of Possible Alerts
  1091. //-----------------------------------------------------------------------------{
  1092.  
  1093. alertcondition(bull_bos_alert, 'Bullish BOS', 'Quebra de Estrutura Bullish')
  1094. alertcondition(bull_choch_alert, 'Bullish CHoCH', 'Bullish CHoCH')
  1095.  
  1096. alertcondition(bear_bos_alert, 'Bearish BOS', 'Quebra de Estrutura Bearish')
  1097. alertcondition(bear_choch_alert, 'Bearish CHoCH', 'Bearish CHoCH')
Comments
  • # text 0.12 KB | 0 0
    1. download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment