#Stiffness MA/Ehlers Clouds
input MALength = 100;
input StiffnessLength = 60;
input Threshold = 90;
input ELlength = 23;
input coloredCandlesOn = no;
input show_ema_cloud_Ehlers = yes;
input show_ema_cloud_MA = no;
input show_Ehlers_arrows = yes;
input show_MA_arrows = no;
def price = (high + low) / 2;
def coeff = ELlength * price * price - 2 * price * sum(price, ELlength)[1] + sum(price * price, ELlength)[1];
plot Ehlers = sum(coeff * price, ELlength) / sum(coeff, ELlength);
Ehlers.SetDefaultColor(Color.light_gray);
def c = close;
def o = open;
def MAValue = MovingAverage(AverageType.SIMPLE, close, MALength);
def MACorValue = MAValue - 0.2 * StDev(close, MALength);
def NumAboveMA = if close > MAValue then NumAboveMA[1] + 1 else 0;
def MAValue2 = sum(coeff * price, ELlength) / sum(coeff, ELlength);
def MACorValue2 = MAValue2 - 0.2 * StDev(close, ELLength);
def NumAboveMA2 = if close > MAValue2 then NumAboveMA2[1] + 1 else 0;
def Stiffness_Ehlers = 100 * (NumAboveMA2 / StiffnessLength);
def Stiffness_Ehlers_2 = ExpAverage(Stiffness_Ehlers, StiffnessLength);
plot MA_COR = MACorValue;
plot MA = MAValue;
plot MA_COR2 = MACorValue2;
plot MA2 = MAValue2;
#def compressed = MAValue2 - MACorValue2;
#def compression = if (compressed <= 1) then 1 else 0;
#plot Squeeze_Alert = if compression then 1 else 0;
#Squeeze_Alert.SetPaintingStrategy(PaintingStrategy.POINTS);
#Squeeze_Alert.SetLineWeight(5);
#Squeeze_Alert.SetDefaultColor(Color.white);
def MA_Cor2_UP = if ((MA_cor2 > MA_cor2[1])) then 1 else 0;
def MA_Cor2_dn = if ((MA_cor2 < MA_cor2[1])) then 1 else 0;
def MA_Cor_UP = if ((MA_cor > MA_cor[1])) then 1 else 0;
def MA_Cor_dn = if ((MA_cor < MA_cor[1])) then 1 else 0;
plot MA_Cor2_UP_1 = if show_MA_arrows and MA_Cor2_UP and !MA_Cor2_UP[1] then low else Double.NaN;
MA_Cor2_UP_1.SetPaintingStrategy(PaintingSTrategy.ARROW_up);
MA_Cor2_UP_1.SetDefaultColor(Color.white);
plot MA_Cor2_DN_1 = if show_MA_arrows and MA_Cor2_DN and !MA_Cor2_DN[1] then high else Double.NaN;
MA_Cor2_DN_1.SetPaintingSTrategy(paintingSTrategy.Arrow_DOWN);
MA_Cor2_DN_1.SetDefaultColor(Color.white);
plot MA_Cor_UP_2 = if show_Ehlers_arrows and MA_Cor2_UP and !MA_Cor2_UP[1] then low else Double.NaN;
MA_Cor_UP_2.SetPaintingStrategy(PaintingSTrategy.ARROW_up);
MA_Cor_UP_2.SetDefaultColor(Color.white);
plot MA_Cor_DN_2 = if show_Ehlers_arrows and MA_Cor2_DN and !MA_Cor2_DN[1] then high else Double.NaN;
MA_Cor_DN_2.SetPaintingSTrategy(paintingSTrategy.Arrow_DOWN);
MA_Cor_DN_2.SetDefaultColor(Color.white);
AddCloud(if show_ema_cloud_Ehlers and MA_Cor2 < close then MA_cor2 else MA2, MA_cor2, Color.LIGHT_red, Color.green);
AddCloud(if show_ema_cloud_Ehlers and MA_Cor2 > close then MA2 else MA_COR2, MA2, Color.LIGHT_red, Color.green);
AddCloud(if show_ema_cloud_MA and ((MA_cor > MA_cor[1])and(MA > MA[1])) then MA_cor else MA, MA, Color.LIGHT_red, Color.green);
AddCloud(if show_ema_cloud_MA and ((MA_cor < MA_cor[1])and(MA < MA[1])) then MA else MA_COR, MA_COR, Color.LIGHT_red, Color.green);
#plot Crossup3 = Crossup2;
#Crossup3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_Up);
#Crossup3.SetDefaultColor(Color.Orange);
#Crossup3.SetLineWeight(arrow_size2);
#plot Crossdown3 = Crossdown2;
#Crossdown3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_Down);
#Crossdown3.SetDefaultColor(Color.orange);
#Crossdown3.SetLineWeight(arrow_size2);
#plot Stiffness = Stiffness_1;
#plot StiffnessEMA = StiffnessEMA_1;
#Stiffness.SetDefaultColor(GetColor(3));
#StiffnessEMA.SetDefaultColor(GetColor(4));