Here is the Multi Timeframe MACD indicator for ThinkorSwim to plot the MACD from a higher timeframe onto a lower timeframe. Came up in Discord so thought I would put it here also.
https://tos.mx/8q9D46
Code:
declare lower;
input midTermPeriod = {"1 min", "3 min", "5 min", "15 min", "30 min", "60 min", "120 min", "Daily", default "Weekly", "Monthly"};
input longTermPeriod = {"3 min", "5 min", "15 min", "30 min", "60 min", "120 min", "Daily", "Weekly", default "Monthly"};
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input midTermFastLength = 12;
input midTermSlowLength = 26;
input midTermMACDLength = 9;
input longTermFastLength = 12;
input longTermSlowLength = 26;
input longTermMACDLength = 9;
def middleAggregation;
switch (midTermPeriod) {
case "1 min":
middleAggregation = AggregationPeriod.MIN;
case "3 min":
middleAggregation = AggregationPeriod.THREE_MIN;
case "5 min":
middleAggregation = AggregationPeriod.FIVE_MIN;
case "15 min":
middleAggregation = AggregationPeriod.FIFTEEN_MIN;
case "30 min":
middleAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
middleAggregation = AggregationPeriod.HOUR;
case "120 min":
middleAggregation = AggregationPeriod.TWO_HOURS;
case "Daily":
middleAggregation = AggregationPeriod.DAY;
case "Weekly":
middleAggregation = AggregationPeriod.WEEK;
case "Monthly":
middleAggregation = AggregationPeriod.MONTH;
}
def highestAggregation;
switch (longTermPeriod) {
case "3 min":
highestAggregation = AggregationPeriod.THREE_MIN;
case "5 min":
highestAggregation = AggregationPeriod.FIVE_MIN;
case "15 min":
highestAggregation = AggregationPeriod.FIFTEEN_MIN;
case "30 min":
highestAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
highestAggregation = AggregationPeriod.HOUR;
case "120 min":
highestAggregation = AggregationPeriod.TWO_HOURS;
case "Daily":
highestAggregation = AggregationPeriod.DAY;
case "Weekly":
highestAggregation = AggregationPeriod.WEEK;
case "Monthly":
highestAggregation = AggregationPeriod.MONTH;
}
DefineGlobalColor("UpTrend", color.GREEN);
DefineGlobalColor("DownTrend", color.RED);
DefineGlobalColor("NoTrend", color.LIGHT_GRAY);
def timeFrame = getAggregationPeriod();
def testTimeFrames = if timeFrame < middleAggregation and middleAggregation < highestAggregation then yes else no;
AddLabel(yes, if testTimeFrames then "Time Frames Are Correct" else "Time Frames Are Wrong", if testTimeFrames then color.GREEN else color.RED);
# This section is for the chart level MACD
def fastAvg = ExpAverage(close, fastLength);
def slowAvg = ExpAverage(close, slowLength);
plot Value = fastAvg - slowAvg;
Value.SetDefaultColor(color.CYAN);
plot Avg = ExpAverage(Value, MACDLength);
Avg.SetDefaultColor(color.YELLOW);
plot Diff = (value - avg)*3;
# This section is for the medium term MACD
def midTermFastAvg = ExpAverage(close(period = middleAggregation)[1] , midTermFastLength);
def midTermSlowAvg = ExpAverage(close(period = middleAggregation)[1] , midTermSlowLength);
def midTermValue = midTermFastAvg - midTermSlowAvg;
def midTermAvg = ExpAverage(midTermValue, midTermMACDLength);
plot midTermDiff = (midTermValue - midTermAvg)*3;
midTermDiff.Hide();
midTermDiff.HideBubble();
# This section is for the long term MACD
def longTermFastAvg = ExpAverage(close(period = highestAggregation)[1] , longTermFastLength);
def longTermSlowAvg = ExpAverage(close(period = highestAggregation)[1] , longTermSlowLength);
def longTermValue = longTermFastAvg - longTermSlowAvg;
def longTermAvg = ExpAverage(longTermValue, longTermMACDLength);
plot longTermDiff = (longTermValue - longTermAvg)*3;
longTermDiff.Hide();
longTermDiff.HideBubble();
def midTermLower = midTermDiff < midTermDiff[1];
def midTermHigher = midTermDiff > midTermDiff[1];
rec midTermSignal = if midTermLower then yes else if midTermSignal[1] == yes and midTermHigher == no then yes else no;
#plot test = midTermSignal;
def longTermLower = longTermDiff < longTermDiff[1];
def longTermHigher = longTermDiff > longTermDiff[1];
rec longTermSignal = if longTermLower then yes else if longTermSignal[1] == yes and longTermHigher == no then yes else no;
midTermDiff.AssignValueColor(if midTermSignal then color.RED else color.BLUE);
longTermDiff.AssignValueColor(if longTermSignal then color.RED else color.BLUE);
def upTrend = Diff > Diff[1] and midTermSignal == no and longTermSignal == no;
def downTrend = Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes;
Diff.AssignValueColor(if upTrend then GlobalColor("UpTrend") else if downTrend then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
def longSignal = upTrend[1] == 1;
plot upTrendAlert = if longSignal then 0 else Double.NaN;
upTrendAlert.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
upTrendAlert.SetDefaultColor(Color.CYAN);
upTrendAlert.SetLineWeight(3);
Alert(upTrendAlert == 0, "MTF Uptrend", Alert.BAR, Sound.RING);
def shortSignal = downTrend[1] == 1;
plot downTrendAlert = if shortSignal then 0 else Double.NaN;
downTrendAlert.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
downTrendAlert.SetDefaultColor(Color.MAGENTA);
downTrendAlert.SetLineWeight(3);
Alert(downTrendAlert == 0, "MTF Downtrend", Alert.BAR, Sound.RING);
plot zeroLine = if close[-1] > 0 then 0 else Double.Nan;
zeroLine.AssignValueColor(if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then GlobalColor("UpTrend") else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
zeroLine.SetPaintingStrategy(PaintingStrategy.POINTS);
zeroLine.SetLineWeight(3);
https://tos.mx/8q9D46
Multi Timeframe MACD Labels
MTF labels of MACD added to Hahn Tech MTF MACD. You will need to enter the time frames above the chart time frame you desire. Labels will show low time frame , middle, and high time frame and be colored red or green.
Code:
# Code from Hahn Tech had no header.
# Added labels for three time frames.
# The chart time frame and two user chosen higher time frames. Labels turn red or green depending on MACD.
# Additions by Horserider 9/30/2019
input midTermPeriod = {"1 min", "3 min", "5 min", "15 min", "30 min", "60 min", "120 min", "Daily", default "Weekly", "Monthly"};
input longTermPeriod = {"3 min", "5 min", "15 min", "30 min", "60 min", "120 min", "Daily", "Weekly", default "Monthly"};
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input midTermFastLength = 12;
input midTermSlowLength = 26;
input midTermMACDLength = 9;
input longTermFastLength = 12;
input longTermSlowLength = 26;
input longTermMACDLength = 9;
def middleAggregation;
switch (midTermPeriod) {
case "1 min":
middleAggregation = AggregationPeriod.MIN;
case "3 min":
middleAggregation = AggregationPeriod.THREE_MIN;
case "5 min":
middleAggregation = AggregationPeriod.FIVE_MIN;
case "15 min":
middleAggregation = AggregationPeriod.FIFTEEN_MIN;
case "30 min":
middleAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
middleAggregation = AggregationPeriod.HOUR;
case "120 min":
middleAggregation = AggregationPeriod.TWO_HOURS;
case "Daily":
middleAggregation = AggregationPeriod.DAY;
case "Weekly":
middleAggregation = AggregationPeriod.WEEK;
case "Monthly":
middleAggregation = AggregationPeriod.MONTH;
}
def highestAggregation;
switch (longTermPeriod) {
case "3 min":
highestAggregation = AggregationPeriod.THREE_MIN;
case "5 min":
highestAggregation = AggregationPeriod.FIVE_MIN;
case "15 min":
highestAggregation = AggregationPeriod.FIFTEEN_MIN;
case "30 min":
highestAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
highestAggregation = AggregationPeriod.HOUR;
case "120 min":
highestAggregation = AggregationPeriod.TWO_HOURS;
case "Daily":
highestAggregation = AggregationPeriod.DAY;
case "Weekly":
highestAggregation = AggregationPeriod.WEEK;
case "Monthly":
highestAggregation = AggregationPeriod.MONTH;
}
DefineGlobalColor("UpTrend", color.GREEN);
DefineGlobalColor("DownTrend", color.RED);
DefineGlobalColor("NoTrend", color.LIGHT_GRAY);
def timeFrame = getAggregationPeriod();
def testTimeFrames = if timeFrame < middleAggregation and middleAggregation < highestAggregation then yes else no;
AddLabel(yes, if testTimeFrames then "Time Frames Are Correct" else "Time Frames Are Wrong", if testTimeFrames then color.GREEN else color.RED);
# This section is for the chart level MACD
def fastAvg = ExpAverage(close, fastLength);
def slowAvg = ExpAverage(close, slowLength);
plot Value = fastAvg - slowAvg;
Value.SetDefaultColor(color.CYAN);
plot Avg = ExpAverage(Value, MACDLength);
Avg.SetDefaultColor(color.YELLOW);
plot Diff = (value - avg)*3;
# This section is for the medium term MACD
def midTermFastAvg = ExpAverage(close(period = middleAggregation) , midTermFastLength);
def midTermSlowAvg = ExpAverage(close(period = middleAggregation) , midTermSlowLength);
def midTermValue = midTermFastAvg - midTermSlowAvg;
def midTermAvg = ExpAverage(midTermValue, midTermMACDLength);
plot midTermDiff = (midTermValue - midTermAvg)*3;
midTermDiff.Hide();
midTermDiff.HideBubble();
# This section is for the long term MACD
def longTermFastAvg = ExpAverage(close(period = highestAggregation) , longTermFastLength);
def longTermSlowAvg = ExpAverage(close(period = highestAggregation) , longTermSlowLength);
def longTermValue = longTermFastAvg - longTermSlowAvg;
def longTermAvg = ExpAverage(longTermValue, longTermMACDLength);
plot longTermDiff = (longTermValue - longTermAvg)*3;
longTermDiff.Hide();
longTermDiff.HideBubble();
def midTermLower = midTermDiff < midTermDiff[1];
def midTermHigher = midTermDiff > midTermDiff[1];
rec midTermSignal = if midTermLower then yes else if midTermSignal[1] == yes and midTermHigher == no then yes else no;
#plot test = midTermSignal;
def longTermLower = longTermDiff < longTermDiff[1];
def longTermHigher = longTermDiff > longTermDiff[1];
rec longTermSignal = if longTermLower then yes else if longTermSignal[1] == yes and longTermHigher == no then yes else no;
midTermDiff.AssignValueColor(if midTermSignal then color.RED else color.BLUE);
longTermDiff.AssignValueColor(if longTermSignal then color.RED else color.BLUE);
Diff.AssignValueColor(if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then GlobalColor("UpTrend") else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
plot zeroLine = if close[-1] > 0 then 0 else Double.Nan;
zeroLine.AssignValueColor(if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then GlobalColor("UpTrend") else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
zeroLine.SetPaintingStrategy(PaintingStrategy.POINTS);
zeroLine.SetLineWeight(3);
AddLabel(yes, if value > avg == yes then "MACD Short" else "MACD Short", if value < avg == no then color.GREEN else color.RED);
AddLabel(yes, if midtermsignal == yes then "MACD MID" else "MACD Mid", if midtermsignal == no then color.GREEN else color.RED);
AddLabel(yes, if longtermsignal == yes then "MACD Long" else "MACD Long", if longtermsignal == no then color.GREEN else color.RED);