Zero-Lag MA Trend Levels [ChartPrime] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
PriLLYX.png


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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
151 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top