Advertisement
xmd79

Swing Points and Liquidity - By Leviathan

Dec 14th, 2023
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © LeviathanCapital
  3.  
  4. //@version=5
  5. indicator("Swing Points and Liquidity - By Leviathan", overlay=true, max_boxes_count=500, max_lines_count=500, max_labels_count = 500)
  6.  
  7. // Inputs
  8. swingSizeR = input.int(10, 'Bars Right-Left', inline='brl')
  9. swingSizeL = input.int(15, '-', inline='brl')
  10. showBoxes = input.bool(true, 'Show Boxes ', inline='aa')
  11. showSwingLines = input.bool(true, 'Show Lines', inline='aa')
  12. showBubbles = input.bool(true, 'Show Labels ', inline='bb')
  13. showVol = input.bool(false, 'Show Volume', inline='bb')
  14. showOId = input.bool(false, 'Show OI Δ   ', inline='cc')
  15. extendtilfilled = input.bool(true, 'Extend Until Fill', inline='cc')
  16.  
  17. // Conditions
  18. hidefilled = input.bool(false, 'Hide Filled', group='Conditions')
  19. voltresh = input.int(0, 'Volume >', group='Conditions')
  20. oitresh = input.int(0, 'OI Δ (abs.) >', group='Conditions')
  21. pnoid = input.string('/', 'Only Swings With', options = ['Positive OI Delta', 'Negative OI Delta', '/'], group='Conditions')
  22. // Appearance inputs
  23. showhighs = input.bool(true, '', inline='sh', group='Appearance')
  24. showlows = input.bool(true, '', inline='sl', group='Appearance')
  25. sellcol = input.color(#aa2430, 'Lows (Line - Label - Box)', inline = 'sh', group='Appearance')
  26. buycol = input.color(#66bb6a, 'Highs (Line - Label - Box)', inline='sl', group='Appearance')
  27. sellcolB = input.color(#aa2430, '', inline='sh', group='Appearance')
  28. buycolB = input.color(#66bb6a, '', inline = 'sl', group='Appearance')
  29. sellboxCol = input.color(#80192231, '', inline = 'sh', group='Appearance')
  30. buyboxCol = input.color(#66bb6a31, '', inline='sl', group='Appearance')
  31. lineStyle = input.string('Dotted', 'Line Style + Width', ['Solid', 'Dashed', 'Dotted'], inline='l', group='Appearance')
  32. lineWid = input.int(1, '', inline='l', group='Appearance')
  33. boxWid = input.float(0.7, 'Box Width + Type ', step=0.1, inline='xx', group='Appearance')
  34. boxStyle = input.string('TYPE 1', '', options=['TYPE 1', 'TYPE 2'], inline='xx', group='Appearance')
  35. labelsize = input.string('Size: Tiny', 'Text Style        ', options = ['Size: Normal','Size: Large', 'Size: Small', 'Size: Tiny', 'Size: Auto' ], inline='txt', group = 'Appearance' )
  36. texthalign = input.string('Right','', options = ['Middle', 'Right', 'Left'], inline='txt', group = 'Appearance')
  37. lookback = input.bool(false, '', inline='lb')
  38. daysBack = input.float(150, 'Lookback (D)               ',inline='lb')
  39. // OI Data
  40. binance = input.bool(true, 'Binance USDT.P', inline = 'src', group = 'Open Interest')
  41. binance2 = input.bool(true, 'Binance USD.P', inline = 'src', group = 'Open Interest')
  42. binance3 = input.bool(true, 'Binance BUSD.P', inline = 'src2', group = 'Open Interest')
  43. bitmex = input.bool(true, 'BitMEX USD.P', inline = 'src2', group = 'Open Interest')
  44. bitmex2 = input.bool(true, 'BitMEX USDT.P ', inline = 'src3', group = 'Open Interest')
  45. kraken = input.bool(true, 'Kraken USD.P', inline = 'src3', group = 'Open Interest')
  46.  
  47. // Calculating inRange, used for lookback in days
  48. MSPD = 24 * 60 * 60 * 1000
  49. lastBarDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), hour(timenow), minute(timenow), second(timenow))
  50. thisBarDate = timestamp(year, month, dayofmonth, hour, minute, second)
  51. daysLeft = math.abs(math.floor((lastBarDate - thisBarDate) / MSPD))
  52. inRange = lookback ? (daysLeft < daysBack) : true
  53.  
  54. //Pivot calculations
  55. int prevHighIndex= na, int prevLowIndex= na, bool highActive= false, bool lowActive= false, bool h= false, bool l= false
  56. pivHi = ta.pivothigh(high, swingSizeL, swingSizeR)
  57. pivLo = ta.pivotlow(low, swingSizeL, swingSizeR)
  58.  
  59. if not na(pivHi)
  60. h := true
  61. prevHighIndex := bar_index - swingSizeR
  62. if not na(pivLo)
  63. l := true
  64. prevLowIndex := bar_index - swingSizeR
  65.  
  66. // Getting OI data
  67. mex = syminfo.basecurrency=='BTC' ? 'XBT' : string(syminfo.basecurrency)
  68. oid1 = nz(request.security('BINANCE' + ":" + string(syminfo.basecurrency) + 'USDT.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  69. oid2 = nz(request.security('BINANCE' + ":" + string(syminfo.basecurrency) + 'USD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  70. oid3 = nz(request.security('BINANCE' + ":" + string(syminfo.basecurrency) + 'BUSD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  71. oid4 = nz(request.security('BITMEX' + ":" + mex + 'USD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  72. oid5 = nz(request.security('BITMEX' + ":" + mex + 'USDT.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  73. oid6 = nz(request.security('KRAKEN' + ":" + string(syminfo.basecurrency) + 'USD.P_OI', timeframe.period, close-close[1], ignore_invalid_symbol = true), 0)
  74.  
  75. 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)
  76.  
  77.  
  78.  
  79.  
  80. //Volume, OI, box width
  81. vol = volume[swingSizeR]
  82.  
  83. oitreshcond = oitresh > 0 ? math.abs(deltaOI[swingSizeR])>oitresh : true
  84. voltreshcond = voltresh > 0 ? vol > voltresh : true
  85. oicond = pnoid=='Positive OI Delta' ? deltaOI[swingSizeR]>0 : pnoid=='Negative OI Delta' ? deltaOI[swingSizeR]<0 : true
  86.  
  87. color CLEAR = color.rgb(0,0,0,100)
  88. boxWid1 = 0.001 * boxWid
  89.  
  90.  
  91. // Styles
  92. boxStyle(x) =>
  93. switch x
  94. 'TYPE 1' => h ? pivHi : l ? pivLo : na
  95. 'TYPE 2' => h ? pivHi * (1 - boxWid1) : l ? pivLo * (1 + boxWid1) : na
  96. lineStyle(x) =>
  97. switch x
  98. 'Solid' => line.style_solid
  99. 'Dashed' => line.style_dashed
  100. 'Dotted' => line.style_dotted
  101. switchtextsize(textsize) =>
  102. switch textsize
  103. 'Size: Normal' => size.normal
  104. 'Size: Small' => size.small
  105. 'Size: Tiny' => size.tiny
  106. 'Size: Auto' => size.auto
  107. 'Size: Large' => size.large
  108. switchhalign(texthalign) =>
  109. switch texthalign
  110. 'Middle' => text.align_center
  111. 'Right' => text.align_right
  112. 'Left' => text.align_left
  113.  
  114. //Swing level labels
  115. var levelBoxes = array.new_box(), var levelLines = array.new_line()
  116. if h and inRange and showhighs and oitreshcond and voltreshcond and oicond
  117. 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))
  118. hLine = line.new(prevHighIndex, pivHi, bar_index, pivHi, color = showSwingLines ? sellcol : CLEAR, style=lineStyle(lineStyle), width=lineWid)
  119. array.push(levelBoxes, hBox)
  120. array.push(levelLines, hLine)
  121. if l and inRange and showhighs and oitreshcond and voltreshcond and oicond
  122. 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))
  123. lLine = line.new(prevLowIndex, pivLo, bar_index, pivLo, color = showSwingLines ? buycol : CLEAR, style=lineStyle(lineStyle), width=lineWid)
  124. array.push(levelBoxes, lBox)
  125. array.push(levelLines, lLine)
  126.  
  127. // Looping over the full array of lines and updating them, and deleting them if they have been touched
  128. size = array.size(levelBoxes)
  129. if size > 0
  130. for i = 0 to size - 1
  131. j = size - 1 - i
  132. box = array.get(levelBoxes, j)
  133. line = array.get(levelLines, j)
  134. level = line.get_y2(line)
  135. filled = (high >= level and low <= level)
  136.  
  137. if filled and extendtilfilled and not hidefilled
  138. array.remove(levelLines, j)
  139. array.remove(levelBoxes, j)
  140. continue
  141.  
  142. box.set_right(box, bar_index+1)
  143. line.set_x2(line, bar_index+1)
  144. if filled and hidefilled
  145. array.remove(levelLines, j)
  146. array.remove(levelBoxes, j)
  147. line.delete(line)
  148. box.delete(box)
  149.  
  150. if not filled and not extendtilfilled
  151. array.remove(levelLines, j)
  152. array.remove(levelBoxes, j)
  153. continue
  154. box.set_right(box, bar_index[0]+4)
  155. line.set_x2(line, bar_index[0]+4)
  156.  
  157.  
  158. // Deleting the oldest lines if array is too big
  159. if array.size(levelBoxes) >= 500
  160. int i = 0
  161. while array.size(levelBoxes) >= 500
  162. box = array.get(levelBoxes, i)
  163. line = array.get(levelLines, i)
  164. box.delete(box)
  165. line.delete(line)
  166. array.remove(levelBoxes, i)
  167. array.remove(levelLines, i)
  168. i += 1
  169.  
  170. // Plotting circle labels
  171. 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)
  172. 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement