Ultimate MACD Indicator for ThinkorSwim

Here is a MACD indicator for ThinkorSwim. It uses a color coded MACD line to show changes. There is a moving average of the MACD signal line. And MACD bands to show volatility. The signal would be the cross of MACD (Green/RED) and signal line (White). A photo is also below to give tips on trading the indicator. The lengths can be adjusted to suit your trading. (Since the below photo zero line was added)

View attachment 1466

Original post is below with updated standard code.

View attachment 5278

Any questions just ask.

Here is the code. Code updated to current version:

Code:
#
# Ultimate MACD by Horserider 8/30/2019
# Standard version
# Also have short and long term versions. Can accomplish all three by just adjusting inputs to fit your trading style.
# Standard MACD 12,26,9 BB length 20.
# Short term MACD 6, 13, 6 BB length 5.
# Long term MACD 48, 104, 36 BB length 20.
# Added zero line on suggestion of Ahmar824 1/12/2019.

declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageTypeMACD = {SMA, default EMA, Wilders};

input price = close;
input displace = 0;

def MACD_Data = MACD(fastLength = fastLength, slowLength = slowLength, MACDLength = MACDLength);
plot MACD_Line = MACD_Data;

MACD_Line.DefineColor("Up", Color.GREEN);
MACD_Line.DefineColor("Down", Color.RED);
MACD_Line.DefineColor("Even", Color.WHITE);
MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] then MACD_Line.Color("Up") else (if MACD_Line == MACD_Line[1] then MACD_Line.Color("Even") else MACD_Line.Color("Down")));
MACD_Line.SetLineWeight(3);

plot zero = 0;
zero.setDefaultColor(color.LIGHT_ORANGE);
zero.setLineWeight (1) ;

def Value;
plot Avg;
switch (AverageTypeMACD) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);

case Wilders:
    Value = WildersAverage(price, fastLength) - WildersAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);
}
Avg.SetDefaultColor(Color.WHITE);


#plot BB;
#Bollinger BandsSMA,EMA
input AverageTypeBB = {default SMA, EMA, HMA};
input displaceBB = 0;
input lengthBB = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

plot upperBand;
plot lowerBand;
def midline;

switch (AverageTypeBB) {
case SMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).Midline;
case EMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
case HMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
}

upperBand.SetDefaultColor(Color.GRAY);
upperBand.DefineColor("Up", Color.CYAN);
upperBand.DefineColor("Down", Color.PINK);
upperBand.DefineColor("Even", Color.GRAY);
upperBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));
upperBand.SetLineWeight(2);

lowerBand.SetDefaultColor(Color.GRAY);
lowerBand.DefineColor("Up", Color.CYAN);
lowerBand.DefineColor("Down", Color.PINK);
lowerBand.DefineColor("Even", Color.GRAY);
lowerBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));

plot midline1 = midline;

Update to show versatility of the study to fit different trading styles. Will include shares to each variation but same can be accomplished by changing the inputs. Added a zero line.

Standard MACD 12,26,9 BB length 20. https://tos.mx/X1IBznK
Short term MACD 6, 13, 6 BB length 5. https://tos.mx/tpa1Ek8
Long term MACD 48, 104, 36 BB length 20. https://tos.mx/iUs5wqK
I like your ultimate MacD it is what I have been looking for. Now I just need to learn how to use it properly. Any suggestions?
 

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

I like your ultimate MacD it is what I have been looking for. Now I just need to learn how to use it properly. Any suggestions?

1. Here is an overview on how to use oscillators:
https://usethinkscript.com/threads/how-to-read-an-oscillator-in-thinkorswim.11497/

2. The long term MACD is used to define trend.
3. The short term MACD finds entry
4. Crosses above / below bollinger bands highlight areas of support and resistance.

5. Never look at just one timeframe:
https://usethinkscript.com/threads/best-time-frame-for-day-trading-for-thinkorswim.12209/

6. Never use in isolation:
https://usethinkscript.com/threads/basics-for-developing-a-good-strategy.8058/
 
Last edited by a moderator:
Here is a MACD indicator for ThinkorSwim. It uses a color coded MACD line to show changes. There is a moving average of the MACD signal line. And MACD bands to show volatility. The signal would be the cross of MACD (Green/RED) and signal line (White). A photo is also below to give tips on trading the indicator. The lengths can be adjusted to suit your trading. (Since the below photo zero line was added)

