Gradiente
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jacobfabris

//@version=4
study("Gradiente",overlay=false)
//--- setting periods 1 and 2
P1 = input(defval = 9, minval = 4,maxval=2000)
P2 = input(defval = 21,minval = 4,maxval=2000)
//--- calculating EMA
a = ema(close,P1)
b = ema(close,P2)
//--- strating counter + and -
c = 1.0
d = -1.0
c:= a>b ? nz(c[1])+(a-b) : 1.0
d:= a<b ? nz(d[1])+(a-b) : -1.0
cp = 0.0
cn = 0.0
//--- calculating area bwtween EMA when + and -
cp:= a>b  ? nz(cp[1])+1 : 1
cn:= a<b  ? nz(cn[1])+1 : 1
//--- calculating the gradient of increase of area
gp= c/cp 
gn= d/cn
//--- filtering spikes
sp = ema(gp,3)
sn = ema(gn,3)
//--- stablishing the colors
corp = gp == 1 ? #FF000000 : gp>1 and gp>gp[1] ?  color.green : gp>1 and gp<gp[1] and gp>sp ? color.white : color.gray 
corn = gn == -1 ? #FF000000 : gn<-1 and gn<gn[1] ?  color.red : gn<-11 and gn>gn[1] and gn<sn ? color.white : color.gray 
//--- ploting
plot(gp,color=corp,linewidth =2)
plot(gn,color=corn,linewidth =2)