declare lower;
input FastLength = 12; # Faster of the two moving averages
input SlowLength = 26; # Slower of the two moving averages
input FastAvgSmoothingLength = 9;
input FastEMA_Length = 8;
def FastEMAavg = ExpAverage(close,FastEMA_Length);
def P1L = 1;
def PP5L = 0.5;
def ZL = 0;
def MP5L = -0.5;
def M1L = -1;
def SlowAvg = ExpAverage(close,SlowLength); # ema of the slow moving average
def FastAvg = ExpAverage(close,FastLength); # ema of the fast moving average
plot FastSlowPctDiff = ((FastAvg-SlowAvg)/SlowAvg) * 100; # Percent difference between FastAvg and SlowAvg
def FastPctDiff = ((FastAvg - FastAvg[1])/FastAvg[1]) * 100; # Percent difference betwee current and previous bar
plot FastPctDiffAvg = ExpAverage(FastPctDiff,FastAvgSmoothingLength);
FastPctDiffAvg.SetDefaultColor(color.cyan);
def PDGTP = if FastSlowPctDiff > FastSlowPctDiff[1] then 1 else 0;
def PDLTP = if FastSlowPctDiff < FastSlowPctDiff[1] then 1 else 0;
FastSlowPctDiff.AssignValueColor(if PDGTP then Color.GREEN else if PDLTP then Color.RED else Color.YELLOW);
FastSlowPctDiff.SetLineWeight(2);
plot ZLine = ZL;
ZLine.SetDefaultColor(Color.white);
plot P1Line = P1L;
P1Line.SetDefaultColor(color.green);
Plot M1Line = M1L;
M1Line.SetDefaultColor(color.red);
Plot PP5Line = PP5L;
PP5Line.SetDefaultColor(Color.yellow);
PP5Line.SetPaintingStrategy(PaintingStrategy.DASHES);
Plot MP5Line = MP5L;
MP5Line.SetDefaultColor(color.yellow);
MP5Line.SetPaintingStrategy(PaintingStrategy.DASHES);
plot FastEMAImproving = if FastEMAavg > FastEMAavg[1] then 1 else 0;
FastEMAImproving.Hide();
AddLabel(yes,FastEMA_Length + " EMA Improving", if FastEMAImproving then color.green else if FastEMAavg == FastEMAavg[1] then color.gray else color.red);
AddLabel(yes,FastLength + " EMA Improving",if FastAvg > FastAvg[1] then color.green else color.red);
def BullConditionGo = if FastPctDiffAvg > FastPctDiffAvg[1] and FastSlowPctDiff > FastSlowPctDiff[1] and FastAvg > FastAvg[1] then 1 else 0;
AddLabel(yes, if BullConditionGo then " Bull Go " else "Bull No Go",if BullConditionGo then color.green else color.red);
def BullConditionGoPlus = if BullConditionGo and FastSlowPctDiff > 0 then 1 else 0;
AddLabel(yes,if BullConditionGoPlus then "Bull Go Plus" else " ", if BullConditionGoPlus then color.green else color.gray);
def BearConditionGo = if FastPctDiffAvg < FastPctDiffAvg[1] and FastSlowPctDiff < FastSlowPctDiff[1] and FastAvg < FastAvg[1] then 1 else 0;
AddLabel(yes, if BearConditionGo then " Bear Go " else "Bear No Go",if BearConditionGo then color.green else color.red);
def BearConditionGoPlus = if BearConditionGo and FastSlowPctDiff < 0 then 1 else 0;
AddLabel(yes,if BearConditionGoPlus then "Bear Go Plus" else " ", if BearConditionGoPlus then color.green else color.gray);
# Conditions for scanning
plot BullGoPlus = if BullConditionGoPlus then 1 else 0;
BullGoPlus.Hide();
plot BearGoPlus = if BearConditionGoPlus then 1 else 0;
BearGoPlus.Hide();
plot highestPos = if Highest(FastSlowPctDiff,5) <= 1 then 1 else 0;
highestPos.Hide();
plot lowestPos = if Highest(FastSlowPctDiff,5) >= 0 then 1 else 0;
lowestPos.Hide();
def TanAngle = ATan(close);
AddLabel(yes,"'Angle Improving'", if (TanAngle > TanAngle[1]) then color.green else color.red);
AddLabel(yes,"Previous 'Angle': " + TanAngle[1],color.gray);
AddLabel(yes,"Current 'Angle': " + TanAngle,color.gray);