Multi-TimeFrame (MTF) MACD Indicator for ThinkorSwim

I added this to a 5 min chart and nothing shows up. Do I need to add EMA's to the chart? I don't have them as yet.
All of the aggregations must be equal to or greater than the chart timeframe. The 3 aggs are currently 1min, 2min and 3min. So you would have to edit those 3 aggs to be all 5min or greater on a 5min chart. There is also an ema component in the above script already.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Multiple Time Frame MACD combines 3 time frames into one indicator. The color will only be green if it is green during all 3 time frames. Same for red, else the color is light gray. You must set the mid term and long term to the next 2 highest from the time frame you use. In this example, the 15M TF is used. So select the 30M and 1H for indicator to work properly.

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 "Indicator is Correct" else "Indicator is Wrong", if testTimeFrames  then color.GREEN else color.RED);

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;

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();

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);

Bh6mi1J.jpg
[/IMG]
@BenTen
How to get Multi-Time frame MACD indicator like the image above. If MACD line golden crossed Signal line then green else red.
 
Last edited:
Bh6mi1J.jpg
[/IMG]
@BenTen
How to get Multi-Time frame MACD indicator like the image above. If MACD line golden crossed Signal line then green else red.

Test this. Seems to work.

Capture.jpg
Ruby:
script mac {
    input macdAverageType = { SMA, default EMA };
    input macdFastLength = 12;
    input macdSlowLength = 26;
    input macdAvgLength = 9;

    input period = AggregationPeriod.MONTH;
    def Value =  MovAvgExponential(close(period = period), macdFastLength) -  MovAvgExponential(close(period = period), macdSlowLength);
    def Avg = MovAvgExponential(MovAvgExponential(close(period = period), macdFastLength) -  MovAvgExponential(close(period = period), macdSlowLength), macdAvgLength);
    plot Diff = Value - Avg;

}
def diff = mac();
AddLabel(yes, "M ", if diff > 0 then Color.GREEN else Color.RED);
def diff1 = mac(period = "WEEK");
AddLabel(yes, "W ", if diff1 > 0 then Color.GREEN else Color.RED);
def diff2 = mac(period = "FOUR_DAYS");
AddLabel(yes, "4D ", if diff2 > 0 then Color.GREEN else Color.RED);
def diff3 = mac(period = "THREE_DAYS");
AddLabel(yes, "3D ", if diff3 > 0 then Color.GREEN else Color.RED);
def diff4 = mac(period = "TWO_DAYS");
AddLabel(yes, "2D ", if diff4 > 0 then Color.GREEN else Color.RED);
def diff5 = mac(period = "DAY");
AddLabel(yes, "1D ", if diff5 > 0 then Color.GREEN else Color.RED);
def diff6 = mac(period = "FOUR_HOURS");
AddLabel(yes, "4H ", if diff6 > 0 then Color.GREEN else Color.RED);
def diff7 = mac(period = "TWO_HOURS");
AddLabel(yes, "2H ", if diff7 > 0 then Color.GREEN else Color.RED);
def diff8 = mac(period = "HOUR");
AddLabel(yes, "1H ", if diff8 > 0 then Color.GREEN else Color.RED);
 
@jokerad MTF indicators like this one can't be used in the scanner as the scanner does not work with studies that contain multiple aggregations
But you can certainly use the ToS MACD study and load it multiple times on as many time frames as you wish:
lWNheTl.png
 
Hi @SleepyZ,

Sorry to bug you again with this but would you be able to tell me how I can also set the MACD "AVG" with the MTF to the same timeframe as the MACD for the particular time frame?

In the example below, I'm just testing out the 1 hour MACD and wanted to have the color change based on MACD above or below AVG line, and going up or down but since the AVG is just the default, it's not working without the MTF AVG. If I use "0", it's always shows gray.

script mac {
input macdAverageType = { SMA, default EMA };
input macdFastLength = 12;
input macdSlowLength = 26;
input macdAvgLength = 9;

input period = AggregationPeriod.MONTH;
def Value = MovAvgExponential(close(period = period), macdFastLength) - MovAvgExponential(close(period = period), macdSlowLength);
def Avg = MovAvgExponential(MovAvgExponential(close(period = period), macdFastLength) - MovAvgExponential(close(period = period), macdSlowLength), macdAvgLength);
plot Diff = Value - Avg;
}

