# Trend Advisor Market Phases (MA type selectable)
declare upper;
input price = close;
input displace = 0;
# ---- MA length inputs ----
input fastavg = 50;
input slowavg = 200;
input avglength = 10; # pullback average length
# ---- MA type inputs (choose Simple, Exponential, Weighted, Wilders, Hull) ----
input fastAvgType = AverageType.SIMPLE; # default can be changed
input slowAvgType = AverageType.SIMPLE;
input pbAvgType = AverageType.EXPONENTIAL;
# ---- compute moving averages using the chosen types ----
def fastMA = MovingAverage(fastAvgType, price[-displace], fastavg);
plot fastmaPlot = fastMA;
fastmaPlot.SetDefaultColor(Color.Green);
def slowMA = MovingAverage(slowAvgType, price[-displace], slowavg);
plot slowmaPlot = slowMA;
slowmaPlot.SetDefaultColor(Color.Red);
# ---- pullback average (used for entries) ----
def pbavg = MovingAverage(pbAvgType, price[-displace], avglength);
# price/volume defs & labels
def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def SV = V * (H - C) / (H - L);
def BV = V * (C - L) / (H - L);
AddLabel(yes, "Buyer Vol Strong ", if high > high[1] and low > low[1] and BV*1.05 > SV then Color.GREEN else color.black);
AddLabel(yes, "Seller Vol Strong", if high < high[1] and low < low[1] and SV*1.05 > BV then Color.MAGENTA else color.black);
AddLabel(yes, "Price Strong ", if high > high[1] and high [1] > high[2] and low > low[1] and low[1] > low[2] then Color.GREEN else color.black);
AddLabel(yes, "Price Weak", if high < high[1] and high[1] < high[2] and low < low[1] and low[1] < low[2] then Color.MAGENTA else color.black);
# Phases using fastMA & slowMA
def bullphase = fastMA > slowMA && price > fastMA && price > slowMA;
def accphase = fastMA < slowMA && price > fastMA && price > slowMA;
def recphase = fastMA < slowMA && price < slowMA && price > fastMA;
def bearphase = fastMA < slowMA && price < fastMA && price < slowMA;
def distphase = fastMA > slowMA && price < fastMA && price < slowMA;
def warnphase = fastMA > slowMA && price > slowMA && price < fastMA;
# pullback signals (use pbavg)
input bullpullback = yes;
input bearpullback = yes;
def bullishpb = low < pbavg && open > pbavg;
def bearishpb = high > pbavg && open < pbavg;
def bullpb = bullphase and bullishpb;
def bearpb = bearphase and bearishpb;
plot bullpb1 = if bullpullback and bullpb then 1 else Double.NaN;
bullpb1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullpb1.SetDefaultColor(Color.CYAN);
bullpb1.SetLineWeight(1);
plot bearpb1 = if bearpullback and bearpb then 1 else Double.NaN;
bearpb1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearpb1.SetDefaultColor(Color.RED);
bearpb1.SetLineWeight(1);
# phase plots (hidden) + labels
plot buphase = bullphase; buphase.Hide();
plot acphase = accphase; acphase.Hide();
plot rephase = recphase; rephase.Hide();
plot bephase = bearphase; bephase.Hide();
plot dphase = distphase; dphase.Hide();
plot wphase = warnphase; wphase.Hide();
AddLabel(bullphase, " Bull Phase" , if bullphase then Color.GREEN else Color.BLACK);
AddLabel(accphase, " Accumation Phase ", if accphase then Color.LIGHT_GREEN else Color.BLACK);
AddLabel(recphase, " Recovery Phase ", if recphase then Color.LIGHT_ORANGE else Color.BLACK);
AddLabel(warnphase, " Warning Phase ", if warnphase then Color.ORANGE else Color.BLACK);
AddLabel(distphase, " Distribution Phase ", if distphase then Color.LIGHT_RED else Color.BLACK);
AddLabel(bearphase, " Bear Phase ", if bearphase then Color.RED else Color.BLACK);
input colorbars = yes;
assignPriceColor(
if !colorbars then color.current else
if bullphase then Color.GREEN else
if bearphase then Color.RED else Color.current);
Alert(condition = bullphase[1] and !bullphase, text = "Bull Phase", sound = Sound.Bell, "alert type" = Alert.BAR);
Alert(condition = accphase[1] and !accphase, text = "Accumation Phase", sound = Sound.Bell, "alert type" = Alert.BAR);
Alert(condition = recphase[1] and !recphase, text = "Recovery Phase", sound = Sound.Bell, "alert type" = Alert.BAR);
Alert(condition = warnphase[1] and !warnphase, text = "Warning Phase", sound = Sound.Bell, "alert type" = Alert.BAR);
Alert(condition = distphase[1] and !distphase, text = "Distribution Phase", sound = Sound.Bell, "alert type" = Alert.BAR);
Alert(condition = bearphase[1] and !bearphase, text = "Bear Phase", sound = Sound.Bell, "alert type" = Alert.BAR);