declare upper;

input short_average = 5;

input medium_average = 10;

input long_average = 20;

input average_type = {default "SMA", "EMA"};

input showArrows = yes;

def MA1;

def MA2;

def MA3;

switch (average_type) {

case "SMA":

MA1 = Average(close, short_average);

MA2 = Average(close, medium_average);

MA3 = Average(close, long_average);

case "EMA":

MA1 = ExpAverage(close, short_average);

MA2 = ExpAverage(close, medium_average);

MA3 = ExpAverage(close, long_average);

}

# define e-signal and crossover point

def Eup = MA1 > MA2 && MA2 > MA3;

def Edn = MA1 < MA2 && MA2 < MA3;

def CrossUp = close > MA1 && Eup && !Eup[1];

def CrossDn = close < MA1 && Edn && !Edn[1];

# Define up and down signals

def higherHigh = close > Highest(Max(open, close), 3)[1];

def lowerLow = close < Lowest(Min(open, close), 3)[1];

def SignalUp = if (CrossUp && higherHigh)

then 1

else if (CrossUp[1] && higherHigh && !higherHigh[1])

then 1

else if (CrossUp[2] && higherHigh && !higherHigh[1] && !higherHigh[2])

then 1

else Double.NaN;

def SignalDn = if (CrossDn && lowerLow)

then 1

else if (CrossDn[1] && lowerLow && !lowerLow[1])

then 1

else if (CrossDn[2] && lowerLow && !lowerLow[1] && !lowerLow[2])

then 1

else Double.NaN;

# Plot the moving average lines

plot ln1 = MA1;

ln1.SetDefaultColor(CreateColor(145, 210, 144));

ln1.SetLineWeight(2);

plot ln2 = MA2;

ln2.SetDefaultColor(CreateColor(111, 183, 214));

ln2.SetLineWeight(2);

plot ln3 = MA3;

ln3.SetDefaultColor(CreateColor(249, 140, 182));

ln3.SetLineWeight(2);

# Show an arrow

plot ArrowUp = if SignalUp and showArrows then low * 0.997 else Double.NaN;

ArrowUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

ArrowUp.SetLineWeight(4);

ArrowUp.SetDefaultColor(Color.YELLOW);

plot ArrowDn = if SignalDn and showArrows then high * 1.003 else Double.NaN;

ArrowDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

ArrowDn.SetLineWeight(4);

ArrowDn.SetDefaultColor(Color.CYAN);

# Add label for Eup or Edn

AddLabel(Eup, "E Up", CreateColor(134, 202, 93));

AddLabel(Edn, "E Dn", CreateColor(232, 148, 157));

# Calculate Meter Values

def sl1 = ma1 > ma1[1];

def sl2 = ma2 > ma2[1];

def sl3 = ma3 > ma3[1];

def p1 = close > ma1;

def p2 = close > ma2;

def p3 = close > ma3;

def u1 = 2 * (close > open);

def u2 = 2 * (close > high[1]);

def d1 = 2 * (close < open);

def d2 = 2 * (close < low[1]);

def UPstr = sl1 + sl2 + sl3 + p1 + p2 + p3 + u1 + u2;

def DNstr = !sl1 + !sl2 + !sl3 + !p1 +!p2 + !p3 + d1 + d2;

# Add Strength Meter

AddLabel(Eup or Edn, " ", if Eup and UPstr >= 3 then CreateColor(128, 199, 94) else if Edn and DNstr >= 3 then CreateColor(232, 143, 153) else CreateColor(177,177,166));

AddLabel(Eup or Edn, " ", if Eup and UPstr >= 6 then CreateColor(99, 197, 72) else if Edn and DNstr >= 6 then CreateColor(238, 123, 136) else CreateColor(177,177,166));

AddLabel(Eup or Edn, " ", if Eup and UPstr >= 8 then CreateColor(65, 195, 50) else if Edn and DNstr >= 8 then CreateColor(245, 101, 117) else CreateColor(177,177,166));

AddLabel(Eup or Edn, " ", if Eup and UPstr == 10 then CreateColor(41, 192, 36) else if Edn and DNstr == 10 then CreateColor(249, 81, 101) else CreateColor(177,177,166));