Trend crossier
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Arivadis

//@version=5

indicator("Trend crossier",overlay = true, max_bars_back = 500)
vector_step = input.int(40, "step for each range for vector")
vector_length = input.int(7, title = "ranges inside each vector")
ema_length = input.int(7, "Ema for vector")
Vector_counter(source)=>
    vector_array = array.new_float(0, na)
    ema_vector = ta.ema(source, ema_length)
    counter_ = 0
    step_vect_var = 1
    array_var = array.new_float(0, na)
    while counter_ < vector_length
        array.push(vector_array, source[step_vect_var])
        counter_ += 1
        step_vect_var += vector_step

    while array.size(vector_array) > 2
        for i = array.size(vector_array) - 1 to 1 
            array.push(array_var, math.avg(array.get(vector_array, i), array.get(vector_array, i- 1)))
        for i = array.size(vector_array) - 1 to 1
            if i < array.size(array_var) - 1
                array.set(vector_array, i, array.get(array_var, i))
            else
                array.remove(vector_array, i)
        for i = array.size(array_var) - 1 to 1
            array.remove(array_var, i)
            
    diff4 = array.get(vector_array,0) - array.get(vector_array,1)


step = input.int(100, minval = 1, title = "Length every step")
free_space = input.int(10, minval = 1, title = "Give some space to reach max/min")
timeframe = input.int(240, title = "Input high timeframe", options = [60, 240,1440])
timeframe_low = input.int(15, title = "Input low timeframe", options = [5, 15, 30, 60])
close4 = request.security(syminfo.tickerid, str.tostring(timeframe) , Vector_counter(close))
close_low = request.security(syminfo.tickerid, str.tostring(timeframe_low) , Vector_counter(close))






vect_mid = Vector_counter(close)
vect_high = close4
vect_low = close_low

lines_min_to_level = input.int(1, title = "how many lines mean as 1 level (1 + N)")
plot_all_levels = input.bool(false, "Plot every level???")
mult = input.float(2, minval = 1.0, title = 'adr mult')
var plot_lines = 10
float line_1_h = ta.highest(close4, step)[free_space]
float line_1_l = ta.lowest(close4, step)[free_space]
float line_2_h = ta.highest(close4, step)[step+free_space]
float line_2_l = ta.lowest(close4, step)[step]
float line_3_h = ta.highest(close4, step)[step*2 +free_space]
float line_3_l = ta.lowest(close4, step)[step*2 +free_space]
float line_4_h = ta.highest(close4, step)[step*3 +free_space]
float line_4_l = ta.lowest(close4, step)[step*3 +free_space]
float line_5_h = ta.highest(close4, step)[step*4 +free_space]
float line_5_l = ta.lowest(close4, step)[step*4 +free_space]
float line_6_h = ta.highest(close4, step)[step*5 +free_space]
float line_6_l = ta.lowest(close4, step)[step*5 +free_space]
float line_7_h = ta.highest(close4, step)[step*6 +free_space]
float line_7_l = ta.lowest(close4, step)[step*6 +free_space]
float line_8_h = ta.highest(close4, step)[step*7 +free_space]
float line_8_l = ta.lowest(close4, step)[step*7 +free_space]
float line_9_h = ta.highest(close4, step)[step*8 +free_space]
float line_9_l = ta.lowest(close4, step)[step*8 +free_space]
float line_10_h = ta.highest(close4, step)[step*9 +free_space]
float line_10_l = ta.lowest(close4, step)[step*9 +free_space]
line_above = line_1_h + (line_1_h * 0.010)
line_below = line_1_l + (line_1_l * 0.010)
SMA = ta.sma(close, 7)
plot(SMA, title = "SMA")

croosing_counter = 0.0

if SMA > line_1_h
    croosing_counter += 0.33
if SMA > line_1_l
    croosing_counter += 0.33
if SMA > line_2_h
    croosing_counter += 0.33
if SMA > line_2_l
    croosing_counter += 0.33
if SMA > line_3_h
    croosing_counter += 0.33
if SMA > line_3_l
    croosing_counter += 0.33
if SMA > line_4_h
    croosing_counter += 0.33
if SMA > line_4_l
    croosing_counter += 0.33
if SMA > line_5_h
    croosing_counter += 0.33
if SMA > line_5_l
    croosing_counter += 0.33
