xmd79

FunctionLinearRegression

Oct 12th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 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. // © RicardoSantos
  3.  
  4. //@version=5
  5.  
  6. // @description Method for Linear Regression using array sample points.
  7. library(title='FunctionLinearRegression', overlay=true)
  8.  
  9. // @function Performs Linear Regression over the provided sample points.
  10. // @param sample_x float array, sample points X value.
  11. // @param sample_y float array, sample points Y value.
  12. // @returns tuple with:
  13. // _predictions: Array with adjusted Y values.
  14. // _max_dev: Max deviation from the mean.
  15. // _min_dev: Min deviation from the mean.
  16. // _stdev/_sizeX: Average deviation from the mean.
  17. export linreg (float[] sample_x, float[] sample_y) => //{
  18. //| Returns a linear regression channel using (X,Y) vector points.
  19. int _sizeX = array.size(id=sample_x)
  20. int _sizeY = array.size(id=sample_y)
  21. //
  22. switch
  23. (_sizeX < 1) => runtime.error('FunctionLinearRegression -> linreg(): "sample_x" has the wrong size.')
  24. (_sizeX != _sizeY) => runtime.error('FunctionLinearRegression -> linreg(): "sample_x" and "sample_y" size must match.')
  25. //
  26. float _sumX = array.sum(id=sample_x)
  27. float _sumY = array.sum(id=sample_y)
  28. float _sumXY = 0.0
  29. float _sumX2 = 0.0
  30. float _sumY2 = 0.0
  31. //
  32. for _i = 0 to _sizeY - 1 by 1
  33. float _Xi = nz(array.get(id=sample_x, index=_i))
  34. float _Yi = nz(array.get(id=sample_y, index=_i))
  35. _sumXY := _sumXY + _Xi * _Yi
  36. _sumX2 := _sumX2 + math.pow(_Xi, 2)
  37. _sumY2 := _sumY2 + math.pow(_Yi, 2)
  38. _sumY2
  39. //
  40. float _a = (_sumY * _sumX2 - _sumX * _sumXY) / (_sizeX * _sumX2 - math.pow(_sumX, 2))
  41. float _b = (_sizeX * _sumXY - _sumX * _sumY) / (_sizeX * _sumX2 - math.pow(_sumX, 2))
  42. float[] _predictions = array.new_float(size=0, initial_value=0.0)
  43. float _max_dev = 0.0
  44. float _min_dev = 0.0
  45. float _stdev = 0.0
  46. //
  47. for _i = 0 to _sizeX - 1 by 1
  48. float _vector = _a + _b * array.get(id=sample_x, index=_i)
  49. array.push(id=_predictions, value=_vector)
  50. //
  51. float _Yi = array.get(id=sample_y, index=_i)
  52. float _diff = _Yi - _vector
  53. if _diff > _max_dev
  54. _max_dev := _diff
  55. _max_dev
  56. if _diff < _min_dev
  57. _min_dev := _diff
  58. _min_dev
  59. _stdev := _stdev + math.abs(_diff)
  60. _stdev
  61. //
  62. [_predictions, _max_dev, _min_dev, _stdev / _sizeX]
  63. //{ usage:
  64. //{ remarks:
  65. // https://www.statisticshowto.com/probability-and-statistics/regression-analysis/find-a-linear-regression-equation/#FindaLinear
  66. //}}}
  67.  
  68. // @function Method for drawing the Linear Regression into chart.
  69. // @param sample_x float array, sample point X value.
  70. // @param sample_y float array, sample point Y value.
  71. // @param extend string, default=extend.none, extend lines.
  72. // @param mid_color color, default=color.blue, middle line color.
  73. // @param mid_style string, default=line.style_solid, middle line style.
  74. // @param mid_width int, default=2, middle line width.
  75. // @param std_color color, default=color.aqua, standard deviation line color.
  76. // @param std_style string, default=line.style_dashed, standard deviation line style.
  77. // @param std_width int, default=1, standard deviation line width.
  78. // @param max_color color, default=color.purple, max range line color.
  79. // @param max_style string, default=line.style_dotted, max line style.
  80. // @param max_width int, default=1, max line width.
  81. // @returns line array.
  82. export draw (
  83. float[] sample_x, float[] sample_y, string extend=extend.none,
  84. color mid_color=color.blue, string mid_style=line.style_solid, int mid_width=2,
  85. color std_color=color.aqua, string std_style=line.style_dashed, int std_width=1,
  86. color max_color=color.purple, string max_style=line.style_dotted, int max_width=1
  87. ) => //{
  88.  
  89. [D, Dmax, Dmin, Dstdev] = linreg(sample_x, sample_y)
  90. string _xloc = xloc.bar_index
  91. var line ln_mid = line.new(bar_index, open, bar_index, open, _xloc, extend, mid_color, mid_style, mid_width)
  92. var line ln_max = line.new(bar_index, open, bar_index, open, _xloc, extend, max_color, max_style, max_width)
  93. var line ln_min = line.new(bar_index, open, bar_index, open, _xloc, extend, max_color, max_style, max_width)
  94. var line ln_upper = line.new(bar_index, open, bar_index, open, _xloc, extend, std_color, std_style, std_width)
  95. var line ln_lower = line.new(bar_index, open, bar_index, open, _xloc, extend, std_color, std_style, std_width)
  96.  
  97. int first = 0
  98. int last = array.size(id=sample_x) - 1
  99. ln_y1 = array.get(id=D, index=first)
  100. ln_y2 = array.get(id=D, index=last)
  101.  
  102. line.set_xy1(id=ln_mid, x=int(array.get(id=sample_x, index=first)), y=ln_y1)
  103. line.set_xy2(id=ln_mid, x=int(array.get(id=sample_x, index=last)), y=ln_y2)
  104. line.set_xy1(id=ln_max, x=int(array.get(id=sample_x, index=first)), y=ln_y1 + Dmax)
  105. line.set_xy2(id=ln_max, x=int(array.get(id=sample_x, index=last)), y=ln_y2 + Dmax)
  106. line.set_xy1(id=ln_min, x=int(array.get(id=sample_x, index=first)), y=ln_y1 + Dmin)
  107. line.set_xy2(id=ln_min, x=int(array.get(id=sample_x, index=last)), y=ln_y2 + Dmin)
  108. line.set_xy1(id=ln_upper, x=int(array.get(id=sample_x, index=first)), y=ln_y1 + Dstdev)
  109. line.set_xy2(id=ln_upper, x=int(array.get(id=sample_x, index=last)), y=ln_y2 + Dstdev)
  110. line.set_xy1(id=ln_lower, x=int(array.get(id=sample_x, index=first)), y=ln_y1 - Dstdev)
  111. line.set_xy2(id=ln_lower, x=int(array.get(id=sample_x, index=last)), y=ln_y2 - Dstdev)
  112.  
  113. var line[] _lines = array.from(ln_mid, ln_max, ln_min, ln_upper, ln_lower)
  114. //}
  115.  
  116. int length = input(10)
  117. var float[] prices = array.new_float(size=length, initial_value=open)
  118. var int[] indices = array.new_int(size=length, initial_value=0)
  119. if ta.pivothigh(2, 2)
  120. e = array.pop(id=prices)
  121. i = array.pop(id=indices)
  122. array.unshift(id=prices, value=high[2])
  123. array.unshift(id=indices, value=bar_index[2])
  124. if ta.pivotlow(2, 2)
  125. e = array.pop(id=prices)
  126. i = array.pop(id=indices)
  127. array.unshift(id=prices, value=low[2])
  128. array.unshift(id=indices, value=bar_index[2])
  129.  
  130. lines = draw(indices, prices)
  131.  
  132.  
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment