Multi-TimeFrame (MTF) MACD Indicator for ThinkorSwim

horserider

Well-known member
VIP
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);
 

horserider

Well-known member
VIP
@joshuadstout Hmmm just tried it worked fine. Give more details how you added it or show the chart. Maybe then it can be figured out. Or delete it and re add and see if that solves the problem.
 

dcruz11

New member
I'd like to know if the macd is crossed up or down on multiple time frames...and show as a box like these below.

Right now I have one that shows if the current time frame is crossed up or down with the "M".
The other time flags on the chart are for the ttm squeeze, but I was imaging the same thing just for macd crossed up or down.

qiWr9ji.jpg
 

horserider

Well-known member
VIP
Send me the money !!!!

Add these lines to the MTF MACD and comment out or delete the "declare lower"

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

You can now choose your time frames as usual in that studyand have the label display red or green for the short, mid and long time frames you have chosen.

hahaha just put it together not tested. See if it works and let everyone know.
 
Does anyone have a multi time frame MacD indicator similar to this EMA trend indicator below, or can this one be changed to macd. I think a lot of traders would like it. Thanks guys!

Code:
input MAtype = AverageType.EXPONENTIAL;
input ShortAverage = 5;
input LongAverage = 21;

def MA_month_close;
def MA_month_shortAvg;
def MA_month_longAvg;
def MA_month_higher;
def MA_month_lower;
def MA_month_same;
def MA_month_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.MONTH {
    MA_month_close = close(period="Month");
    MA_month_shortAvg = MovingAverage(MAtype, MA_month_close, ShortAverage);
    MA_month_longAvg = MovingAverage(MAtype,MA_month_close, LongAverage);
    MA_month_nowcrossing = if Crosses(MA_month_shortAvg, MA_month_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_month_same = if MA_month_nowcrossing == 1 then 1 else Double.NaN;
    if MA_month_same == 1 {
        MA_month_higher = Double.NaN;
        MA_month_lower = Double.NaN;
    } else {
        MA_month_higher = if MA_month_shortAvg >= MA_month_longAvg[1] then 1 else Double.NaN;
        MA_month_lower = if MA_month_shortAvg < MA_month_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_month_close = 0;
    MA_month_shortAvg = 0;
    MA_month_longAvg = 0;
    MA_month_higher = Double.NaN;
    MA_month_lower = Double.NaN;
    MA_month_same = Double.NaN;
    MA_month_nowcrossing = 0;
}
AddLabel(MA_month_higher, "M", Color.DARK_GREEN);
AddLabel(MA_month_lower, "M", Color.DARK_RED);
AddLabel(MA_month_same, "M", Color.WHITE);

def MA_week_close;
def MA_week_shortAvg;
def MA_week_longAvg;
def MA_week_higher;
def MA_week_lower;
def MA_week_same;
def MA_week_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.WEEK {
    MA_week_close = close(period="Week");
    MA_week_shortAvg = MovingAverage(MAtype, MA_week_close, ShortAverage);
    MA_week_longAvg = MovingAverage(MAtype,MA_week_close, LongAverage);
    MA_week_nowcrossing = if Crosses(MA_week_shortAvg, MA_week_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_week_same = if MA_week_nowcrossing == 1 then 1 else Double.NaN;
    if MA_week_same == 1 {
        MA_week_higher = Double.NaN;
        MA_week_lower = Double.NaN;
    } else {
        MA_week_higher = if MA_week_shortAvg >= MA_week_longAvg[1] then 1 else Double.NaN;
        MA_week_lower = if MA_week_shortAvg < MA_week_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_week_close = 0;
    MA_week_shortAvg = 0;
    MA_week_longAvg = 0;
    MA_week_higher = Double.NaN;
    MA_week_lower = Double.NaN;
    MA_week_same = Double.NaN;
    MA_week_nowcrossing = 0;
}
AddLabel(MA_week_higher, "W", Color.DARK_GREEN);
AddLabel(MA_week_lower, "W", Color.DARK_RED);
AddLabel(MA_week_same, "W", Color.WHITE);

def MA_4day_close;
def MA_4day_shortAvg;
def MA_4day_longAvg;
def MA_4day_higher;
def MA_4day_lower;
def MA_4day_same;
def MA_4day_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_DAYS {
    MA_4day_close = close(period="4 days");
    MA_4day_shortAvg = MovingAverage(MAtype, MA_4day_close, ShortAverage);
    MA_4day_longAvg = MovingAverage(MAtype,MA_4day_close, LongAverage);
    MA_4day_nowcrossing = if Crosses(MA_4day_shortAvg, MA_4day_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_4day_same = if MA_4day_nowcrossing == 1 then 1 else Double.NaN;
    if MA_4day_same == 1 {
        MA_4day_higher = Double.NaN;
        MA_4day_lower = Double.NaN;
    } else {
        MA_4day_higher = if MA_4day_shortAvg >= MA_4day_longAvg[1] then 1 else Double.NaN;
        MA_4day_lower = if MA_4day_shortAvg < MA_4day_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_4day_close = 0;
    MA_4day_shortAvg = 0;
    MA_4day_longAvg = 0;
    MA_4day_higher = Double.NaN;
    MA_4day_lower = Double.NaN;
    MA_4day_same = Double.NaN;
    MA_4day_nowcrossing = 0;
}
AddLabel(MA_4day_higher, "4D", Color.DARK_GREEN);
AddLabel(MA_4day_lower, "4D", Color.DARK_RED);
AddLabel(MA_4day_same, "4D", Color.WHITE);

def MA_3day_close;
def MA_3day_shortAvg;
def MA_3day_longAvg;
def MA_3day_higher;
def MA_3day_lower;
def MA_3day_same;
def MA_3day_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.THREE_DAYS {
    MA_3day_close = close(period="3 days");
    MA_3day_shortAvg = MovingAverage(MAtype, MA_3day_close, ShortAverage);
    MA_3day_longAvg = MovingAverage(MAtype,MA_3day_close, LongAverage);
    MA_3day_nowcrossing = if Crosses(MA_3day_shortAvg, MA_3day_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_3day_same = if MA_3day_nowcrossing == 1 then 1 else Double.NaN;
    if MA_3day_same == 1 {
        MA_3day_higher = Double.NaN;
        MA_3day_lower = Double.NaN;
    } else {
        MA_3day_higher = if MA_3day_shortAvg >= MA_3day_longAvg[1] then 1 else Double.NaN;
        MA_3day_lower = if MA_3day_shortAvg < MA_3day_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_3day_close = 0;
    MA_3day_shortAvg = 0;
    MA_3day_longAvg = 0;
    MA_3day_higher = Double.NaN;
    MA_3day_lower = Double.NaN;
    MA_3day_same = Double.NaN;
    MA_3day_nowcrossing = 0;
}
AddLabel(MA_3day_higher, "3D", Color.DARK_GREEN);
AddLabel(MA_3day_lower, "3D", Color.DARK_RED);
AddLabel(MA_3day_same, "3D", Color.WHITE);

def MA_2day_close;
def MA_2day_shortAvg;
def MA_2day_longAvg;
def MA_2day_higher;
def MA_2day_lower;
def MA_2day_same;
def MA_2day_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.TWO_DAYS {
    MA_2day_close = close(period="2 days");
    MA_2day_shortAvg = MovingAverage(MAtype, MA_2day_close, ShortAverage);
    MA_2day_longAvg = MovingAverage(MAtype,MA_2day_close, LongAverage);
    MA_2day_nowcrossing = if Crosses(MA_2day_shortAvg, MA_2day_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_2day_same = if MA_2day_nowcrossing == 1 then 1 else Double.NaN;
    if MA_2day_same == 1 {
        MA_2day_higher = Double.NaN;
        MA_2day_lower = Double.NaN;
    } else {
        MA_2day_higher = if MA_2day_shortAvg >= MA_2day_longAvg[1] then 1 else Double.NaN;
        MA_2day_lower = if MA_2day_shortAvg < MA_2day_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_2day_close = 0;
    MA_2day_shortAvg = 0;
    MA_2day_longAvg = 0;
    MA_2day_higher = Double.NaN;
    MA_2day_lower = Double.NaN;
    MA_2day_same = Double.NaN;
    MA_2day_nowcrossing = 0;
}
AddLabel(MA_2day_higher, "2D", Color.DARK_GREEN);
AddLabel(MA_2day_lower, "2D", Color.DARK_RED);
AddLabel(MA_2day_same, "2D", Color.WHITE);

def MA_1day_close;
def MA_1day_shortAvg;
def MA_1day_longAvg;
def MA_1day_higher;
def MA_1day_lower;
def MA_1day_same;
def MA_1day_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.DAY {
    MA_1day_close = close(period="Day");
    MA_1day_shortAvg = MovingAverage(MAtype, MA_1day_close, ShortAverage);
    MA_1day_longAvg = MovingAverage(MAtype,MA_1day_close, LongAverage);
    MA_1day_nowcrossing = if Crosses(MA_1day_shortAvg, MA_1day_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_1day_same = if MA_1day_nowcrossing == 1 then 1 else Double.NaN;
    if MA_1day_same == 1 {
        MA_1day_higher = Double.NaN;
        MA_1day_lower = Double.NaN;
    } else {
        MA_1day_higher = if MA_1day_shortAvg >= MA_1day_longAvg[1] then 1 else Double.NaN;
        MA_1day_lower = if MA_1day_shortAvg < MA_1day_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_1day_close = 0;
    MA_1day_shortAvg = 0;
    MA_1day_longAvg = 0;
    MA_1day_higher = Double.NaN;
    MA_1day_lower = Double.NaN;
    MA_1day_same = Double.NaN;
    MA_1day_nowcrossing = 0;
}
AddLabel(MA_1day_higher, "1D", Color.DARK_GREEN);
AddLabel(MA_1day_lower, "1D", Color.DARK_RED);
AddLabel(MA_1day_same, "1D", Color.WHITE);

def MA_4hour_close;
def MA_4hour_shortAvg;
def MA_4hour_longAvg;
def MA_4hour_higher;
def MA_4hour_lower;
def MA_4hour_same;
def MA_4hour_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_HOURS {
    MA_4hour_close = close(period="4 hours");
    MA_4hour_shortAvg = MovingAverage(MAtype, MA_4hour_close, ShortAverage);
    MA_4hour_longAvg = MovingAverage(MAtype,MA_4hour_close, LongAverage);
    MA_4hour_nowcrossing = if Crosses(MA_4hour_shortAvg, MA_4hour_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_4hour_same = if MA_4hour_nowcrossing == 1 then 1 else Double.NaN;
    if MA_4hour_same == 1 {
        MA_4hour_higher = Double.NaN;
        MA_4hour_lower = Double.NaN;
    } else {
        MA_4hour_higher = if MA_4hour_shortAvg >= MA_4hour_longAvg[1] then 1 else Double.NaN;
        MA_4hour_lower = if MA_4hour_shortAvg < MA_4hour_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_4hour_close = 0;
    MA_4hour_shortAvg = 0;
    MA_4hour_longAvg = 0;
    MA_4hour_higher = Double.NaN;
    MA_4hour_lower = Double.NaN;
    MA_4hour_same = Double.NaN;
    MA_4hour_nowcrossing = 0;
}
AddLabel(MA_4hour_higher, "4h", Color.DARK_GREEN);
AddLabel(MA_4hour_lower, "4h", Color.DARK_RED);
AddLabel(MA_4hour_same, "4h", Color.WHITE);

def MA_2hour_close;
def MA_2hour_shortAvg;
def MA_2hour_longAvg;
def MA_2hour_higher;
def MA_2hour_lower;
def MA_2hour_same;
def MA_2hour_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.TWO_HOURS {
    MA_2hour_close = close(period="2 hours");
    MA_2hour_shortAvg = MovingAverage(MAtype, MA_2hour_close, ShortAverage);
    MA_2hour_longAvg = MovingAverage(MAtype,MA_2hour_close, LongAverage);
    MA_2hour_nowcrossing = if Crosses(MA_2hour_shortAvg, MA_2hour_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_2hour_same = if MA_2hour_nowcrossing == 1 then 1 else Double.NaN;
    if MA_2hour_same == 1 {
        MA_2hour_higher = Double.NaN;
        MA_2hour_lower = Double.NaN;
    } else {
        MA_2hour_higher = if MA_2hour_shortAvg >= MA_2hour_longAvg[1] then 1 else Double.NaN;
        MA_2hour_lower = if MA_2hour_shortAvg < MA_2hour_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_2hour_close = 0;
    MA_2hour_shortAvg = 0;
    MA_2hour_longAvg = 0;
    MA_2hour_higher = Double.NaN;
    MA_2hour_lower = Double.NaN;
    MA_2hour_same = Double.NaN;
    MA_2hour_nowcrossing = 0;
}
AddLabel(MA_2hour_higher, "2h", Color.DARK_GREEN);
AddLabel(MA_2hour_lower, "2h", Color.DARK_RED);
AddLabel(MA_2hour_same, "2h", Color.WHITE);

def MA_1hour_close;
def MA_1hour_shortAvg;
def MA_1hour_longAvg;
def MA_1hour_higher;
def MA_1hour_lower;
def MA_1hour_same;
def MA_1hour_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.HOUR {
    MA_1hour_close = close(period="1 hour");
    MA_1hour_shortAvg = MovingAverage(MAtype, MA_1hour_close, ShortAverage);
    MA_1hour_longAvg = MovingAverage(MAtype,MA_1hour_close, LongAverage);
    MA_1hour_nowcrossing = if Crosses(MA_1hour_shortAvg, MA_1hour_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_1hour_same = if MA_1hour_nowcrossing == 1 then 1 else Double.NaN;
    if MA_1hour_same == 1 {
        MA_1hour_higher = Double.NaN;
        MA_1hour_lower = Double.NaN;
    } else {
        MA_1hour_higher = if MA_1hour_shortAvg >= MA_1hour_longAvg[1] then 1 else Double.NaN;
        MA_1hour_lower = if MA_1hour_shortAvg < MA_1hour_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_1hour_close = 0;
    MA_1hour_shortAvg = 0;
    MA_1hour_longAvg = 0;
    MA_1hour_higher = Double.NaN;
    MA_1hour_lower = Double.NaN;
    MA_1hour_same = Double.NaN;
    MA_1hour_nowcrossing = 0;
}
AddLabel(MA_1hour_higher, "1h", Color.DARK_GREEN);
AddLabel(MA_1hour_lower, "1h", Color.DARK_RED);
AddLabel(MA_1hour_same, "1h", Color.WHITE);

def MA_30min_close;
def MA_30min_shortAvg;
def MA_30min_longAvg;
def MA_30min_higher;
def MA_30min_lower;
def MA_30min_same;
def MA_30min_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.THIRTY_MIN {
    MA_30min_close = close(period="30 min");
    MA_30min_shortAvg = MovingAverage(MAtype, MA_30min_close, ShortAverage);
    MA_30min_longAvg = MovingAverage(MAtype,MA_30min_close, LongAverage);
    MA_30min_nowcrossing = if Crosses(MA_30min_shortAvg, MA_30min_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_30min_same = if MA_30min_nowcrossing == 1 then 1 else Double.NaN;
    if MA_30min_same == 1 {
        MA_30min_higher = Double.NaN;
        MA_30min_lower = Double.NaN;
    } else {
        MA_30min_higher = if MA_30min_shortAvg >= MA_30min_longAvg[1] then 1 else Double.NaN;
        MA_30min_lower = if MA_30min_shortAvg < MA_30min_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_30min_close = 0;
    MA_30min_shortAvg = 0;
    MA_30min_longAvg = 0;
    MA_30min_higher = Double.NaN;
    MA_30min_lower = Double.NaN;
    MA_30min_same = Double.NaN;
    MA_30min_nowcrossing = 0;
}
AddLabel(MA_30min_higher, "30m", Color.DARK_GREEN);
AddLabel(MA_30min_lower, "30m", Color.DARK_RED);
AddLabel(MA_30min_same, "30m", Color.WHITE);

def MA_15min_close;
def MA_15min_shortAvg;
def MA_15min_longAvg;
def MA_15min_higher;
def MA_15min_lower;
def MA_15min_same;
def MA_15min_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN {
    MA_15min_close = close(period="15 min");
    MA_15min_shortAvg = MovingAverage(MAtype, MA_15min_close, ShortAverage);
    MA_15min_longAvg = MovingAverage(MAtype,MA_15min_close, LongAverage);
    MA_15min_nowcrossing = if Crosses(MA_15min_shortAvg, MA_15min_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_15min_same = if MA_15min_nowcrossing == 1 then 1 else Double.NaN;
    if MA_15min_same == 1 {
        MA_15min_higher = Double.NaN;
        MA_15min_lower = Double.NaN;
    } else {
        MA_15min_higher = if MA_15min_shortAvg >= MA_15min_longAvg[1] then 1 else Double.NaN;
        MA_15min_lower = if MA_15min_shortAvg < MA_15min_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_15min_close = 0;
    MA_15min_shortAvg = 0;
    MA_15min_longAvg = 0;
    MA_15min_higher = Double.NaN;
    MA_15min_lower = Double.NaN;
    MA_15min_same = Double.NaN;
    MA_15min_nowcrossing = 0;
}
AddLabel(MA_15min_higher, "15m", Color.DARK_GREEN);
AddLabel(MA_15min_lower, "15m", Color.DARK_RED);
AddLabel(MA_15min_same, "15m", Color.WHITE);

def MA_10min_close;
def MA_10min_shortAvg;
def MA_10min_longAvg;
def MA_10min_higher;
def MA_10min_lower;
def MA_10min_same;
def MA_10min_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.TEN_MIN {
    MA_10min_close = close(period="10 min");
    MA_10min_shortAvg = MovingAverage(MAtype, MA_10min_close, ShortAverage);
    MA_10min_longAvg = MovingAverage(MAtype,MA_10min_close, LongAverage);
    MA_10min_nowcrossing = if Crosses(MA_10min_shortAvg, MA_10min_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_10min_same = if MA_10min_nowcrossing == 1 then 1 else Double.NaN;
    if MA_10min_same == 1 {
        MA_10min_higher = Double.NaN;
        MA_10min_lower = Double.NaN;
    } else {
        MA_10min_higher = if MA_10min_shortAvg >= MA_10min_longAvg[1] then 1 else Double.NaN;
        MA_10min_lower = if MA_10min_shortAvg < MA_10min_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_10min_close = 0;
    MA_10min_shortAvg = 0;
    MA_10min_longAvg = 0;
    MA_10min_higher = Double.NaN;
    MA_10min_lower = Double.NaN;
    MA_10min_same = Double.NaN;
    MA_10min_nowcrossing = 0;
}
AddLabel(MA_10min_higher, "10m", Color.DARK_GREEN);
AddLabel(MA_10min_lower, "10m", Color.DARK_RED);
AddLabel(MA_10min_same, "10m", Color.WHITE);

def MA_shortAvgmin_close;
def MA_shortAvgmin_shortAvg;
def MA_shortAvgmin_longAvg;
def MA_shortAvgmin_higher;
def MA_shortAvgmin_lower;
def MA_shortAvgmin_same;
def MA_shortAvgmin_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN {
    MA_shortAvgmin_close = close(period="5 min");
    MA_shortAvgmin_shortAvg = MovingAverage(MAtype, MA_shortAvgmin_close, ShortAverage);
    MA_shortAvgmin_longAvg = MovingAverage(MAtype,MA_shortAvgmin_close, LongAverage);
    MA_shortAvgmin_nowcrossing = if Crosses(MA_shortAvgmin_shortAvg, MA_shortAvgmin_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_shortAvgmin_same = if MA_shortAvgmin_nowcrossing == 1 then 1 else Double.NaN;
    if MA_shortAvgmin_same == 1 {
        MA_shortAvgmin_higher = Double.NaN;
        MA_shortAvgmin_lower = Double.NaN;
    } else {
        MA_shortAvgmin_higher = if MA_shortAvgmin_shortAvg >= MA_shortAvgmin_longAvg[1] then 1 else Double.NaN;
        MA_shortAvgmin_lower = if MA_shortAvgmin_shortAvg < MA_shortAvgmin_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_shortAvgmin_close = 0;
    MA_shortAvgmin_shortAvg = 0;
    MA_shortAvgmin_longAvg = 0;
    MA_shortAvgmin_higher = Double.NaN;
    MA_shortAvgmin_lower = Double.NaN;
    MA_shortAvgmin_same = Double.NaN;
    MA_shortAvgmin_nowcrossing = 0;
}
AddLabel(MA_shortAvgmin_higher, "5m", Color.DARK_GREEN);
AddLabel(MA_shortAvgmin_lower, "5m", Color.DARK_RED);
AddLabel(MA_shortAvgmin_same, "5m", Color.WHITE);

def MA_4min_close;
def MA_4min_shortAvg;
def MA_4min_longAvg;
def MA_4min_higher;
def MA_4min_lower;
def MA_4min_same;
def MA_4min_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_MIN {
    MA_4min_close = close(period="4 min");
    MA_4min_shortAvg = MovingAverage(MAtype, MA_4min_close, ShortAverage);
    MA_4min_longAvg = MovingAverage(MAtype,MA_4min_close, LongAverage);
    MA_4min_nowcrossing = if Crosses(MA_4min_shortAvg, MA_4min_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_4min_same = if MA_4min_nowcrossing == 1 then 1 else Double.NaN;
    if MA_4min_same == 1 {
        MA_4min_higher = Double.NaN;
        MA_4min_lower = Double.NaN;
    } else {
        MA_4min_higher = if MA_4min_shortAvg >= MA_4min_longAvg[1] then 1 else Double.NaN;
        MA_4min_lower = if MA_4min_shortAvg < MA_4min_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_4min_close = 0;
    MA_4min_shortAvg = 0;
    MA_4min_longAvg = 0;
    MA_4min_higher = Double.NaN;
    MA_4min_lower = Double.NaN;
    MA_4min_same = Double.NaN;
    MA_4min_nowcrossing = 0;
}
AddLabel(MA_4min_higher, "4m", Color.DARK_GREEN);
AddLabel(MA_4min_lower, "4m", Color.DARK_RED);
AddLabel(MA_4min_same, "4m", Color.WHITE);

def MA_3min_close;
def MA_3min_shortAvg;
def MA_3min_longAvg;
def MA_3min_higher;
def MA_3min_lower;
def MA_3min_same;
def MA_3min_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.THREE_MIN {
    MA_3min_close = close(period="3 min");
    MA_3min_shortAvg = MovingAverage(MAtype, MA_3min_close, ShortAverage);
    MA_3min_longAvg = MovingAverage(MAtype,MA_3min_close, LongAverage);
    MA_3min_nowcrossing = if Crosses(MA_3min_shortAvg, MA_3min_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_3min_same = if MA_3min_nowcrossing == 1 then 1 else Double.NaN;
    if MA_3min_same == 1 {
        MA_3min_higher = Double.NaN;
        MA_3min_lower = Double.NaN;
    } else {
        MA_3min_higher = if MA_3min_shortAvg >= MA_3min_longAvg[1] then 1 else Double.NaN;
        MA_3min_lower = if MA_3min_shortAvg < MA_3min_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_3min_close = 0;
    MA_3min_shortAvg = 0;
    MA_3min_longAvg = 0;
    MA_3min_higher = Double.NaN;
    MA_3min_lower = Double.NaN;
    MA_3min_same = Double.NaN;
    MA_3min_nowcrossing = 0;
}
AddLabel(MA_3min_higher, "3m", Color.DARK_GREEN);
AddLabel(MA_3min_lower, "3m", Color.DARK_RED);
AddLabel(MA_3min_same, "3m", Color.WHITE);

def MA_2min_close;
def MA_2min_shortAvg;
def MA_2min_longAvg;
def MA_2min_higher;
def MA_2min_lower;
def MA_2min_same;
def MA_2min_nowcrossing;
if GetAggregationPeriod() <= AggregationPeriod.TWO_MIN {
    MA_2min_close = close(period="2 min");
    MA_2min_shortAvg = MovingAverage(MAtype, MA_2min_close, ShortAverage);
    MA_2min_longAvg = MovingAverage(MAtype,MA_2min_close, LongAverage);
    MA_2min_nowcrossing = if Crosses(MA_2min_shortAvg, MA_2min_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_2min_same = if MA_2min_nowcrossing == 1 then 1 else Double.NaN;
    if MA_2min_same == 1 {
        MA_2min_higher = Double.NaN;
        MA_2min_lower = Double.NaN;
    } else {
        MA_2min_higher = if MA_2min_shortAvg >= MA_2min_longAvg[1] then 1 else Double.NaN;
        MA_2min_lower = if MA_2min_shortAvg < MA_2min_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_2min_close = 0;
    MA_2min_shortAvg = 0;
    MA_2min_longAvg = 0;
    MA_2min_higher = Double.NaN;
    MA_2min_lower = Double.NaN;
    MA_2min_same = Double.NaN;
    MA_2min_nowcrossing = 0;
}
AddLabel(MA_2min_higher, "2m", Color.DARK_GREEN);
AddLabel(MA_2min_lower, "2m", Color.DARK_RED);
AddLabel(MA_2min_same, "2m", Color.WHITE);


def MA_1min_close;
def MA_1min_shortAvg;
def MA_1min_longAvg;
def MA_1min_higher;
def MA_1min_lower;
def MA_1min_same;
def MA_1min_nowcrossing;
if GetAggregationPeriod() == AggregationPeriod.MIN {
    MA_1min_close = close(period="1 min");
    MA_1min_shortAvg = MovingAverage(MAtype, MA_1min_close, ShortAverage);
    MA_1min_longAvg = MovingAverage(MAtype,MA_1min_close, LongAverage);
    MA_1min_nowcrossing = if Crosses(MA_1min_shortAvg, MA_1min_longAvg, CrossingDirection.ANY) then 1 else 0;
    MA_1min_same = if MA_1min_nowcrossing == 1 then 1 else Double.NaN;
    if MA_1min_same == 1 {
        MA_1min_higher = Double.NaN;
        MA_1min_lower = Double.NaN;
    } else {
        MA_1min_higher = if MA_1min_shortAvg >= MA_1min_longAvg[1] then 1 else Double.NaN;
        MA_1min_lower = if MA_1min_shortAvg < MA_1min_longAvg[1] then 1 else Double.NaN;
    }
} else {
    MA_1min_close = 0;
    MA_1min_shortAvg = 0;
    MA_1min_longAvg = 0;
    MA_1min_higher = Double.NaN;
    MA_1min_lower = Double.NaN;
    MA_1min_same = Double.NaN;
    MA_1min_nowcrossing = 0;
}
AddLabel(MA_1min_higher, "1m", Color.DARK_GREEN);
AddLabel(MA_1min_lower, "1m", Color.DARK_RED);
AddLabel(MA_1min_same, "1m", Color.WHITE);
 

mike101

New member
Hi,

I have code but its not working, it shows me downtrend color, also study is open on small time frame only. It doesnt plot when chart is in 30 mins or 1 hour.

See below attached chart for more info. The study doesnt plot green dots when the MACD is positive.

https://tos.mx/vGnVMBp
Code:
#multi time frame MACD

input smallTimeFrame = AggregationPeriod.THREE_MIN; #hint time1:  Plot @ 0.
input MediumTimeFrame = AggregationPeriod.FIVE_MIN; #hint time2:  Plot @ 0.5
input largeTimeFrame = AggregationPeriod.FIFTEEN_MIN; #hint time3:  Plot @ 1.0
input largestTimeFrame = AggregationPeriod.Thirty_MIN; #hint time4:  Plot @ 1.5
#############
declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSingals = no;


def Value1 = MovingAverage(averageType, close(period = smallTimeFrame), fastLength) - MovingAverage(averageType, close(period = smallTimeFrame), slowLength);
def Avg1 = MovingAverage(averageType, Value1, MACDLength);
def Diff1 = Value1 – Avg1;
def ZeroLine1 = 0;

def SmallMACD = if Diff1 > ZeroLine1 then 1 else if Diff1 < ZeroLine1 then -1 else 0;

plot MACD1 = if IsNaN(close) then Double.NaN else 1;
MACD1.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD1.SetLineWeight(2);
DefineGlobalColor("UpTrend", Color.GREEN);
DefineGlobalColor("DownTrend", Color.MAGENTA);
MACD1.AssignValueColor(if SmallMACD > 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));


#Timeframe 2

def Value2 = MovingAverage(averageType, close(period = mediumTimeFrame), fastLength) - MovingAverage(averageType, close(period = mediumTimeFrame), slowLength);
def Avg2 = MovingAverage(averageType, Value2, MACDLength);
def Diff2 = Value2 – Avg2;
def ZeroLine2 = 0;

def MediumMACD = if Diff2 > ZeroLine2 then 1 else if Diff2 < ZeroLine2 then -1 else 0;

plot MACD2 = if IsNaN(close) then Double.NaN else 2;
MACD2.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD2.SetLineWeight(2);
DefineGlobalColor("UpTrend", Color.GREEN);
DefineGlobalColor("DownTrend", Color.MAGENTA);
MACD2.AssignValueColor(if MediumMACD > 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));

#Timeframe 3

def Value3 = MovingAverage(averageType, close(period = largeTimeFrame), fastLength) - MovingAverage(averageType,close(period = largeTimeFrame), slowLength);
def Avg3 = MovingAverage(averageType, Value3, MACDLength);
def Diff3 = Value3 – Avg3;
def ZeroLine3 = 0;

def largeMACD = if Diff3 > ZeroLine3 then 1 else if Diff3 < ZeroLine3 then -1 else 0;

plot MACD3 = if IsNaN(close) then Double.NaN else 3;
MACD3.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD3.SetLineWeight(2);
DefineGlobalColor("UpTrend", Color.GREEN);
DefineGlobalColor("DownTrend", Color.MAGENTA);
MACD3.AssignValueColor(if LargeMACD > 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));

#Timeframe 4

def Value4 = MovingAverage(averageType, close(period = largestTimeFrame), fastLength) - MovingAverage(averageType,close(period = largestTimeFrame), slowLength);
def Avg4 = MovingAverage(averageType, Value4, MACDLength);
def Diff4 = Value4 – Avg4;
def ZeroLine4 = 0;

def largestMACD = if Diff4 > ZeroLine4 then 1 else if Diff4 < ZeroLine4 then -1 else 0;

plot MACD4 = if IsNaN(close) then Double.NaN else 4;
MACD4.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD4.SetLineWeight(2);
DefineGlobalColor("UpTrend", Color.GREEN);
DefineGlobalColor("DownTrend", Color.MAGENTA);
MACD4.AssignValueColor(if LargestMACD > 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));

any help greatly appreciated.
 
Last edited:

tomsk

Well-known member
VIP
@mike101 I have fixed up your code so that the MTF plots properly. Also I have resequenced and cleaned up the code for variables not used as well as utilized more meaningful variable names for any future work you may wish to do. Load this study on a 1 minute chart of AAPL as an example

Code:
# MACD MTF
# Modified by tomsk
# 11.9.2019

declare lower;

input Agg1 = AggregationPeriod.THREE_MIN;
input Agg2 = AggregationPeriod.FIVE_MIN;
input Agg3 = AggregationPeriod.FIFTEEN_MIN;
input Agg4 = AggregationPeriod.Thirty_MIN;

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

DefineGlobalColor("UpTrend", Color.GREEN);
DefineGlobalColor("DownTrend", Color.MAGENTA);

# Aggregation 1

def Value1 = MovingAverage(averageType, close(period = Agg1), fastLength) -
             MovingAverage(averageType, close(period = Agg1), slowLength);
def Avg1 = MovingAverage(averageType, Value1, MACDLength);
def Diff1 = Value1 – Avg1;
def MACDAgg1 = if Diff1 > 0 then 1 else if Diff1 < 0 then -1 else 0;
plot MACD1 = if IsNaN(close) then Double.NaN else 1;
MACD1.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD1.SetLineWeight(2);
MACD1.AssignValueColor(if MACDAgg1 == 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));

# Aggregation 2

def Value2 = MovingAverage(averageType, close(period = Agg2), fastLength) -
             MovingAverage(averageType, close(period = Agg2), slowLength);
def Avg2 = MovingAverage(averageType, Value2, MACDLength);
def Diff2 = Value2 – Avg2;
def MACDAgg2 = if Diff2 > 0 then 1 else if Diff2 < 0 then -1 else 0;
plot MACD2 = if IsNaN(close) then Double.NaN else 2;
MACD2.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD2.SetLineWeight(2);
MACD2.AssignValueColor(if MACDAgg2 == 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));

# Aggregation 3

def Value3 = MovingAverage(averageType, close(period = Agg3), fastLength) -
             MovingAverage(averageType,close(period = Agg3), slowLength);
def Avg3 = MovingAverage(averageType, Value3, MACDLength);
def Diff3 = Value3 – Avg3;
def MACDAgg3 = if Diff3 > 0 then 1 else if Diff3 < 0 then -1 else 0;
plot MACD3 = if IsNaN(close) then Double.NaN else 3;
MACD3.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD3.SetLineWeight(2);
MACD3.AssignValueColor(if MACDAgg3 == 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));

# Aggregation 4

def Value4 = MovingAverage(averageType, close(period = Agg4), fastLength) -
             MovingAverage(averageType,close(period = Agg4), slowLength);
def Avg4 = MovingAverage(averageType, Value4, MACDLength);
def Diff4 = Value4 – Avg4;
def MACDAgg4 = if Diff4 > 0 then 1 else if Diff4 < 0 then -1 else 0;
plot MACD4 = if IsNaN(close) then Double.NaN else 4;
MACD4.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD4.SetLineWeight(2);
MACD4.AssignValueColor(if MACDAgg4 == 1 then GlobalColor("UpTrend") else GlobalColor("DownTrend"));
 

tomsk

Well-known member
VIP
@HighBredCloud @netarchitech In light of your earlier note, as a proof of concept here is version 2 of the MACD MTF that incorporates the 4 colors of the standard TOS MACD depending on the following states.

Positive and Up
Positive and Down
Negative and Down
Negative and Up

Please note that this is not the standard way a typical MTF functions and is provided here as a proof of concept in case you are interested.

Code:
# MACD MTF
# Modified by tomsk
# 11.13.2019

# V11.09.2019 - tomsk - clean up mike101's code so that the MTF plots properly.
# V11.13.2019 - tomsk - added additional color coding similar to standard TOS MACD

declare lower;

input Agg1 = AggregationPeriod.THREE_MIN;
input Agg2 = AggregationPeriod.FIVE_MIN;
input Agg3 = AggregationPeriod.FIFTEEN_MIN;
input Agg4 = AggregationPeriod.Thirty_MIN;

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

def PosUp = 1;
def PosDn = 2;
def NegDn = 3;
def NegUp = 4;

DefineGlobalColor("Positive and Up", Color.GREEN);
DefineGlobalColor("Positive and Down", Color.DARK_GREEN);
DefineGlobalColor("Negative and Down", Color.RED);
DefineGlobalColor("Negative and Up", Color.DARK_RED);

# Aggregation 1

def Value1 = MovingAverage(averageType, close(period = Agg1), fastLength) -
             MovingAverage(averageType, close(period = Agg1), slowLength);
def Avg1 = MovingAverage(averageType, Value1, MACDLength);
def Diff1 = Value1 – Avg1;
def MACDAgg1 = if Diff1 >= 0
               then if Diff1 > Diff1[1]
                    then PosUp
                    else PosDn
               else if Diff1 < Diff1[1]
                    then NegDn
                    else NegUp;
plot MACD1 = if IsNaN(close) then Double.NaN else 1;
MACD1.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD1.SetLineWeight(2);
MACD1.AssignValueColor(if MACDAgg1 == PosUp then GlobalColor("Positive and Up")
                       else if MACDAgg1 == PosDn then GlobalColor("Positive and Down")
                       else if MACDAgg1 == NegDn then GlobalColor("Negative and Down")
                       else GlobalColor("Negative and Up"));
# Aggregation 2

def Value2 = MovingAverage(averageType, close(period = Agg2), fastLength) -
             MovingAverage(averageType, close(period = Agg2), slowLength);
def Avg2 = MovingAverage(averageType, Value2, MACDLength);
def Diff2 = Value2 – Avg2;
def MACDAgg2 = if Diff2 >= 0
               then if Diff2 > Diff2[1]
                    then PosUp
                    else PosDn
               else if Diff2 < Diff2[1]
                    then NegDn
                    else NegUp;
plot MACD2 = if IsNaN(close) then Double.NaN else 2;
MACD2.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD2.SetLineWeight(2);
MACD2.AssignValueColor(if MACDAgg2 == PosUp then GlobalColor("Positive and Up")
                       else if MACDAgg2 == PosDn then GlobalColor("Positive and Down")
                       else if MACDAgg2 == NegDn then GlobalColor("Negative and Down")
                       else GlobalColor("Negative and Up"));
# Aggregation 3

def Value3 = MovingAverage(averageType, close(period = Agg3), fastLength) -
             MovingAverage(averageType,close(period = Agg3), slowLength);
def Avg3 = MovingAverage(averageType, Value3, MACDLength);
def Diff3 = Value3 – Avg3;
def MACDAgg3 = if Diff3 >= 0
               then if Diff3 > Diff3[1]
                    then PosUp
                    else PosDn
               else if Diff3 < Diff3[1]
                    then NegDn
                    else NegUp;
plot MACD3 = if IsNaN(close) then Double.NaN else 3;
MACD3.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD3.SetLineWeight(2);
MACD3.AssignValueColor(if MACDAgg3 == PosUp then GlobalColor("Positive and Up")
                       else if MACDAgg3 == PosDn then GlobalColor("Positive and Down")
                       else if MACDAgg3 == NegDn then GlobalColor("Negative and Down")
                       else GlobalColor("Negative and Up"));
# Aggregation 4

def Value4 = MovingAverage(averageType, close(period = Agg4), fastLength) -
             MovingAverage(averageType,close(period = Agg4), slowLength);
def Avg4 = MovingAverage(averageType, Value4, MACDLength);
def Diff4 = Value4 – Avg4;
def MACDAgg4 = if Diff4 >= 0
               then if Diff4 > Diff4[1]
                    then PosUp
                    else PosDn
               else if Diff4 < Diff4[1]
                    then NegDn
                    else NegUp;
plot MACD4 = if IsNaN(close) then Double.NaN else 4;
MACD4.SetPaintingStrategy(PaintingStrategy.POINTS);
MACD4.SetLineWeight(2);
MACD4.AssignValueColor(if MACDAgg4 == PosUp then GlobalColor("Positive and Up")
                       else if MACDAgg4 == PosDn then GlobalColor("Positive and Down")
                       else if MACDAgg4 == NegDn then GlobalColor("Negative and Down")
                       else GlobalColor("Negative and Up"));

# END CODE
 

tomsk

Well-known member
VIP
Here is the relevant section of the code that describes the condition you are interested in.
Basically if the variable "Diff" is uptrend, then GREEN is displayed. If downtrend then RED
Otherwise if no trend then GRAY is displayed

Code:
DefineGlobalColor("UpTrend", color.GREEN);
DefineGlobalColor("DownTrend", color.RED);
DefineGlobalColor("NoTrend", color.LIGHT_GRAY);

Diff.AssignValueColor(if upTrend then GlobalColor("UpTrend") 
                      else if downTrend then GlobalColor("DownTrend") 
                      else GlobalColor("NoTrend") );
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
 

yamiyugi

New member
Alright, thanks tomsk. Coz in my chart i only have green and grey color but no red color. Appreciate your advice.
 

tomsk

Well-known member
VIP
What does it mean when it says time frames are wrong?

@Miket This likely means the aggregation period selection for the study is incorrect
Within the code, there are 2 aggregation periods to choose - midTermPeriod and longTermPeriod
Make sure the aggregation period for midTermPeriod is less than the aggregation period of longTermPeriod
The other REQUIREMENT is that the aggregation of the chart you're on must not be larger than the midTermPeriod agg.

For example, if your selection for midTermPeriod is 5 min and for longTermPeriod is 15 min, then your chart aggregation must be 5 mins or less
If you're interested here is the code snippet that details this

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

ykd2018x

Member
2019 Donor
VIP
can anybody convert this code to tos script and back test stretergy ??

the original post is a weekly script of trading view , link is below , appricate and thanks for help and taking interst in advance
.

https://www.tradingview.com/script/...il&utm_term=0_d411e4719e-c9fcf3d090-277452033

Code:
//@version=2
strategy(title="MACD Multi-MA Strategy", overlay=false, calc_on_order_fills=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, currency="USD", default_qty_value=100)

src = close
len1 = input(8, "FAST LOOKBACK")
len2 = input(144, "SLOW LOOKBACK")

/////////////////////////////////////////////
length = len2-len1
ma = vwma(src, length)
plot(ma, title="VWMA", color=lime)


length1 = len2-len1
ma1 = rma(src, length1)
plot(ma1, title="RMA", color=purple)

length2 = len2-len1
ma2 = sma(src, length2)
plot(ma2, title="SMA", color=red)


length3 = len2-len1
ma3 = wma(src, length3)
plot(ma3, title="WMA", color=orange)

length4 = len2-len1
ma4 = ema(src, length4)
plot(ma4, title="EMA", color=yellow)





long = ma > ma[1] and ma1 > ma1[1] and ma2 > ma2[1] and ma3 > ma3[1] and ma4 > ma4[1]
short = ma < ma[1] and ma1 < ma1[1] and ma2 < ma2[1] and ma3 < ma3[1] and ma4 < ma4[1]


strategy.entry("Long", strategy.long, when=long)
strategy.entry("Short", strategy.short, when=short)
 
Last edited by a moderator:

diazlaz

Well-known member
2019 Donor
VIP
Hi @ykd2018x

thanks for sharing - looked interesting, here is an initial port and my interpretation of it, all, please feel free to tweak and make adjustments.

x4u5Lq0.png


Setup:

TnNxY5t.png


Ruby:
#MACD Multi-MA for ThinkorSwim V1.0
#
#CREDITS
# Craig_Claussen
# https://www.tradingview.com/script/34lAHcyo-MACD-Multi-MA-Strategy
#
#CHANGELOG
# 2019.12.05 1.0 @diazlaz - Initial Port

#
#
#DESCRIPTION
# This script applies the average of each major MA ( SMA , RMA, EMA , WVMA, WMA )
# to the MACD formula
#
#
#INSTRUCTIONS
# Add to upper and lower study
# Set upper = showArrows = yes; ColorBars = yes; showPlots = no;
# Set lower = showArrows = no; ColorBars = no; showPlots = yes;
#
#

#INPUTS
input src = close;
input len1 = 8; #Fast Lookback
input len2 = 144; #Slow Lookback
input showPlots = yes; #show MA plots (lower study)
input showArrows = no; #show Arrows (upper study)
input showColorBars = no; #show color bars (optional upper study)

#CORE
def length = len2-len1;
def ma = sum(src * volume, length) / sum(volume,length);

def length1 = len2-len1;
def ma1 = MovingAverage(AverageType.WEIGHTED, src, length1);

def length2 = len2-len1;
def ma2 = Average(src, length2);

def length3 = len2-len1;
def ma3 = wma(src, length3);

def length4 = len2-len1;
def ma4 = ExpAverage(src, length4);

#STATES
def long = ma > ma[1] and ma1 > ma1[1] and ma2 > ma2[1] and ma3 > ma3[1] and ma4 > ma4[1];
def short = ma < ma[1] and ma1 < ma1[1] and ma2 < ma2[1] and ma3 < ma3[1] and ma4 < ma4[1];
def sState = if long then 100 else if short then -100 else sState[1];

#PLOTS
plot pMA1 = ma1;
plot pMA2 = ma2;
plot pMA3 = ma3;
plot pMA4 = ma4;
pMA1.SetHiding(!showPlots);
pMA2.SetHiding(!showPlots);
pMA3.SetHiding(!showPlots);
pMA4.SetHiding(!showPlots);

# ARROWS
plot pUP = sState crosses above 0;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);
pUP.SetHiding(!showArrows);

plot pDown = sState crosses below 0;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);
pDown.SetHiding(!showArrows);

#COLORBARS
AssignPriceColor(if showColorBars then
if sState > 0 then Color.GREEN
  else Color.RED
else
  COLOR.CURRENT
);

#END OF MACD Multi-MA for ThinkorSwim V1.0
 

tomsk

Well-known member
VIP
@mik707 The study absolutely works for a 15 minute chart. You'll have to set your timeframes APPROPRIATELY or else it will flag an error like "Secondary period cannot be less than primary". In the study you will find the following aggregation periods defined

Code:
input Agg1 = AggregationPeriod.THREE_MIN;
input Agg2 = AggregationPeriod.FIVE_MIN;
input Agg3 = AggregationPeriod.FIFTEEN_MIN;
input Agg4 = AggregationPeriod.Thirty_MIN;


These are the settings I have defined for a 3 minute aggregation chart. If you are using a 15 minute aggregation chart, then your settings should look something like the following. You can only reference HIGHER aggregations, but not a lower aggregation than the chart you’re on. Thus on a 15 minute chart, Agg1 must reflect a 15 minute aggregation period. You can modify these settings from the user interface

Code:
input Agg1 = AggregationPeriod.FIFTEEN_MIN;
input Agg2 = AggregationPeriod.Thirty_MIN;
input Agg3 = AggregationPeriod.HOUR;
input Agg4 = AggregationPeriod.TWO_HOURS;


I have previously written about using secondary aggregations at the following thread, please read and be familiar with it.

https://usethinkscript.com/threads/...s-mtf-labels-for-thinkorswim.1129/#post-10157
 

J007RMC

Well-known member
2019 Donor
I came across this macd study and I rather like it

Code:
plot Data = close;# MACD by the math
# Mobius
# Mobius at MyTrade

declare lower;

input agg1 = AggregationPeriod.Day;
input agg2 = AggregationPeriod.Hour;
Input fastEMA = 13;
Input slowEMA = 21;
Input Smooth  =  8;
def value   = close;
def value2 = close(period = agg1);
def value3 = close(period = agg2);

  def FSignal = (value * .15) +  (.85  * ExpAverage(value, fastEMA)[1]);
  def SSignal = (value * .075) + (.925 * ExpAverage(value, slowEMA)[1]);
plot MACD    = FSignal - SSignal;
plot MACDSL  = ExpAverage(MACD, Smooth);
plot zero    = 0;

     MACD.SetpaintingStrategy(PaintingStrategy.Line);
     MACD.SetLineWeight(2);
     MACD.AssignValueColor(if MACD > MACD[1] and MACD[1] > MACD[2]
                           then Color.Green else Color.Red);
     MACDSL.AssignValueColor(if MACDSL > MACDSL[1] and MACDSL[1] > MACDSL[2]
                             then Color.Blue else Color.Yellow);

  def FSignal2 = (value2 * .15) +  (.85  * ExpAverage(value2, fastEMA)[1]);
  def SSignal2 = (value2 * .075) + (.925 * ExpAverage(value2, slowEMA)[1]);
plot MACD2   = FSignal2 - SSignal2;
plot MACDSL2  = ExpAverage(MACD2, Smooth);

     MACD2.SetpaintingStrategy(PaintingStrategy.Line);
     MACD2.SetLineWeight(2);
     MACD2.AssignValueColor(if MACD2 > MACD2[1] and MACD2[1] > MACD2[2]
                           then Color.White else Color.Plum);
     MACDSL2.AssignValueColor(if MACDSL2 > MACDSL2[1] and MACDSL2[1] > MACDSL2[2]
                             then Color.Orange else Color.Dark_Orange);

  def FSignal3 = (value3 * .15) +  (.85  * ExpAverage(value3, fastEMA)[1]);
  def SSignal3 = (value3 * .075) + (.925 * ExpAverage(value3, slowEMA)[1]);
plot MACD3    = FSignal3 - SSignal3;
plot MACDSL3  = ExpAverage(MACD3, Smooth);


     MACD3.SetpaintingStrategy(PaintingStrategy.Line);
     MACD3.SetLineWeight(2);
     MACD3.AssignValueColor(if MACD3 > MACD3[1] and MACD3[1] > MACD3[2]
                           then Color.Gray else Color.Cyan);
     MACDSL3.AssignValueColor(if MACDSL3 > MACDSL3[1] and MACDSL3[1] > MACDSL3[2]
                             then Color.Magenta else Color.Pink);


# End Code

Came across a Mobius range indicator GACD it appears to be very telling I'm using this on intraday.
Code:
plot Data = close;# GACD (G)aussian (A)verage (C)onvergence (D)ivergence
# Mobius
# V01.01.2019
# Study shows Long Term Trend with shading above and below the zero line. Short Term Trend with faster and slower Gaussian Average color line plots. Convergence and Divergence is displayed by the Histogram. Crosses of the Averages and therefore crosses of the Histogram above an below zero are marked with corresponding colored vertical lines. A label has been added to remind the user of trend indication.
# Note: This study plots on Mobile without some of the features.

declare lower;

input length  = 21;
input length2 = 34;
input length3 = 21;

script G 
    {
    input iData = close;
    input length = 21;
    def h = high;
    def l = low;
    def c = close;
    def betaDenom = 10 * (Log(Sum((Max(h, c[1]) - Min(l, c[1])), length) /
            (Highest(h, length) - Lowest(l, length)))
            / Log(length));
    def w = (2 * Double.Pi / length);
    def beta = (1 - Cos(w)) / (Power(1.414, 2.0 / betaDenom) - 1 );
    def alpha = (-beta + Sqrt(beta * beta + 2 * beta));
    def G = Power(alpha, 4) * iData + 
                 4 * (1 – alpha) * G[1] – 6 * Power( 1 - alpha, 2 ) * G[2] + 
                 4 * Power( 1 - alpha, 3 ) * G[3] - Power( 1 - alpha, 4 ) * G[4];
    plot data = G;
}
plot a = G(close, length) - G(close, length2);
plot b = G(a, length3);
plot d = a - b;
plot "0" = if IsNaN(close) 
           then Double.NaN 
           else 0;
"0".SetDefaultColor(Color.WHITE);
"0".HideTitle();
a.AssignValueColor(if a > b 
                        then Color.GREEN 
                        else Color.RED);
a.HideBubble();
a.HideTitle();
b.AssignValueColor(if a > b 
                        then Color.GREEN 
                        else Color.RED);
b.HideBubble();
b.HideTitle();
d.SetLineWeight(4);
d.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
d.AssignValueColor(if d > 0 and d > d[1] 
                        then Color.GREEN
                        else if d > 0 and d < d[1] 
                             then Color.BLUE
                             else if d < 0 and d < d[1] 
                             then Color.RED
                        else Color.YELLOW);
d.HideBubble();
AddCloud(a, b, Color.GREEN, Color.RED);
AddVerticalLine(d crosses 0, "", if d crosses above 0 
                                 then Color.GREEN 
                                 else Color.RED);
AddCloud(if Max(a, b) < 0 
         then Max(a, b) 
         else Double.NaN, 0, Color.LIGHT_RED, Color.LIGHT_RED);
AddCloud(if Min(a, b) > 0
         then 0 
         else Double.NaN, Min(a, b), Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddLabel(1, if Max(a, b) < 0 and a < b
            then "Trend Down"
            else if Max(a, b) < 0 and a > b
            then "Trend Down Slowing"
            else if between(0, b, a) and d > d[1]
            then "Trend Reversing"
            else if Min(a, b) > 0 and a > b
            then "Trend Up"
            else if Min(a, b) > 0 and a < b
            then "Trend Up Slowing"
            else if Min(a, b) > 0 and d > d[1]
            then "Trend Up Accelerating"
            else if Min(a, b) > 0 and a < b and  d < d[1]
            then "Trend Up decelerating"
            else "Undetermined Trend", if Max(a, b) < 0
                                       then color.light_red
                                       else if Min(a, b) > 0
                                       then color.light_green
                                       else color.white);
           
# End Code GACD
 
Last edited:

Similar threads

Top