RicardoSantos

[RS]Fractal Regression Channel V0

EXPERIMENTAL:
Fractals/fibs/linear regression
Remove from Favorite Scripts Add to Favorite Scripts
//@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)

This script is a work of art, many thanks!
Reply
Home Stock Screener Forex Screener Crypto Screener Economic Calendar Shows How It Works Chart Features Pricing House Rules Moderators Website & Broker Solutions Widgets Charting Solutions Lightweight Charting Library Help Center Refer a friend Feature Request Blog & News Twitter
Profile Profile Settings Account and Billing Refer a friend My Support Tickets Help Center Ideas Published Followers Following Private Messages Chat Sign Out