Bollingerbandwidth For ThinkOrSwim

Happymono

New member
Looked around to see if there were any scripts similar but to no avail the idea being that the color will shift in regards to the bands contracting or expanding
also curious if the color shift could be plotted onto the middle bollinger moving average to lessen the amount of lower indicators

r3hhSTgz


Code:
//@version=3
// Hector R. Madrid : Bollinger Bands Width.  : 6/JUN/2014 22:57 : 2.0
// The contractions and expansions of the bands are
// represented by two different colors.
//

study(title="Madrid Bollinger Bands Width", shorttitle="MBBW")
src = input(close, type=source)
length = input(34, minval=1), mult = input(2.0, minval=0.001, maxval=50)

basis = sma(src, length)
dev = stdev(src, length)
upper1 = basis + dev
lower1 = basis - dev
upper2 = basis + 2*dev
lower2 = basis - 2*dev

bbw = (upper2 - lower2)*100/lower2

// Output
plot(bbw, color=bbw-ema(bbw,5)>=0?teal:navy, linewidth=2, style=area)
 
Check out the existing BollingerBandwidth indicator in ThinkorSwim.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2020
#

declare lower;

input averageType = AverageType.Simple;
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_Up = 2.0;
input BulgeLength = 150;
input SqueezeLength = 150;

def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).UpperBand;
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).LowerBand;
def midLine = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).MidLine;

plot Bandwidth = (upperBand - lowerBand) / midLine * 100;
Bandwidth.SetDefaultColor(GetColor(1));

plot Bulge = Highest(Bandwidth, BulgeLength);
Bulge.SetDefaultColor(GetColor(8));
Bulge.setStyle(Curve.SHORT_DASH);

plot Squeeze = Lowest(Bandwidth, SqueezeLength);
Squeeze.SetDefaultColor(GetColor(8));
Squeeze.setStyle(Curve.SHORT_DASH);
 

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

Thank you for the suggestion though i'm embarrassed to say i'm no pro at reading that default and was why i looked into visual green/red light sort of band width indicator
 
Hi Everyone, I've been using the TD Ameritrade Bollinger Bandwidth indicator, but I want a version where the colors are dynamically set for increasing, decreasing, and even.

I tried coding it myself but I was getting an error

I have posted the code, if someone could please help me i'd be very grateful.

Code:
declare lower;

input averageType = AverageType.exponential;
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_Up = 2.0;
input BulgeLength = 150;
input SqueezeLength = 150;

def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).UpperBand;
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).LowerBand;
def midLine = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_Up, averageType).MidLine;

plot Bandwidth = (upperBand - lowerBand) / midLine * 100;
Bandwidth.SetDefaultColor(GetColor(1));

plot Bulge = Highest(Bandwidth, BulgeLength);
Bulge.SetDefaultColor(GetColor(8));
Bulge.setStyle(Curve.SHORT_DASH);

plot Squeeze = Lowest(Bandwidth, SqueezeLength);
Squeeze.SetDefaultColor(GetColor(8));
Squeeze.setStyle(Curve.SHORT_DASH);

