#//@version=4
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © KivancOzbilgic
#//developer: Gerald Appel
#//author: @kivancozbilgic
#study("MACD ReLoaded","MACDRe", overlay=false, resolution="")
# Converted by Sam4Cok@Samer800 - 05/2024
declare lower;
input colorBars = yes;
input timeframe = {Default "Chart", "Manual"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input displayOptions = { "Histogram", "Lines", Default "Histogram & Lines"};
input MovAvgType = { "SMA", "EMA", "WMA", "DEMA", "TMA", default "VAR", "WWMA", "ZLEMA", "TSF", "HULL", "TILL"};
input Source = FundamentalType.CLOSE; #, title="Source")
input fastLength = 12; #, "Short Moving Average Length", minval=1)
input slowLength = 26; #, "Long Moving Average Length", minval=1)
input signalLength = 9; #, "Trigger Length", minval=1)
input tillsonT3Factor = 0.7; #, "TILLSON T3 Volume Factor", step=0.1)
def na = Double.NaN;
def last = IsNaN(close);
def showHistogram = displayOptions==displayOptions."Histogram" or displayOptions==displayOptions."Histogram & Lines";
def showLines = displayOptions==displayOptions."Lines" or displayOptions==displayOptions."Histogram & Lines";
def src;
Switch (timeframe) {
Case "Manual":
src = Fundamental(FundamentalType = Source, Period = manualTimeframe);
Default :
src = Fundamental(FundamentalType = Source);
}
#-- Colors
DefineGlobalColor("exUp",CreateColor(0, 230, 118));
DefineGlobalColor("Up", CreateColor(178, 223, 219));
DefineGlobalColor("exDn",CreateColor(239, 83, 80));
DefineGlobalColor("Dn", CreateColor(255, 205, 210));
DefineGlobalColor("macd", CreateColor(66, 107, 230));
DefineGlobalColor("signal", CreateColor(230, 0, 0));
#--- Functions
#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;
}
script f_var {
input src = close;
input length = 20;
def alpha = 2/(length + 1);
def ud1 = if src > src[1] then src - src[1] else 0;
def dd1 = if src < src[1] then src[1] - src else 0;
def UD = sum(ud1, 9);
def DD = sum(dd1, 9);
def vCMO = (UD - DD) / (UD + DD);
def CMO = if isNaN(vCMO) then 0.0 else AbsValue(vCMO);
def VAR = (alpha * CMO * src)+(1 - alpha * CMO) * if(isNaN(VAR[1]), 0, VAR[1]);
plot out = if Length ==1 then src else var;
}
#Wwma_Func(src, length) =>
script Wwma_Func {
input src = close;
input length = 2;
def alpha = 1 / length;
def WWMA = alpha * src + (1 - alpha) * if(isNaN(WWMA[1]), 0, WWMA[1]);
plot return = WWMA;
}
#Zlema_Func(src, length) =>
script Zlema_Func {
input src = close;
input length = 2;
def zxLag = if length / 2 == Round(length / 2, 0) then length / 2 else (length - 1) / 2;
def zxEMAData = src + (src - src[zxLag]);
def ZLEMA = ExpAverage(zxEMAData, length);
plot return = ZLEMA;
}
#Tsf_Func(src, length) =>
script Tsf_Func {
input src = close;
input length = 2;
def lrc = Inertia(src, length);
def lrc1 = linreg(src, length, 1);
def lrs = lrc - lrc1;
def TSF = Inertia(src, length) + lrs;
plot retur = TSF;
}
#Till_Func(src, length) =>
script Till_Func {
input src = close;
input length = 2;
input T3a1 = 0.7;
def T3e1 = ExpAverage(src, length);
def T3e2 = ExpAverage(T3e1,length);
def T3e3 = ExpAverage(T3e2,length);
def T3e4 = ExpAverage(T3e3,length);
def T3e5 = ExpAverage(T3e4,length);
def T3e6 = ExpAverage(T3e5,length);
def T3c1 = -T3a1*T3a1*T3a1;
def T3c2 = 3 * T3a1 * T3a1 + 3 * T3a1 * T3a1 * T3a1;
def T3c3 =-6 * T3a1 * T3a1 - 3 * T3a1 - 3 * T3a1 * T3a1 * T3a1;
def T3c4 = 1 + 3 * T3a1 + T3a1 * T3a1 * T3a1 + 3 * T3a1 * T3a1;
def T3 = T3c1 * T3e6 + T3c2 * T3e5 + T3c3 * T3e4 + T3c4 * T3e3;
plot retur = T3;
}
#ma(src, length, type) =>
script getMA {
input src = close;
input length = 80;
input type = "SMA";
input T3a1 = 0.7;
def ma =
if type == "SMA" then Average(src, length) else
if type == "EMA" then ExpAverage(src, length) else
if type == "WMA" then WMA(src, length) else
if type == "DEMA" then DEMA(src, length) else
if type == "TMA" then MovAvgTriangular(src, length) else
if type == "VAR" then f_var(src, length) else
if type == "WWMA" then WWMA_Func(src, length) else
if type == "ZLEMA" then Zlema_Func(src, length) else
if type == "TSF" then Tsf_Func(src, length) else
if type == "TILL" then Till_Func(src, length, T3a1) else
if type == "HULL" then HullMovingAvg(src, length) else Average(src, length);
plot result = ma;
}
def MA12 = getMA(src, fastLength, MovAvgType, tillsonT3Factor);
def MA26 = getMA(src, slowLength, MovAvgType, tillsonT3Factor);
def src2 = MA12 - MA26;
def MATR = getMA(src2, signalLength, MovAvgType, tillsonT3Factor);
def hist = src2 - MATR;
#-- Plots
plot MACDRe = if showLines and !last[-1] then src2 else na; # "MACDRe"
plot Trigger = if showLines and !last then MATR else na; # "TRIGGER"
plot histo = if showhistogram and !last then hist else na; # "Histogram"
MACDRe.SetLineWeight(2);
Trigger.SetLineWeight(2);
MACDRe.SetDefaultColor(GlobalColor("macd"));
Trigger.SetDefaultColor(GlobalColor("signal"));
histo.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
histo.AssignValueColor(if hist>=0 then (if hist[1] < hist then GlobalColor("exUp") else GlobalColor("Up")) else
(if hist[1] < hist then GlobalColor("Dn") else GlobalColor("exDn")));
AssignPriceColor(if !colorBars then Color.CURRENT else if hist>=0 then
(if hist[1] < hist then GlobalColor("exUp") else GlobalColor("Up")) else
(if hist[1] < hist then GlobalColor("Dn") else GlobalColor("exDn")));
#-- END of CODE