RicardoSantos

[RS]Fractal Regression Channel V0

EXPERIMENTAL:
Fractals/fibs/linear regression
开源脚本

本着真正的TradingView精神,该脚本的作者将其开源发布,以便交易者可以理解和验证它。为作者喝彩!您可以免费使用它,但在出版物中重复使用此代码受网站规则的约束。 您可以收藏它以在图表上使用。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想在图表上使用此脚本?
//@version=2
study(title='[RS]Fractal Regression Channel V0', shorttitle='FRC', overlay=true)

f_falling_linear_regression(_src, _window)=>
    _h = highest(_src, _window)
    _h_fractal = _src[1] >= _h[1] and _src < _h
    _h0h = valuewhen(_h_fractal, _src[1], 0)
    _h1h = valuewhen(_h_fractal, _src[1], 1)
    _h0n = valuewhen(_h_fractal, n[1], 0)
    _h1n = valuewhen(_h_fractal, n[1], 1)
    _price_range = _h0h < _h1h ? _h0h-_h1h : _price_range[1]
    _bar_range = _h0h < _h1h ? _h0n-_h1n : _bar_range[1]
    _step = _price_range/_bar_range
    _return_regression = _h0h+(_step*(n-_h0n))
    [_h0h, _step, _return_regression]

f_rising_linear_regression(_src, _window)=>
    _l = lowest(_src, _window)
    _l_fractal = _src[1] <= _l[1] and _src > _l
    _l0l = valuewhen(_l_fractal, _src[1], 0)
    _l1l = valuewhen(_l_fractal, _src[1], 1)
    _l0n = valuewhen(_l_fractal, n[1], 0)
    _l1n = valuewhen(_l_fractal, n[1], 1)
    _price_range = _l0l > _l1l ? _l0l-_l1l : _price_range[1]
    _bar_range = _l0l > _l1l ? _l0n-_l1n : _bar_range[1]
    _step = _price_range/_bar_range
    _return_regression = _l0l+(_step*(n-_l0n))
    [_l0l, _step, _return_regression]

window = input(3)
grid_size = input(1)
[h_value, h_step, h_regression] = f_falling_linear_regression(high, window)
[l_value, l_step, l_regression] = f_rising_linear_regression(low, window)

avg_h_step = cum(h_step)/(n+1)
avg_l_step = cum(l_step)/(n+1)


h_base = na(h_base[1]) ? high : high >= h_base[1] ? high : h_base[1]+avg_h_step//high >= h_base[1] ? high : high >= h_regression ? h_base[1]-avg_h_step : h_regression
l_base = na(l_base[1]) ? low : low <= l_base[1] ? low : l_base[1]+avg_l_step//low <= l_base[1] ? low : low <= l_regression ? l_base[1]+avg_l_step : l_regression

direction = na(direction[1]) ? 1 : direction[1] < 0 and rising(l_base, 1) and not falling(h_base,1) ? 1 : direction[1] > 0 and falling(h_base, 1) and not rising(l_base,1) ? -1 : direction[1]
base0 = direction > 0 ? l_base : h_base
base = change(direction)!=0 ? na : base0
grid_block = direction > 0 ? (avg_l_step*grid_size) : (avg_h_step*grid_size)

plot(title='-1.618(-34)', series=base + grid_block*-34, style=linebr , color=direction>0?blue:fuchsia, linewidth=1)
plot(title='-0.618(-13)', series=base + grid_block*-13, style=linebr , color=direction>0?blue:fuchsia, linewidth=1)
plot(title='0(0)', series=base, style=linebr, color=black, linewidth=3)
plot(title='0.236(5)', series=base + grid_block*5, style=linebr , color=direction>0?green:maroon, linewidth=2)
plot(title='0.382(8)', series=base + grid_block*8, style=linebr , color=direction>0?green:maroon, linewidth=2)
plot(title='0.618(13)', series=base + grid_block*13, style=linebr , color=direction>0?lime:red, linewidth=1)
plot(title='1(21)', series=base + grid_block*21, style=linebr , color=direction>0?black:black, linewidth=1)
plot(title='1.618(34)', series=base + grid_block*34, style=linebr , color=direction>0?olive:orange, linewidth=1)
plot(title='2.618(55)', series=base + grid_block*55, style=linebr , color=direction>0?olive:orange, linewidth=1)
plot(title='4.272(89)', series=base + grid_block*89, style=linebr , color=direction>0?blue:fuchsia, linewidth=1)
plot(title='6.827(144)', series=base + grid_block*144, style=linebr , color=direction>0?navy:aqua, linewidth=1)