Vector Flow Channel
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5
indicator(title="Vector Flow Channel V0", shorttitle="VFC0", overlay=true)

float decay = ta.atr(10)*input.float(defval=0.005, title='Decay Rate:')

int stage1 = input.int(defval=05, title='Stage Start 1:', inline='stage')
int stage2 = input.int(defval=10, title='2:', inline='stage')
int stage3 = input.int(defval=20, title='3:', inline='stage')
int stage1length = input.int(defval=100, title='Stage length 1:', inline='stagelength')
int stage2length = stage1length + input.int(defval=100, title='2:', inline='stagelength')

TopVector (float source, int start1=1, int start2=1, int start3=1, int length1=1, int length2=1) => //{
    var float _top = source
    int _final_count = 0
    float _previous = nz(_top[1], source)
    _top := source >= _previous ? source : _previous - (decay * _final_count[1])
    int _counter = bar_index - ta.valuewhen(close >= _top, bar_index, 0)
    switch
        _counter <= length1 => _final_count := start1
        _counter <= length2 => _final_count := start2
        =>                     _final_count := start3
    _top
//}

BotVector (float source, int start1=1, int start2=1, int start3=1, int length1=1, int length2=1) => //{
    var float _bot = source
    int _final_count = 0
    float _previous = nz(_bot[1], source)
    _bot := source <= _previous ? source : _previous + (decay * _final_count[1])
    int _counter = bar_index - ta.valuewhen(close <= _bot, bar_index, 0)
    switch
        _counter <= length1 => _final_count := start1
        _counter <= length2 => _final_count := start2
        =>                     _final_count := start3
    _bot
//}

float topvector01 = TopVector(close, stage1, stage2, stage3, stage1length, stage2length)
float botvector01 = BotVector(close, stage1, stage2, stage3, stage1length, stage2length)
pt = plot(topvector01, color=(topvector01 == close ? na : color.black))
pb = plot(botvector01, color=(botvector01 == close ? na : color.black))
fill(pt, pb, color=color.new(color.blue, 85))
