# 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