RicardoSantos

[RS]Fractal Regression Channel V0

EXPERIMENTAL:
Fractals/fibs/linear regression
Script de código aberto

Dentro do verdadeiro espírito TradingView, o autor deste script publicou ele como um script de código aberto, para que os traders possam compreender e checar ele. Um viva ao autor! Você pode usá-lo gratuitamente, mas a reutilização deste código em uma publicação é regida pelas Regras da Casa. Você pode favoritá-lo para usá-lo em um gráfico.

Aviso legal

As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.

Quer usar esse script no gráfico?
//@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)