View attachment 1466

Original post is below with updated standard code.

View attachment 5278

Any questions just ask.

Here is the code. Code updated to current version:

Code:
#
# Ultimate MACD by Horserider 8/30/2019
# Standard version
# Also have short and long term versions. Can accomplish all three by just adjusting inputs to fit your trading style.
# Standard MACD 12,26,9 BB length 20.
# Short term MACD 6, 13, 6 BB length 5.
# Long term MACD 48, 104, 36 BB length 20.
# Added zero line on suggestion of Ahmar824 1/12/2019.

declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageTypeMACD = {SMA, default EMA, Wilders};

input price = close;
input displace = 0;

def MACD_Data = MACD(fastLength = fastLength, slowLength = slowLength, MACDLength = MACDLength);
plot MACD_Line = MACD_Data;

MACD_Line.DefineColor("Up", Color.GREEN);
MACD_Line.DefineColor("Down", Color.RED);
MACD_Line.DefineColor("Even", Color.WHITE);
MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] then MACD_Line.Color("Up") else (if MACD_Line == MACD_Line[1] then MACD_Line.Color("Even") else MACD_Line.Color("Down")));
MACD_Line.SetLineWeight(3);

plot zero = 0;
zero.setDefaultColor(color.LIGHT_ORANGE);
zero.setLineWeight (1) ;

def Value;
plot Avg;
switch (AverageTypeMACD) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);

case Wilders:
    Value = WildersAverage(price, fastLength) - WildersAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);
}
Avg.SetDefaultColor(Color.WHITE);


#plot BB;
#Bollinger BandsSMA,EMA
input AverageTypeBB = {default SMA, EMA, HMA};
input displaceBB = 0;
input lengthBB = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

plot upperBand;
plot lowerBand;
def midline;

switch (AverageTypeBB) {
case SMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).Midline;
case EMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
case HMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
}

upperBand.SetDefaultColor(Color.GRAY);
upperBand.DefineColor("Up", Color.CYAN);
upperBand.DefineColor("Down", Color.PINK);
upperBand.DefineColor("Even", Color.GRAY);
upperBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));
upperBand.SetLineWeight(2);

lowerBand.SetDefaultColor(Color.GRAY);
lowerBand.DefineColor("Up", Color.CYAN);
lowerBand.DefineColor("Down", Color.PINK);
lowerBand.DefineColor("Even", Color.GRAY);
lowerBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));

plot midline1 = midline;

Update to show versatility of the study to fit different trading styles. Will include shares to each variation but same can be accomplished by changing the inputs. Added a zero line.

Standard MACD 12,26,9 BB length 20. https://tos.mx/X1IBznK
Short term MACD 6, 13, 6 BB length 5. https://tos.mx/tpa1Ek8
Long term MACD 48, 104, 36 BB length 20. https://tos.mx/iUs5wqK
This is a great study. Appreciate your sharing. After looking through this thread, I didn't see where there are alerts written. If I missed it, please let me know. With that said, are you able to write in Alerts for when the MACD line turns green or red? I've tried, but to no avail.
 
This is a great study. Appreciate your sharing. After looking through this thread, I didn't see where there are alerts written. If I missed it, please let me know. With that said, are you able to write in Alerts for when the MACD line turns green or red? I've tried, but to no avail.

Add to the bottom of your study:
Ruby:
def MACDrising = MACD_Line > MACD_Line[1] ;

# Alerts
Alert( MACDrising and !MACDrising[1], "MACD UP", Alert.Bar, Sound.Chimes);
Alert(!MACDrising and  MACDrising[1], "MACD DN", Alert.Bar, Sound.Bell);

This says, alert when MACD is rising but wasn't rising on the previous bar and vice-versa
 
Last edited:
Is it possible to receive sound alerts for when the macd_line is greater than or equal to the upper band and less than or equal to the lower band? TIA.
 
Is it possible to receive sound alerts for when the macd_line is greater than or equal to the upper band and less than or equal to the lower band? TIA.
Add this code snippet to the bottom of your study:


