Consecutive Candles lite | Multi Timeframe
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

indicator("CC lite • Yata", 
     overlay = true, 
     timeframe = "")

// -----------------------------------
groupCC = ""
// -----------------------------------

lastN = input.string("6 to 9", title="Show", options=["6 to 9", "7, 8 and 9", "8 and 9", "Only 9", "None"], inline="CC1", group=groupCC)
showSR	= input(false, title="Show Support and Resistance", inline="CC", group=groupCC)

Coun13 = input(true, title="Std 13", inline="CC1", group=groupCC)
CounCCA13 = input(true, title="Ag. 13", inline="CC1", group=groupCC)

locB = input.string("Below", title="Label: Buy"	, options=["Above", "Below"], inline="CC5", group=groupCC)
locA = input.string("Above", title="Sell"		, options=["Above", "Below"], inline="CC5", group=groupCC)

locBc = locB == "Above" ? location.abovebar : locB == "Below" ? location.belowbar : na
locAc = locA == "Above" ? location.abovebar : locA == "Below" ? location.belowbar : na

// -----------------------------------

i_style0 = "Label"
i_style1 = "Arrow"
i_style2 = "Triangle"
i_style3 = "Circle"
i_style4 = "Cross"

Bstyle = input.string(i_style0, title="Style: Buy"	, options=[i_style0, i_style1, i_style2, i_style3, i_style4], inline="CC6", group=groupCC)
Sstyle = input.string(i_style0, title="Sell"		, options=[i_style0, i_style1, i_style2, i_style3, i_style4], inline="CC6", group=groupCC)

f_getStyleB(_inputStyle) =>
    _return = _inputStyle == i_style1 ? shape.arrowup		:
              _inputStyle == i_style2 ? shape.triangleup	:
              _inputStyle == i_style3 ? shape.circle		:
              _inputStyle == i_style4 ? shape.cross			: shape.labelup
    _return

f_getStyleS(_inputStyle) =>
    _return = _inputStyle == i_style1 ? shape.arrowdown		: 
              _inputStyle == i_style2 ? shape.triangledown	:
              _inputStyle == i_style3 ? shape.circle		:
              _inputStyle == i_style4 ? shape.xcross		: shape.labeldown
    _return

// -----------------------------------

sellSet = 0
sellSet := close > close[4] ? sellSet[1] == 9 ? 1 : sellSet[1] + 1 : 0

buySet = 0
buySet := close < close[4] ? buySet[1] == 9 ? 1 : buySet[1] + 1 : 0

// -----------------------------------

highest9		= ta.highest(9)
highTrendLine	= 0.0
highTrendLine	:= buySet == 9 ? highest9 : close > highTrendLine[1] ? 0 : highTrendLine[1]

lowest9			= ta.lowest(9)
lowTrendLine	= 0.0
lowTrendLine	:= sellSet == 9 ? lowest9 : close < lowTrendLine[1] ? 0 : lowTrendLine[1]

