Ultimate MACD Indicator for ThinkorSwim

H

horserider

Well-known member
VIP
Warehouse
To celebrate the Appreciation Day here is a MACD indicator. 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.





Any questions just ask.

Here is the code:
Code:
# Ultimate MACD by Horserider 8/30/2019


declare lower;
input fastLength = 6;
input slowLength = 13;
input MACDLength = 6;
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);

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;
 
Last edited:
S

San

Member
VIP
@horserider, Can you post the script, or pls let me know how to get this screen...
 
Shinthus

Shinthus

Member
VIP
Would it be considered multicollinearity if we use both your Ultimate RSI and Ultimate MACD simultaneously?
 
H

horserider

Well-known member
VIP
Warehouse
@San And everyone, sorry I forgot to put the code. It is now added to the original post.
@Shinthus Technically yes. But we can do it if we wish. If it works to make you money why not.
 
H

horserider

Well-known member
VIP
Warehouse
@BenTen Yes similar but lacking the average signal line. The lowering of the deviation is putting MACD line outside the bands most of the time. I see what is being done to get the signals. I prefer to have the bands behaving more as traditional BB would.
With the settings as shown the signals will be in close proximation of MM.
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@horserider The other MACD with Bollinger Bands was created in 2011 so quite outdated. It was using different MACD settings and 10 SMA with 1.0 Standard Deviations instead of 20 SMA and 2.0 stdv. Same concept though. Still cool to play around with different inputs and data points.
 
S

San

Member
VIP
@San And everyone, sorry I forgot to put the code. It is now added to the original post.
@Shinthus Technically yes. But we can do it if we wish. If it works to make you money why not.
Thanks...
 
T

Talochka

Member
Hello, nice chart! Do you have instructions on how to use this Ultimate MACD?
Please post.
Thank you
PS or direct me to the existing post.
 
H

horserider

Well-known member
VIP
Warehouse
@Talochka Please look at post # 1 for tips. Main signal is MACD crossing the SMA line(white).
 
T

Talochka

Member
To celebrate the Appreciation Day here is a MACD indicator. 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.





Any questions just ask.

Here is the code:
Code:
# Ultimate MACD by Horserider 8/30/2019


declare lower;
input fastLength = 6;
input slowLength = 13;
input MACDLength = 6;
input AverageTypeMACD = {SMA, default EMA, HMA, 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);

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 HMA:
    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;


Question: you said: "The lengths can be adjusted to suit your trading. " What length?
 
H

horserider

Well-known member
VIP
Warehouse
@Talochka If you have the study on the chart left click and choose "Edit study ...". All the lengths are there to edit.
 
markos

markos

Well-known member
VIP
Warehouse
Would it be considered multicollinearity if we use both your Ultimate RSI and Ultimate MACD simultaneously?
Hey @Shinthus just checking in. Did you get this and the RSI to work together?
 
S

Sree

New member
declare lower;

case HMA:
Value = ExpAverage(price, fastLength) - ExpAverage(price, slowLength);
Avg = ExpAverage(Value, MACDLength);

Hi,

If HMA refers to Hull moving average, then I think the formula used in the code is wrong and replicates the EMA formula. Could you please verify and confirm?

HMA Formula - (Source Fidelity Investments)
HMA= WMA(2*WMA(n/2) − WMA of n),sqrt of n)

Thanks
 
H

horserider

Well-known member
VIP
Warehouse
@Sree You are correct, my error duplicating EXP. I have removed the HMA.
 
T

Thepremier

New member
This indicator works great with the TTM Squeeze. Once the MACD gets above or below the Bollinger band that's your entry!!! Thanks!!
 
markos

markos

Well-known member
VIP
Warehouse
Hi,
If HMA refers to Hull moving average, then I think the formula used in the code is wrong and replicates the EMA formula. Could you please verify and confirm?
HMA Formula - (Source Fidelity Investments)
HMA= WMA(2*WMA(n/2) − WMA of n),sqrt of n) Thanks
Would one of you mathamagicians please look over this HMA formula from Alan Hull.com and let us know if it is the same as what Fidelity investments as above? Just curious, that's all, Sometimes these things hurt my brain. Why did I ever want to be a Quality Engineer? I Dunno.🤷‍♂️

Hull Moving Average (HMA) formula
Integer(SquareRoot(Period)) WMA [2 x Integer(Period/2) WMA(Price) - Period WMA(Price)]
 
Last edited:
L

Lambert58

New member
Warehouse
Good Evening everyone,

I am new to this forum, feel very fortunate to have found all of you. I loaded this into my TOS studies, and that was successful. The MACD lines are very compressed in the lower study window. When I zoom in they become more compressed. Any help would be very much appreciated.
 

Top