Donchian Channel / Bollinger Bands Squeeze For ThinkOrSwim

Donchian Channel / Bollinger Bands Squeeze For ThinkOrSwim

This shows fully customizable Donchian Channels, Bollinger Bands, and Keltner Channels. It also shows squeeze as a blue cloud when the Bollinger Band moves into the Keltner Channel. I did it with the help of rad14733.
Ruby:
# Working example of how TTM_Squeeze works
# Coded by rad14733 for usethinkscript.com
# Edited by Bladeof300- added Donchian Channels, and made it more customizable.
# Original code created 2020-12-23
# Use reference calls to TOS BB's and plot midline and bands

# Working example of how TTM_Squeeze works
# Coded by rad14733 for usethinkscript.com
# Original code created 2020-12-23

# Use reference calls to TOS BB's and plot midline and bands
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;
#Donchian Inputs
input length_used_for_calc = 15;
input Use_Other_Time_Frame = yes;
input Time_Frame = AggregationPeriod.DAY;

#Shift is the same as displace, but shift is for the Donchian Channels
input shift = 1;

#Donchian Code

def h = if Use_Other_Time_Frame then high(period = Time_Frame) else high;
def l = if Use_Other_Time_Frame then low(period = Time_Frame) else low;
def highline = Highest(high, length_used_for_calc);
def lowline = Lowest(low, length_used_for_calc);

plot High_Line = (highline[shift]);
High_Line.SetDefaultColor(Color.YELLOW);
Plot Low_Line=(lowline[shift]);
Low_Line.SetDefaultColor(Color.YELLOW);

#BollingerCode

plot bb_midline = BollingerBands().MidLine;
bb_midline.SetDefaultColor(Color.GRAY);

# Tried using gradient colors to help indicate when a squeeze may occur(I recommend using OBV paired w/ a short exp moving average to predict this instead if they trend the same way a breakout should follow)
# AssignNormGradientColor(length, Color.BLACK, Color.GRAY);
# AssignNormGradientColor(length, Color.GRAY, Color.BLACK);

# for selectable color use this SetDefaultColor(Color.GRAY);

plot bb_upperband = BollingerBands().UpperBand;
bb_upperBand.SetDefaultColor(Color.GRAY);


plot bb_lowerband = BollingerBands().LowerBand;
bb_lowerBand.SetDefaultColor(Color.GRAY);


# Use reference calls to TOS KC's and plot midline and bands

plot kc_midline = KeltnerChannels().Avg;
kc_midline.SetDefaultColor(Color.YELLOW);
kc_midline.Hide(); #Both midlines are the same and we only need one

plot kc_upperband = KeltnerChannels().Upper_Band;
kc_upperband.SetDefaultColor(Color.GREEN);

plot kc_lowerband = KeltnerChannels().Lower_Band;
kc_lowerband.SetDefaultColor(Color.GREEN);


# When the BB upper and lower bands cross inside the KC's we are in a Squeeze

DefineGlobalColor("Squeeze", Color.BLUE);
DefineGlobalColor("UnSqueezed", Color.CURRENT);

AddCloud(kc_upperband, bb_upperband, GlobalColor("Squeeze"), GlobalColor("UnSqueezed"));
AddCloud(bb_lowerband, kc_lowerband, GlobalColor("Squeeze"), GlobalColor("UnSqueezed"));


# Add vertical lines to further demarcate start and end of Squeeze

AddVerticalLine(bb_upperband crosses below kc_upperband and bb_lowerband crosses above kc_lowerband, "Squeeze On", Color.RED);
AddVerticalLine(bb_upperband crosses above kc_upperband and bb_lowerband crosses below kc_lowerband, "Squeeze Off", Color.GREEN);
 
Last edited by a moderator:

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