ThinkorSwim Stock Breakout Scanner

CDJay

Member
I'm looking for a ThinkorSwim breakout scanner to find stocks coming out of consolidation and finally breaking above or below their base.

I've been using the relative volume scan that Sonny wrote in July, and have found it quite effective in identifying stocks that could potentially rise quickly. However, sometimes I'm a few minutes too late to safely ride the wave, while on others I'm two hours too late; the stocks still fit the parameters the scan searches on — good relative volume, price 10% above yesterday's close — but the positive volume has dried up and the stocks are fading. Finally, I'll cycle through that watchlist later in the day and be surprised to learn that stock X had a prolonged comeback starting at 1:30, but I was again too late to take advantage.

I've made a number of attempts at writing a scan to try to find stocks that are percolating at that moment, meaning either a series of long green bars (on the 1M) or a prolonged period of consecutive greens, but too often the scan is calling attention to a stock that has had minimal volume for a significant portion of the day other than the latest two greens. Basically, an outlier for the past 90 minutes.

My guess is an effective scan will involve custom studies, but I'm not getting it right yet. Here's my latest attempt, which is still finding stocks at the wrong time.
  • Unusual Volume: Current bar has increased 200% over the last 20 bars (Day Aggregation)
  • Unusual Volume: Current bar has increased 200% over the last 10 bars (1M Aggregation)
  • Price Change: Close has increased 1% greater than the last bar (1M Aggregation)
  • Custom: 9D MovAvgExponential crosses above the 21D MovAvgExponential (1M Aggregation)
So let me give the conditions for what I think each step should be:
  1. Identify all stocks that have been trading at an above average rate since the start of the day.
  2. Whittle down to those stocks whose last bar had significantly more positive volume versus the last X amount of minutes — meaning price is rising with heavy volume — and/or whittle down to those stocks whose last two or three bars have been growing in price, even if the volume is only somewhat higher than the last X amount of minutes.
  3. Whittle down further to those stocks whose 9D MovAvgExponential is crossing above the 21D MovAvgExponential. Or perhaps crossing above the VWAP. Or maybe "is above" would be better than crossing above. Also, I'm not sure if the Price Change above would be necessary if this is working properly.
The bottom line is I'm hoping someone can help with a scan that alerts to what we all can see visually twenty times a day: "If only I had been watching ABCD at 11:43, because the volume and price were starting to fly and that was the time to buy."

Thanks for any help or input.
 

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

Thank you for the detailed post. I would first start by going through the forum and look for any existing scanner or indicator that I can build off of. Check out the scanner below (source).

Code:
#Hint:Scan/plot for a increase or decrease % of an inputted Parameter and average-length
#Give an up arrow where true
input price = volume;#hint Price:Parameter being measured
input choice = {default increase, decrease};#hint Choice: Choose Increase or Decrease %
input percent = 20;#hint percent: Enter the percent  increse/decrease
input length = 50;#hint length: The average length being evaluated
def avg = average(price, length)[1];
def chg = 100*(price/avg -1);
plot scan;
switch (choice) {
case increase:
    scan = chg >= percent;
case decrease:
    scan = chg <= -percent;
}
#### Below items not needed for a scan ####
#scan.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
scan.SetpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
scan.SetLineWeight(5);
scan.SetDefaultColor(Color.White);
AddLabel(yes,Percent + " % " + Choice + " of the " + length + "-bar-average of a selected input price",color.white);
 
I use this code in the morning to filter through stocks that are already moving outside their normal range daily range but it could work for sort of for that 3rd condition...it will look over the last 20 bars and average out the high and low and will alert you if the range for that bar jumps above the avg range by 150% signaling some sort of breakout or breakdown.

Put this code in a study then in when scanning add a custom study as a filter using thinkscript option put in the following code to scan
for scanner...

Code:
rangehist().rangepercentage is greater than 150

now set it to 5 min in the scan

Code:
#RangeHist
declare lower;
def todaysrange=(high-low);
def yesterdaysrange=(high[1]-low[1]);
def range3=(high[3]-low[3]);
def range4=(high[4]-low[4]);
def range5=(high[5]-low[5]);
def range6=(high[6]-low[6]);
def range7=(high[6]-low[6]);
def range8=(high[7]-low[8]);
def range9=(high[9]-low[9]);
def range10=(high[10]-low[10]);
def range11=(high[11]-low[11]);
def range12=(high[12]-low[12]);
def range13=(high[13]-low[13]);
def range14=(high[14]-low[14]);
def range15=(high[15]-low[15]);
def range16=(high[16]-low[16]);
def range17=(high[17]-low[17]);
def range18=(high[18]-low[18]);
def range19=(high[19]-low[19]);
def range20=(high[20]-low[20]);

plot rangevalue=todaysrange;
rangevalue.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
rangevalue.SetLineWeight(3);
def avgrange=((todaysrange+yesterdaysrange+range3+range4+range5+range6+range7+range8+range9+range10+range11+range12+range13+range14+range15+range16+range17+range18+range19+range20)/20);
plot avgrange1=avgrange;
def rangepercentage=((todaysrange/avgrange)*100);
AddLabel(yes, (roundDown(rangevalue/avgrange)*100)+ " % Daily");
def avgclose=average(close,20);
def rangepercentofavgclose =((avgrange/avgclose)*100);
AddLabel(yes, (roundDown(avgrange/avgclose)*100)+ " %Avg Close");

Another scan code from Mobius.

Code:
# Consolidation and Breakout
# Mobius
 
def consolidation = ((Log(Sum((Max(high, close[1]) - Min(low, close[1])), 13) /
(Highest(high, 13) - Lowest(low, 13)))
/ Log(13)) > .7);
def breakout = close > highest(high[1], 13);
plot cond = consolidation[1] and breakout;
 
@HighBredCloud @stockula @BenTen Save the code as a study named RangeHist...No it doesn't need to be turned into a scanner you can just use the internal variables as a reference for the scan so in the scanner choose study then custom study and put the following code in the thinkscript (be sure to name the study RangeHist)

Code:
#Code:
RangeHist().Rangepercentage is greater than 100
#end code

That will single out all the stocks that have already moved more than there avg range over the last 20 periods so if you set it to Days it will be over the last 20 days if you set it to 5 min it will be over the last 20- 5 min bars generally in the first 5 minutes you'll get about 100 or so that have moved more than there avg range for an entire avg day and those are the one you want to trade, I use to go by relative volume but its a lot harder to code that for the first few minutes of the day this was a work around that basically generated the same ideas if the relative volume is a lot higher the range was sure blow up too

There is another variable in the study that I added that gives you the percentage of AVG range for the last 20 periods divided by the the AVG close for those 20 periods so you can see what percentage the range is relative to the close price higher percentage means more movement in the stock price relative to other stocks for instance today aapl was 113% of its daily range and the avg range is approximately 3% of its close but Macys was 106% of its Daily Range but its avg range is 6% of its close so if your trading macys your probably going to get more bang for your buck than if you are trading appl.

Code:
#code:
RangeHist().rangepercentofavgclose is greater than 3
#end code

Where the last number is the minimum percent you want to search for.

NEW UPDATE

@stockula @HighBredCloud Here is a modified version for a watchlist column... for the range percentage that is color based I would set it to five min, so just scan for your first 2 variables then save as a watchlist add this as a column, I was thinking about your strategy you might want to incorporate some new high(bullish) or low(bearish) of the day counter that counts new highs in a given period set of 2 min periods seems like the strategy would be more successful if there was a trend of like 10 new hod instead of just near the one and done hod then chop/ just sell off bc then the chop is more likely to be consolidation/basing for the next move...I also would like to mention if the open range is really high it will be like 2-3 hours before the avg calms down on my code so that the current range might exceed the avg on a 5 min time frame just an fyi, I might explore using a ema instead of a sma in the code later on

Code:
declare lower;
def todaysrange=(high-low);
def yesterdaysrange=(high[1]-low[1]);
def range3=(high[3]-low[3]);
def range4=(high[4]-low[4]);
def range5=(high[5]-low[5]);
def range6=(high[6]-low[6]);
def range7=(high[6]-low[6]);
def range8=(high[7]-low[8]);
def range9=(high[9]-low[9]);
def range10=(high[10]-low[10]);
def range11=(high[11]-low[11]);
def range12=(high[12]-low[12]);
def range13=(high[13]-low[13]);
def range14=(high[14]-low[14]);
def range15=(high[15]-low[15]);
def range16=(high[16]-low[16]);
def range17=(high[17]-low[17]);
def range18=(high[18]-low[18]);
def range19=(high[19]-low[19]);
def range20=(high[20]-low[20]);

#plot rangevalue=todaysrange;
#rangevalue.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#rangevalue.SetLineWeight(3);
def avgrange=((todaysrange+yesterdaysrange+range3+range4+range5+range6+range7+range8+range9+range10+range11+range12+range13+range14+range15+range16+range17+range18+range19+range20)/20);
#plot avgrange1=avgrange;
def rangepercentage=((todaysrange/avgrange)*100);
#AddLabel(yes, (roundDown(todaysrange/avgrange)*100)+ " % Daily");
plot rangepercentage1=rangepercentage;
def avgclose=average(close,20);
def rangepercentofavgclose =((avgrange/avgclose)*100);
#AddLabel(yes, (roundDown(avgrange/avgclose)*100)+ " %Avg Close");
def condition1 = rangepercentage1 <50;
def condition2 = rangepercentage1 >100 and rangepercentage1 is less than 150;
def condition3 = rangepercentage1 >150 and rangepercentage1 is less than 200;
def condition4 = rangepercentage1 >200 and rangepercentage1 is less than 300;
def condition5 = rangepercentage1 >300;

AssignBackgroundColor(if condition1 then color.blue else if condition2 then color.green else if condition3 then color.yellow else if condition4 then color.orange else if condition5 then color.red else color.gray);
 
Hi folks, I hope everyone is well.

I am trying to construct a basic late day breakout scanner on tos. I don't think it requires scripting, as much as knowing which settings to apply on the tos scanner. I was hoping someone could help me with it, it would be greatly appreciated. It is best approached on a 5min time frame.

Setup conditions BULL:
-Stock is trading above prior DAY close
-stock is trading above current day's opening price
-the stock is trading sideways/chopping AT OR NEAR hod for at least 1.5 hours (more accurately, eighteen 5min candles)

-Entry trigger, upside break of the chop zone high bar (preferably slightly lower than the hod)
-Stop loss set to below the low of the chop zone

Setup conditions BEAR:
-Stock is trading below prior DAY close
-stock is trading below current day's opening price
-the stock is trading sideways/chopping AT OR NEAR lod for at least 1.5 hours (more accurately, eighteen 5min candles)

Bear trigger is downside break of chop zone low, stop loss, upside break of chop zone high.

So depending on the stock, its a pretty nice tight stopped setup that can catch late day surges. I'm not sure how to incorporate the prior day close conditions to a 5min candle scan.

Any help is appreciated.

Note: The setup is great, and can fail miserably like any other setup. The entry trigger is not set in stone, but I personally prefer to wait for momentum before jumping in. Also, those who are familiar with the "Opening range break" indicator we have on the forums, I can see ways to incorporate the opening range lines with late day breaks.

Any help with the scanner is greatly apprecaited. Thanks folks.
 
@stockula There are a couple of existing indicators that you can use to fulfill some of the conditions you mentioned above.

Use the script below to scan for stocks trading above or below its previous day's close.

Code:
# Previous Day's Close

input aggregationPeriod = AggregationPeriod.DAY;
def close = close(period = aggregationPeriod)[1];
plot prev_close = close;

Use the script below to scan for stocks trading above or below its current day's opening price.

Code:
input aggregationPeriod = AggregationPeriod.DAY;
def open = open(period = aggregationPeriod);
plot current_open = open;

These two conditions will help you gather a filtered basket of stocks, and then from there, you can continue to filter out the stocks that you want based on your third condition.
 
I would like to figure out how to write a script, or scan to do the following:

As an example if you look at stock SMPL on 12/3, several bullish indicators line up:

1) Stock broke out upwards from a downward trend (consolidation).
2) MACD Histogram was favorable.
3) Slow Stochastics indicated fast crossing slow upward.
4) RSI trended upward.
5) RS (SPX) trended upward.

#1, 3 and 4 are probably the most important although ideally all five would be present.

I would like to develop a script, or scan to encompass all of these studies (and perhaps add others later) and also select stocks in a given price range.

How best can i do this?

Thanks,
 
@cat4scotty This scan might be doable. However, the definitions need refined.
  1. How are you defining: "Stock broke out upwards from a downward trend (consolidation)"? I recommend that you don't use studies that incorporate zig-zags, trend reversals, swing high/lows as they all repaint. Meaning they can signal a break upward and then erase that signal and continue downward.
  2. MACD Histogram "favorable". What does favorable mean to you?
  3. No idea what "Slow Stochastics indicated fast crossing slow upward" means? You want the TOS SlowStoch? It doesn't have a "fast" length.
It would be better if
  1. you provide the actual TOS indicators or custom studies that you want to use and
  2. the actual condition to be satisfied.
  3. Setting them up into a scan is then relatively simple.
If you aren't sure which indicators you want to use then experiment with the built-in indicators load bunches of them on a chart and see which give the signal you are looking for. Take a picture with a big red circle that says "this is what I want". You can do the same thing with the custom studies provided in this forum.
HTH
 
Here is a copy of the chart; https://tos.mx/PVPRuEg

Dashed vertical lines show the 12/3 date.

1) Breakout to me means a change in slope direction. In this case from -ve to +ve slope. This is a bullish breakout from a trend, flag, pennant etc.

2) & 3) See chart.

The indicators I want to use are clearly defined on the chart.

Thanks.
 
SlowStoch Breakout by @cat4scotty

1gE5ZkW.png

jTt2tVW.png


Here is the shared scan code: http://tos.mx/7sqcFhE

There were no results when scanning for: price crossing above 200 and price crossing above 20 and price crossing above 5.

One scenario that does return results is : price crossing above 200 and price >= 20 and price >= 5

@cat4scotty, This has potential. Nice job. Play w/ the "crossing" versus "greater than" and price>ema compared to ema>ema until the setup is optimal for your strategy.

HTH
 
Last edited:
Average price percent increase scan within specified amount of time? Seems this would be great for finding momentum stocks, or stocks declining in price. I haven't found anything built into tos that seems to be able to do this.
 
Hey everyone. Just want to give you all a heads up. I made a simple scan that does exactly what OP desires. It already right within the stock settings.
Go to scan, New Study > Price_change > set it to 5% greater than 1 bar ago. This effectively finds stocks that are seeing volume and momentum. Make sure you click the Ext. setting on the right side of the study and change the daily to the 1 or 5 min or which ever desired timeframe.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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