Advertisement
xmd79

Gann Square of 9

Feb 18th, 2023
158
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 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. // © ThiagoSchmitz
  3.  
  4. //@version=5
  5. indicator("Gann Square of 9", overlay=true, max_lines_count = 500, max_labels_count = 500)
  6.  
  7. type tableCells
  8. int column
  9. int row
  10.  
  11.  
  12. // ╔────────────────────────────────. ■ .──────────────────────────────────────▼
  13. // ║ ⇌ • Inputs • ⇋
  14. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈□┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  15. var int levels = input.int(3, "Levels", group="calculation")
  16. var float starting = input.float(15500.0, "Starting Number", group="calculation")
  17. var float increase = input.float(500.0, "Increasing Number", group="calculation")
  18. var string tablePosition = input.string("Top", "Square Position", ["Top", "Middle", "Bottom"], group="table")
  19. var float width = input.float(3.0, "Cell Width %", group="table")
  20. var float height = input.float(4.0, "Cell Height %", group="table")
  21. var int frameWidth = input.int(2, "Frame Width", group="table")
  22. var color backgroundColor = input.color(color.new(color.black, 70), "Background Color", inline="color", group="table")
  23. var bool showClosestValue = input.bool(true, "Show the closest value with the actual price", group="table")
  24. var color frameColor = input.color(color.new(color.white, 40), "Frame Color", inline="color", group="table")
  25. var color tableTextColor = input.color(color.new(color.white, 20), "Text Color", inline="color", group="table")
  26. var bool showDiagonal = input.bool(true, "Diagonals", group="diagonals", inline="diagonals")
  27. var bool showDiagonalLevels = input.bool(true, "Levels", group="diagonals", inline="diagonals")
  28. var bool showDiagonalLabels = input.bool(true, "Labels", group="diagonals", inline="diagonals")
  29. var int diagonalLevelsShift = input.int(100, "Diagonal Levels Shift", minval = 1, maxval = 500, group="diagonals")
  30. var int diagonalLabelShift = input.int(30, "Diagonal Levels Label Shift", minval = 1, maxval = 200, group="diagonals")
  31. var color diagonalColor = input.color(color.new(color.white, 80), "Diagonal color", inline="diagonal", group="diagonals")
  32. var color linesColor = input.color(color.new(color.white, 40), "Diagonal Levels Color", inline="diagonal", group="diagonals")
  33. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  34. // ║ ⇌ • Inputs • ⇋
  35. // ╚─────────────────────────────────...───────────────────────────────────────▲
  36.  
  37. // ╔────────────────────────────────. ■ .──────────────────────────────────────▼
  38. // ║ ⇌ • Variables • ⇋
  39. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈□┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  40. var string position = switch tablePosition
  41. "Top" => position.top_right
  42. "Middle" => position.middle_right
  43. => position.bottom_right
  44. var int size = levels * 2 + 1
  45. var table tab = table.new(position, size, size, backgroundColor, frameColor, frameWidth, frameColor, frameWidth)
  46. var tableCells[] coordinates = array.new<tableCells>()
  47. var float[] values = array.new_float()
  48. var float[] diagonalValues = array.new_float()
  49. var line[] lines = array.new_line()
  50. var label[] labels = array.new_label()
  51. var int count = 1
  52. var int layer = 2
  53. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  54. // ║ ⇌ • Variables • ⇋
  55. // ╚─────────────────────────────────...───────────────────────────────────────▲
  56.  
  57. // ╔────────────────────────────────. ■ .──────────────────────────────────────▼
  58. // ║ ⇌ • Functions • ⇋
  59. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈□┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  60. createCell(int c, int r, float v, color bgcolor=na) =>
  61. table.cell(tab, c, r, str.tostring(v), width, height, tableTextColor, text_size=size.small, bgcolor=bgcolor)
  62.  
  63. isDiagonal(int c, int l) => c % l == 1
  64.  
  65. getDiagonalColor() => showDiagonal ? diagonalColor : na
  66.  
  67. createCellWithValueAndColor(int column, int row, float value, color col) =>
  68. createCell(column, row, value, col)
  69. pivot = tableCells.new(column, row)
  70. array.push(coordinates, pivot)
  71. array.push(values, value)
  72. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  73. // ║ ⇌ • Functions • ⇋
  74. // ╚─────────────────────────────────...───────────────────────────────────────▲
  75.  
  76. // ╔────────────────────────────────. ■ .──────────────────────────────────────▼
  77. // ║ ⇌ • Build Square • ⇋
  78. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈□┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  79. if barstate.isfirst
  80. int currentColumn = levels
  81. int currentRow = levels
  82. int doubleLevels = levels * 2
  83. float startingValue = starting
  84.  
  85. createCell(currentColumn, currentRow, starting, diagonalColor)
  86. array.push(diagonalValues, startingValue)
  87.  
  88. for i = 1 to doubleLevels by 1
  89. int sign = i % 2 == 1 ? -1 : 1
  90. int limit = i == 1 ? i / 2 : i
  91.  
  92. for j = 0 to i * 2 - 1 by 1
  93. startingValue := startingValue + increase
  94. count := count + 1
  95. layer := math.sqrt(count) == layer + 1 ? layer + 2 : layer
  96. isDiagonal = isDiagonal(count, layer)
  97. col = isDiagonal ? getDiagonalColor() : na
  98. currentColumn := j < limit ? currentColumn + sign : currentColumn
  99. currentRow := j >= limit ? currentRow + sign : currentRow
  100. createCellWithValueAndColor(currentColumn, currentRow, startingValue, col)
  101. if isDiagonal
  102. array.push(diagonalValues, startingValue)
  103.  
  104. if i == doubleLevels
  105. for j = 0 to i - 1 by 1
  106. startingValue := startingValue + increase
  107. count := count + 1
  108. isDiagonal = isDiagonal(count, layer)
  109. col = isDiagonal ? getDiagonalColor() : na
  110. currentColumn := currentColumn - sign
  111. createCellWithValueAndColor(currentColumn, currentRow, startingValue, col)
  112. if isDiagonal
  113. array.push(diagonalValues, startingValue)
  114. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  115. // ║ ⇌ • Build Square • ⇋
  116. // ╚─────────────────────────────────...───────────────────────────────────────▲
  117.  
  118. // ╔────────────────────────────────. ■ .──────────────────────────────────────▼
  119. // ║ ⇌ • Update • ⇋
  120. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈□┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  121. if barstate.islast
  122. index = array.binary_search_leftmost(values, close)
  123. if index > 0
  124. coord = array.get(coordinates, index)
  125. table.cell_set_text_color(tab, coord.column, coord.row, color.red)
  126. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  127. // ║ ⇌ • Update • ⇋
  128. // ╚─────────────────────────────────...───────────────────────────────────────▲
  129.  
  130. // ╔────────────────────────────────. ■ .──────────────────────────────────────▼
  131. // ║ ⇌ • Build and Update Lines/Labels • ⇋
  132. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈□┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  133. if showDiagonalLevels
  134. if array.size(lines) == 0 and array.size(diagonalValues) > 0
  135. for i = 0 to array.size(diagonalValues) - 1 by 1
  136. float value = array.get(diagonalValues, i)
  137. array.push(lines, line.new(bar_index, value, bar_index, value, color=linesColor, extend=extend.right, style=line.style_dotted))
  138. array.push(labels, label.new(bar_index, value, str.tostring(value), yloc=yloc.price, textcolor=linesColor, style=label.style_label_left, color=na))
  139. else
  140. for i = 0 to array.size(lines) - 1 by 1
  141. line lin = array.get(lines, i)
  142. label lab = array.get(labels, i)
  143. line.set_x1(lin, bar_index - diagonalLevelsShift)
  144. line.set_x2(lin, bar_index)
  145. label.set_x(lab, bar_index + diagonalLabelShift)
  146. // ║┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
  147. // ║ ⇌ • Build and Update Lines/Labels • ⇋
  148. // ╚─────────────────────────────────...───────────────────────────────────────▲
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement