xmd79

Swing Points and Liquidity - By Leviathan

Mar 10th, 2023
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 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(false, 'Show Boxes', inline='aa')
  11. showSwingLines = input.bool(true, 'Show Lines', inline='aa')
  12. showBubbles = input.bool(false, 'Show Labels', inline='aa')
  13. showVol = input.bool(false, 'Show Volume', inline='bb')
  14. extendtilfilled = input.bool(true, 'Extend Until Filled', inline='bb')
  15. // Appearance inputs
  16. showhighs = input.bool(true, '', inline='sh', group='Appearance')
  17. showlows = input.bool(true, '', inline='sl', group='Appearance')
  18. sellcol = input.color(#801922, 'Lows (Line - Label - Box)', inline = 'sh', group='Appearance')
  19. buycol = input.color(#66bb6a, 'Highs (Line - Label - Box)', inline='sl', group='Appearance')
  20. sellcolB = input.color(#801922, '', inline='sh', group='Appearance')
  21. buycolB = input.color(#66bb6a, '', inline = 'sl', group='Appearance')
  22. sellboxCol = input.color(#80192231, '', inline = 'sh', group='Appearance')
  23. buyboxCol = input.color(#66bb6a31, '', inline='sl', group='Appearance')
  24. lineStyle = input.string('Dotted', 'Line Style + Width', ['Solid', 'Dashed', 'Dotted'], inline='l', group='Appearance')
  25. lineWid = input.int(1, '', inline='l', group='Appearance')
  26. boxWid = input.float(0.7, 'Box Width + Type ', step=0.1, inline='xx', group='Appearance')
  27. boxStyle = input.string('TYPE 1', '', options=['TYPE 1', 'TYPE 2'], inline='xx', group='Appearance')
  28. labelsize = input.string('Size: Tiny', 'Text Style        ', options = ['Size: Normal','Size: Large', 'Size: Small', 'Size: Tiny', 'Size: Auto' ], inline='txt', group = 'Appearance' )
  29. texthalign = input.string('Right','', options = ['Middle', 'Right', 'Left'], inline='txt', group = 'Appearance')
  30. lookback = input.bool(false, '', inline='lb')
  31. daysBack = input.float(150, 'Lookback (D)               ',inline='lb')
  32.  
  33. // Calculating inRange, used for lookback in days
  34. MSPD = 24 * 60 * 60 * 1000
  35. lastBarDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow), hour(timenow), minute(timenow), second(timenow))
  36. thisBarDate = timestamp(year, month, dayofmonth, hour, minute, second)
  37. daysLeft = math.abs(math.floor((lastBarDate - thisBarDate) / MSPD))
  38. inRange = lookback ? (daysLeft < daysBack) : true
  39.  
  40. //Pivot calculations
  41. int prevHighIndex= na, int prevLowIndex= na, bool highActive= false, bool lowActive= false, bool h= false, bool l= false
  42. pivHi = ta.pivothigh(high, swingSizeL, swingSizeR)
  43. pivLo = ta.pivotlow(low, swingSizeL, swingSizeR)
  44.  
  45. if not na(pivHi)
  46. h := true
  47. prevHighIndex := bar_index - swingSizeR
  48. if not na(pivLo)
  49. l := true
  50. prevLowIndex := bar_index - swingSizeR
  51.  
  52. //Volume, box width
  53. vol = volume[swingSizeR]
  54. color CLEAR = color.rgb(0,0,0,100)
  55. boxWid1 = 0.001 * boxWid
  56.  
  57. // Styles
  58. boxStyle(x) =>
  59. switch x
  60. 'TYPE 1' => h ? pivHi : l ? pivLo : na
  61. 'TYPE 2' => h ? pivHi * (1 - boxWid1) : l ? pivLo * (1 + boxWid1) : na
  62. lineStyle(x) =>
  63. switch x
  64. 'Solid' => line.style_solid
  65. 'Dashed' => line.style_dashed
  66. 'Dotted' => line.style_dotted
  67. switchtextsize(textsize) =>
  68. switch textsize
  69. 'Size: Normal' => size.normal
  70. 'Size: Small' => size.small
  71. 'Size: Tiny' => size.tiny
  72. 'Size: Auto' => size.auto
  73. 'Size: Large' => size.large
  74. switchhalign(texthalign) =>
  75. switch texthalign
  76. 'Middle' => text.align_center
  77. 'Right' => text.align_right
  78. 'Left' => text.align_left
  79.  
  80. //Swing level labels
  81. var levelBoxes = array.new_box(), var levelLines = array.new_line()
  82. if h and inRange and showhighs
  83. 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,text_halign=switchhalign(texthalign),text_valign=text.align_center,text_color=chart.fg_color, text_size=switchtextsize(labelsize))
  84. hLine = line.new(prevHighIndex, pivHi, bar_index, pivHi, color = showSwingLines ? sellcol : CLEAR, style=lineStyle(lineStyle), width=lineWid)
  85. array.push(levelBoxes, hBox)
  86. array.push(levelLines, hLine)
  87. if l and inRange and showhighs
  88. 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,text_halign=switchhalign(texthalign),text_valign=text.align_center,text_color=chart.fg_color, text_size=switchtextsize(labelsize))
  89. lLine = line.new(prevLowIndex, pivLo, bar_index, pivLo, color = showSwingLines ? buycol : CLEAR, style=lineStyle(lineStyle), width=lineWid)
  90. array.push(levelBoxes, lBox)
  91. array.push(levelLines, lLine)
  92.  
  93. // Looping over the full array of lines and updating them, and deleting them if they have been touched
  94. size = array.size(levelBoxes)
  95. if size > 0
  96. for i = 0 to size - 1
  97. j = size - 1 - i
  98. box = array.get(levelBoxes, j)
  99. line = array.get(levelLines, j)
  100. level = line.get_y2(line)
  101. filled = (high >= level and low <= level)
  102.  
  103. if filled and extendtilfilled
  104. array.remove(levelLines, j)
  105. array.remove(levelBoxes, j)
  106. continue
  107.  
  108. box.set_right(box, bar_index+1)
  109. line.set_x2(line, bar_index+1)
  110.  
  111. if not filled and not extendtilfilled
  112. array.remove(levelLines, j)
  113. array.remove(levelBoxes, j)
  114. continue
  115. box.set_right(box, bar_index[0]+4)
  116. line.set_x2(line, bar_index[0]+4)
  117.  
  118.  
  119. // Deleting the oldest lines if array is too big
  120. if array.size(levelBoxes) >= 500
  121. int i = 0
  122. while array.size(levelBoxes) >= 500
  123. box = array.get(levelBoxes, i)
  124. line = array.get(levelLines, i)
  125. box.delete(box)
  126. line.delete(line)
  127. array.remove(levelBoxes, i)
  128. array.remove(levelLines, i)
  129. i += 1
  130.  
  131. // Plotting circle labels
  132. plotshape(showhighs and showBubbles and h ? high[swingSizeR] : na, style=shape.circle, location = location.absolute, offset = -swingSizeR, color=sellcolB, size = size.tiny)
  133. plotshape(showlows and showBubbles and l ? 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