MACD with Bollinger Bands Indicator for ThinkorSwim

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!
 
@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
 
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?
 
@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.
 
Last edited:
@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!
 
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:
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!
 
@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;
 
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:
@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:
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.
 
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?
 
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.
 
it will not let me load a picture (and Yes I followed the instructions from this site) so I will try and explain what I am looking for help on this for. I wanted to know if anyone could help me with adding arrows. I would like a down arrow on the first dot after the green/yellow bullish dots. I not sure what color it is because I am colorblind. but after the bullish dots turn any color then the bullish color on the next dot show a down arrow. I also want a up arrow for the first dot after a red Dot (Bearish dots) that is no longer Red.


Thanks in advanced and thanks for the help everyone has given me on this site.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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