Bollinger Band Stops Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
I didn't find a BB stops indicator on TV, so I wrote one myself. You can select various ma types as a base anchor for the calculations. This indicator can have various use cases similar to Supertrend or Parabolic SAR: as a trend indicator, as a training stop loss,...

eIP0suu.png

TOIf0AS.png


thinkScript Code

Code:
# Bollinger Band Stops
# Author: Kory Gill (@korygill)
# Converted for BenTen at useThinkScript.com
# Original Idea: https://www.tradingview.com/script/7kI0k6Xp-Bollinger-Band-stops-JD/

declare upper;
declare once_per_bar;

input baseMA = {default sma, ema, wma, vwma, swma, dema, hullema, tema};
input length = 20;
input mult = 1; #hint mult: 0.001 to 50
input colorFill = yes;

def vClose = close;
def bn = BarNumber();
def nan = double.NaN;

def ema1 = MovingAverage(AverageType.EXPONENTIAL, vClose, length);
def ema2 = MovingAverage(AverageType.EXPONENTIAL, ema1, length);
def ema3 = MovingAverage(AverageType.EXPONENTIAL, ema2, length);
def tema = 3 * (ema1 - ema2) + ema3;

def wma = MovingAverage(AverageType.WEIGHTED, vClose, length);

def basis;
switch (baseMA)
{
case sma: basis = MovingAverage(AverageType.SIMPLE, vClose, length);
case ema: basis = MovingAverage(AverageType.EXPONENTIAL, vClose, length);
case wma: basis = MovingAverage(AverageType.WEIGHTED, vClose, length);
case vwma: basis = MovingAverage(AverageType.EXPONENTIAL, vClose, length);
case swma: basis = wma[3]/6 + 2*wma[2]/6 + 2*wma[1]/6 + wma;
case dema: basis = ema2;
case hullema: basis = MovingAverage(AverageType.HULL, vClose, length);
case tema: basis = tema;
}

def dev = mult * stdev(vClose, length);
def upper = basis + dev;
def lower = basis - dev;

def up = if bn == 1
         then nan
         else if Crosses(vClose, upper, CrossingDirection.ABOVE)
         then 1
         else if Crosses(vClose, lower, CrossingDirection.BELOW)
         then 0
         else up[1];

def dn = if bn == 1
         then nan
         else if Crosses(vClose, lower, CrossingDirection.BELOW)
         then 1
         else if Crosses(vClose, upper, CrossingDirection.ABOVE)
         then 0
         else dn[1];

def changeUp = dn[1] and up;
def changeDn = up[1] and dn;
def stopLine = if up then lower else if dn then upper else nan;

plot p1 = stopLine;
p1.SetLineWeight(2);
p1.AssignValueColor(if stopLine == upper then Color.Red else Color.Green);

AddCloud(stopLine, vClose, CreateColor(80,0,0), CreateColor(0,80,0));

#plot pChangeDn = if changeDn then lower else nan;
#plot pChangeUp = if changeUp then upper else nan;
#pChangeDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#pChangeUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#pChangeDn.SetDefaultColor(Color.Red);
#pChangeUp.SetDefaultColor(Color.Green);
 
There are 8 choices for the baseMA. BollingerBands use the Simple moving average, so that will match. The other 7 provide different results.
 
I like this indicator. I was checking it out today and found a bug in the code - basically the toggle for color fill doesn't work.

if you change this line

Code:
AddCloud(stopLine, vClose, CreateColor(80,0,0), CreateColor(0,80,0));

to this

Code:
AddCloud(if colorfill then stopLine else double.nan, if colorfill then vClose else double.nan, CreateColor(80,0,0), CreateColor(0,80,0));

then the colorfill toggle will start working. Thanks!
 
I like this indicator. I was checking it out today and found a bug in the code - basically the toggle for color fill doesn't work.

if you change this line

Code:
AddCloud(stopLine, vClose, CreateColor(80,0,0), CreateColor(0,80,0));

to this

Code:
AddCloud(if colorfill then stopLine else double.nan, if colorfill then vClose else double.nan, CreateColor(80,0,0), CreateColor(0,80,0));

then the colorfill toggle will start working. Thanks!
Can you help me modify the addcloud function to only show cloud for bearish condition?
 
@Trader_Rich
Bollinger Band Stops Scanner
Add the following code to the bottom of your study for the scan and scan for pChangeUp is true or pChangeDn is true
Ruby:
plot pChangeDn = changeDn ;
plot pChangeUp = changeUp ;
pChangeDn.hide() ;
pChangeUp.hide() ;
6ByzLWo.png



Bollinger Band Stops Watchlist
Add the following code to the bottom of your study and copied it into your watchlist column.
Ruby:
AddLabel(yes, " ");
AssignBackgroundColor(if stopLine == upper then Color.Red else Color.Green);
a1.png
 
Cloud does not change from bullish to bearish or bearish to bullish you have to go to another stock and then come back and then the cloud will change. It seems like its a bug. I did the code change @bigboss said to change but no luck for me. Anyone has this issues?
 
Add to the bottom of your script and copy & paste into the strategy tab:
Ruby:
#Long:
AddOrder(OrderType.BUY_TO_OPEN, changeUp, name = "buy", tickColor = GetColor(0), arrowColor = GetColor(0));
AddOrder(OrderType.SELL_TO_CLOSE, changeDn, name="sell", tickColor = GetColor(1), arrowColor = GetColor(1));

#To short:
AddOrder(OrderType.SELL_TO_OPEN, changeUp, name = "buy", tickColor = GetColor(0), arrowColor = GetColor(0));
AddOrder(OrderType.BUY_TO_CLOSE, changeDn, name="sell", tickColor = GetColor(1), arrowColor = GetColor(1));
 
Last edited:
I am having an issue because this indicator does not always refresh correctly - meaning it stays in one direction when the price action dictates that it should flip over. I know this is true because if I uncheck the study and then recheck it and apply it then all is well. Any ideas for a fix?
 

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
264 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