#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © OskarGallard
#indicator("Double CCI", "Double CCI", overlay=false,
# Converted by Sam4COK@Samer800 - 05/2023
declare lower;
input show_CCI = yes;#, "═══════ Show Commodity Channel Index 1 ═══════")
input length_CCI = 34;#, "Length"
input MA_Type = {SMA, EMA, SMMA, VAWMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, default ALMA, SWMA, Gaussian, TRAEM, KAMA, MAV, AMA, LSMA, WILD};
input source_CCI = {"Close", "Open", "VWAP", "HL2", "HLC3", "OHLC4", "HLCC4", "High", "Low", default "vwap(Close)", "vwap(Open)", "vwap(High)", "vwap(Low)", "AVG(vwap(H,L))", "AVG(vwap(O,C))"};
input show_cross = yes; # "Show crosses at line 100")
input show_signal = no; # "Show Signal"
input length_signal = 10; # "Length"
input MA_Type_Sig = {SMA, EMA, SMMA, VAWMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian, TRAEM, KAMA, MAV, AMA, LSMA, default WILD};
input show_ST = yes;#(true, "SuperTrend Background?", inline='st1', group='SuperTrend')
input ML = 0;#, "CCI Midline Pivot", inline='st1', group='SuperTrend')
input Factor = 3;#, "ATR Factor", minval=1, maxval=100, step=0.1, inline='st2', group='SuperTrend')
input Pd = 3;#, "ATR Period", minval=1, maxval=100, inline='st2', group='SuperTrend')
input show_CCI2 = no;#false, "═══════ Show Commodity Channel Index 2 ═══════")
input length_CCI2 = 14;#, "Length", minval=1, inline='cci2')
input MA_Type2 = {default SMA, EMA, SMMA, VAWMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian, TRAEM, KAMA, MAV, AMA, LSMA, MAMA, WILD};
input source_CCI2 = {"Close", "Open", "VWAP", "HL2", default "HLC3", "OHLC4", "HLCC4", "High", "Low", "vwap(Close)", "vwap(Open)", "vwap(High)", "vwap(Low)", "AVG(vwap(H,L))", "AVG(vwap(O,C))"};
input uTrad = yes;#(true, "Use Traditional Formula of @JustUncleL")
input mLevel = 100;#, "Mid Level Trigger Lines +/-", minval=10)
input hLevel = 200;#, "High Level Trigger Lines +/-", minval=10)
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def last = isNaN(close);
#--- Color.
DefineGlobalColor("Red1" , CreateColor(255, 128, 128));
DefineGlobalColor("Red2" , CreateColor(255, 51, 51));
DefineGlobalColor("Red3" , CreateColor(163, 0, 0));
DefineGlobalColor("blue1" , CreateColor(128, 128, 255));
DefineGlobalColor("blue2" , CreateColor(51, 51, 255));
DefineGlobalColor("blue3" , CreateColor(0, 0, 163));
#export f_Vwap(simple string tf, float src, float src_v) =>
script f_Vwap {
input src = close;
def today = GetDay();
def tf = today != today[1];
def src_v = volume;
def start0 = tf - tf[1];
def sumSrc0 = src * src_v;
def sumVol0 = src_v;
def sumSrc2 = src_v * Sqr(src);
def sumSrc1 = CompoundValue(1, if start0 then sumSrc0 else sumSrc0 + sumSrc1[1], sumSrc0);
def sumVol1 = CompoundValue(1, if start0 then sumVol0 else sumVol0 + sumVol1[1], sumVol0);
def sumVol2 = CompoundValue(1, if start0 then sumSrc2 else sumSrc2 + sumVol2[1], sumSrc2);
def Vwap = sumSrc1 / sumVol1;
plot wap = Vwap;
}
#pine_dev(source, length) =>
script dev {
input source = close;
input length = 10;
def mean = Average(source, length);
def sum = fold i = 0 to length with p do
p + AbsValue(source[i] - mean);
def dev = sum / length;
plot out = dev;
}
#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
script nz {
input data = close;
input repl = 0;
def ret_val = if !IsNaN(data) then data else repl;
plot return = ret_val;
}
#ama (source, length, fast, slow) =>
script ama {
input source = close;
input length = 14;
input fast = 2;
input slow = 30;
def fastAlpha = 2 / (fast + 1);
def slowAlpha = 2 / (slow + 1);
def hh = Highest(high, length + 1);
def ll = Lowest(low, length + 1);
def mltp = if (hh - ll) != 0 then AbsValue(2 * source - ll - hh) / (hh - ll) else 0;
def ssc = mltp * (fastAlpha - slowAlpha) + slowAlpha;
def ama;
ama = CompoundValue(1, ama[1] + Power(ssc, 2) * (source - ama[1]), source);
plot return = ama;
}
#rms(source, length)=>
script rms {
input source = close;
input length = 14;
def rms = Sqrt(Sum(Power(source, 2), length) / length);
plot return = rms;
}
#mav (source, length) =>
script mav {
input source = close;
input length = 14;
def mav = ((SimpleMovingAvg(source, length)[1] * (length - 1)) + source) / length;
plot return = mav;
}
#kama(xPrice, Length)=>
script kama {
input xPrice = close;
input Length = 14;
def xvnoise = AbsValue(xPrice - xPrice[1]);
def nfastend = 0.666;
def nslowend = 0.0645;
def nsignal = AbsValue(xPrice - xPrice[Length]);
def nnoise = Sum(xvnoise, Length);
def nefratio = if nnoise != 0 then nsignal / nnoise else 0;
def nsmooth = Power(nefratio * (nfastend - nslowend) + nslowend, 2);
def nAMA;
nAMA = CompoundValue(1, nAMA[1] + nsmooth * (xPrice - nAMA[1]), xPrice);
plot returen = nAMA;
}
#Gaussianma(values, length) =>
script Gaussian {
input values = close;
input length = 20;
def stddev = length / 4;
def indices = length - 1;
def weights = Exp(-0.5 * (Power((indices - length), 2) / Power(stddev, 2)));
def sum = Sum(values * weights, length);
def gMA = sum / Sum(weights, length);
plot return = gMA;
}
#pine_swma(source) =>
script swma {
input source = close;
def swma = source[3] * 1 / 6 + source[2] * 2 / 6 + source[1] * 2 / 6 + source[0] * 1 / 6;
plot retun = swma;
}
#export zlSma(float src, simple int len) =>
script zlSma {
input src = close;
input len = 14;
def lsma = Inertia(src, len);
def lsma2 = Inertia(lsma, len);
def eq = lsma - lsma2;
def zlsma = lsma + eq;
plot return = zlsma;
}
#export zlDema(float src, simple int len) =>
script zlDema {
input src = close;
input len = 14;
def zdema1 = ExpAverage(src, len);
def zdema2 = ExpAverage(zdema1, len);
def dema1 = 2 * zdema1 - zdema2;
def zdema12 = ExpAverage(dema1, len);
def zdema22 = ExpAverage(zdema12, len);
def zldema = 2 * zdema12 - zdema22;
plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
input src = close;
input len = 14;
def ema1 = ExpAverage(src, len);
def ema2 = ExpAverage(ema1, len);
def ema3 = ExpAverage(ema2, len);
def tema1 = 3 * (ema1 - ema2) + ema3;
def ema1a = ExpAverage(tema1, len);
def ema2a = ExpAverage(ema1a, len);
def ema3a = ExpAverage(ema2a, len);
def zltema = 3 * (ema1a - ema2a) + ema3a;
plot return = zltema;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
input series = close;
input windowsize = 9;
input Offset = 0.85;
input Sigma = 6;
def m = Offset * (windowsize - 1);
def s = windowsize / Sigma;
def norm = fold z = 0 to windowsize with CW do
CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));
def sum = fold y = 0 to windowsize with WS do
WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);
plot ALMA = sum / norm ;
}
#export mcginley(float src, simple int len)=>
script mcginley {
input src = close;
input len = 14;
def mg;
def t = ExpAverage(src, len);
mg = CompoundValue(1 , if IsNaN(mg[1]) then t else mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)), t);
plot return = mg;
}
# vawma(len, src = hlc3, len = na, startingWeight = 1) =>
script vawma {
input src = hlc3;
input len = 5;
input startingWeight = 1;
def last = len - 1;
def vol = fold i1 = 0 to last with p1 do
p1 + volume[i1] * (last - i1 + startingWeight);
def sum = fold i2 = 0 to last with p2 do
p2 + (src[i2] * volume[i2] * (last - i2 + startingWeight));
def vawma = if vol == 0 then Double.NaN else sum / vol;
plot out = vawma;
}
#vwma(source, length)
script VWMA {
input src = close;
input len = 15;
def v = volume;
def VWMA = SimpleMovingAvg(src * nz(v, 1), len) / SimpleMovingAvg(nz(v, 1), len);
plot result = VWMA;
}
#trAdjEma(lengthy) =>
script trAdjEma {
input src = close;
input length = 60;
input mult = 1;
def tr = TrueRange(high, close, low);
def alpha = 2.0 / (length + 1);
def trL = Lowest(tr, length);
def trH = Highest(tr, length);
def trAdj = if (trH - trL) != 0 then (tr - trL) / (trH - trL) else 0;
def trEMA = ExpAverage(src, length);
def trAdjEma;
trAdjEma = CompoundValue(1, trAdjEma[1] + (alpha * (1 + (trAdj * mult)) * (src - trAdjEma[1])), trEMA);
plot trAdEma = trAdjEma;
}
#export multiMa(float source, simple int length, string type) =>
script ma {
input type = "SMA";
input source = close;
input length = 14;
input multForTraem = 1;
input lsma_offset = 0;
def w = WMA(source, length);
def wild = CompoundValue(1, wild[1] + (source - wild[1]) / length, source);
def multiMa =
if type == "WILD" then wild else
if type == "SMA" then SimpleMovingAvg(source, length) else
if type == "EMA" then ExpAverage(source, length) else
if type == "SMMA" then CompoundValue(1, if IsNaN(w[1]) then Average(source, length) else
(w[1] * (length - 1) + source) / length, Average(source, length)) else
if type == "WMA" then WMA(source, length) else
if type == "KAMA" then KAMA(source, length) else
if type == "MAV" then MAV(source, length) else
if type == "AMA" then AMA(source, length) else
if type == "VWMA" then vwma(source, length) else
if type == "DEMA" then DEMA(source, length) else
if type == "VAWMA" then VAWMA(source, length) else
if type == "TEMA" then TEMA(source, length) else
if type == "LSMA" then Inertia(source[-lsma_offset], length) else
if type == "RMS" then RMS(source, length) else
if type == "ZLSMA" then zlSma(source, length) else
if type == "ZLDEMA" then zlDema(source, length) else
if type == "ZLTEMA" then zlTema(source, length) else
if type == "McGinley" then mcginley(source, length) else
if type == "ALMA" then ALMA(source, length) else
if type == "SWMA" then SWMA(source) else
if type == "Gaussian" then Gaussian(source, length) else
if type == "TRAEM" then trAdjEma(source, length, multForTraem) else
if type == "HMA" then HullMovingAvg(source, length ) else Double.NaN;
plot return = multiMa;
}
def src_CCI;
switch (source_CCI) {
case "vwap(Close)" :
src_CCI = f_vwap(close);
case "VWAP" :
src_CCI = Vwap();
case "Close" :
src_CCI = close;
case "Open" :
src_CCI = open;
case "HL2":
src_CCI = hl2;
case "HLC3" :
src_CCI = hlc3;
case "OHLC4" :
src_CCI = ohlc4;
case "High" :
src_CCI = high;
case "Low" :
src_CCI = low;
case "vwap(Open)" :
src_CCI = f_Vwap(open);
case "vwap(High)" :
src_CCI = f_vwap(high);
case "vwap(Low)" :
src_CCI = f_vwap(low);
case "AVG(vwap(H,L))" :
src_CCI = (f_vwap(high) + f_vwap(low)) / 2;
case "AVG(vwap(O,C))" :
src_CCI = (f_vwap(open) + f_vwap(close)) / 2;
case "HLCC4" :
src_CCI = (high+Low+Close*2) / 4;
}
def src_CCI2;
switch (source_CCI2) {
case "vwap(Close)" :
src_CCI2 = f_vwap(close);
case "VWAP" :
src_CCI2 = Vwap();
case "Close" :
src_CCI2 = close;
case "Open" :
src_CCI2 = open;
case "HL2":
src_CCI2 = hl2;
case "HLC3" :
src_CCI2 = hlc3;
case "OHLC4" :
src_CCI2 = ohlc4;
case "High" :
src_CCI2 = high;
case "Low" :
src_CCI2 = low;
case "vwap(Open)" :
src_CCI2 = f_Vwap(open);
case "vwap(High)" :
src_CCI2 = f_vwap(high);
case "vwap(Low)" :
src_CCI2 = f_vwap(low);
case "AVG(vwap(H,L))" :
src_CCI2 = (f_vwap(high) + f_vwap(low)) / 2;
case "AVG(vwap(O,C))" :
src_CCI2 = (f_vwap(open) + f_vwap(close)) / 2;
case "HLCC4" :
src_CCI2 = (high+Low+Close*2) / 4;
}
def media = ma(MA_Type, src_CCI, length_CCI);
def cci = (src_CCI - media) / (0.015 * dev(src_CCI, length_CCI));
def signal_cci = ma(MA_Type_Sig, cci, length_signal);
def media2 = ma(MA_Type2, src_CCI2, length_CCI2);
def cciNT = (src_CCI2 - media2) / (0.015 * dev(src_CCI2, length_CCI2));
def cciT = (src_CCI2 - media2) / (0.015 * dev(AbsValue(src_CCI2 - media2), length_CCI2));
def cci2 = if uTrad then cciT else cciNT;
def Up = hl2 - Factor * ATR(LENGTH = Pd);
def Dn = hl2 + Factor * ATR(LENGTH = Pd);
def TrendUp = if cci[1] > ML then Max(Up, TrendUp[1]) else Up;
def TrendDown = if cci[1] < ML then Min(Dn, TrendDown[1]) else Dn;
def Trend;
def Trend_ = if Trend[1] == 0 then 1 else Trend[1];
Trend = if cci > ML then 1 else if cci < ML then -1 else Trend_;
def Trailingsl = if Trend == 1 then TrendUp else TrendDown;
def SuperTrend = Trailingsl;
def color_super = open >= SuperTrend;
def cci_Dn = if cci2 < -hLevel then -1 else if cci2 < -mLevel then -2 else -3;
def cci_Up = if cci2 > hLevel then 1 else if cci2 > mLevel then 2 else 3;
def color_cci2 = if cci2 >= 0 then cci_Up else cci_Dn;
def UPshape = if cci > 100 then cci else na;
def DNshape = if cci < -100 then cci else na;
def color_cci = cci >= 0;# ? color.new(#80FF00, 5) : color.new(#FF4000, 5)
def color_signal = signal_cci > signal_cci[1];# ? color.white : color.gray
def cross_dn = cci[0] < 100 and cci[1] > 100;
def cross_up = cci[0] > -100 and cci[1] < -100;
plot cciPlot = if show_CCI then cci else na;#, "CCI", color_cci, 2)
plot UP_Shape = if show_CCI then UPshape else na;#, "UP Shape", color.new(#407F00, 25), 4, plot.style_circles)
plot DN_Shape = if show_CCI then DNshape else na;#, "DOWN Shape", color.new(#7F2000, 25), 4, plot.style_circles)
cciPlot.SetLineWeight(2);
UP_Shape.SetLineWeight(3);
DN_Shape.SetLineWeight(3);
cciPlot.AssignValueColor(if color_cci then Color.GREEN else Color.RED);
UP_Shape.SetDefaultColor(Color.LIME);
DN_Shape.SetDefaultColor(Color.PINK);
UP_Shape.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
DN_Shape.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
plot CrossDn = if show_cross then if cross_dn then 300 else na else na;#, "100 Cross Down", shape.diamond, location.top, color.new(#FF4000, 0))
plot CrossUp = if show_cross then if cross_up then 300 else na else na;#, "100 Cross Up", shape.diamond, location.bottom, color.new(#80FF00, 0))
plot cciSignal = if show_signal then signal_cci else na;#, "Signal(CCI)", color_signal, 2)
cciSignal.AssignValueColor(if color_signal then Color.WHITE else Color.DARK_GRAY);
CrossDn.SetPaintingStrategy(PaintingStrategy.SQUARES);
CrossUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
CrossDn.SetDefaultColor(Color.MAGENTA);
CrossUp.SetDefaultColor(Color.CYAN);
AddCloud(if show_ST then if color_super then pos else neg else na, if color_super then neg else pos, Color.DARK_GREEN, Color.DARK_RED);#title='SuperTrend Background')
plot hist_CCI = if show_CCI2 then cci2 else na; #, "CCI Histogram"
hist_CCI.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
hist_CCI.AssignValueColor(if color_cci2==-1 then GlobalColor("Red1") else
if color_cci2==-2 then GlobalColor("Red2") else
if color_cci2==-3 then GlobalColor("Red3") else
if color_cci2== 1 then GlobalColor("blue1") else
if color_cci2== 2 then GlobalColor("blue2") else GlobalColor("blue3"));
plot hUp2 = if show_CCI2 and !last then hLevel else na; # "High Upper Band"
plot hUp1 = if (show_CCI2 or show_CCI) and !last then mLevel else na; # "Mid Upper Band",
plot hline = if show_CCI and !last then ML else na; # "CCI Midline Pivot"
plot hDn2 = if (show_CCI2 or show_CCI) and !last then -mLevel else na; # 'Mid Lower Band'
plot hDn1 = if show_CCI2 and !last then -hLevel else na; # 'High Lower Band'
hUp2.SetDefaultColor(Color.MAGENTA);
hUp1.SetDefaultColor(Color.PLUM);
hline.SetDefaultColor(Color.GRAY);
hDn2.SetDefaultColor(Color.VIOLET);
hDn1.SetDefaultColor(Color.CYAN);
hUp2.SetPaintingStrategy(PAintingStrategy.DASHES);
hDn1.SetPaintingStrategy(PAintingStrategy.DASHES);
#--- END of CODE