Trendinator Lite
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ©marcuskhlim
//@version=5
indicator(shorttitle="Trendinator Lite", title="Trendinator Lite", overlay=true)
var bars_since_started_trending_up = 0
var bars_since_started_trending_down = 0
var bars_since_bb_up_in_no_trending = 0
var bars_since_bb_down_in_no_trending = 0
var started_trending_up = false
var started_trending_down = false
var stopped_trending = true
var got_bb_up_touch = false
var got_bb_down_touch = false
var float highest_bollinger_up_touch = na
var float highest_close = 0
var float lowest_bollinger_down_touch = na
var float lowest_close = 0
var color colorForNextCandle = na

basis = ta.sma(close, 20)
dev = 2.0 * ta.stdev(close, 20)
upper = basis + dev
lower = basis - dev

consider_bar_up = true
consider_bar_down = true

if started_trending_up
	x = bar_index
	y = high
	txt = str.tostring(bars_since_started_trending_up)
	
    if high > highest_bollinger_up_touch
        bars_since_started_trending_up := 0
		highest_bollinger_up_touch := high
	else
		bars_since_started_trending_up := bars_since_started_trending_up + 1

    if low <= lower or bars_since_started_trending_up >= 20
		got_bb_up_touch := false
        started_trending_up := false
        started_trending_down := false
        stopped_trending := true
        highest_bollinger_up_touch := na
        lowest_bollinger_down_touch := na
		if high >= upper
			consider_bar_up := false


if started_trending_down
    if low < lowest_bollinger_down_touch
        bars_since_started_trending_down := 0
		lowest_bollinger_down_touch := low
	else
		bars_since_started_trending_down := bars_since_started_trending_down + 1
    
    if high >= upper or bars_since_started_trending_down >= 20
		got_bb_down_touch := false
        started_trending_down := false
        started_trending_up := false
        stopped_trending := true
        highest_bollinger_up_touch := na
        lowest_bollinger_down_touch := na
		if low <= lower
			consider_bar_down := false
		

if stopped_trending

	bars_since_bb_up_in_no_trending := bars_since_bb_up_in_no_trending + 1
	
	bars_since_bb_down_in_no_trending := bars_since_bb_down_in_no_trending + 1
	

    if high > highest_bollinger_up_touch and close > highest_close and got_bb_up_touch and low > lower
        started_trending_up := true
        stopped_trending := false
        bars_since_started_trending_up := 0
        got_bb_up_touch := false
    

    if low < lowest_bollinger_down_touch and close < lowest_close and got_bb_down_touch and high < upper
        started_trending_down := true    
        stopped_trending := false

        bars_since_started_trending_down := 0
        got_bb_down_touch := false
		
    if low <= lower and consider_bar_down
        
        if na(lowest_bollinger_down_touch) or low <= lowest_bollinger_down_touch
			bars_since_bb_down_in_no_trending := 0
            lowest_bollinger_down_touch := low
            if not got_bb_down_touch
				got_bb_down_touch := true
                lowest_close := close
            else
                if close < lowest_close
		            lowest_close := close
       
			if high < upper			
				got_bb_up_touch := false
		if high >= upper
			highest_bollinger_up_touch := high
		else	
			highest_bollinger_up_touch := na
        
            
    if high >= upper and consider_bar_up
        
        if na(highest_bollinger_up_touch) or high >= highest_bollinger_up_touch
			bars_since_bb_up_in_no_trending := 0
            highest_bollinger_up_touch := high
            if not got_bb_up_touch
				got_bb_up_touch := true
                highest_close := close
            else
                if close > highest_close
		            highest_close := close      
			if low > lower			         
				got_bb_down_touch := false
		if low <= lower
			lowest_bollinger_down_touch := low
		else	
			lowest_bollinger_down_touch := na

        
	if not na(highest_bollinger_up_touch) and high > highest_bollinger_up_touch
		highest_bollinger_up_touch := high
		
	if not na(lowest_bollinger_down_touch) and low < lowest_bollinger_down_touch
		lowest_bollinger_down_touch := low		
	
	if close > highest_close
		highest_close := close
		
	if close < lowest_close
		lowest_close := close


// Colour background
backgroundColour = if started_trending_up
    color.new(color.green, 85)
else if started_trending_down
    color.new(color.red, 85)
else if stopped_trending
    color.new(color.yellow, 85)

upper_bb=plot(upper,'upper_bb',offset=0,color=color.rgb(0, 0,255))
lower_bb=plot(lower,'lower_bb',offset=0,color=color.rgb(0, 0,255))
fill(upper_bb, lower_bb, colorForNextCandle)
colorForNextCandle := backgroundColour