MACD with Bollinger Bands Indicator for ThinkorSwim

omgitschester

New member
VIP
So about the watchlist scan for the MACDBB, i tried the previous code that was posted in the thread but its not working for me, condition wise.

I would like to scan for the tickers that crossed the zero line with one dot above or below.

Sorry guys, i dont code but thanks for all the info!
 

rad14733

Well-known member
VIP
So about the watchlist scan for the MACDBB, i tried the previous code that was posted in the thread but its not working for me, condition wise.

I would like to scan for the tickers that crossed the zero line with one dot above or below.

Sorry guys, i dont code but thanks for all the info!

Please supply the Post # of the code you are attempting to use as that is always helpful when referring to code you haven't supplied... There are multiple copies throughout this topic and without reviewing every copy we can't be sure of specific changes that have been made along the way...
 

MerryDay

Administrative
Staff member
Staff
VIP
@omgitschester "not working for me" is not enough detail about whatever issue you are facing for anyone to help you.
Here is the scan that you described. It does work.
rHj5Mhf.png

HTH
 

CaptainCrunch

New member
VIP
This indicator (most people called it MACD BB) plots MACD along with the Bollinger Bands as a lower study on your ThinkorSwim chart. The usage is fairly simple and up for interpretation. You can use it to identify short term trends or search for squeeze.

Y5YNfKx.png

VuKsabx.png


thinkScript Code

Code:
# TS_MACD_BB
# By Eric Purdy, ThinkScripter LLC
# http://www.thinkscripter.com
# [email protected]
# Last Update 07 Feb 2011

declare lower;

input price = close;
input BBlength = 10;
input BBNum_Dev = 1.0;
input MACDfastLength = 12;
input MACDslowLength = 26;
input MACDLength = 5;

def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);

plot MACD_Dots = MACD_Data;
plot MACD_Line = MACD_Data;

plot BB_Upper = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).UpperBand;
plot BB_Lower = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).Lowerband;
plot BB_Midline = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).MidLine;

BB_Upper.SetDefaultColor(Color.GRAY);
BB_Lower.SetDefaultColor(Color.GRAY);
BB_Midline.SetDefaultColor(Color.GRAY);
BB_Midline.SetStyle(Curve.SHORT_DASH);

MACD_Line.SetDefaultColor(Color.WHITE);

MACD_Dots.SetStyle(Curve.POINTS);
MACD_Dots.SetLineWeight(2);
MACD_Dots.AssignValueColor(if MACD_Line > MACD_Line[1] then Color.White

else Color.DARK_RED);

plot zero = 0;
zero.AssignValueColor(if MACD_Line < 0 then Color.RED else Color.GREEN);
zero.SetLineWeight(2);

Shareable Link

https://tos.mx/fK5Zaj

Note: The default Bollinger Bands indicator in ThinkorSwim uses the 20 Simple Moving Average with 2.0 Standard Deviation. If you want to keep it that way make sure you adjust the settings for this indicator.
Hello, thank you so much for this indicator, it’s very accurate.
I’m trying to understand the code, but I don’t see how you use the
input price = close;
I understand that it’s the current price of the stock, but I don’t see the price referenced anywhere else in the code. Am I missing something?
 

MerryDay

Administrative
Staff member
Staff
VIP
@CaptainCrunch input price = close; Is a standard line in TOS studies. Thus when customizing a study, it will usually show up.
If you are eliminating unneeded lines in a study that you are using. Feel free to attempt to cut that code, if you get no error msgs then it was extraneous. If an error msg occurs, paste it back in. I did most of my learning of scripting in this matter.
 

CaptainCrunch

New member
VIP
@CaptainCrunch input price = close; Is a standard line in TOS studies. Thus when customizing a study, it will usually show up.
If you are eliminating unneeded lines in a study that you are using. Feel free to attempt to cut that code, if you get no error msgs then it was extraneous. If an error msg occurs, paste it back in. I did most of my learning of scripting in this matter.
Got it, thank you!
 

iSultan

Member
VIP
Ben...you're a genius with this indicator! I've been testing it for a week or so and put it to use this week. I've tweaked it with different timeframes 2/6/10 and each has a different setting since it needs to adjust for those timeframes - 2min is 20, 6min is 15 and the 10min is 12. For a put...I wait until all 3 have crossed below the bottom line of the Bollinger band and for a call all 3 need to be above the top line of the Bollinger band, I also use the MACD histogram for additional confirmation - that is set to 48/104/36 on each timeframe.