def diff = mac();
AddLabel(yes, "M ", if diff > 0 then Color.GREEN else Color.RED);
def diff1 = mac(period = "WEEK");
AddLabel(yes, "W ", if diff1 > 0 then Color.GREEN else Color.RED);
def diff2 = mac(period = "FOUR_DAYS");
AddLabel(yes, "4D ", if diff2 > 0 then Color.GREEN else Color.RED);
def diff3 = mac(period = "THREE_DAYS");
AddLabel(yes, "3D ", if diff3 > 0 then Color.GREEN else Color.RED);
def diff4 = mac(period = "TWO_DAYS");
AddLabel(yes, "2D ", if diff4 > 0 then Color.GREEN else Color.RED);
def diff5 = mac(period = "DAY");
AddLabel(yes, "1D ", if diff5 > 0 then Color.GREEN else Color.RED);
def diff6 = mac(period = "FOUR_HOURS");
AddLabel(yes, "4H ", if diff6 > 0 then Color.GREEN else Color.RED);
def diff7 = mac(period = "TWO_HOURS");
AddLabel(yes, "2H ", if diff7 > 0 then Color.GREEN else Color.RED);

#I modified this section to change colors based on criteria below

def diff8 = mac(period = "HOUR");
#def avg8 = diff(period = "HOUR"); "Did not work"

AddLabel(yes, "1H",
#MACD is over Avg and going up
if Diff8 > 0 and Diff8 > diff8[1] then Color.green else
#MACD is over Avg and going down
if diff8 > 0 and diff8 < diff8[1] then color.orange else
#MACD is under Avg and going up
if diff8 < 0 and diff8 > diff8[1] then Color.Cyan else
#MACD is under Avg and going down
if diff8 < 0 and diff8 < diff8[1] then Color.Red else Color.Gray);
 
Hi @SleepyZ,

Sorry to bug you again with this but would you be able to tell me how I can also set the MACD "AVG" with the MTF to the same timeframe as the MACD for the particular time frame?

In the example below, I'm just testing out the 1 hour MACD and wanted to have the color change based on MACD above or below AVG line, and going up or down but since the AVG is just the default, it's not working without the MTF AVG. If I use "0", it's always shows gray.

See if this helps. It accounts for all comparisons of diff8 to include an equal (=) aspect. I changed the colors to match the macd scheme. The color will appear as some shade of green if diff8 >= 0 and some shade of red if diff8 <= 0

Ruby:
script mac {
input macdAverageType = { SMA, default EMA };
input macdFastLength = 12;
input macdSlowLength = 26;
input macdAvgLength = 9;

input period = AggregationPeriod.MONTH;
def Value = MovAvgExponential(close(period = period), macdFastLength) - MovAvgExponential(close(period = period), macdSlowLength);
def Avg = MovAvgExponential(MovAvgExponential(close(period = period), macdFastLength) - MovAvgExponential(close(period = period), macdSlowLength), macdAvgLength);
plot Diff = Value - Avg;
}

def diff = mac();
AddLabel(yes, "M ", if diff > 0 then Color.GREEN else Color.RED);
def diff1 = mac(period = "WEEK");
AddLabel(yes, "W ", if diff1 > 0 then Color.GREEN else Color.RED);
def diff2 = mac(period = "FOUR_DAYS");
AddLabel(yes, "4D ", if diff2 > 0 then Color.GREEN else Color.RED);
def diff3 = mac(period = "THREE_DAYS");
AddLabel(yes, "3D ", if diff3 > 0 then Color.GREEN else Color.RED);
def diff4 = mac(period = "TWO_DAYS");
AddLabel(yes, "2D ", if diff4 > 0 then Color.GREEN else Color.RED);
def diff5 = mac(period = "DAY");
AddLabel(yes, "1D ", if diff5 > 0 then Color.GREEN else Color.RED);
def diff6 = mac(period = "FOUR_HOURS");
AddLabel(yes, "4H ", if diff6 > 0 then Color.GREEN else Color.RED);
def diff7 = mac(period = "TWO_HOURS");
AddLabel(yes, "2H ", if diff7 > 0 then Color.GREEN else Color.RED);

#I modified this section to change colors based on criteria below

def diff8 = mac(period = "HOUR");
#def avg8 = diff(period = "HOUR"); "Did not work"

AddLabel(yes, "1H",
#MACD is over Avg and going up
if Diff8 >= 0 and Diff8 >= diff8[1] then Color.green else
#MACD is over Avg and going down
if diff8 >= 0 and diff8 <= diff8[1] then color.dark_green else #orange else
#MACD is under Avg and going up
if diff8 <= 0 and diff8 >= diff8[1] then Color.dark_red else #Cyan else
#MACD is under Avg and going down
if diff8 <= 0 and diff8 <= diff8[1] then Color.Red else Color.Gray);
 