bandwidth.DefineColor("Up", GetColor(1));
bandwidth.DefineColor("Down", GetColor(0));
bandwidth.DefineColor("even", GetColor(2));
bandwidth.AssignValueColor(if bandwidth > bandwidth[1] then
bandwidth.color("Up") else (if bandwidth < bandwidth[1] then
bandwidth.color("down") else bandwidth.color("even)));
 
@acjtumio1987 The following is one of my Bollinger Band Studies... Perhaps you will find the code helpful...

Ruby:
# BollingerBands_rad14733
# Modified by rad14733
# Added trend indicating midLine
# Added optional cloud between bands

input price = close;
input displace = 0;
input length = 21;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input showCloud = yes;

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

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
MidLine.AssignValueColor(if MidLine > MidLine[1] then Color.LIME else if MidLine < MidLine[1] then Color.MAGENTA else Color.LIGHT_GRAY);
MidLine.SetLineWeight(2);

plot UpperBand = MidLine + num_Dev_Up * sDev;
UpperBand.AssignValueColor(if MidLine > MidLine[1] then Color.LIME else if MidLine < MidLine[1] then Color.MAGENTA else Color.LIGHT_GRAY);
#UpperBand.SetDefaultColor(Color.WHITE);
#UpperBand.SetDefaultColor(CreateColor(50, 150, 250));
UpperBand.SetLineWeight(2);

plot LowerBand = MidLine + num_Dev_Dn * sDev;
LowerBand.AssignValueColor(if MidLine > MidLine[1] then Color.LIME else if MidLine < MidLine[1] then Color.MAGENTA else Color.LIGHT_GRAY);
#LowerBand.SetDefaultColor(Color.WHITE);
#LowerBand.SetDefaultColor(CreateColor(50, 150, 250));
LowerBand.SetLineWeight(2);

AddCloud(if showCloud == yes then LowerBand else Double.NaN, if showCloud  == yes then UpperBand else Double.NaN, Color.GRAY, Color.GRAY);
 
Last edited:
@acjtumio1987 The following is one of my Bollinger Band Studies... Perhaps you will find the code helpful...

Ruby:
# BollingerBands_rad14733
# Modified by rad14733
# Added trend indicating midLine
# Added optional cloud between bands

input price = close;
input displace = 0;
input length = 21;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input showCloud = yes;

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

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
MidLine.AssignValueColor(if MidLine > MidLine[1] then Color.LIME else if MidLine < MidLine[1] then Color.MAGENTA else Color.LIGHT_GRAY);
MidLine.SetLineWeight(2);

plot UpperBand = MidLine + num_Dev_Up * sDev;
UpperBand.AssignValueColor(if MidLine > MidLine[1] then Color.LIME else if MidLine < MidLine[1] then Color.MAGENTA else Color.LIGHT_GRAY);
#UpperBand.SetDefaultColor(Color.WHITE);
#UpperBand.SetDefaultColor(CreateColor(50, 150, 250));
UpperBand.SetLineWeight(2);

plot LowerBand = MidLine + num_Dev_Dn * sDev;
LowerBand.AssignValueColor(if MidLine > MidLine[1] then Color.LIME else if MidLine < MidLine[1] then Color.MAGENTA else Color.LIGHT_GRAY);
#LowerBand.SetDefaultColor(Color.WHITE);
#LowerBand.SetDefaultColor(CreateColor(50, 150, 250));
LowerBand.SetLineWeight(2);

AddCloud(if showCloud == yes then LowerBand else Double.NaN, if showCloud  == yes then UpperBand else Double.NaN, Color.GRAY, Color.GRAY);
Thank you very much this actually is helpful!
 
Thanks to Moderator MerryDay, my previous question of "how to scan BollingerBands daily trend" was answered and now I can scan BollingerBands gap expansion movement.

This is further question related previous one "how to can BollingerBands daily trend and catch steep gap expansion movement?"

In order to identify steep gap expansion between UpperBands and LowerBands over time (say compared to 2 days ago and yesterday), is there any way for setting scan parameter for catching gap ratio increasing?
For example, when we refer gap between UpperBands and LowerBands on 2 days ago and yesterday (1 day ago), can we scan stock which has more than 50% gap increasing (or any expansion ratio at least steep enough) ?

This scan is not directly compared indicator value between 2 days ago and yesterday, but calculated value (gap between UpperBands value - LowerBands value).
Does TOS may offer that we can create such customized scan ?
 
@Razor_Morozumi It would take complex coding to accomplish what you are asking. There may be another way to approach it. TOS has a built-in indicator BollingerBandWidth that oscillates depending on that ratio. You could try playing w/ it and/or search this forum to find out how others are playing w/ this ratio.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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