#
# TD Ameritrade IP Company, Inc. (c) 2012-2023
#
# 20230428 - MTF version added per user request
# I like the REMACD but wondering if it is possible to make a version that is for higher timeframes that you can overlay onto a chart? I don't need the PMACDlevel PMACDsignal indicators, if that would make it easier. Only the PMACDeq and MA_PMACDeq.
input agg = AggregationPeriod.FIVE_MIN;
input price = FundamentalType.CLOSE;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageType = {SMA, default EMA};
input MACDLevel = 0.0;
def fastCoeff = 2 / (1 + fastLength);
def slowCoeff = 2 / (1 + slowLength);
def prevFastEMA = ExpAverage(Fundamental(price, period = agg), fastLength)[1];
def prevSlowEMA = ExpAverage(Fundamental(price, period = agg), slowLength)[1];
def prevFastSMA = Average(Fundamental(price, period = agg), fastLength)[1];
def prevSlowSMA = Average(Fundamental(price, period = agg), slowLength)[1];
def prevDiff;
def crossDiff;
def denominator;
plot PMACDeq;
plot PMACDlevel;
plot MA_PMACDeq;
plot PMACDsignal;
switch (AverageType) {
case SMA:
crossDiff = slowLength * Fundamental(price, period = agg)[fastLength] - fastLength * Fundamental(price, period = agg)[slowLength];
denominator = slowLength - fastLength;
prevDiff = prevFastSMA - prevSlowSMA;
PMACDlevel = (crossDiff + fastLength * slowLength * (MACDLevel - prevDiff)) / denominator;
if IsNaN(close[1]) {
PMACDeq = Double.NaN;
MA_PMACDeq = Double.NaN;
} else {
PMACDeq = crossDiff / denominator;
MA_PMACDeq = Average(PMACDeq, MACDLength);
}
PMACDsignal = (crossDiff + fastLength * slowLength * (Average(prevDiff, MACDLength - 1) - prevDiff)) / denominator;
case EMA:
crossDiff = prevFastEMA * fastCoeff - prevSlowEMA * slowCoeff;
denominator = fastCoeff - slowCoeff;
prevDiff = prevFastEMA - prevSlowEMA;
PMACDeq = crossDiff / denominator;
PMACDlevel = (MACDLevel - prevDiff + crossDiff) / denominator;
MA_PMACDeq = ExpAverage(PMACDeq, MACDLength);
PMACDsignal = (ExpAverage(prevDiff, MACDLength) - prevDiff + crossDiff) / denominator;
}
PMACDeq.SetDefaultColor(GetColor(1));
MA_PMACDeq.SetDefaultColor(GetColor(8));
PMACDlevel.SetDefaultColor(GetColor(3));
PMACDsignal.SetDefaultColor(GetColor(5));
PMACDlevel.sethiding(yes);
PMACDsignal.sethiding(yes);