if SMA > line_6_h
    croosing_counter += 0.33
if SMA > line_6_l
    croosing_counter += 0.33
if SMA > line_7_h
    croosing_counter += 0.33
if SMA > line_7_l
    croosing_counter += 0.33
if SMA > line_8_h
    croosing_counter += 0.33
if SMA > line_8_l
    croosing_counter += 0.33
if SMA > line_9_h
    croosing_counter += 0.33
if SMA > line_9_l
    croosing_counter += 0.33
if SMA > line_above
    croosing_counter += 0.33
if SMA > line_below
    croosing_counter += 0.33


float line_1_h_ = ta.highest(close, step)[free_space]
float line_1_l_ = ta.lowest(close, step)[free_space]
float line_2_h_ = ta.highest(close, step)[step+free_space]
float line_2_l_ = ta.lowest(close, step)[step]
float line_3_h_ = ta.highest(close, step)[step*2 +free_space]
float line_3_l_ = ta.lowest(close, step)[step*2 +free_space]
float line_4_h_ = ta.highest(close, step)[step*3 +free_space]
float line_4_l_ = ta.lowest(close, step)[step*3 +free_space]
float line_5_h_ = ta.highest(close, step)[step*4 +free_space]
float line_5_l_ = ta.lowest(close, step)[step*4 +free_space]
float line_6_h_ = ta.highest(close, step)[step*5 +free_space]
float line_6_l_ = ta.lowest(close, step)[step*5 +free_space]
float line_7_h_ = ta.highest(close, step)[step*6 +free_space]
float line_7_l_ = ta.lowest(close, step)[step*6 +free_space]
float line_8_h_ = ta.highest(close, step)[step*7 +free_space]
float line_8_l_ = ta.lowest(close, step)[step*7 +free_space]
float line_9_h_ = ta.highest(close, step)[step*8 +free_space]
float line_9_l_ = ta.lowest(close, step)[step*8 +free_space]
float line_10_h_ = ta.highest(close, step)[step*9 +free_space]
float line_10_l_ = ta.lowest(close, step)[step*9 +free_space]
line_above_ = line_1_h_ + (line_1_h_ * 0.010)
line_below_ = line_1_l_ + (line_1_l_ * 0.010)



if SMA > line_1_h_
    croosing_counter += 0.17
if SMA > line_1_l_
    croosing_counter += 0.17
if SMA > line_2_h_
    croosing_counter += 0.17
if SMA > line_2_l_
    croosing_counter += 0.17
if SMA > line_3_h_
    croosing_counter += 0.17
if SMA > line_3_l_
    croosing_counter += 0.17
if SMA > line_4_h_
    croosing_counter += 0.17
if SMA > line_4_l_
    croosing_counter += 0.17
if SMA > line_5_h_
    croosing_counter += 0.17
if SMA > line_5_l_
    croosing_counter += 0.17
if SMA > line_6_h_
    croosing_counter += 0.17
if SMA > line_6_l_
    croosing_counter += 0.17
if SMA > line_7_h_
    croosing_counter += 0.17
if SMA > line_7_l_
    croosing_counter += 0.17
if SMA > line_8_h_
    croosing_counter += 0.17
if SMA > line_8_l_
    croosing_counter += 0.17
if SMA > line_9_h_
    croosing_counter += 0.17
if SMA > line_9_l_
    croosing_counter += 0.17
if SMA > line_above_
    croosing_counter += 0.17
if SMA > line_below_
    croosing_counter += 0.17



var table atrDisplay = table.new(position.top_right, 1, 1, bgcolor = color.gray, frame_width = 2, frame_color = color.black)

table.cell(atrDisplay, 0, 0, str.tostring(croosing_counter, format.mintick), text_color = color.white)

// computing the strong lines of trend

levels = array.new_float(0)
array.push(levels, line_1_h)
array.push(levels, line_1_l)
array.push(levels, line_2_h)
array.push(levels, line_2_l)
array.push(levels, line_3_h)
array.push(levels, line_3_l)
array.push(levels, line_4_h)
array.push(levels, line_4_l)
array.push(levels, line_5_h)
array.push(levels, line_5_l)
array.push(levels, line_6_h)
array.push(levels, line_6_l)
array.push(levels, line_7_h)
array.push(levels, line_7_l)
array.push(levels, line_8_h)
array.push(levels, line_8_l)
array.push(levels, line_9_h)
array.push(levels, line_9_l)
array.push(levels, line_above)
array.push(levels, line_below)
array.push(levels, line_1_h_)
array.push(levels, line_1_l_)
array.push(levels, line_2_h_)
array.push(levels, line_2_l_)
array.push(levels, line_3_h_)
array.push(levels, line_3_l_)
array.push(levels, line_4_h_)
array.push(levels, line_4_l_)
array.push(levels, line_5_h_)
array.push(levels, line_5_l_)
array.push(levels, line_6_h_)
array.push(levels, line_6_l_)
array.push(levels, line_7_h_)
array.push(levels, line_7_l_)
array.push(levels, line_8_h_)
array.push(levels, line_8_l_)
array.push(levels, line_9_h_)
array.push(levels, line_9_l_)
array.push(levels, line_above_)
array.push(levels, line_below_)
array.sort(levels, order = order.ascending)
ADR = ta.sma((high - low)  * mult, step * 10) 






the_level_array = array.new_int(0)




counter = 1
if array.size(levels) > 0
    for i = 0 to array.size(levels) - 2 by 1
        val = array.get(levels, i)
        val2 = array.get(levels, i+1)

        if val + ADR > val2
            array.push(the_level_array, counter)
        else
            counter += 1
            array.push(the_level_array, 0)


strong_levels_array = array.new_float(0)

counter_of_level_power = array.new_int(0)

if array.size(the_level_array) > 0
    for i = 0 to array.get(the_level_array, array.size(the_level_array) - 1) by 1
        counter_ = 0
        summ_of_levels = 0.0
        for j = 0 to array.size(the_level_array) - 1
            if array.get(the_level_array, j) == i and array.get(the_level_array, j) != 0
                counter_ += 1
                summ_of_levels += array.get(levels, j)
        if counter_ >= lines_min_to_level
            array.push(strong_levels_array, summ_of_levels / counter_)
            array.push(counter_of_level_power, counter_)
        counter_ := 0
        summ_of_levels := 0.0

val = array.size(strong_levels_array) > 0 ? array.size(strong_levels_array) - 1 : na





var last_strong_array = array.new_line(0)
if array.size(last_strong_array) > 0
    for i = 0 to  array.size(last_strong_array) - 1
        line.delete(array.get(last_strong_array, i))
    array.clear(last_strong_array)

if array.size(strong_levels_array) > 0
    for x = 0 to array.size(strong_levels_array) - 1 by 1
        mid = array.get(strong_levels_array, x)
        with_ = array.get(counter_of_level_power, x)
        with = 1
        trans = 0
        if with_ > 2 and with_ < 4
            trans := 70
            with := 6
        else if with_ >= 4 and with_ < 6
            trans := 50
            with := 6
        else
            trans := 30
            with := 6

        array.push(last_strong_array, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=color.new(color.aqua, trans),width =with ))


var all_lines = array.new_line(0)
if array.size(all_lines) > 0
    for i = 0 to  array.size(all_lines) - 1
        line.delete(array.get(all_lines, i))
    array.clear(all_lines)

if plot_all_levels and array.size(levels) > 0
    for x = 0 to (array.size(levels) > 0 ? array.size(levels) - 1 : na) by 1
        mid = array.get(levels, x)
        array.push(all_lines, line.new(x1=bar_index, y1=mid, x2=bar_index - 1, y2=mid, extend=extend.both, color=color.new(color.blue, 60),style = line.style_solid, width=2))





line_vect_mid = line.new(bar_index, high, bar_index + vector_step, high + vect_mid, xloc=xloc.bar_index,extend = extend.none ,color = color.new(#81cc20, 60), width = 2)
line_vect_high = line.new(bar_index, high + (high * 0.005), bar_index + vector_step,high + vect_high + (high * 0.005), xloc=xloc.bar_index,extend = extend.none ,color = color.new(#8422c5, 60), width = 4)
line_vect_low = line.new(bar_index, high - (high * 0.005), bar_index  + vector_step,high + vect_low - (high * 0.005),xloc=xloc.bar_index,extend = extend.none ,color = color.new(color.rgb(183, 50, 50), 60), width = 1)
line.delete(line_vect_mid[1])
line.delete(line_vect_high[1])
line.delete(line_vect_low[1])