Alerts --> MACD Above UpperBand and MACD Below LowerBand
# Alerts
Alert(MACD_line >= upperBand, "above Upper", Alert.Bar, Sound.Ding);
Alert(MACD_line <= lowerBand, "below Lower", Alert.Bar, Sound.Bell);
 
Thank you, is there a way to add an up arrow and down arrow when the macd line turns green or red?
Append this code snippet to the bottom of your study:

MACD Change In Trend Arrows
Ruby:
def MACDrising = MACD_Line > MACD_Line[1] ;

plot uparrow = if MACDrising and !MACDrising[1] then MACD_Line else double.NaN;
uparrow.SetPaintingStrategy(PaintingStrategy.ARROW_up);
uparrow.SetDefaultColor(color.cyan) ;
uparrow.SetLineWeight(3);

plot dnarrow = if !MACDrising and MACDrising[1] then MACD_Line else double.NaN;
dnarrow.SetPaintingStrategy(PaintingStrategy.ARROw_DOWN);
dnarrow.SetDefaultColor(color.magenta) ;
dnarrow.SetLineWeight(3);
 
Add to the bottom of your study:
Ruby:
def MACDrising = MACD_Line > MACD_Line[1] ;

# Alerts
Alert( MACDrising and !MACDrising[1], "MACD UP", Alert.Bar, Sound.Chimes);
Alert(!MACDrising and  MACDrising[1], "MACD DN", Alert.Bar, Sound.Bell);

This says, alert when MACD is rising but wasn't rising on the previous bar and vice-versa
would the ultimate macd indicator work with a weighted moving average MACD line?
 
would the ultimate macd indicator work with a weighted moving average MACD line?

WMA
Gives more weight to recent data points, making it more responsive to current market conditions. WMAs are useful for short-term trading strategies, such as identifying market trends, potential support and resistance levels, and determining entry and exit points for trades.

EMA
Also gives more weight to recent data points, but the rate of decrease between one price and its preceding price is exponential, not consistent. EMAs react faster as a trend changes direction, and some traders view them as a clearer indicator of a trend.

If you are using the MACD to find reversals, the EMA would be preferred.
If you are using it more for support or as a range; then the WMA would be preferred.

Here is the Ultimate MACD with WMA added to the choice of averages in the settings:
Ruby:
#
# Ultimate MACD by Horserider 8/30/2019
# Standard version
# Also have short and long term versions. Can accomplish all three by just adjusting inputs to fit your trading style.
# Standard MACD 12,26,9 BB length 20.
# Short term MACD 6, 13, 6 BB length 5.
# Long term MACD 48, 104, 36 BB length 20.
# Added zero line on suggestion of Ahmar824 1/12/2019.

declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageTypeMACD = {SMA, WMA, default EMA, Wilders};

input price = close;
input displace = 0;

def MACD_Data = MACD(fastLength = fastLength, slowLength = slowLength, MACDLength = MACDLength);
plot MACD_Line = MACD_Data;

MACD_Line.DefineColor("Up", Color.GREEN);
MACD_Line.DefineColor("Down", Color.RED);
MACD_Line.DefineColor("Even", Color.WHITE);
MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] then MACD_Line.Color("Up") else (if MACD_Line == MACD_Line[1] then MACD_Line.Color("Even") else MACD_Line.Color("Down")));
MACD_Line.SetLineWeight(3);

plot zero = 0;
zero.setDefaultColor(color.LIGHT_ORANGE);
zero.setLineWeight (1) ;

def Value;
plot Avg;
switch (AverageTypeMACD) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case WMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);

case Wilders:
    Value = WildersAverage(price, fastLength) - WildersAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);
}
Avg.SetDefaultColor(Color.WHITE);


#plot BB;
#Bollinger BandsSMA,EMA
input AverageTypeBB = {default SMA, EMA, HMA};
input displaceBB = 0;
input lengthBB = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

plot upperBand;
plot lowerBand;
def midline;

switch (AverageTypeBB) {
case SMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).Midline;
case EMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
case HMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
}

upperBand.SetDefaultColor(Color.GRAY);
upperBand.DefineColor("Up", Color.CYAN);
upperBand.DefineColor("Down", Color.PINK);
upperBand.DefineColor("Even", Color.GRAY);
upperBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));
upperBand.SetLineWeight(2);