I'm 3 for 3 this week for $7500 profit. This indicator has made me become very patient, by waiting for the "perfect" moment when they have all crossed over.

Thank you for having the ability to see that indicators like this would be helpful!

Kbnmv0E.jpg
Can anyone show me those lines ( For a put...I wait until all 3 have crossed below the bottom line of the Bollinger band and for a call all 3 need to be above the top line of the Bollinger band, ) ..

I mean the bottom line of the Bollinger and the top line of the Bollinger band
 
Last edited:

s1111

Active member
Can anyone show me those lines ( For a put...I wait until all 3 have crossed below the bottom line of the Bollinger band and for a call all 3 need to be above the top line of the Bollinger band, ) ..

The line he's talking about is 0 line .
 
@BenTen. Hi Ben, I have been wandering in this forum for some time. This MACDBB is simple enough for me to understand 90% of it. There is one line I don't understand, i.e.
"MACD_Dots.AssignValueColor(if MACD_Line > MACD_Line[1] then Color.White"

This condition statement is important for interpretation. What is the meaning of MACD_Line[1] in trading?

From the coding standpoint, MACD_Data is taken from MACD_Data, and MACD_Data is an array returned from the MACD subroutine. So MACD_Line[1] would be the first element of the array. Would MACD_Line[1] change from time to time if I change time range?

Thanks!
 

rad14733

Well-known member
VIP
@BenTen. Hi Ben, I have been wandering in this forum for some time. This MACDBB is simple enough for me to understand 90% of it. There is one line I don't understand, i.e.
"MACD_Dots.AssignValueColor(if MACD_Line > MACD_Line[1] then Color.White"

This condition statement is important for interpretation. What is the meaning of MACD_Line[1] in trading?

From the coding standpoint, MACD_Data is taken from MACD_Data, and MACD_Data is an array returned from the MACD subroutine. So MACD_Line[1] would be the first element of the array. Would MACD_Line[1] change from time to time if I change time range?

Thanks!

The [1] is an index which points to the previous value... By comparing the current value to the previous value it is possible to determine trend, up or down, and color accordingly... By examining the code snippet below you'll see that the MACD_Data variable has been reassigned for formatting purposes further down in the code...

Ruby:
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);

plot MACD_Dots = MACD_Data;
plot MACD_Line = MACD_Data;
 

Britt95

New member
Thank you very much @BenTen for posting this and @Hypoluxa for sharing your strategy. It looks pretty solid.

I've been monkeying around with it today and became inspired. I'm generally a scalper, so I modified the color settings in the script a bit to guide me. Possibly it could help you or someone else here. It has a bit of a "don't diddle in the middle" vibe.

Edit: Since I posted this, I've made another addition. It is a cloud that displays when there is a standard bollinger/kelter squeeze (aka ttm_squeeze) going on. Just another confirm/deny layer. The new code is now displayed below but it is not displayed in the image shown.

ODubnuW.png



Code:
# TS_MACD_BB

# By Eric Purdy, ThinkScripter LLC

# http://www.thinkscripter.com

# [email protected]

# Last Update 07 Feb 2011

# modification to color scheme by Rick_K 12/26/20.


declare lower;


input price = close;

input BBlength = 10;

input BBNum_Dev = 1.0;

input MACDfastLength = 12;

input MACDslowLength = 26;

input MACDLength = 5;


def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);


plot MACD_Dots = MACD_Data;

plot MACD_Line = MACD_Data;


plot BB_Upper = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).UpperBand;

plot BB_Lower = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).Lowerband;

plot BB_Midline = reference BollingerBands(price = MACD_Line, length = BBlength, Num_Dev_Dn = -BBNum_Dev, Num_Dev_Up = BBNum_Dev).MidLine;


BB_Upper.SetDefaultColor(Color.CYAN);

BB_Lower.SetDefaultColor(Color.CYAN);

#BB_Midline.SetDefaultColor(Color.GRAY);

#BB_Midline.SetStyle(Curve.SHORT_DASH);


#MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] then color.green else color.red);

MACD_Line.AssignValueColor(if MACD_Line > MACD_Line[1] and MACD_Line >= BB_Upper then Color.GREEN else if MACD_Line < MACD_Line[1] and MACD_Line >= BB_Upper then Color.DARK_GREEN else if MACD_Line < MACD_Line[1] and MACD_Line <= BB_Lower then Color.RED else if MACD_Line > MACD_Line[1] and MACD_Line <= BB_Lower then Color.DARK_RED else Color.GRAY);

