xmd79

Visible Range

Dec 5th, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 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. // © kaigouthro
  3.  
  4. import kaigouthro/hsvColor/11 as h
  5. import kaigouthro/calc/5
  6. import kaigouthro/font/4
  7.  
  8. //@version=5
  9. indicator("Visible Range",'Range View',true)
  10.  
  11. //input distances
  12. var int _d1 = input.int(5, 'Bar Distance for Viewing ', 5,50,5)
  13. var int _d2 = input.int(20, 'Second Bar Distance Mult.', 20,50,1) * _d1
  14.  
  15. // set line
  16. var line _highvisible = na
  17. var line _current = na
  18. var line _lowvisible = na
  19. var line _rangeline = na
  20. var line _highdist = na
  21. var line _lowdist = na
  22. var line _midpoint = na
  23. var label _rangeline_lb = na
  24. var label _highdist_lb = na
  25. var label _lowdist_lb = na
  26. var linefill _midfill = na
  27.  
  28. // create lines to fill
  29. if barstate.isfirst
  30. _highvisible := line.new (chart.left_visible_bar_time,close,chart.right_visible_bar_time,close, xloc.bar_time ,extend.left, #88888888,line.style_dotted,1)
  31. _lowvisible := line.new (chart.left_visible_bar_time,close,chart.right_visible_bar_time,close, xloc.bar_time ,extend.left, #88888888,line.style_dotted,1)
  32. _current := line.new (last_bar_index[10],close,last_bar_index,close , xloc.bar_index ,extend.right, #88888888,line.style_dotted,1)
  33. _midpoint := line.new (last_bar_index[10],close,last_bar_index,close , xloc.bar_index ,extend.none, #88888888,line.style_dotted,1)
  34. _rangeline := line.new (last_bar_index[10],close,last_bar_index,close )
  35. _highdist := line.new (last_bar_index[10],close,last_bar_index,close )
  36. _lowdist := line.new (last_bar_index[10],close,last_bar_index,close )
  37. _rangeline_lb := label.new (last_bar_index ,close, '',size = size.normal )
  38. _highdist_lb := label.new (last_bar_index ,close, '',size = size.small )
  39. _lowdist_lb := label.new (last_bar_index ,close, '',size = size.small )
  40. _midfill := linefill.new(_current,_midpoint,#00000000)
  41.  
  42. // force update value each calc
  43. var _newalc = -1
  44. _newalc *= -1
  45.  
  46. //get and set values
  47. if ta.change(_newalc) and (time > chart.left_visible_bar_time)
  48. varip _highest = close
  49. varip _lowest = close
  50. _lefttime = chart.left_visible_bar_time
  51. _righttime = chart.right_visible_bar_time
  52. _close = close
  53. _highest := time >= _lefttime ? math.max(_highest , high ) : close
  54. _lowest := time >= _lefttime ? math.min(_lowest , low ) : close
  55. _avg = math.avg ( _highest , _lowest )
  56. line.set_xy2 ( _current , last_bar_index - _d1 , _close )
  57. line.set_xy1 ( _current , last_bar_index - _d2 , _close )
  58. line.set_xy2 ( _midpoint , last_bar_index - _d1 , _avg )
  59. line.set_xy1 ( _midpoint , last_bar_index - _d2 , _avg )
  60. line.set_xy1 ( _highvisible , _lefttime , _highest )
  61. line.set_xy2 ( _highvisible , _righttime , _highest )
  62. line.set_xy1 ( _lowvisible , _lefttime , _lowest )
  63. line.set_xy2 ( _lowvisible , _righttime , _lowest )
  64. line.set_xy1 ( _rangeline , last_bar_index - _d2 , _highest )
  65. line.set_xy2 ( _rangeline , last_bar_index - _d2 , _lowest )
  66. line.set_xy1 ( _highdist , last_bar_index - _d1 , _close )
  67. line.set_xy2 ( _highdist , last_bar_index - _d1 , _highest )
  68. line.set_xy1 ( _lowdist , last_bar_index - _d1 , _close )
  69. line.set_xy2 ( _lowdist , last_bar_index - _d1 , _lowest )
  70. label.set_xy ( _lowdist_lb , last_bar_index - _d1 , math.avg(_close, _lowest ))
  71. _range = (_highest / _lowest - 1 )
  72. _lowdif = (_close / _lowest - 1 )
  73. _highdif = (_close / _highest - 1 )
  74. _colrangeline = h.hsv(calc.percentOfDistance(_close - _avg , _lowest - _avg , _highest - _avg ) * 120 , .8,1,1)
  75. _collowdist = h.hsv(calc.percentOfDistance(_close , _lowest , _highest ) * 120 , .8,1,1)
  76. _colhighdist = h.hsv(calc.percentOfDistance(_close , _highest , _lowest ) * 120 , .8,1,1)
  77. linefill.set_color( _midfill, color.new(_avg > _close ? _colhighdist : _collowdist,90))
  78. label.set_text ( _lowdist_lb , font.uni(str.format('{0,number,#.##} % ', 100 * (_close / _lowest - 1 ) ), 15))
  79. label.set_color ( _lowdist_lb , _collowdist )
  80. line.set_color ( _lowdist , _collowdist )
  81. label.set_xy ( _rangeline_lb , last_bar_index - _d2 , math.avg(_highest, _lowest ))
  82. label.set_text ( _rangeline_lb , font.uni(str.format('{0,number,#.##} % from avg \n {1,number,#.##} % Range', 100 * (_close / _avg - 1 ), 100 * (_highest / _lowest - 1 ) ), 15))
  83. label.set_color ( _rangeline_lb , _colrangeline )
  84. line.set_color ( _rangeline , _colrangeline )
  85. label.set_xy ( _highdist_lb , last_bar_index - _d1 , math.avg(_close, _highest ))
  86. label.set_text ( _highdist_lb , font.uni(str.format('{0,number,#.##} %', 100 * (_close / _highest - 1 ) ), 15))
  87. label.set_color ( _highdist_lb , _colhighdist )
  88. line.set_color ( _highdist , _colhighdist )
  89.  
Advertisement
Add Comment
Please, Sign In to add comment