lowerBand.SetDefaultColor(Color.GRAY);
lowerBand.DefineColor("Up", Color.CYAN);
lowerBand.DefineColor("Down", Color.PINK);
lowerBand.DefineColor("Even", Color.GRAY);
lowerBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));

plot midline1 = midline;
 
WMA
Gives more weight to recent data points, making it more responsive to current market conditions. WMAs are useful for short-term trading strategies, such as identifying market trends, potential support and resistance levels, and determining entry and exit points for trades.

EMA
Also gives more weight to recent data points, but the rate of decrease between one price and its preceding price is exponential, not consistent. EMAs react faster as a trend changes direction, and some traders view them as a clearer indicator of a trend.

If you are using the MACD to find reversals, the EMA would be preferred.
If you are using it more for support or as a range; then the WMA would be preferred.

Here is the Ultimate MACD with WMA added to the choice of averages in the settings:
Ruby:
#
# Ultimate MACD by Horserider 8/30/2019
# Standard version
# Also have short and long term versions. Can accomplish all three by just adjusting inputs to fit your trading style.
# Standard MACD 12,26,9 BB length 20.
# Short term MACD 6, 13, 6 BB length 5.
# Long term MACD 48, 104, 36 BB length 20.
# Added zero line on suggestion of Ahmar824 1/12/2019.

declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageTypeMACD = {SMA, WMA, default EMA, Wilders};

input price = close;
input displace = 0;

def MACD_Data = MACD(fastLength = fastLength, slowLength = slowLength, MACDLength = MACDLength);
plot MACD_Line = MACD_Data;

MACD_Line.DefineColor("Up", Color.GREEN);
MACD_Line.DefineColor("Down", Color.RED);
MACD_Line.DefineColor("Even", Color.WHITE);
MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] then MACD_Line.Color("Up") else (if MACD_Line == MACD_Line[1] then MACD_Line.Color("Even") else MACD_Line.Color("Down")));
MACD_Line.SetLineWeight(3);

plot zero = 0;
zero.setDefaultColor(color.LIGHT_ORANGE);
zero.setLineWeight (1) ;

def Value;
plot Avg;
switch (AverageTypeMACD) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case WMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);

case Wilders:
    Value = WildersAverage(price, fastLength) - WildersAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);
}
Avg.SetDefaultColor(Color.WHITE);


#plot BB;
#Bollinger BandsSMA,EMA
input AverageTypeBB = {default SMA, EMA, HMA};
input displaceBB = 0;
input lengthBB = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

plot upperBand;
plot lowerBand;
def midline;

switch (AverageTypeBB) {
case SMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).Midline;
case EMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
case HMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
}

upperBand.SetDefaultColor(Color.GRAY);
upperBand.DefineColor("Up", Color.CYAN);
upperBand.DefineColor("Down", Color.PINK);
upperBand.DefineColor("Even", Color.GRAY);
upperBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));
upperBand.SetLineWeight(2);

lowerBand.SetDefaultColor(Color.GRAY);
lowerBand.DefineColor("Up", Color.CYAN);
lowerBand.DefineColor("Down", Color.PINK);
lowerBand.DefineColor("Even", Color.GRAY);
lowerBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));

plot midline1 = midline;
works exactly how wanted it. i spend many many hours trying to figure it out. i'm learning. Thank you!
 
Here is a MACD indicator for ThinkorSwim. It uses a color coded MACD line to show changes. There is a moving average of the MACD signal line. And MACD bands to show volatility. The signal would be the cross of MACD (Green/RED) and signal line (White). A photo is also below to give tips on trading the indicator. The lengths can be adjusted to suit your trading. (Since the below photo zero line was added)

View attachment 1466

Original post is below with updated standard code.

View attachment 5278

Any questions just ask.

Here is the code. Code updated to current version:

Code:
#
# Ultimate MACD by Horserider 8/30/2019
# Standard version
# Also have short and long term versions. Can accomplish all three by just adjusting inputs to fit your trading style.
# Standard MACD 12,26,9 BB length 20.
# Short term MACD 6, 13, 6 BB length 5.
# Long term MACD 48, 104, 36 BB length 20.
# Added zero line on suggestion of Ahmar824 1/12/2019.

