
Author Message:
The Zero-Lag MA Trend Levels [ChartPrime] indicator combines a Zero-Lag Moving Average (ZLMA) with a standard Exponential Moving Average (EMA) to provide a dynamic view of the market trend. This indicator uses a color-changing cloud to represent shifts in trend momentum and plots key levels when trend reversals are detected. The addition of trend level boxes helps identify significant price zones where market shifts occur, with retest signals aiding in spotting potential continuation or reversal points.
CODE:
CSS:
#// Indicator for TOS
#// © ChartPrime
#indicator("Zero-Lag MA Trend Levels [ChartPrime]", overlay = true)
# Converted by Sam4Cok@Samer800 - 10/2024
input timeframe = AggregationPeriod.MIN;
input source = FundamentalType.CLOSE;
input movAvgLength = 15; #// Length for the moving average calculations
input showTrendLevels = yes; #// Toggle to show trend levels
def na = Double.NaN;
def last = isNaN(Close);
def current = GetAggregationPeriod();
def tf = Max(Current, timeframe);
def src = Fundamental(source, Period = tf);
#-- Color
DefineGlobalColor("up", CreateColor(0, 98, 177));
def tr = TrueRange(high(Period = tf), close(Period = tf), low(Period = tf));
def atr = WildersAverage(tr, 200); # // Average True Range (ATR) for trend levels
def emaValue = ExpAverage(src, movAvgLength); # // EMA of the closing price
def correction = src + (src - emaValue); # // Correction factor for zero-lag calculation
def zlma = ExpAverage(correction, movAvgLength); # // Zero-Lag Moving Average (ZLMA)
def signalUp = (zlma > emaValue) and (zlma[1] <= emaValue[1]); # // Signal for bullish crossover
def signalDn = (zlma < emaValue) and (zlma[1] >= emaValue[1]); # // Signal for bearish crossunder
#// Determine the color of ZLMA based on its direction
def zlma_color = if zlma > zlma[3] then 1 else if zlma < zlma[3] then -1 else 0;
def ema_col = if emaValue < zlma then 1 else -1; # // Determine the EMA color
#// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
plot zlmaLine = zlma;
plot emaLine = emaValue;
zlmaLine.AssignValueColor(if zlma_color>0 then Color.GREEN else Color.RED);
emaLine.AssignValueColor(if ema_col>0 then Color.GREEN else Color.RED);
AddCloud(zlmaLine , emaLine, Color.DARK_GREEN, Color.DARK_RED);
AddCloud(if zlma_color>0 then zlmaLine else na, emaLine, Color.CURRENT, Color.DARK_GREEN);
AddCloud(if zlma_color<0 then zlmaLine else na, emaLine, Color.DARK_RED, Color.CURRENT);
# Levels
def check_signals = signalUp or signalDn;
def topDn; def botDn; def topDn1; def botDn1; def cntDn;
if showTrendLevels and signalDn {
cntDn = 0;
topDn = zlma + atr;
botDn = zlma;
topDn1 = na;
botDn1 = na;
} else if emaValue < zlma {
cntDn = if cntDn[1] < 4 then cntDn[1] + 1 else 0;
topDn1 = if cntDn==1 then topDn[1] else topDn1[1];
botDn1 = if cntDn==1 then botDn[1] else botDn1[1];
topDn = na;
botDn = na;
} else {
cntDn = if !last[4] then cntDn[1] else 0;
topDn = if !last[4] then topDn[1] else na;
botDn = if !last[4] then botDn[1] else na;
topDn1 = if !last[4] then topDn1[1] else na;
botDn1 = if !last[4] then botDn1[1] else na;
}
def topUp; def botUp; def topUp1; def botUp1; def cntUp;
if showTrendLevels and signalUp {
cntUp = 0;
topUp = zlma;
botUp = zlma - atr;
topUp1 = 0;
botUp1 = 0;
} else if emaValue > zlma {
cntUp = if cntUp[1] < 4 then cntUp[1] + 1 else 0;
topUp1 = if cntUp==1 then topUp[1] else topUp1[1];
botUp1 = if cntUp==1 then botUp[1] else botUp1[1];
topUp = na;
botUp = na;
} else {
cntUp = if !last[4] then cntUp[1] else 0;
topUp = if !last[4] then topUp[1] else na;
botUp = if !last[4] then botUp[1] else na;
topUp1 = if !last[4] then topUp1[1] else na;
botUp1 = if !last[4] then botUp1[1] else na;
}
def topLvlUp = if topUp then topUp else na;
def botLvlUp = if botUp then botUp else na;
def topLvlDn = if topDn then topDn else na;
def botLvlDn = if botDn then botDn else na;
def topLvlUp1 = if topUp1 then topUp1 else na;
def botLvlUp1 = if botUp1 then botUp1 else na;
def topLvlDn1 = if topDn1 then topDn1 else na;
def botLvlDn1 = if botDn1 then botDn1 else na;
def crossBelow = (high(Period = tf) < botLvlDn) and (high(Period = tf)[1] >= botLvlDn[1]);
def crossAbove = (low(Period = tf) > topLvlUp) and (low(Period = tf)[1] <= topLvlUp[1]);
def crossDn = crossBelow and !check_signals[1] and !check_signals and emaValue > zlma;
def crossUp = crossAbove and !check_signals and !check_signals[1] and emaValue < zlma;
#// Plot shapes for up and down signals
plot SigUp = if signalUp and isNaN(topLvlUp[1]) then zlma else na;
plot SigDn = if signalDn and isNaN(botLvlDn[1]) then zlma else na;
plot dnCross = if crossDn[-1] and !crossDn then high else na;
plot upCross = if crossUp[-1] and !crossUp then low else na;
SigUp.SetDefaultColor(Color.CYAN);
SigDn.SetDefaultColor(Color.MAGENTA);
SigUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
SigDn.SetPaintingStrategy(PaintingStrategy.SQUARES);
dnCross.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
upCross.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
dnCross.SetDefaultColor(Color.RED);
upCross.SetDefaultColor(Color.GREEN);
AddCloud(topLvlUp, botLvlUp, GlobalColor("up"), GlobalColor("up"), yes);
AddCloud(topLvlDn, botLvlDn, Color.PLUM, Color.PLUM, yes);
AddCloud(topLvlUp1[-1], botLvlUp1[-1], GlobalColor("up"), GlobalColor("up"), yes);
AddCloud(topLvlDn1[-1], botLvlDn1[-1], Color.PLUM, Color.PLUM, yes);
#-- END of CODE