Hello!

Can someone help me with two features? Ive been trying to accomplish this for 3 days and I need help

I would like create a label or convert the label's in the link below to make the label change from (bullish / bearish), based on the up or down arrow within a certain amount of bars based on timeframe.

Features Ask:
  • I would like the label to change bullish or bearish based on the up arrow and down arrow instead of diff or or zero line of Macd
  • The ability to input the bar range per short, mid, and long label,
- this way if I change the short time frame from 30mins to 15min I can adjust the bars range from 12 to 24 (number of bars in the trading day per timeframe)

https://usethinkscript.com/threads/multi-timeframe-mtf-macd-indicator-for-thinkorswim.474/

@horserider - kudos on this script btw really cool!

Background - I have had some false read's on the lower timeframes and with some backtesting I noticed if the the most recent arrow is bullish on the 15min, the most recent bullish arrow on the 1min has a higher chance of being a profitable play.

@BenTen / @chewie76 - I use BTD and hotzone RSI from you guys and I noticed a similar correlation between timeframes as well but on the RSI side. so just cc you guys incase this ask may help your trading since your tools helps me so much!


Here's my arrow script below:

def up = close is greater than TEMA(tema)and MACD("fast length" = 5, "slow length" = 10, "macd length" = 9)."Diff" ;

plot UPARROW= UP;
UPARROW.SetPaintingStrategy(PaintingStrategy.boolEAN_ARROW_UP);
UPARROW.SetDefaultColor(Color.green);


def down = close is less than TEMA(tema) and MACD("fast length" = 5, "slow length" = 10, "macd length" = 9)."Diff";

plot UPARROW= UP;
UPARROW.SetPaintingStrategy(PaintingStrategy.boolEAN_ARROW_UP);
UPARROW.SetDefaultColor(Color.green);

plot DOWNARROW= DOWN;
DOWNARROW.SetPaintingStrategy(PaintingStrategy.boolEAN_ARROW_DOWN);
DOWNARROW.SetDefaultColor(Color.red);
 
Hello!

Can someone help me with two features? Ive been trying to accomplish this for 3 days and I need help

I would like create a label or convert the label's in the link below to make the label change from (bullish / bearish), based on the up or down arrow within a certain amount of bars based on timeframe.

Features Ask:
  • I would like the label to change bullish or bearish based on the up arrow and down arrow instead of diff or or zero line of Macd
  • The ability to input the bar range per short, mid, and long label,
- this way if I change the short time frame from 30mins to 15min I can adjust the bars range from 12 to 24 (number of bars in the trading day per timeframe)

https://usethinkscript.com/threads/multi-timeframe-mtf-macd-indicator-for-thinkorswim.474/
MTF Indicators cannot be used in watchlists
 
Ive tried mult combos, I cant make this MACD-2Lines into and MTF... suggestions??


declare lower;
input fastLength = 4.5;
input slowLength = 10;
input MACDLength = 20;
INPUT SHOW_VERT_U= no;
INPUT SHOW_VERT_D= no;

input averageType = AverageType.EXPONENTIAL;

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
plot Avg = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
plot ZeroLine = 0;

Value.SetDefaultColor(Color.blue);
Value.setLineWeight(2);
Avg.SetDefaultColor(Color.red);
Avg.setLineWeight(2);
ZeroLine.SetDefaultColor(Color.black);
 
Ive tried mult combos, I cant make this MACD-2Lines into and MTF... suggestions??


declare lower;
input fastLength = 4.5;
input slowLength = 10;
input MACDLength = 20;
INPUT SHOW_VERT_U= no;
INPUT SHOW_VERT_D= no;

input averageType = AverageType.EXPONENTIAL;

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
plot Avg = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
plot ZeroLine = 0;

Value.SetDefaultColor(Color.blue);
Value.setLineWeight(2);
Avg.SetDefaultColor(Color.red);
Avg.setLineWeight(2);
ZeroLine.SetDefaultColor(Color.black);

Here is a mod using my post from above https://usethinkscript.com/threads/multi-timeframe-mtf-macd-indicator-for-thinkorswim.474/post-81363

Some indicators, like the MACD need to have the original MACD indicator code adjusted for a MTF there rather than the reference to MACD in the offshoot MACD-2Lines that you want made into a MTF. See value and avg definitions.

