Chandelier Exit ZLSMA For ThinkOrSwim

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

I really just need the indicator, not the whole strategy. Thanks for any help provided.
https://www.tradingview.com/script/njG2EVJ0-Chandelier-Exit-ZLSMA-Strategy/
check the below

CSS:
# https://www.tradingview.com/v/njG2EVJ0/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © simwai
#indicator('Chandelier Exit ZLSMA', shorttitle='CE_ZLSMA', overlay=true)
# request from useThinkScript.com member.
# Converted by Sam4Cok@Samer800    - 07/2023
#// -- Colors --
DefineGlobalColor("rajah", CreateColor(242, 166, 84));# // orange
DefineGlobalColor("magicMint", CreateColor(171, 237, 198));
DefineGlobalColor("languidLavender", CreateColor(232, 215, 255));
DefineGlobalColor("mediumAquamarine", CreateColor(104, 223, 153));
DefineGlobalColor("carrotOrange", CreateColor(239, 146, 46));

#// -- Inputs --
input ColorInTradeBars = yes;
input source = ohlc4;
input atrLength = 1;                # 'ATR Period'
input atrMultiplier = 2.0;          # 'ATR Multiplier'
input showBuySellLabels = no;       # 'Show Buy/Sell Labels ?'
input isSignalLabelEnabled = yes;   # 'Show Signal Labels ?'
input PriceForExtremums = {default "Close Price", "High/Low Price"};   #'Use Close Price for Extremums ?'
input zlsmaLineColor = no;          # 'Enable Rising/Decreasing Highlightning'
input zlsmaLength = 50;             # 'length'
input offset = 0;                   # 'Offset'

def na = Double.NaN;
#pine_linreg(src, len, offset=0) =>
Script linreg {
input src = close;
input len = 100;
input offset = 0;
    def na = Double.NaN;
    def bar_index = isNaN(close);
    def x_sum = if bar_index then na else
                fold i = 0 to len with p do
                 p + i;
    def xx_sum = if bar_index then na else
                fold ii = 0 to len with pp do
                 pp + ii * ii;
    def y_sum = sum(src, len);
    def xy_sum = fold j = 0 to len with q do
                  q  + j * GetValue(src, len - j - 1);
    def slope = (len * xy_sum - x_sum * y_sum) / (len * xx_sum - x_sum * x_sum);
    def intercept = (y_sum - slope * x_sum) / len;
    def linreg = intercept + slope * (len - offset - 1);
    plot out = linreg;
}
#// -- CE - Credits to @everget --
def haClose = source[1];#1 / 4 * ohlc;
def nATR = ATR(Length = atrLength);
def atr = atrMultiplier * nATR[1];
def srcHi;
def srcLo;
switch (PriceForExtremums) {
case  "Close Price" :
    srcHi = Highest(haClose, atrLength);
    srcLo = Lowest(haClose, atrLength);
case "High/Low Price" :
    srcHi = Highest(high[1], atrLength);
    srcLo = Lowest(low[1], atrLength);
}
def longStop;
def longStop_ = srcHi - atr;
def longStopPrev = if longStop[1] == 0 then longStop_ else longStop[1];
longStop = if haClose > longStopPrev then Max(longStop_, longStopPrev) else longStop_;
def shortStop;
def shortStop_ = srcLo + atr;
def shortStopPrev = if shortStop[1] == 0 then shortStop_ else shortStop[1];
shortStop = if haClose < shortStopPrev then Min(shortStop_, shortStopPrev) else shortStop_;

def dir = if dir[1] == 0 then 1 else
          if haClose > shortStopPrev then 1 else if haClose < longStopPrev then -1 else dir[1];
def buySignal = dir == 1 and dir[1] == -1;
def sellSignal = dir == -1 and dir[1] == 1;

AddChartBubble(buySignal and showBuySellLabels, longStop, "Buy", GlobalColor("mediumAquamarine"), no);
AddChartBubble(sellSignal and showBuySellLabels, shortStop, "Sell", GlobalColor("carrotOrange"), yes);

def changeCond = dir != dir[1];
#// -- ZLSMA - Credits to @netweaver2011 --
def lsma  = linreg(haClose, zlsmaLength, offset);
def lsma2 = linreg(lsma, zlsmaLength, offset);
def eq = lsma - lsma2;
def zlsma = lsma + eq;

