Advertisement
Guest User

hurst

a guest
Jul 16th, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. //@version=4
  2. study(
  3. title="Function - Hurst Exponent",
  4. shorttitle="H",
  5. overlay=false
  6. )
  7. // https://www.financialwisdomforum.org/gummy-stuff/hurst.htm
  8. // https://robotwealth.com/demystifying-the-hurst-exponent-part-2/
  9. // another method: https://blog.quantinsti.com/hurst-exponent/
  10. // http://analytics-magazine.org/the-hurst-exponent-predictability-of-time-series/
  11. // original He: http://mosaic.mpi-cbg.de/docs/Racine2011.pdf
  12. // were:
  13. // R(n) = range = max(dataset) - min(dataset)
  14. // S(n) = stdev(dataset)
  15. // C = arbitrary constant?
  16.  
  17. f_sum(_src, _length)=>
  18. _length_adjusted = _length < 1 ? 1 : _length
  19. _sum = 0.0
  20. for _i = 0 to (_length_adjusted - 1)
  21. _sum := _sum + _src[_i]
  22. _return = _sum
  23.  
  24. f_sma(_src, _length)=>
  25. _length_adjusted = _length < 1 ? 1 : _length
  26. _sum = 0.0
  27. for _i = 0 to (_length_adjusted - 1)
  28. _sum := _sum + _src[_i]
  29. _return = _sum / _length_adjusted
  30.  
  31. f_stdev(src, len, average) =>
  32. if false//not barstate.islast
  33. na
  34. else
  35. sumOfSquares = 0.0
  36. for i = 0 to len - 1
  37. deltaSrc = src[i] - average
  38. sumOfSquares := sumOfSquares + deltaSrc * deltaSrc
  39. sqrt(sumOfSquares / len)
  40.  
  41. f_highest(_src, _length)=>
  42. _adjusted_length = _length < 1 ? 1 : _length
  43. _value = _src
  44. for _i = 0 to (_adjusted_length-1)
  45. _value := _src[_i] >= _value ? _src[_i] : _value
  46. _return = _value
  47.  
  48. f_lowest(_src, _length)=>
  49. _adjusted_length = _length < 1 ? 1 : _length
  50. _value = _src
  51. for _i = 0 to (_adjusted_length-1)
  52. _value := _src[_i] <= _value ? _src[_i] : _value
  53. _return = _value
  54.  
  55. f_draw_line(_x1, _y1, _x2, _y2, _color, _width)=>
  56. var line _li = na
  57. line.delete(_li)
  58. _li := line.new(
  59. x1=bar_index[100] + _x1, y1=_y1,
  60. x2=bar_index[100] + _x2, y2=_y2,
  61. xloc=xloc.bar_index, extend=extend.none,
  62. color=_color, style=line.style_solid, width=_width
  63. )
  64.  
  65. f_hurst_exponent(_src, _length)=>
  66. _mean = f_sma(_src, _length)
  67. _dif = _src-_mean
  68. _sum_of_dif = sum(_dif, _length)
  69. _h = f_highest(_dif, _length)
  70. _l = f_lowest(_dif, _length)
  71. _range = _h - _l
  72. _s = f_stdev(_src, _length, _mean)
  73. _rescaled = _range / _s
  74. _log_r = log(_rescaled)
  75. _log_n = log(_length)
  76. _log_r / _log_n
  77.  
  78.  
  79. f_hurst_estimate(_src, _length)=>
  80. _dif = close - close[_length]
  81. _mean = cum(abs(_dif)) / (bar_index + 1)
  82. _delta = _dif - _mean
  83. _std = sqrt(cum(_delta * _delta) / (bar_index + 1))
  84. var float _max = na
  85. var float _min = na
  86. _max := max(nz(_max, _dif), _dif)
  87. _min := min(nz(_min, _dif), _dif)
  88. _r = _max - _min
  89. _rs = _r / _std
  90. _log = log(_rs) / log(_length)
  91.  
  92. f_hurst_exponent_m1(_src, _length)=>
  93. //described here:
  94. //https://www.financialwisdomforum.org/gummy-stuff/hurst.htm
  95. //calculate range:
  96. _mean = f_sma(_src, _length)
  97. _deviation = _src-_mean
  98. _sum_of_dev = f_sum(_deviation, _length)
  99. _h = f_highest(_sum_of_dev, _length)
  100. _l = f_lowest(_sum_of_dev, _length)
  101. _range = _h - _l
  102. //calculate stdev:
  103. _s = f_stdev(_src, _length, _mean)
  104. //rescale
  105. _rescaled = _range / _s
  106. //log the values
  107. _log_r = log(_rescaled)
  108. _log_n = log(_length)
  109. _log_r / _log_n
  110.  
  111. src = close, length = input(100), step = input(10)
  112.  
  113. he = f_hurst_exponent(close, length)
  114. plot(he, color=color.blue)
  115. he1 = f_hurst_exponent_m1(close, length)
  116. plot(he1, color=color.orange)
  117. plot(linreg(he, 100, 0), color=color.red)
  118.  
  119. he2 = f_hurst_estimate(close, length)
  120. plot(he2, color=color.teal)
  121.  
  122. f_iterate_hurst_exponent(_src, _length, _step)=>
  123. _text = ""
  124. _sh = 0.0
  125. _se = 0.0
  126. _sh1 = 0.0
  127. for _i=0 to _length by _step
  128. _h = f_hurst_exponent(_src, max(1, _i))
  129. _e = f_hurst_estimate(_src, max(1, _i))
  130. _h1 = f_hurst_exponent_m1(_src, max(1, _i))
  131. _sh := _sh + nz(_h, 0.0)
  132. _se := _se + nz(_e, 0.0)
  133. _sh1 := _sh1 + nz(_h1, 0.0)
  134. _text := _text + "\n" + tostring(_i, "#") + " : " + tostring(_h, "#.###") + ": " + tostring(_h1, "#.###") + " : " + tostring(_e, "#.###")
  135. _sh := _sh / ((1+_length)/step)
  136. _se := _se / ((1+_length)/step)
  137. _sh1 := _sh1 / ((1+_length)/step)
  138. _text := _text + "\n" + "He: " + tostring(_sh, "#.###") + " : " + tostring(_sh1, "#.###") + " : " + tostring(_se, "#.###")
  139.  
  140. tex = "h - : " + tostring(he, "#.###") + " : " + tostring(he1, "#.###") + " : " + tostring(he2, "#.###")
  141. tex := tex + f_iterate_hurst_exponent(close, length, step)
  142. var label _la = na
  143. label.delete(_la)
  144. _la := label.new(
  145. x= bar_index, y=0.0,
  146. text=tex, xloc=xloc.bar_index, yloc=yloc.price,
  147. color=color.new(color.silver, 80), style=label.style_labeldown, textcolor=color.black, size=size.small
  148. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement