Trendy Bars Counter
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Trendy Bars Counter indicates the number of bars in trend.
// Meaning of the color of the numbers plotted on graph is:
//    Green: If trend is up
//    Red: If trend is down
// 
// Minimum Number Of Trendy Bars: if trendy bars count is greater than this value trendy bars count will be plotted 
//
// Meaning of plotted chars:
// Numbers: sequential number of bars in trend
// ^: Sequential number of bars in trend are bigger than 10 and trend is positive
// v: Sequential number of bars in trend are bigger than 10 and trend is negative
// © agbay

//@version=5
indicator(title="Trendy Bars Counter", shorttitle="Tdy Bar", overlay = true)
max_bars = input.int(defval =  20, minval = 1, title = "Maximum Historical Bar Count")
min_trend = input.int(defval =  0, minval = 0, title = "Minimum Number Of Trendy Bars")

max1_bars = max_bars-1
strend = 0
//----------------------------------------------------------------------------------------------
//  Calculate trend
//----------------------------------------------------------------------------------------------
if not na(open[max1_bars])
    trend = 0
    counter = 0
    var prev_o = open[max1_bars]
    var prev_c = close[max1_bars]
    var prev_h = high[max1_bars]
    var prev_l =low[max1_bars]

    var prev_min    = prev_o < prev_c ? prev_o : prev_c
    var prev_max    = prev_o > prev_c ? prev_o : prev_c



    for bi=max1_bars to 0
        if bi<0
            continue
        o = open[bi]
        h = high[bi]
        l = low[bi]
        c = close[bi]

        min = o < c ? o : c
        max = o > c ? o : c

        if o==c and prev_o==prev_c
            trend := 0
            counter := 0
        else if prev_min < c and c < prev_h and o!=c
            trend := 0
            counter := 0
        else if prev_c < c and o!=c
            if trend > 0 
                trend := trend + 1
                counter := counter + 1
            else 
                trend := 1
                prev_c := c
                counter := 0
        else if prev_c > c and o!=c
            if trend > 0
                trend := -1
                prev_c := c
                counter := 0
            else  
                trend := trend - 1 
                counter := counter + 1
        prev_o := o
        prev_c := c
        prev_l := o
        prev_h := c
        
        prev_min := min
        prev_max := max

    strend := counter>=min_trend ? trend : 0

plotchar(strend==1,   title="",  char="1", color=color.green, location=location.abovebar)
plotchar(strend==2,   title="",  char="2", color=color.green, location=location.abovebar)
plotchar(strend==3,   title="",  char="3", color=color.green, location=location.abovebar)
plotchar(strend==4,   title="",  char="4", color=color.green, location=location.abovebar)
plotchar(strend==5,   title="",  char="5", color=color.green, location=location.abovebar)
plotchar(strend==6,   title="",  char="6", color=color.green, location=location.abovebar)
plotchar(strend==7,   title="",  char="7", color=color.green, location=location.abovebar)
plotchar(strend==8,   title="",  char="8", color=color.green, location=location.abovebar)
plotchar(strend==9,   title="",  char="9", color=color.green, location=location.abovebar)
plotchar(strend>=10,   title="",  char="^", color=color.green, location=location.abovebar)

plotchar(strend==-1,   title="",  char="1", color=color.red, location=location.belowbar)
plotchar(strend==-2,   title="",  char="2", color=color.red, location=location.belowbar)
plotchar(strend==-3,   title="",  char="3", color=color.red, location=location.belowbar)
plotchar(strend==-4,   title="",  char="4", color=color.red, location=location.belowbar)
plotchar(strend==-5,   title="",  char="5", color=color.red, location=location.belowbar)
plotchar(strend==-6,   title="",  char="6", color=color.red, location=location.belowbar)
plotchar(strend==-7,   title="",  char="7", color=color.red, location=location.belowbar)
plotchar(strend==-8,   title="",  char="8", color=color.red, location=location.belowbar)
plotchar(strend==-9,   title="",  char="9", color=color.red, location=location.belowbar)
plotchar(strend<=-10,   title="",  char="v", color=color.red, location=location.belowbar)