Consecutive positive/negative candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fbaghyari

//@version=5
indicator("Consecutive positive/negative candles")
period = input.int(10, "sma period")
var negative = 0
var positive = 0
var maxPositive = 0
var maxNegative = 0

if close<close[1]
    negative := negative - 1
else
    negative := 0
if close>close[1]
    positive := positive + 1
else
    positive := 0
if negative <= maxNegative
    maxNegative := negative
if positive >= maxPositive
    maxPositive := positive
    
nSMA = ta.sma(negative, period)
pSMA = ta.sma(positive, period)
    
plot(positive,title="Positive candles" ,color=color.green, style=plot.style_histogram)
plot(negative,title="Negative candles" ,color=color.red, style=plot.style_histogram)

plot(nSMA, title="Negative sma" ,color=color.orange)
plot(pSMA, title="positive sma" ,color=color.blue)

plot(maxPositive, "max consecutive positive", color=color.blue)
plot(maxNegative, "max consecutive negative", color=color.orange)