xmd79

Balance Zone Extension

Apr 7th, 2023
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 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. // © mcthatsme
  3.  
  4. //@version=5
  5. indicator("Balance Zone Extension", overlay=true, max_lines_count=500)
  6.  
  7. //Inputs
  8. balance_High = input.float (defval=3900, title="Top of Balance Zone")
  9. balance_Low = input.float (defval=3790, title="Bottom of Balance Zone")
  10. numLines = input.string(defval="8" , title="Number of Sections per Balance Zone", options=["2", "4", "8", "16", "32"], tooltip="How many times the box is halved. Warning: Max line limit is 500")
  11. num_Boxes_Above = input.int (defval=2 , title="Number of Boxes Above Balance Zone")
  12. num_Boxes_Below = input.int (defval=2 , title="Number of Boxes Below Balance Zone")
  13.  
  14. //Balance Zone Colors
  15. balance_zone_color = input.color(defval=color.new(color.gray, 90) , title="Balance Zone Background Color", group="Balance Zone Colors")
  16. even_zone_color = input.color(defval=color.new(color.blue, 90) , title="Even Zone Color" , group="Balance Zone Colors")
  17. odd_zone_color = input.color(defval=color.new(color.yellow, 90), title="Odd Zone Color" , group="Balance Zone Colors")
  18.  
  19. HL_lineColor = input.color (defval=color.new(color.white, 50), title="Balance High/Low Line Color" , group="Line Colors")
  20. HL_linewidth = input.int (defval=2 , title="Balance High/Low Line Thickness", group="Line Colors")
  21. HL_styleOption = input.string( defval="solid (─)" , title='Balance High/Low Line Style' , group="Line Colors", options=["solid (─)", "dashed (╌)", "dotted (┈)"])
  22. HL_lineStyle = (HL_styleOption == "dotted (┈)") ? line.style_dotted :
  23. (HL_styleOption == "dashed (╌)") ? line.style_dashed :
  24. line.style_solid
  25.  
  26. Half_lineColor = input.color (defval=color.new(color.yellow, 50), title="50% Line Color" , group="Line Colors")
  27. Half_linewidth = input.int (defval=1 , title="50% Line Thickness", group="Line Colors")
  28. Half_styleOption = input.string(defval="dashed (╌)" , title='50% Line Style' , group="Line Colors", options=["solid (─)", "dashed (╌)", "dotted (┈)"])
  29. Half_lineStyle = (Half_styleOption == "dotted (┈)") ? line.style_dotted :
  30. (Half_styleOption == "dashed (╌)") ? line.style_dashed :
  31. line.style_solid
  32.  
  33. Bal_lineColor = input.color (defval=color.new(color.blue, 50), title="Other Balance Line Color" , group="Line Colors")
  34. Bal_linewidth = input.int (defval=1 , title="Other Balance Line Thickness", group="Line Colors")
  35. Bal_styleOption = input.string(defval="dotted (┈)" , title='Other Balance Line Style' , group="Line Colors", options=["solid (─)", "dashed (╌)", "dotted (┈)"])
  36. Bal_lineStyle = (Bal_styleOption == "dotted (┈)") ? line.style_dotted :
  37. (Bal_styleOption == "dashed (╌)") ? line.style_dashed :
  38. line.style_solid
  39.  
  40. lines_Per_Box = str.tonumber(numLines)
  41. sectionSize = 1/lines_Per_Box
  42. balance_Vals = array.new_float(0)
  43.  
  44. var BALANCE_HIGH_LINES = array.new_line(0)
  45. var BALANCE_LOW_LINES = array.new_line(0)
  46. var BALANCE_50_LINES = array.new_line(0)
  47. var BALANCE_LINES = array.new_line(0)
  48. var LINE_FILLS = array.new_linefill(0)
  49.  
  50. //Value Calculations
  51. balance_Range = balance_High - balance_Low
  52. balance_50 = balance_Range / 2
  53.  
  54. if lines_Per_Box > 1
  55. for i = 1 to lines_Per_Box - 1
  56. if sectionSize * i != 0.5
  57. array.push(balance_Vals, balance_Low + balance_Range * (i * 1/lines_Per_Box))
  58.  
  59. //Delete Lines before creating new ones
  60. if array.size(BALANCE_HIGH_LINES) > 0
  61. sizeArrHigh = array.size(BALANCE_HIGH_LINES) - 1
  62.  
  63. for i = sizeArrHigh to 0
  64. line.delete(array.get(BALANCE_HIGH_LINES, i))
  65. array.remove(BALANCE_HIGH_LINES, i)
  66.  
  67. if array.size(BALANCE_LOW_LINES) > 0
  68. sizeArrLow = array.size(BALANCE_LOW_LINES) - 1
  69.  
  70. for i = sizeArrLow to 0
  71. line.delete(array.get(BALANCE_LOW_LINES, i))
  72. array.remove(BALANCE_LOW_LINES, i)
  73.  
  74. if array.size(BALANCE_LINES) > 0
  75. sizeArrBal = array.size(BALANCE_LINES) - 1
  76.  
  77. for i = sizeArrBal to 0
  78. line.delete(array.get(BALANCE_LINES, i))
  79. array.remove(BALANCE_LINES, i)
  80.  
  81. if array.size(BALANCE_50_LINES) > 0
  82. sizeArr50 = array.size(BALANCE_50_LINES) - 1
  83.  
  84. for i = sizeArr50 to 0
  85. line.delete(array.get(BALANCE_50_LINES, i))
  86. array.remove(BALANCE_50_LINES, i)
  87.  
  88. if array.size(LINE_FILLS) > 0
  89. sizeArrLineFill = array.size(LINE_FILLS) - 1
  90.  
  91. for i = sizeArrLineFill to 0
  92. linefill.delete(array.get(LINE_FILLS, i))
  93. array.remove(LINE_FILLS, i)
  94.  
  95. //Add lines to chart based on user inputs
  96. //Original Balance Zone lines
  97. array.push(BALANCE_HIGH_LINES, line.new(bar_index, balance_High, bar_index[1], balance_High, color=HL_lineColor, style=HL_lineStyle,width=HL_linewidth, extend=extend.both))
  98. array.push(BALANCE_LOW_LINES, line.new(bar_index, balance_Low , bar_index[1], balance_Low, color=HL_lineColor, style=HL_lineStyle,width=HL_linewidth, extend=extend.both))
  99. array.push(BALANCE_50_LINES, line.new(bar_index, balance_Low + balance_50, bar_index[1], balance_Low + balance_50, color=Half_lineColor, style=Half_lineStyle, width=Half_linewidth, extend=extend.both))
  100.  
  101. if lines_Per_Box > 2
  102. for j = 0 to array.size(balance_Vals) - 1
  103. array.push(BALANCE_LINES , line.new(bar_index, array.get(balance_Vals, j), bar_index[1], array.get(balance_Vals, j), color=Bal_lineColor, style=Bal_lineStyle,width=Bal_linewidth, extend=extend.both))
  104.  
  105. //Balance zone color fill
  106. array.push(LINE_FILLS, linefill.new(array.get(BALANCE_HIGH_LINES, 0), array.get(BALANCE_LOW_LINES, 0), balance_zone_color))
  107.  
  108. //For loops to cycle through balance zone extensions
  109. if num_Boxes_Above > 0
  110. for i = 1 to num_Boxes_Above
  111. array.push(BALANCE_HIGH_LINES, line.new(bar_index, balance_High + balance_Range * i, bar_index[1], balance_High + balance_Range * i, color=HL_lineColor, style=HL_lineStyle,width=HL_linewidth, extend=extend.both))
  112. array.push(BALANCE_50_LINES, line.new(bar_index, balance_Low + (balance_Range * i + balance_50), bar_index[1], balance_Low + (balance_Range * i + balance_50), color=Half_lineColor, style=Half_lineStyle, width=Half_linewidth, extend=extend.both))
  113.  
  114. if lines_Per_Box > 2
  115. for j = 0 to array.size(balance_Vals) - 1
  116. array.push(BALANCE_LINES, line.new(bar_index, array.get(balance_Vals, j) + balance_Range * i, bar_index[1], array.get(balance_Vals, j) + balance_Range * i, color=Bal_lineColor, style=Bal_lineStyle,width=Bal_linewidth, extend=extend.both))
  117.  
  118. if i % 2 == 1
  119. array.push(LINE_FILLS, linefill.new(array.get(BALANCE_HIGH_LINES, i - 1), array.get(BALANCE_HIGH_LINES, i), odd_zone_color))
  120. else
  121. array.push(LINE_FILLS, linefill.new(array.get(BALANCE_HIGH_LINES, i - 1), array.get(BALANCE_HIGH_LINES, i), even_zone_color))
  122.  
  123. if num_Boxes_Below > 0
  124. for i = 1 to num_Boxes_Below
  125. array.push(BALANCE_LOW_LINES, line.new(bar_index, balance_Low - balance_Range * i, bar_index[1], balance_Low - balance_Range * i, color=HL_lineColor, style=HL_lineStyle,width=HL_linewidth, extend=extend.both))
  126. array.push(BALANCE_50_LINES, line.new(bar_index, balance_Low - (balance_Range * i - balance_50), bar_index[1], balance_Low - (balance_Range * i - balance_50), color=Half_lineColor, style=Half_lineStyle, width=Half_linewidth, extend=extend.both))
  127.  
  128. if lines_Per_Box > 2
  129. for j = 0 to array.size(balance_Vals) - 1
  130. array.push(BALANCE_LINES, line.new(bar_index, array.get(balance_Vals, j) - balance_Range * i, bar_index[1], array.get(balance_Vals, j) - balance_Range * i, color=Bal_lineColor, style=Bal_lineStyle,width=Bal_linewidth, extend=extend.both))
  131.  
  132. if i % 2 == 1
  133. array.push(LINE_FILLS, linefill.new(array.get(BALANCE_LOW_LINES, i - 1), array.get(BALANCE_LOW_LINES, i), odd_zone_color))
  134. else
  135. array.push(LINE_FILLS, linefill.new(array.get(BALANCE_LOW_LINES, i - 1), array.get(BALANCE_LOW_LINES, i), even_zone_color))
  136.  
Advertisement
Add Comment
Please, Sign In to add comment