S_color	= input.color(color.new(#90EE90, 25), title="|", inline="CC", group=groupCC)
R_color	= input.color(color.new(#E21B22, 25), title="", inline="CC", group=groupCC)

plot(showSR and lowTrendLine > 0 ? lowTrendLine : na, style=plot.style_circles, linewidth=1, color=S_color, title="Support")
plot(showSR and highTrendLine > 0 ? highTrendLine : na, style=plot.style_circles, linewidth=1, color=R_color, title="Resistance")

// -----------------------------------

buyCounCC		= 0.0
buyCounCC8Close	= 0.0
isBuyCounCC		= close < low[2]
nonQBuy13		= isBuyCounCC and math.abs(buyCounCC[1]) == 12 and low > buyCounCC8Close[1]
buyCounCC		:= buySet == 9 ? isBuyCounCC ? 1 : 0 : sellSet == 9 or highTrendLine == 0 ? 14 : nonQBuy13 ? -12 : isBuyCounCC ? math.abs(buyCounCC[1]) + 1 : -math.abs(buyCounCC[1])
nonQBuy13		:= nonQBuy13 and buyCounCC == -12
buyCounCC8Close	:= buyCounCC == 8 ? close : buyCounCC8Close[1]

sellCounCC		= 0.0
sellCounCC8Close = 0.0
isSellCounCC	= close > high[2]
nonQSell13		= isSellCounCC and math.abs(sellCounCC[1]) == 12 and high < sellCounCC8Close[1]
sellCounCC		:= sellSet == 9 ? isSellCounCC ? 1 : 0 : buySet == 9 or lowTrendLine == 0 ? 14 : nonQSell13 ? -12 : isSellCounCC ? math.abs(sellCounCC[1]) + 1 : -math.abs(sellCounCC[1])
nonQSell13		:= nonQSell13 and sellCounCC == -12
sellCounCC8Close := sellCounCC == 8 ? close : sellCounCC8Close[1]

isAggressiveBuy	= low < low[2]
aggressiveBuy	= 0.0
aggressiveBuy	:= buySet == 9 ? isAggressiveBuy ? 1 : 0 : sellSet == 9 or highTrendLine == 0 ? 14 : isAggressiveBuy ? math.abs(aggressiveBuy[1]) + 1 : -math.abs(aggressiveBuy[1])

isAggressiveSell = high > high[2]
aggressiveSell	 = 0.0
aggressiveSell	 := sellSet == 9 ? isAggressiveSell ? 1 : 0 : buySet == 9 or lowTrendLine == 0 ? 14 : isAggressiveSell ? math.abs(aggressiveSell[1]) + 1 : -math.abs(aggressiveSell[1])

// -----------------------------------

color_btext = input.color(color.new(color.white	, 0), title="Colors: Buy", inline="C", group=groupCC)
color_stext = input.color(color.new(color.white	, 0), title="Sell", inline="C", group=groupCC)

color_b6 = input.color(color.new(#77F1B0		, 0), title="6", inline="CB", group=groupCC)
color_b7 = input.color(color.new(#00ADAD		, 0), title="7", inline="CB", group=groupCC)
color_b8 = input.color(color.new(#0067FF		, 0), title="8", inline="CB", group=groupCC)
color_b9 = input.color(color.new(#0014C4		, 0), title="9", inline="CB", group=groupCC)

color_s6 = input.color(color.new(color.yellow	, 0), title="6", inline="CS", group=groupCC)
color_s7 = input.color(color.new(color.orange	, 0), title="7", inline="CS", group=groupCC)
color_s8 = input.color(color.new(color.red		, 0), title="8", inline="CS", group=groupCC)
color_s9 = input.color(color.new(color.maroon	, 0), title="9", inline="CS", group=groupCC)

color_b13 = input.color(color.new(color.blue	, 0), title="13", inline="CB", group=groupCC)
color_s13 = input.color(color.new(#d69d00		, 0), title="13", inline="CS", group=groupCC)
color_bA13 = input.color(color.new(#6400d6		, 0), title="A13", inline="CB", group=groupCC)
color_sA13 = input.color(color.new(#ff8629		, 0), title="A13", inline="CS", group=groupCC)

// -----------------------------------

plotshape(lastN == "6 to 9" ? 
     buySet == 6 : na, location=locBc, style=f_getStyleB(Bstyle), size=size.auto, color=color_b6, text="", title="lb6", textcolor=color_btext)
plotshape(lastN == "6 to 9" or lastN == "7, 8 and 9" ? 
     buySet == 7 : na, location=locBc, style=f_getStyleB(Bstyle), size=size.auto, color=color_b7, text="", title="lb7", textcolor=color_btext)
plotshape(lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" ? 
     buySet == 8 : na, location=locBc, style=f_getStyleB(Bstyle), size=size.auto, color=color_b8, text="", title="lb8", textcolor=color_btext)
plotshape(lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" or lastN == "Only 9" ? 
     buySet == 9 : na, location=locBc, style=f_getStyleB(Bstyle), size=size.auto, color=color_b9, text="9", title="lb9", textcolor=color_btext)

plotshape(lastN == "6 to 9" ? 
     sellSet == 6 : na, location=locAc, style=f_getStyleS(Sstyle), size=size.auto, color=color_s6, text="", title="ls6", textcolor=color_stext)
plotshape(lastN == "6 to 9" or lastN == "7, 8 and 9" ? 
     sellSet == 7 : na, location=locAc, style=f_getStyleS(Sstyle), size=size.auto, color=color_s7, text="", title="ls7", textcolor=color_stext)
plotshape(lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" ? 
     sellSet == 8 : na, location=locAc, style=f_getStyleS(Sstyle), size=size.auto, color=color_s8, text="", title="ls8", textcolor=color_stext)
plotshape(lastN == "6 to 9" or lastN == "7, 8 and 9" or lastN == "8 and 9" or lastN == "Only 9" ? 
     sellSet == 9 : na, location=locAc, style=f_getStyleS(Sstyle), size=size.auto, color=color_s9, text="9", title="ls9", textcolor=color_stext)

// -----------------------------------

showST = input(false, title="St. 9", inline="CC1", group=groupCC)

stealthBuy = ta.barssince(buySet == 8) <= 1 and sellSet == 1
stealthSell = ta.barssince(sellSet == 8) <= 1 and buySet == 1

plotshape(showST ? stealthBuy : na, location=locBc, style=f_getStyleB(Bstyle), size=size.auto, color=color_b9, text="s9", title="lb9", textcolor=color_btext)

plotshape(showST ? stealthSell : na, location=locAc, style=f_getStyleS(Sstyle), size=size.auto, color=color_s9, text="s9", title="ls9", textcolor=color_stext)

// -----------------------------------

plotshape(Coun13 ? 
     sellCounCC == 13 : na, location=locAc, style=f_getStyleS(Sstyle), size=size.auto, color=color_s13, text="13", title="lsc13", textcolor=color_stext)
plotshape(Coun13 ? 
     buyCounCC == 13 : na, location=locBc, style=f_getStyleB(Bstyle), size=size.auto, color=color_b13, text="13", title="lbc13", textcolor=color_btext)

plotshape(CounCCA13 ? 
     aggressiveBuy == 13 : na, location=locBc, style=f_getStyleB(Bstyle), size=size.auto, color=color_bA13, text="a13", title="lbc13", textcolor=color_btext)
plotshape(CounCCA13 ? 
     aggressiveSell == 13 : na, location=locAc, style=f_getStyleS(Sstyle), size=size.auto, color=color_sA13, text="a13", title="lsc13", textcolor=color_stext)

// -----------------------------------

ct9 = buySet == 9 or sellSet == 9
ct13 = buyCounCC == 13 or sellCounCC == 13
cta13 = aggressiveBuy == 13 or aggressiveSell == 13
ct913 = ct9 or ct13

alertcondition(ct9	, title="9 Alert"		, message="9 \n\nTicker: {{ticker}}\nTime: {{time}}\nPrice: {{close}}")
alertcondition(ct13	, title="13 Alert"		, message="13 \n\nTicker: {{ticker}}\nTime: {{time}}\nPrice: {{close}}")
alertcondition(cta13	, title="a13 Alert"		, message="a13 \n\nTicker: {{ticker}}\nTime: {{time}}\nPrice: {{close}}")
alertcondition(ct913	, title="9 and 13 Alert", message="9, 13 \n\nTicker: {{ticker}}\nTime: {{time}}\nPrice: {{close}}")