declare lower;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageTypeMACD = {SMA, default EMA, Wilders};

input price = close;
input displace = 0;

def MACD_Data = MACD(fastLength = fastLength, slowLength = slowLength, MACDLength = MACDLength);
plot MACD_Line = MACD_Data;

MACD_Line.DefineColor("Up", Color.GREEN);
MACD_Line.DefineColor("Down", Color.RED);
MACD_Line.DefineColor("Even", Color.WHITE);
MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] then MACD_Line.Color("Up") else (if MACD_Line == MACD_Line[1] then MACD_Line.Color("Even") else MACD_Line.Color("Down")));
MACD_Line.SetLineWeight(3);

plot zero = 0;
zero.setDefaultColor(color.LIGHT_ORANGE);
zero.setLineWeight (1) ;

def Value;
plot Avg;
switch (AverageTypeMACD) {
case SMA:
    Value = Average(price, fastLength) - Average(price, slowLength);
    Avg = Average(Value, MACDLength);
case EMA:
    Value = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);

case Wilders:
    Value = WildersAverage(price, fastLength) - WildersAverage(price, slowLength);
    Avg = ExpAverage(Value, MACDLength);
}
Avg.SetDefaultColor(Color.WHITE);


#plot BB;
#Bollinger BandsSMA,EMA
input AverageTypeBB = {default SMA, EMA, HMA};
input displaceBB = 0;
input lengthBB = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

plot upperBand;
plot lowerBand;
def midline;

switch (AverageTypeBB) {
case SMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).Midline;
case EMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
case HMA:
    upperBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(MACD_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
}

upperBand.SetDefaultColor(Color.GRAY);
upperBand.DefineColor("Up", Color.CYAN);
upperBand.DefineColor("Down", Color.PINK);
upperBand.DefineColor("Even", Color.GRAY);
upperBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));
upperBand.SetLineWeight(2);

lowerBand.SetDefaultColor(Color.GRAY);
lowerBand.DefineColor("Up", Color.CYAN);
lowerBand.DefineColor("Down", Color.PINK);
lowerBand.DefineColor("Even", Color.GRAY);
lowerBand.AssignValueColor(if upperBand > upperBand[1] and lowerBand < lowerBand[1] then upperBand.Color("Up") else (if upperBand < upperBand[1] and lowerBand > lowerBand[1] then upperBand.Color("Down") else upperBand.Color("Even")));

plot midline1 = midline;

Update to show versatility of the study to fit different trading styles. Will include shares to each variation but same can be accomplished by changing the inputs. Added a zero line.

Standard MACD 12,26,9 BB length 20. https://tos.mx/X1IBznK
Short term MACD 6, 13, 6 BB length 5. https://tos.mx/tpa1Ek8
Long term MACD 48, 104, 36 BB length 20. https://tos.mx/iUs5wqK
I am not using BB, only MACD Line, Zero Line, and Avg. Is there a simple way to place a red cloud from 25 to 40 and a green line from -25 to -40? Seems to match up well with RSI Overbought and Oversold.
 
Last edited:
I am not using BB, only MACD Line, Zero Line, and Avg. Is there a simple way to place a red cloud from 25 to 40 and a green line from -25 to -40? Seems to match up well with RSI Overbought and Oversold.


Bounded oscillators such as RSI can have cloud boundaries.
Oscillators such as MACD can not. It will not work. You will have scaling issues.

But if you still want it, here ya go. Add this code snippet to the bottom of your study.
AddCloud(25, 40, color.red, color.red);
AddCloud(-25, -40, color.green, color.green);
 
Last edited:
works exactly how wanted it. i spend many many hours trying to figure it out. i'm learning. Thank you!
I'm trying to add a scan to see when the MACD is increasing on multiple timeframes, so when i put in a study for the MACD using the Weighted Average, it doesn't line up with the logic used in your study. could you assist with adjusting this scan logic to match?

Code:
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.WEIGHTED;
def Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
#plot scan = Diff[1] >= Diff[2];
# OR
plot scan = Diff[1] <= Diff[2];
 
Last edited by a moderator:
Could someone make an alert sound and print up and down arrows when the MACD crosses above or below the white line please? Thanks!!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
139 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