Ruby:
declare lower;
input fastLength = 4.5;
input slowLength = 10;
input MACDLength = 20;
input SHOW_VERT_U = no;
input SHOW_VERT_D = no;

input averageType = AverageType.EXPONENTIAL;
input period = AggregationPeriod.FIVE_MIN;
plot Value = MovAvgExponential(close(period = period), fastLength) - MovAvgExponential(close(period = period), slowLength);
plot Avg = MovAvgExponential(MovAvgExponential(close(period = period), fastLength) - MovAvgExponential(close(period = period), slowLength), MACDLength);
plot ZeroLine = 0;

Value.SetDefaultColor(Color.BLUE);
Value.SetLineWeight(2);
Avg.SetDefaultColor(Color.RED);
Avg.SetLineWeight(2);
ZeroLine.SetDefaultColor(Color.BLACK);
 
Here is a mod using my post from above https://usethinkscript.com/threads/multi-timeframe-mtf-macd-indicator-for-thinkorswim.474/post-81363

Some indicators, like the MACD need to have the original MACD indicator code adjusted for a MTF there rather than the reference to MACD in the offshoot MACD-2Lines that you want made into a MTF. See value and avg definitions.
Thanks Im trying to find a visual via other indicators,like MACD2Lines, that matches the Normalized-MACD, while it gives very good signals it flatlines and you dont know when its arching indicating reversal
 
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.

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);
Thanks for this indicator. I am wondering why it turns green on a 15 and 30 minute time frame and when I look at a macd on a 15 and 30 minute time frame it is not in a cross over position. Does this indicator not indicate that a cross over has happened on selected time frames?
 
Thanks for this indicator. I am wondering why it turns green on a 15 and 30 minute time frame and when I look at a macd on a 15 and 30 minute time frame it is not in a cross over position. Does this indicator not indicate that a cross over has happened on selected time frames?
This may be the answer to that question - see in particular the discussion regarding MTF indicators behavior and how to confirm the signal:

https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/
 
I am not good at coding, so this is probably really simple for those that are. I am trying to get a 5 minute MACD histogram on my 2 minute chart. I have looked at several of the MTF MACD threads and most of that code is over my head. This is the MACD indicator that I'm currently using. I was thinking the simple aggregationperiod line would do the trick, but it doesn't. Is there a simple way to make this code show the 5 minute on the 2 minute chart? Thanks.

#
# MACD with colored slope
#

declare lower;
input aggregationPeriod = AggregationPeriod.Five_Min;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;

plot Value = MACD(fastLength, slowLength, MACDLength, averageType).Value;
plot Value2 = Value;
plot Avg = MACD(fastLength, slowLength, MACDLength, averageType).Avg;
plot ZeroLine = 0;

Value.setpaintingStrategy(paintingStrategy.HISTOGRAM);
Value.assignValueColor(if Value > Value[1] then Color.GREEN else Color.RED);
Value2.assignValueColor(if Value > Value[1] then Color.Green else Color.RED);
Avg.assignValueColor(if Avg > Avg[1] then Color.GREEN else Color.RED);
ZeroLine.SetDefaultColor(GetColor(0));
 
Last edited:
I am not good at coding, so this is probably really simple for those that are. I am trying to get a 5 minute MACD histogram on my 2 minute chart.

This will give you something to work with:
Here is a mod using my post from above https://usethinkscript.com/threads/multi-timeframe-mtf-macd-indicator-for-thinkorswim.474/post-81363

Some indicators, like the MACD need to have the original MACD indicator code adjusted for a MTF there rather than the reference to MACD in the offshoot MACD-2Lines that you want made into a MTF. See value and avg definitions.
 
Here is how I would do that time frame. Set your chart to 2 minutes (Intraday). Set up the MACD to 50/100/15. That should show you a 5 min MACD on the 2 min chart.
I use that setup on a 1 minute chart, then 2 MACDs below. I am running a 1 minute MACD, then a 3 min MACD. One minute MACD set at 10/20/7, The 2nd one set at 30/60/12
 
Here is how I would do that time frame. Set your chart to 2 minutes (Intraday). Set up the MACD to 50/100/15. That should show you a 5 min MACD on the 2 min chart.
I use that setup on a 1 minute chart, then 2 MACDs below. I am running a 1 minute MACD, then a 3 min MACD. One minute MACD set at 10/20/7, The 2nd one set at 30/60/12
I never thought of doing that. After getting the MTF MACD tested, changing the MACD settings like that got the same results most of the time. Thanks for the suggestion.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
256 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top