• Get $40 off VIP by signing up for a free account! Sign Up

Bollinger Bands w/Trend Color For ThinkOrSwim

petergluis

Active member
I figured out how to add cloud colors to show uptrends and downtrends.


Ruby:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input fill= yes;
def sDev = stdev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
def nan      =   Double.NaN;
def Chg      =   If((UpperBand > UpperBand[1] and LowerBand > LowerBand[1]), 1, If((UpperBand < UpperBand[1] and LowerBand < LowerBand[1]), -1, 0));
def Hold     =   CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def LBUp     =   if fill and Hold[0] == 1 then lowerBand else nan;
def UBUp     =   if fill and Hold[0] == 1 then upperband else nan;
def LBDn     =   if fill and Hold[0] == -1 then lowerband else nan;
def UBDn     =   if fill and Hold[0] == -1 then upperband else nan;
DefineGlobalColor("Cloud Up", Color.magenta);
DefineGlobalColor("Cloud Dn", Color.green);
AddCloud(LBUp, UBUp, GlobalColor("Cloud Up"), GlobalColor("Cloud Dn"));
AddCloud(LBDn, UBDn, GlobalColor("Cloud Dn"), GlobalColor("Cloud Up"));
 
Last edited by a moderator:

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

I add four band colors to show uptrends and downtrends.

Ruby:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;
input fill= yes;
def sDev = stdev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
def a = (UpperBand > UpperBand[1] and Midline > Midline[1]);
def b = (UpperBand < UpperBand[1] and LowerBand < LowerBand[1]);
def c = (UpperBand > UpperBand[1] and Midline > Midline[1]);
def d = (UpperBand < UpperBand[1] and midline < midline[1]);
def e = average(close)>midline;
def f = midline<average(close);
def nan      =   Double.NaN;
def Chg      =   If(a, 1, If(b, -1, 0));
def Hold     =   CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def Chg_a      =   If((c or e), 1, If((d or f), -1, 0));
def Hold_a     =   CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def LBUp     =   if fill and Hold[0] == 1 then lowerBand else nan;
def UBUp     =   if fill and Hold[0] == 1 then upperband else nan;
def LBDn     =   if fill and Hold[0] == -1 then lowerband else nan;
def UBDn     =   if fill and Hold[0] == -1 then upperband else nan;
def MBUP     =   if fill and Hold[0] == 1 then midline else nan;
def MBDn     =   if fill and Hold[0] == -1 then Midline else nan;

DefineGlobalColor("Cloud Up", Color.red);
DefineGlobalColor("Cloud_Up", Color.yellow);
DefineGlobalColor("Cloud_Dn", Color.cyan);
DefineGlobalColor("Cloud Dn", Color.green);
AddCloud(LBUp, UBUp, GlobalColor("Cloud Up"), GlobalColor("Cloud Dn"));
AddCloud(LBDn, UBDn, GlobalColor("Cloud Dn"), GlobalColor("Cloud Up"));
AddCloud(MBUp, UBUp, GlobalColor("Cloud_Up"), GlobalColor("Cloud_Dn"));
AddCloud(MBDn, UBDn, GlobalColor("Cloud_Dn"), GlobalColor("Cloud_Up"));
 
Hello, i am looking for Bollinger Bands that will be green when trending up and red when trending down. Thank you in advance:)
 
Hello, i am looking for Bollinger Bands that will be green when trending up and red when trending down. Thank you in advance:)

This bases the trending up/down based upon comparing the midline to midline[1]. It colors the the lines green/red and adds clouds colored likewise

Capture.jpg
Ruby:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;

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

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

LowerBand.assignvalueColor(if midline>=midline[1] then color.green else color.red);
MidLine.assignvalueColor(if midline>=midline[1] then color.green else color.red);
UpperBand.assignvalueColor(if midline>=midline[1] then color.green else color.red);

addcloud(if midline>=midline[1] then upperband else double.nan, midline, color1 = Color.GREEN, color2 = Color.GREEN);
addcloud(if midline<midline[1] then lowerband else double.nan, midline, color1 = Color.red, color2 = Color.red);
 
@SleepyZ This is great, thank you for sharing. Is it possible to color cloud once the following candle confirms (like once the second candle closes above the midline for uptrend paint cloud and the same thing for a down trend closing below the midline paint cloud?
 
Last edited:
@SleepyZ This is great, thank you for sharing. Is it possible to color cloud once the following candle confirms (like once the second candle closes above the midline for uptrend paint cloud and the same thing for a down trend closing below the midline paint cloud?

This seems to work as you requested. This will leave areas with no clouds waiting for your condition.

Capture.jpg
Ruby:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.SIMPLE;

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

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + Num_Dev_Dn * sDev;
plot UpperBand = MidLine + Num_Dev_up * sDev;

LowerBand.AssignValueColor(if MidLine >= MidLine[1] then Color.GREEN else Color.RED);
MidLine.AssignValueColor(if MidLine >= MidLine[1] then Color.GREEN else Color.RED);
UpperBand.AssignValueColor(if MidLine >= MidLine[1] then Color.GREEN else Color.RED);

#Clouds
input showclouds = yes;

def up = if MidLine[1] crosses above MidLine[2] and
         midline > midline[1] and
         close > MidLine
         then 1
         else if up[1] == 1 and MidLine >= MidLine[1]
         then 1
         else if MidLine < MidLine[1]
         then 0
         else up[1];
plot upcloud = up;
#upcloud.setpaintingStrategy(paintingStrategy.VALUES_bELOW);

AddCloud( if showclouds and upcloud
          then UpperBand
          else Double.NaN,
          MidLine, color1 = Color.GREEN, color2 = Color.GREEN);

def dn = if MidLine[1] crosses below MidLine[2] and
         midline < midline[1] and
         close < MidLine
         then 1
         else if dn[1] == 1 and MidLine <= MidLine[1]
         then 1
         else if MidLine > MidLine[1]
         then 0
         else dn[1];

plot dncloud = dn;
#dncloud.setpaintingStrategy(paintingStrategy.VALUES_ABOVE);

AddCloud(if showclouds and dncloud
         then LowerBand
         else Double.NaN, 
         MidLine, color1 = Color.RED, color2 = Color.RED);
#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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