Bull Flag and Bear Flag Formations for ThinkorSwim

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

I have this bull flag indicator I pulled from the post here: https://usethinkscript.com/threads/bull-flag-and-bear-flag-formations-for-thinkorswim.467/

I modified it to work for day traders. I use this indicator while showing the 1 MIN chart, but its aggregation is on the 3 min chart. So I can see ALL bull flag formations on the 3 minute charts, at the 1 min chart level.

HOW TO:
  1. Add the script to your 1 min chart.
  2. ONLY use this on a moving upward trend. I use a 20 chart layout, opening this up with Trade Ideas scanners. I find the Momentum stocks that are continuing to hit new highs. The problem with these stocks, is you never know when they will crash, right? So now you know, when they will push up.
  3. When I place these upward trend charts and this indicator finds a bull flag, it sends me a PING. I look at the chart and I have about 30 seconds - 5 minutes to decide if I will enter the trade.
    1. If its a flag sideways bull flag, I have longer to decide. If its a moving upwards bull flag, I have 30 seconds or so to decide to enter the trade.
  4. I kid you not, 9 times out of 10 bull flags that appear, this one makes money on UPWARD trending stocks 90% of the time.
This is one of my favorite indicators as it makes money ALL day long when paired with a momentum trending up scanner.

Code:
# Bull Flag and Bear Flag Indicator
# Original from https://twitter.com/KRose_TDA/status/1158974048518328320
# [email protected] CBLOLD Search
#To use for scan place # signs before 2 Addlabel statements and change "def" before trigger statement to "plot"
# Modified by BenTen at useThinkScript to Display Signals on Chart
##############Bull Flag
input agg3 = AggregationPeriod.Three_MIN;

def StockPrice = close(period=agg3)>=10;
def trend = simpleMovingAvg("length"=90)> (simpleMovingAvg("length"=90)[5]);
def MacdLow = macdHistogram(24,51,27)[1]< macdHistogram(24,51,27)[2]and close(period=agg3)[1]<close(period=agg3)[2] and close(period=agg3)>high(period=agg3)[1];
#def Momentum = macdHistogram(8,17,9) > macdHistogram(8,17,9)[1];
##############Bear Flag
def StockPriceBear = close(period=agg3)>=10;
def trendBear = simpleMovingAvg("length"=90)< (simpleMovingAvg("length"=90)[5]);
def MacdHighBear = macdHistogram(24,51,27)[1]> macdHistogram(24,51,27)[2]and close(period=agg3)[1]>close(period=agg3)[2] and close(period=agg3)<low(period=agg3)[1];
#########################################
#def trigger = trend and momentum and MacdLow;
def Bulltrigger = trend and MacdLow;
def BearTrigger = TrendBear and MacdHighBear;

Alert (Bulltrigger,"BULL FLAG", Alert.Bar, Sound.Bell);

AddLabel(yes, if Bulltrigger then "Bull Flag" else "",Color.PLUM);
#AddLabel(yes, if Bulltrigger then "Bull Flag" else if BearTrigger then "Bear Flag" else " ",Color.black);
#AssignBackgroundColor(if Bulltrigger then color.YELLOW else if bearTrigger then color.LIGHT_RED else color.WHITE);

# Plot Confirmed Signals
plot bullish = Bulltrigger;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(Color.RED);
bullish.SetLineWeight(1);
plot bearish = BearTrigger;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(Color.CYAN);
bearish.SetLineWeight(1);
 
I added it to my 1 min chart but I'm confused as to what is supposed to happen, I know today is probably a bad day cause everything's red but can you maybe post a screenshot or something showing how its supposed to alert and how it looks on chart @spoiledtechie
 
Any one know how to setup a scanner for Bullish Pennant & Falling Wedge
Have you looked at the Patterns available for Scans...??? Add Filter > Patterns and de-select the ones you don't want to use... Bullish Pennant isn't in the list so you might need to create it using the Pattern Editor... I'll leave it to you to research further in the Learning Center...
 
Do you know how to add the pattern ?
I have never done it but I know it is covered in the Learning Center, hence my pointing you there... No need in my learning something I'm not interested in just so I can explain it to you...
 
This one draws the diagonal lines and tell how many candles are in the wedge

Code:
# insidebar345_01

#  add - look for 3 bar pattern

# insidebar45_01

# find 3,4,5 bar , inside bar patterns
# update from  quadinsidebar_01

# start with ver01.  lines for 5 bar
#  copy 5bar code and chg for  3 bar and 4 bar
# disable 2nd agg

# ---------------------------------

# quadinsidebar_01
# 2020-06-27
# halcyonguy
#  find a quad inside bar (5 bar pattern)
#    1st bar high, bar 0, is higher than the following 4 bars
#    1st bar low, bar 0, is lower than the following 4 bars
#  draw diagonal and horizontal lines
#    draw lines 3 bars beyond pattern
#    calc slope from 1st bar high to 5th bar high. same with lows
#  draw horizontal lines on a 4 bar pattern
#  draw wedges (samll arrows) on bar0
#



#declare upper;
#declare once_per_bar;


# ------------------------------

#  draw diagonal and horizontal lines on a 5 bar pattern
#  draw lines 3 bars beyond


def na = double.nan;
def hi = high;
def lo = low;

# bar count, indenifier bubble, vert offset
def bubvert = 0.998;

input show_3_bar_pattern = yes;
input show_4_bar_pattern = yes;
input show_5_bar_pattern = yes;

#  plot small arrows above and below bar0
input show_small_arrows_bar0 = yes;

#  plot small arrows above and below last bar
input show_small_arrows_last_bar = yes;

input show_bar_count_bubble = yes;
def sbcb =  show_bar_count_bubble;


# ----- 5 bar inside pattern ---------------------------------

# formulas ref'd off of the 1st bar, bar0, the biggest in the pattern
# find a bar bigger than the next 4 bars
def fivebar0 = show_5_bar_pattern and hi > hi[-1] and hi > hi[-2] and hi > hi[-3] and hi > hi[-4] and lo < lo[-1] and lo < lo[-2] and lo < lo[-3] and lo < lo[-4];

# define the 5 bars in an inside bar
#  as time goes on, look back x+ bars at bar0 to see if it was the biggest candle
def fivebar = fivebar0 or fivebar0[1] or fivebar0[2] or fivebar0[3] or fivebar0[4];


#  plot small arrows above and below bar0
plot v5 = fivebar0 and show_small_arrows_bar0;
v5.SetPaintingStrategy(PaintingStrategy.boolean_wedge_UP);
v5.AssignValueColor(Color.white);
v5.SetLineWeight(1);
v5.hidebubble();
plot w5 = fivebar0 and show_small_arrows_bar0;
w5.SetPaintingStrategy(PaintingStrategy.boolean_wedge_down);
w5.AssignValueColor(Color.white);
w5.SetLineWeight(1);
w5.hidebubble();


#  plot small arrows above and below last bar
plot s5 = fivebar0[4] and show_small_arrows_last_bar;
s5.SetPaintingStrategy(PaintingStrategy.boolean_wedge_UP);
s5.AssignValueColor(Color.white);
s5.SetLineWeight(1);
s5.hidebubble();
plot t5 = fivebar0[4] and show_small_arrows_last_bar;
t5.SetPaintingStrategy(PaintingStrategy.boolean_wedge_down);
t5.AssignValueColor(Color.white);
t5.SetLineWeight(1);
t5.hidebubble();

#  sbcb =  show_bar_count_bubble;
addchartbubble(sbcb and fivebar0, (low * bubvert), "5", color.cyan, no);

#  enable slope lines - define bars to draw slope lines.
#  5 bars , and +3 extra bars past the last bar
def en_slope5lines = fivebar or fivebar[1] or fivebar[2] or fivebar[3];

#  calc the slope from 4 gaps, (5 bars)
def bar5gaps = 4;
#  hi slope is neg , lo slope is pos
#  use the 1st bar and last bar to calc slope
def slope5hi2 = (hi[-bar5gaps] - hi[0])/bar5gaps;
def slope5lo2 = (lo[-bar5gaps] - lo[0])/bar5gaps;

#  test data
#addchartbubble(yes,high+0.2, slopehi2 + "\n" + hi[-4] + "\n" + hi[0], color.cyan,yes);
#addchartbubble(yes,low-0.2, slopelo2 + "\n" + lo[-4] + "\n" + lo[0], color.cyan,no);

#  read the slope when fivebar0, and keep it during the 5 bars, and 3 bars after
def slope5hi = if fivebar0 then slope5hi2 else if en_slope5lines then slope5hi[1] else na;
def slope5lo = if fivebar0 then slope5lo2 else if en_slope5lines then slope5lo[1] else na;
# test data  high slope
#addchartbubble(en_slope5lines, high+0.4, slope5hi, color.cyan,yes);

# get barnum at bar0, and keep it during the slope line
def bar0_5number = if fivebar0 then barnumber() else if en_slope5lines then bar0_5number[1] else na;
#addchartbubble(en_slopelines,high+0.2, bar0_number, color.cyan,yes);

#  get the high at bar0, for 5 bar pattern, and keep it during the slope line
def bar0_5high = if fivebar0 then hi else if en_slope5lines then bar0_5high[1] else na;

#  get the low at bar0, for 5 bar pattern, and keep it during the slope line
def bar0_5low = if fivebar0 then lo else if en_slope5lines then bar0_5low[1] else na;

# calc the slope line between 1st and 5th bar highs.
plot diag5hi = if en_slope5lines then ( bar0_5high + (slope5hi * (barnumber() - bar0_5number))) else na;
diag5hi.AssignValueColor(Color.cyan);
diag5hi.SetStyle(Curve.MEDIUM_DASH);
diag5hi.hidebubble();

# calc the slope line between 1st and 5th bar lows.
plot diag5lo = if en_slope5lines then ( bar0_5low + (slope5lo * (barnumber() - bar0_5number))) else na;
diag5lo.AssignValueColor(Color.cyan);
diag5lo.SetStyle(Curve.MEDIUM_DASH);
diag5lo.hidebubble();

# plot a horizontal line at the high of 1st bar
plot horz5hi = if en_slope5lines then bar0_5high else na;
horz5hi.AssignValueColor(Color.cyan);
horz5hi.hidebubble();

# plot a horizontal line at the low of 1st bar
plot horz5lo = if en_slope5lines then bar0_5low else na;
horz5lo.AssignValueColor(Color.cyan);
horz5lo.hidebubble();


# --- 4 bar inside pattern ---------------------------------------


# if a 5 bar exists, dont draw 4 bar lines
#  chk if 4 bar pattern is desired
def draw_4bar = if fivebar then 0 else if show_4_bar_pattern then 1 else 0;

# formulas ref'd off of the 1st bar, bar0, the biggest in the pattern
# find a bar bigger than the next 3 bars
def fourbar0 = draw_4bar and hi > hi[-1] and hi > hi[-2] and hi > hi[-3] and lo < lo[-1] and lo < lo[-2] and lo < lo[-3];
# define the 4 bars in an inside bar
#  as time goes on, look back x+ bars at bar0 to see if it was the biggest candle
def fourbar = fourbar0 or fourbar0[1] or fourbar0[2] or fourbar0[3];

#  plot small arrows above and below bar0
plot v4 = fourbar0 and show_small_arrows_bar0;
v4.SetPaintingStrategy(PaintingStrategy.boolean_wedge_UP);
v4.AssignValueColor(Color.white);
v4.SetLineWeight(1);
v4.hidebubble();
plot w4 = fourbar0 and show_small_arrows_bar0;
w4.SetPaintingStrategy(PaintingStrategy.boolean_wedge_down);
w4.AssignValueColor(Color.white);
w4.SetLineWeight(1);
w4.hidebubble();

#  plot small arrows above and below last bar
plot s4 = fourbar0[3] and show_small_arrows_last_bar;
s4.SetPaintingStrategy(PaintingStrategy.boolean_wedge_UP);
s4.AssignValueColor(Color.white);
s4.SetLineWeight(1);
s4.hidebubble();
plot t4 = fourbar0[3] and show_small_arrows_last_bar;
t4.SetPaintingStrategy(PaintingStrategy.boolean_wedge_down);
t4.AssignValueColor(Color.white);
t4.SetLineWeight(1);
t4.hidebubble();

#  sbcb =  show_bar_count_bubble;
addchartbubble(sbcb and fourbar0, (low * bubvert), "4", color.cyan, no);

#  enable slope lines - define bars to draw slope lines.
#  4 bars , and +3 extra bars past the last bar
def en_slope4lines = fourbar or fourbar[1] or fourbar[2] or fourbar[3];

#  calc the slope from 3 gaps, (4 bars)
def bar4gaps = 3;
#  hi slope is neg , lo slope is pos
#  use the 1st bar and last bar to calc slope
def slope4hi2 = (hi[-bar4gaps] - hi[0])/bar4gaps;
def slope4lo2 = (lo[-bar4gaps] - lo[0])/bar4gaps;

#  test data
#addchartbubble(yes,high+0.2, slope4hi2 + "\n" + hi[-4] + "\n" + hi[0], color.cyan,yes);
#addchartbubble(yes,low-0.2, slope4lo2 + "\n" + lo[-4] + "\n" + lo[0], color.cyan,no);

#  read the slope when fourbar0, and keep it during the 4 bars, and 3 bars after
def slope4hi = if fourbar0 then slope4hi2 else if en_slope4lines then slope4hi[1] else na;
def slope4lo = if fourbar0 then slope4lo2 else if en_slope4lines then slope4lo[1] else na;
# test data  high slope
#addchartbubble(en_slope4lines,high+0.4, slope4hi, color.cyan,yes);

# get barnum at bar0, and keep it during the slope line
def bar0_4number = if fourbar0 then barnumber() else if en_slope4lines then bar0_4number[1] else na;
#addchartbubble(en_slope4lines,high+0.2, bar0_4number, color.cyan,yes);

#  get the high at bar0, for 4 bar pattern, and keep it during the slope line
def bar0_4high = if fourbar0 then hi else if en_slope4lines then bar0_4high[1] else na;

#  get the low at bar0, for 4 bar pattern, and keep it during the slope line
def bar0_4low = if fourbar0 then lo else if en_slope4lines then bar0_4low[1] else na;


# calc the slope line between 1st and 4th bar highs.
plot diag4hi = if en_slope4lines then ( bar0_4high + (slope4hi * (barnumber() - bar0_4number))) else na;
diag4hi.AssignValueColor(Color.cyan);
diag4hi.SetStyle(Curve.MEDIUM_DASH);
diag4hi.hidebubble();

# calc the slope line between 1st and 4th bar lows.
plot diag4lo = if en_slope4lines then ( bar0_4low + (slope4lo * (barnumber() - bar0_4number))) else na;
diag4lo.AssignValueColor(Color.cyan);
diag4lo.SetStyle(Curve.MEDIUM_DASH);
diag4lo.hidebubble();

# plot a horizontal line at the high of 1st bar
plot horz4hi = if en_slope4lines then bar0_4high else na;
horz4hi.AssignValueColor(Color.cyan);
horz4hi.hidebubble();

# plot a horizontal line at the low of 1st bar
plot horz4lo = if en_slope4lines then bar0_4low else na;
horz4lo.AssignValueColor(Color.cyan);
horz4lo.hidebubble();


#  --- 3 bar inside pattern -----------


# if a 5 bar or 4 bar exists, dont draw 3 bar lines
#  chk if 3 bar pattern is desired
def draw_3bar = if fivebar or fourbar then 0 else if show_3_bar_pattern then 1 else 0;


# formulas ref'd off of the 1st bar, bar0, the biggest in the pattern
# find a bar bigger than the next 2 bars
def threebar0 = draw_3bar and hi > hi[-1] and hi > hi[-2] and hi > hi[-3] and lo < lo[-1] and lo < lo[-2] and lo < lo[-3];
# define the 3 bars in an inside bar
#  as time goes on, look back x+ bars at bar0 to see if it was the biggest candle
def threebar = threebar0 or threebar0[1] or threebar0[2] or threebar0[3];


#  plot small arrows above and below bar0
plot v3 = threebar0 and show_small_arrows_bar0;
v3.SetPaintingStrategy(PaintingStrategy.boolean_wedge_UP);
v3.AssignValueColor(Color.white);
v3.SetLineWeight(1);
v3.hidebubble();
plot w3 = threebar0 and show_small_arrows_bar0;
w3.SetPaintingStrategy(PaintingStrategy.boolean_wedge_down);
w3.AssignValueColor(Color.white);
w3.SetLineWeight(1);
w3.hidebubble();


#  plot small arrows above and below last bar
plot s3 = threebar0[3] and show_small_arrows_last_bar;
s3.SetPaintingStrategy(PaintingStrategy.boolean_wedge_UP);
s3.AssignValueColor(Color.white);
s3.SetLineWeight(1);
s3.hidebubble();
plot t3 = threebar0[3] and show_small_arrows_last_bar;
t3.SetPaintingStrategy(PaintingStrategy.boolean_wedge_down);
t3.AssignValueColor(Color.white);
t3.SetLineWeight(1);
t3.hidebubble();


#  sbcb =  show_bar_count_bubble;
addchartbubble(sbcb and threebar0, (low * bubvert), "3", color.cyan, no);

#  enable slope lines - define bars to draw slope lines.
#  3 bars , and +3 extra bars past the last bar
def en_slope3lines = threebar or threebar[1] or threebar[2] or threebar[3];


#  calc the slope from 2 gaps, (3 bars)
def bar3gaps = 2;
#  hi slope is neg , lo slope is pos
#  use the 1st bar and last bar to calc slope
def slope3hi2 = (hi[-bar3gaps] - hi[0])/bar3gaps;
def slope3lo2 = (lo[-bar3gaps] - lo[0])/bar3gaps;

#  test data
#addchartbubble(yes,high+0.2, slope3hi2 + "\n" + hi[-4] + "\n" + hi[0], color.cyan,yes);
#addchartbubble(yes,low-0.2, slope3lo2 + "\n" + lo[-4] + "\n" + lo[0], color.cyan,no);

#  read the slope when threebar0, and keep it during the 3 bars, and 3 bars after
def slope3hi = if threebar0 then slope3hi2 else if en_slope3lines then slope3hi[1] else na;
def slope3lo = if threebar0 then slope3lo2 else if en_slope3lines then slope3lo[1] else na;
# test data  high slope
#addchartbubble(en_slope3lines,high+0.4, slope3hi, color.cyan,yes);

# get barnum at bar0, and keep it during the slope line
def bar0_3number = if threebar0 then barnumber() else if en_slope3lines then bar0_3number[1] else na;
#addchartbubble(en_slope3lines,high+0.2, bar0_3number, color.cyan,yes);

#  get the high at bar0, for 3 bar pattern, and keep it during the slope line
def bar0_3high = if threebar0 then hi else if en_slope3lines then bar0_3high[1] else na;

#  get the low at bar0, for 4 bar pattern, and keep it during the slope line
def bar0_3low = if threebar0 then lo else if en_slope3lines then bar0_3low[1] else na;


# calc the slope line between 1st and 3rd bar highs.
plot diag3hi = if en_slope3lines then (bar0_3high + (slope3hi * (barnumber() - bar0_3number))) else na;
diag3hi.AssignValueColor(Color.cyan);
diag3hi.SetStyle(Curve.MEDIUM_DASH);
diag3hi.hidebubble();

# calc the slope line between 1st and 3rd bar lows.
plot diag3lo = if en_slope3lines then (bar0_3low + (slope3lo * (barnumber() - bar0_3number))) else na;
diag3lo.AssignValueColor(Color.cyan);
diag3lo.SetStyle(Curve.MEDIUM_DASH);
diag3lo.hidebubble();

# plot a horizontal line at the high of 1st bar
plot horz3hi = if en_slope3lines then bar0_3high else na;
horz3hi.AssignValueColor(Color.cyan);
horz3hi.hidebubble();

# plot a horizontal line at the low of 1st bar
plot horz3lo = if en_slope3lines then bar0_3low else na;
horz3lo.AssignValueColor(Color.cyan);
horz3lo.hidebubble();
#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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