MACD_Line.SetLineWeight(1);


#MACD_Dots.AssignValueColor(if MACD_Line > MACD_Line[1] then Color.green else Color.RED);

MACD_Dots.AssignValueColor(if MACD_Line > MACD_Line[1] and MACD_Line > BB_Upper then Color.GREEN else if MACD_Line < MACD_Line[1] and MACD_Line > BB_Upper then Color.DARK_GREEN else if MACD_Line < MACD_Line[1] and MACD_Line < BB_Lower then Color.RED else if MACD_Line > MACD_Line[1] and MACD_Line < BB_Lower then Color.DARK_RED else Color.GRAY);

MACD_Dots.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);

MACD_Dots.SetLineWeight(2);


plot zero = 0;

zero.SetDefaultColor(Color.WHITE);

zero.SetStyle(Curve.SHORT_DASH);

zero.SetLineWeight(1);


## end original (modified) code




######Add Squeeze Cloud##########


###### Keltner Channels


input displace = 0;

input factor = 1.5;

input length = 20;

input averageType = AverageType.EXPONENTIAL;

input trueRangeAverageType = AverageType.EXPONENTIAL;


def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);

def average = MovingAverage(averageType, price, length);

def Avg = average[-displace];

def Upper_Band = average[-displace] + shift[-displace];

def Lower_Band = average[-displace] - shift[-displace];


######## Bollinger Bands

input BBLength2 = 20;

input Num_Dev_Dn = -2.0;

input Num_Dev_up = 2.0;

input bb_averageType = AverageType.SIMPLE;


def sDev = StDev(data = price[-displace], length = BBLength2);


def MidLine = MovingAverage(bb_averageType, data = price[-displace], length = BBLength2);

def LowerBand = MidLine + Num_Dev_Dn * sDev;

def UpperBand = MidLine + Num_Dev_up * sDev;


### end of script

AddCloud(if UpperBand <= Upper_Band and LowerBand >= Lower_Band then BB_Upper else BB_Lower,  BB_Lower,  Color.yellow);


### END
Hi @RickK thank you so much for posting this! Adding the color settings really helped me. I've been actively day trading this algorithm and it assists me on my exits (which saved me a lot of money in the long run!) I was wondering if there is a way to paint the candle sticks the same color as the MACD line? Meaning if the BollingerBandMACD indicator is showing red/gray/green, the candle sticks could reflect that as well?
 
Last edited:

MerryDay

Administrative
Staff member
Staff
VIP
@Britt95 Great minds think alike! I have been coloring my candles with this indicator and it works great!
Add the following code to the end of the study:
Ruby:
# ########################################################
DefineGlobalColor("LabelGreen",  CreateColor(0, 165, 0)) ;
DefineGlobalColor("LabelRed",  CreateColor(225, 0, 0)) ;
input paintbar = yes ;

AssignPriceColor(
if !paintbar then Color.CURRENT else
if MACD_Line > BB_Upper then GlobalColor("LabelGreen") else
if MACD_Line < BB_Lower then GlobalColor("LabelRed") else
Color.gray);
sawVrNF.png

HTH
 
Last edited:

petech

Member
VIP
This is a great indicator. It is very obvious it works really well by looking at past charts. Above the zero line, you want to buy above the BB, below the zero line you want to short below the BB.
 

DudeDastic

New member
Ben...you're a genius with this indicator! I've been testing it for a week or so and put it to use this week. I've tweaked it with different timeframes 2/6/10 and each has a different setting since it needs to adjust for those timeframes - 2min is 20, 6min is 15 and the 10min is 12. For a put...I wait until all 3 have crossed below the bottom line of the Bollinger band and for a call all 3 need to be above the top line of the Bollinger band, I also use the MACD histogram for additional confirmation - that is set to 48/104/36 on each timeframe.

I'm 3 for 3 this week for $7500 profit. This indicator has made me become very patient, by waiting for the "perfect" moment when they have all crossed over.

Thank you for having the ability to see that indicators like this would be helpful!

Kbnmv0E.jpg
Interesting strategy.

One question: does the zero line play any part in this?
 

petech

Member
VIP
Is there a way to set up a scan with this indicator where the current dot above the zero line, be a white dot, but the proceeding dot being not white. The proceeding dot can be above, below, or at the zero line.
 

Similar threads

Top