EXPERIMENTAL:Time, Fibs and Linear regression
//@version=2 study(title='[RS]Tapestry Weaver V0', shorttitle='TW', 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] f_neg_na(_src)=>_return=change(_src)<0?na:_src f_pos_na(_src)=>_return=change(_src)>0?na:_src f_lr_rising(_n_start, _l, _h, _step)=>_return = na(_return[1]) ? 0 : n < _n_start ? _l : _return[1] > _h ? _l : _return[1]+_step f_lr_falling(_n_start, _l, _h, _step)=>_return = na(_return[1]) ? 0 : n < _n_start ? _h : _return[1] < _l ? _h : _return[1]+_step window = input(title='Lookback Window:', type=integer, defval=3) grid_size = input(title='Grid Multiplier', type=float, defval=1) USE_ALT_MAX = input(title='Box Maximum, (<0 = uses price High Appex):', type=float, defval=-1) USE_ALT_MIN = input(title='Box Minimum, (<0 = uses price Low Appex):', type=float, defval=-1) [_, l_step, _] = f_rising_linear_regression(low, window) [_, h_step, _] = f_falling_linear_regression(high, window) la_step = cum(l_step)/(n+1) ha_step = cum(h_step)/(n+1) high_extreme = USE_ALT_MAX >= 0 ? USE_ALT_MAX : na(high_extreme[1]) ? high : high >= high_extreme[1] ? high : high_extreme[1] low_extreme = USE_ALT_MIN >= 0 ? USE_ALT_MIN : na(low_extreme[1]) ? low : low <= low_extreme[1] ? low : low_extreme[1] r00 = f_neg_na(f_lr_rising(1, low_extreme, high_extreme, la_step)) r01 = f_neg_na(f_lr_rising(2, low_extreme, high_extreme, la_step)) r02 = f_neg_na(f_lr_rising(3, low_extreme, high_extreme, la_step)) r03 = f_neg_na(f_lr_rising(5, low_extreme, high_extreme, la_step)) r04 = f_neg_na(f_lr_rising(8, low_extreme, high_extreme, la_step)) r05 = f_neg_na(f_lr_rising(13, low_extreme, high_extreme, la_step)) r06 = f_neg_na(f_lr_rising(21, low_extreme, high_extreme, la_step)) r07 = f_neg_na(f_lr_rising(34, low_extreme, high_extreme, la_step)) r08 = f_neg_na(f_lr_rising(55, low_extreme, high_extreme, la_step)) r09 = f_neg_na(f_lr_rising(89, low_extreme, high_extreme, la_step)) r10 = f_neg_na(f_lr_rising(144, low_extreme, high_extreme, la_step)) r11 = f_neg_na(f_lr_rising(233, low_extreme, high_extreme, la_step)) r12 = f_neg_na(f_lr_rising(377, low_extreme, high_extreme, la_step)) r13 = f_neg_na(f_lr_rising(610, low_extreme, high_extreme, la_step)) r14 = f_neg_na(f_lr_rising(987, low_extreme, high_extreme, la_step)) f00 = f_pos_na(f_lr_falling(1, low_extreme, high_extreme, ha_step)) f01 = f_pos_na(f_lr_falling(2, low_extreme, high_extreme, ha_step)) f02 = f_pos_na(f_lr_falling(3, low_extreme, high_extreme, ha_step)) f03 = f_pos_na(f_lr_falling(5, low_extreme, high_extreme, ha_step)) f04 = f_pos_na(f_lr_falling(8, low_extreme, high_extreme, ha_step)) f05 = f_pos_na(f_lr_falling(13, low_extreme, high_extreme, ha_step)) f06 = f_pos_na(f_lr_falling(21, low_extreme, high_extreme, ha_step)) f07 = f_pos_na(f_lr_falling(34, low_extreme, high_extreme, ha_step)) f08 = f_pos_na(f_lr_falling(55, low_extreme, high_extreme, ha_step)) f09 = f_pos_na(f_lr_falling(89, low_extreme, high_extreme, ha_step)) f10 = f_pos_na(f_lr_falling(144, low_extreme, high_extreme, ha_step)) f11 = f_pos_na(f_lr_falling(233, low_extreme, high_extreme, ha_step)) f12 = f_pos_na(f_lr_falling(377, low_extreme, high_extreme, ha_step)) f13 = f_pos_na(f_lr_falling(610, low_extreme, high_extreme, ha_step)) f14 = f_pos_na(f_lr_falling(987, low_extreme, high_extreme, ha_step)) plot(title='+1', series=r00, style=linebr, color=black) plot(title='+2', series=r01, style=linebr, color=black) plot(title='+3', series=r02, style=linebr, color=black) plot(title='+5', series=r03, style=linebr, color=black) plot(title='+8', series=r04, style=linebr, color=black) plot(title='+13', series=r05, style=linebr, color=black) plot(title='+21', series=r06, style=linebr, color=black) plot(title='+34', series=r07, style=linebr, color=black) plot(title='+55', series=r08, style=linebr, color=black) plot(title='+89', series=r09, style=linebr, color=black) plot(title='+144', series=r10, style=linebr, color=black) plot(title='+233', series=r11, style=linebr, color=black) plot(title='+377', series=r12, style=linebr, color=black) plot(title='+610', series=r13, style=linebr, color=black) plot(title='+987', series=r14, style=linebr, color=black) plot(title='-1', series=f00, style=linebr, color=black) plot(title='-2', series=f01, style=linebr, color=black) plot(title='-3', series=f02, style=linebr, color=black) plot(title='-5', series=f03, style=linebr, color=black) plot(title='-8', series=f04, style=linebr, color=black) plot(title='-13', series=f05, style=linebr, color=black) plot(title='-21', series=f06, style=linebr, color=black) plot(title='-34', series=f07, style=linebr, color=black) plot(title='-55', series=f08, style=linebr, color=black) plot(title='-89', series=f09, style=linebr, color=black) plot(title='-144', series=f10, style=linebr, color=black) plot(title='-233', series=f11, style=linebr, color=black) plot(title='-377', series=f12, style=linebr, color=black) plot(title='-610', series=f13, style=linebr, color=black) plot(title='-987', series=f14, style=linebr, color=black)