def zColor = if zlsmaLineColor then if zlsma > zlsma[1] then 1 else -1 else 0;
plot zlsmaLine = zlsma;    # 'ZLSMA'
zlsmaLine.SetLineWeight(2);
zlsmaLine.AssignValueColor( if zColor>0 then GlobalColor("magicMint") else
                            if zColor<0 then GlobalColor("rajah") else GlobalColor("languidLavender"));

#// -- Signals --
def enterLong;def enterShort; def signalCache;def signalCachei;
def isTradeOpen;def enterLong_;def enterShort_;def exitShort_;def exitLong_;

def enterLongi  = buySignal  and (haClose Crosses Above zlsma);
def enterShorti = sellSignal and (haClose Crosses Below zlsma);
def exitLongi   = (source < zlsma) and (haClose >= zlsma[1]);
def exitShorti  = (source > zlsma) and (haClose <= zlsma[1]);

def exitLong =  exitLongi;
def exitShort = exitShorti;

if (signalCache[1] == 1) {
    signalCachei = 0;
    enterLong = yes;
    enterShort = enterShorti;
    } else
if (signalCache[1] == -1) {
    signalCachei = 0;
    enterLong = enterLongi;
    enterShort = yes;
} else {
    signalCachei = signalCache[1];
    enterLong  = enterLongi;
    enterShort = enterShorti;
}

if (isTradeOpen[1] == 0) {
    if (exitShort and !enterLong) {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = no;
    } else
    if (exitLong and !enterShort) {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = no;
        exitShort_  = exitShort;
    } else
    if (enterLong and exitShort) {
        isTradeOpen = 1;
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = no;
    } else
    if (enterShort and exitLong) {
        isTradeOpen = -1;
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = no;
        exitShort_  = exitShort;
    } else
    if (enterLong) {
        isTradeOpen = 1;
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else
    if (enterShort) {
        isTradeOpen = -1;
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
}
    } else
if (isTradeOpen[1] == 1) {
    if (exitShort) {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = no;
    } else
    if (enterLong) {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = no;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else
    if (enterShort and exitLong) {
        isTradeOpen = isTradeOpen[1];
        signalCache = -1;
        enterLong_  = enterLong;
        enterShort_ = no;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else
    if (exitLong) {
        isTradeOpen = 0;
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
}
    } else
if (isTradeOpen[1] == -1) {
    if (exitLong) {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = no;
        exitShort_  = exitShort;
    } else
    if (enterShort) {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = no;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else
    if (enterLong and exitShort) {
        isTradeOpen = isTradeOpen[1];
        signalCache = 1;
        enterLong_  = no;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else
    if (exitShort) {
        isTradeOpen = 0;
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
    } else {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
}
    } else {
        isTradeOpen = isTradeOpen[1];
        signalCache = signalCachei;
        enterLong_  = enterLong;
        enterShort_ = enterShort;
        exitLong_   = exitLong;
        exitShort_  = exitShort;
}

plot longExit = if isSignalLabelEnabled and exitLong_ then zlsma else na;
plot shortExit = if isSignalLabelEnabled and exitShort_ then zlsma else na;
longExit.SetLineWeight(3);
shortExit.SetLineWeight(3);
longExit.SetDefaultColor(Color.CYAN);
shortExit.SetDefaultColor(Color.MAGENTA);
longExit.SetPaintingStrategy(PaintingStrategy.POINTS);
shortExit.SetPaintingStrategy(PaintingStrategy.POINTS);

AddChartBubble(isSignalLabelEnabled and enterLong_, zlsma, "L",GlobalColor("mediumAquamarine"), no);
AddChartBubble(isSignalLabelEnabled and enterShort_, zlsma, "S", GlobalColor("carrotOrange"), yes);


AssignPriceColor(if !ColorInTradeBars then Color.CURRENT else
                 if isTradeOpen == 1 then GlobalColor("mediumAquamarine") else
                 if isTradeOpen == -1 then GlobalColor("carrotOrange") else Color.CURRENT);


#-- END of CODE
 
@samer800 I use the ZLSMA line from your code above, by itself as an indicator. How can I create a scan for when the ZLSMA line changes color? I'd like to use a single scan to detect any color change of the line, rather than a scan for long and another scan for short. Please help me out with this one! Thanks.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
357 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