#EMA_x5_Stack
#Created by rad14733 for usethinkscript.com
#v1.0 2021-01-02
#NOTE: EMA crossovers can be set to only paint based on long term trends.
# When configured, EMA crossovers will only paint when filtered by longer term trends (ema1 <> ema5).
# EMA Length's
input ema1Length = 8;
input ema2Length = 21;
input ema3Length = 34;
input ema4Length = 55;
input ema5Length = 89;
input showXovers = yes;
# Display EMA's Stacked Label
input show_stacked_label = yes;
input show_ema_label = yes;
# Conditionally Plot EMA's
plot ema1 = ExpAverage(close, ema1Length);
ema1.SetPaintingStrategy(PaintingStrategy.LINE);
ema1.SetStyle(Curve.FIRM);
ema1.SetLineWeight(1);
ema1.SetDefaultColor(Color.GREEN);
plot ema2 = ExpAverage(close, ema2Length);
ema2.SetPaintingStrategy(PaintingStrategy.LINE);
ema2.SetStyle(Curve.FIRM);
ema2.SetLineWeight(1);
ema2.SetDefaultColor(Color.YELLOW);
plot ema3 = ExpAverage(close, ema3Length);
ema3.SetPaintingStrategy(PaintingStrategy.LINE);
ema3.SetStyle(Curve.FIRM);
ema3.SetLineWeight(1);
ema3.SetDefaultColor(Color.RED);
plot ema4 = ExpAverage(close, ema4Length);
ema4.SetPaintingStrategy(PaintingStrategy.LINE);
ema4.SetStyle(Curve.FIRM);
ema4.SetLineWeight(1);
ema4.SetDefaultColor(Color.MAGENTA);
plot ema5 = ExpAverage(close, ema5Length);
ema5.SetPaintingStrategy(PaintingStrategy.LINE);
ema5.SetStyle(Curve.FIRM);
ema5.SetLineWeight(1);
ema5.SetDefaultColor(Color.BLUE);
# Calculate EMA Crossovers
plot x1a2 = showXovers and ema1 crosses above ema2;
x1a2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
x1a2.SetStyle(Curve.FIRM);
x1a2.SetLineWeight(3);
x1a2.SetDefaultColor(Color.YELLOW);
plot x1b2 = showXovers and ema1 crosses below ema2;
x1b2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
x1b2.SetStyle(Curve.FIRM);
x1b2.SetLineWeight(3);
x1b2.SetDefaultColor(Color.DARK_ORANGE);
# Define Global Colors For Clouds
# Crosses Above
DefineGlobalCOlor("x1a2", Color.GREEN);
DefineGlobalCOlor("x2a3", Color.DARK_GREEN);
DefineGlobalCOlor("x3a4", Color.YELLOW);
DefineGlobalCOlor("x4a5", Color.WHITE);
# Crosses Below
DefineGlobalCOlor("x1b2", Color.RED);
DefineGlobalCOlor("x2b3", Color.DARK_RED);
DefineGlobalCOlor("x3b4", Color.MAGENTA);
DefineGlobalCOlor("x4b5", Color.BLUE);
# Paint Clouds
input showClouds = yes;
AddCloud(if showClouds then ema1 else Double.NaN, if showClouds then ema2 else Double.NaN, GlobalColor("x1a2"), GlobalColor("x1b2"));
AddCloud(ema2, ema3, GlobalColor("x2a3"), GlobalColor("x2b3"));
AddCloud(ema3, ema4, GlobalColor("x3a4"), GlobalColor("x3b4"));
AddCloud(ema4, ema5, GlobalColor("x4a5"), GlobalColor("x4b5"));
# Create Chart Label to signify that EMA's are Stacked
def stackedUp = ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5;
def stackedDn = ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5;
AddLabel(show_ema_label, "" + ema1Length + "|" + ema2Length + "|" + ema3Length + "|" + ema4Length + "|" + ema5Length + "|" + "EMAs", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
AddLabel(show_stacked_label, "EMAs Stacked", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
# END EMA_5_Stack