Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- study(
- title="Function - Hurst Exponent",
- shorttitle="H",
- overlay=false
- )
- // https://www.financialwisdomforum.org/gummy-stuff/hurst.htm
- // https://robotwealth.com/demystifying-the-hurst-exponent-part-2/
- // another method: https://blog.quantinsti.com/hurst-exponent/
- // http://analytics-magazine.org/the-hurst-exponent-predictability-of-time-series/
- // original He: http://mosaic.mpi-cbg.de/docs/Racine2011.pdf
- // were:
- // R(n) = range = max(dataset) - min(dataset)
- // S(n) = stdev(dataset)
- // C = arbitrary constant?
- f_sum(_src, _length)=>
- _length_adjusted = _length < 1 ? 1 : _length
- _sum = 0.0
- for _i = 0 to (_length_adjusted - 1)
- _sum := _sum + _src[_i]
- _return = _sum
- f_sma(_src, _length)=>
- _length_adjusted = _length < 1 ? 1 : _length
- _sum = 0.0
- for _i = 0 to (_length_adjusted - 1)
- _sum := _sum + _src[_i]
- _return = _sum / _length_adjusted
- f_stdev(src, len, average) =>
- if false//not barstate.islast
- na
- else
- sumOfSquares = 0.0
- for i = 0 to len - 1
- deltaSrc = src[i] - average
- sumOfSquares := sumOfSquares + deltaSrc * deltaSrc
- sqrt(sumOfSquares / len)
- f_highest(_src, _length)=>
- _adjusted_length = _length < 1 ? 1 : _length
- _value = _src
- for _i = 0 to (_adjusted_length-1)
- _value := _src[_i] >= _value ? _src[_i] : _value
- _return = _value
- f_lowest(_src, _length)=>
- _adjusted_length = _length < 1 ? 1 : _length
- _value = _src
- for _i = 0 to (_adjusted_length-1)
- _value := _src[_i] <= _value ? _src[_i] : _value
- _return = _value
- f_draw_line(_x1, _y1, _x2, _y2, _color, _width)=>
- var line _li = na
- line.delete(_li)
- _li := line.new(
- x1=bar_index[100] + _x1, y1=_y1,
- x2=bar_index[100] + _x2, y2=_y2,
- xloc=xloc.bar_index, extend=extend.none,
- color=_color, style=line.style_solid, width=_width
- )
- f_hurst_exponent(_src, _length)=>
- _mean = f_sma(_src, _length)
- _dif = _src-_mean
- _sum_of_dif = sum(_dif, _length)
- _h = f_highest(_dif, _length)
- _l = f_lowest(_dif, _length)
- _range = _h - _l
- _s = f_stdev(_src, _length, _mean)
- _rescaled = _range / _s
- _log_r = log(_rescaled)
- _log_n = log(_length)
- _log_r / _log_n
- f_hurst_estimate(_src, _length)=>
- _dif = close - close[_length]
- _mean = cum(abs(_dif)) / (bar_index + 1)
- _delta = _dif - _mean
- _std = sqrt(cum(_delta * _delta) / (bar_index + 1))
- var float _max = na
- var float _min = na
- _max := max(nz(_max, _dif), _dif)
- _min := min(nz(_min, _dif), _dif)
- _r = _max - _min
- _rs = _r / _std
- _log = log(_rs) / log(_length)
- f_hurst_exponent_m1(_src, _length)=>
- //described here:
- //https://www.financialwisdomforum.org/gummy-stuff/hurst.htm
- //calculate range:
- _mean = f_sma(_src, _length)
- _deviation = _src-_mean
- _sum_of_dev = f_sum(_deviation, _length)
- _h = f_highest(_sum_of_dev, _length)
- _l = f_lowest(_sum_of_dev, _length)
- _range = _h - _l
- //calculate stdev:
- _s = f_stdev(_src, _length, _mean)
- //rescale
- _rescaled = _range / _s
- //log the values
- _log_r = log(_rescaled)
- _log_n = log(_length)
- _log_r / _log_n
- src = close, length = input(100), step = input(10)
- he = f_hurst_exponent(close, length)
- plot(he, color=color.blue)
- he1 = f_hurst_exponent_m1(close, length)
- plot(he1, color=color.orange)
- plot(linreg(he, 100, 0), color=color.red)
- he2 = f_hurst_estimate(close, length)
- plot(he2, color=color.teal)
- f_iterate_hurst_exponent(_src, _length, _step)=>
- _text = ""
- _sh = 0.0
- _se = 0.0
- _sh1 = 0.0
- for _i=0 to _length by _step
- _h = f_hurst_exponent(_src, max(1, _i))
- _e = f_hurst_estimate(_src, max(1, _i))
- _h1 = f_hurst_exponent_m1(_src, max(1, _i))
- _sh := _sh + nz(_h, 0.0)
- _se := _se + nz(_e, 0.0)
- _sh1 := _sh1 + nz(_h1, 0.0)
- _text := _text + "\n" + tostring(_i, "#") + " : " + tostring(_h, "#.###") + ": " + tostring(_h1, "#.###") + " : " + tostring(_e, "#.###")
- _sh := _sh / ((1+_length)/step)
- _se := _se / ((1+_length)/step)
- _sh1 := _sh1 / ((1+_length)/step)
- _text := _text + "\n" + "He: " + tostring(_sh, "#.###") + " : " + tostring(_sh1, "#.###") + " : " + tostring(_se, "#.###")
- tex = "h - : " + tostring(he, "#.###") + " : " + tostring(he1, "#.###") + " : " + tostring(he2, "#.###")
- tex := tex + f_iterate_hurst_exponent(close, length, step)
- var label _la = na
- label.delete(_la)
- _la := label.new(
- x= bar_index, y=0.0,
- text=tex, xloc=xloc.bar_index, yloc=yloc.price,
- color=color.new(color.silver, 80), style=label.style_labeldown, textcolor=color.black, size=size.small
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement