Bull Flag and Bear Flag Formations for ThinkorSwim

Hello team,
I have recently enrolled. does anyone ever did the megaphone pattern setup on the SPX of any stocks, if so can you please share the study if possible. Thanks
This post is about SCANNING for a megaphone pattern, which is also known as a broadening formation.

If you are looking for DRAWING a megaphone on your chart, @Pelonsax has a rudimentary script that does that on the first post of this thread:
https://usethinkscript.com/threads/rob-smiths-the-strat-indicator-for-thinkorswim.3312/

Look for this text in the first post:
Pelonsax said:
BROADENING FORMATIONS (1ST DRAFT)
This study will only find 1-3 combos and extend diagonal lines outward.

Anyway, you already know what a megaphone/broadening formation is, but I wanted to be sure to provide context for those who come upon this thread.

Investopedia said:
A broadening formation is a price chart pattern identified by technical analysts. It is characterized by increasing price volatility and diagrammed as two diverging trend lines, one rising and one falling. It usually occurs after a significant rise, or fall, in the action of security prices. It is identified on a chart by a series of higher pivot highs and lower pivot lows.
https://www.investopedia.com/terms/b/broadeningformation.asp

So, my simple definition of broadening is taking out prior highs AND taking out prior lows.
I figure that if you zoom out far enough, it will show up as an outside bar on your chart.
#TheStrat calls this a 3. You can check out the @Pelonsax thread, linked above in this post, if you're curious about #TheSTRAT.

Anyway, on to the code:
First, there is an input called Window. The window defines the "number of bars" that you consider for taking out previous highs AND lows.
(For example, on higher time frames (monthly, quarterly, yearly), you can use windows of lesser bars and that will often be sufficient to show price action broadening in both directions.
On a lower time frame (day, 4-hour, 30-minute), you may need multiple bars to show broadening, to give price time to move from one side of the formation to the other.

The code defines a broadening as high taking out previous highs and low taking out previous lows. The high and low have to be taken out within the same window to be a valid broadening for this code.

The megaphone in this code requires back-to-back broadening: A broadening for the current Window against its prior Window, AND a broadening for the previous Window against its prior Window.

I've tested this scan with these parameters:
version 1: scan in: S&P 500, Timeframe: Quarter, input Window = 2
version 2: scan in: S&P 500, Timeframe = Daily, input window = 50;

Both sets returned somewhat reasonable-looking results on the daily timeframe. Might want to experiment with timeframes and windows to get something you like.

Code:
input window =50; 
def highWindow = highest(high, window);
def lowWindow = lowest(low, window);

def broadening = highWindow > highWindow[window] && lowWindow < lowWindow[window];
def megaPhone = broadening && broadening[window];
plot scan = megaPhone;
 
Here's another that can be turned into scan by using # in front of plot......
# [email protected] CBLOLD Search
#To use for scan place # signs before 2 Addlabel statements and change "def" before trigger statement to "plot"
##############Bull Flag
def StockPrice = close>=10;
def trend = simpleMovingAvg("length"=30)> (simpleMovingAvg("length"=30)[5]);
def MacdLow = macdHistogram(8,17,9)[1]< macdHistogram(8,17,9)[2]and close[1]<close[2] and close>high[1];
#def Momentum = macdHistogram(8,17,9) > macdHistogram(8,17,9)[1];
##############Bear Flag
def StockPriceBear = close>=10;
def trendBear = simpleMovingAvg("length"=30)< (simpleMovingAvg("length"=30)[5]);
def MacdHighBear = macdHistogram(8,17,9)[1]> macdHistogram(8,17,9)[2]and close[1]>close[2] and close<low[1];
#########################################
#def trigger = trend and momentum and MacdLow;
def Bulltrigger = trend and MacdLow;
def BearTrigger = TrendBear and MacdHighBear;

#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);

I wanted to use a Market Phase Study to narrow OPTION scan results to return only those in Bear Phase. I added a study filter, pasted the entire MP code, which, of course, created too many plots. I took a WAG and changed all but Bear from PLOT to DEF and it seemed to actually work. Could I have just #'d them out? Would it be preferable to # them out? Or does it not matter? I'm a coding Dotard so knowing the difference will be helpful. Tks!!
 
I wanted to use a Market Phase Study to narrow OPTION scan results to return only those in Bear Phase. I added a study filter, pasted the entire MP code, which, of course, created too many plots. I took a WAG and changed all but Bear from PLOT to DEF and it seemed to actually work. Could I have just #'d them out? Would it be preferable to # them out? Or does it not matter? I'm a coding Dotard so knowing the difference will be helpful. Tks!!
When I am editing a code, I comment (#) out the extraneous lines that I don't need. If an error message results, I put it back in.
I verify that my results are the same as before I commented out the lines.

Having extraneous lines in a script that just plots on a chart doesn't usually have a huge effect.
However, commenting/deleting all unnecessary labels, plots, calculations when using the script in a scan will sometimes make all the difference in avoiding the dreaded "too complex" error.

Hope this helps.
 
Last edited:
When I am editing a code, I comment (#) out the extraneous lines that I don't need. If an error message results, I put it back in.
I verify that my results are the same as before I commented out the lines.

Having extraneous lines in a script that just plots on a chart doesn't usually have a huge effect.
However, commenting/deleting all unnecessary labels, plots, calculations when using the script in a scan will sometimes make all the difference in avoiding the dreaded "too complex" error.

Hope this helps.
Thank you, I use pretty much the same method so I can at least attempt to track what I changed, but you add a great point about the "too complex warning", which I do get when altering my ORB stuff and might be able to fix that by deleting vs #ing out. ~ Love your Hurricane Flamingo!
 
is it "RSI" crosses ABOVE RSI()."OverBought"?!

Your syntax is not correct and will result in an error.
The most complete reference for RSI is:
reference RSI()."RSI"
but the RSI plot is the default plot, so you will see RSI() also used.
You cannot leave off the brackets: RSI() as that is how you tell ToS to go look for a study
Otherwise, it assumes that you are defining something that you happen to be naming RSI.

As far as the crosses above function.
It depends on what you are looking for.
The OP was looking for bullish flags in Overbought territory, so it would be:
Bull_Bear_Flags_Formation()."bullish" is true
RSI()."RSI" >= RSI()."OverBought"

You can use crosses above but keep in mind that you are asking that the heavens align on one bar.
This severely limits results. Because the heavens don't always comply. ;)
 
Last edited:

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