TTP Green/Red Consecutive Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheTradingParrot

//@version=5
indicator("TTP Green/Red Consecutive Candles v0.1")

redCandle = close < close[1]
greenCandle = close >= close[1]

candleChange = (redCandle and greenCandle[1]) or (greenCandle and redCandle[1])

bsGreen = ta.barssince(greenCandle)
bsRed = ta.barssince(redCandle)

threshold = input.int(5, "Signal threshold") 

varip mG = 0
varip mR = 0

showDistribution = input.bool(false, "Show distribution values")
maL = input.int(50,"Length", group = "Moving Average")
maM = input.float(1.0, "Factor", group = "Moving Average")

maGreen = ta.ema(bsGreen, maL) * maM
maRed = ta.ema(bsRed,maL) * maM

plot(-1* maGreen, "red ma", style=plot.style_histogram, color=color.new(color.orange,50))
plot(maRed, "green ma", style=plot.style_histogram, color=color.new(color.lime,50))

if bsGreen > mG
    mG := bsGreen
if bsRed > mR
    mR := bsRed

plot(-1*bsGreen,"red", color.red)
plot(bsRed, "green", color.green)

plot(mR,"max red", color=color.white, style = plot.style_circles)
plot(-1*mG, "max green", color=color.white, style = plot.style_circles)

max = 30
varip redDistr = array.new<int>(max,0)
varip greenDistr = array.new<int>(max,0)

if candleChange
    if redCandle
        prev = array.get(greenDistr, bsRed[1])
        log.info("prev={0} bsRed[1]={1}",prev, bsRed[1])
        array.set(greenDistr, bsRed[1], prev + 1)
    if greenCandle
        prev = array.get(redDistr, bsGreen[1])
        array.set(redDistr, bsGreen[1], prev + 1)

maxG = max
maxR = max

for i = max to 1
    g = array.get(greenDistr, i-1)
    r = array.get(redDistr, i-1)
    if maxG == max and g > 0
        maxG := i
    if maxR == max and r > 0
        maxR := i

if barstate.isconfirmed and barstate.islastconfirmedhistory and showDistribution
    myLabel = label.new(x=bar_index , y=0, color=color.black, textcolor=color.white, style=label.style_label_left, size=size.large, 
         text= "Green:\n"+str.tostring(greenDistr) + "\nmaxG=" +str.tostring(maxG) + "\nRed:\n"+str.tostring(redDistr) +"\nmaxR="+str.tostring(maxR), textalign=text.align_left )


displayThresholdSignals = input.bool(false, "Display Threshold signals")
displayMASignals = input.bool(false, "Display MA signals")


signalShortThreshold =  bsRed >= threshold
signalLongThreshold =  bsGreen >= threshold

plotshape(displayThresholdSignals and signalShortThreshold, "threshold signal short", style = shape.labeldown, location = location.top, text="TH", textcolor = color.white, color=color.red, display = display.pane)
plotshape(displayThresholdSignals and signalLongThreshold, "threshold signal long", style = shape.labelup, location = location.bottom, text="TH", textcolor = color.white, color=color.green, display = display.pane)

plot(signalShortThreshold ? 1 : 0, "Threshold Short Signal", display = display.data_window)
plot(signalLongThreshold ? 1: 0 , "Threshold Long Signal", display = display.data_window)

alertcondition(signalShortThreshold, "Threshold Short Signal")
alertcondition(signalLongThreshold, "Threshold Long Signal")

signalShortAboveMA = bsRed > maRed
signalLongAboveMA = bsGreen > maGreen

plotshape(displayMASignals  and signalShortAboveMA, "MA signal short", style = shape.labeldown, location = location.top, text="MA", textcolor = color.white, color=color.red, display = display.pane)
plotshape(displayMASignals  and signalLongAboveMA, "MA signal long", style = shape.labelup, location = location.bottom, text="MA", textcolor = color.white, color=color.green, display = display.pane)

plot( signalShortAboveMA ? 1 : 0, "MA Short Signal", display = display.data_window)
plot(signalLongAboveMA ? 1: 0 , "MA Long Signal", display = display.data_window)

alertcondition(signalShortAboveMA, "MA Short Signal")
alertcondition(signalLongAboveMA, "MA Long Signal")

