ThinkorSwim Stock Breakout Scanner

CDJay

Member
VIP
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.
 

BenTen

Administrative
Staff
VIP
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);
 

Jynx

New member
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;
 

Jynx

New member
@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);
 

stockula

New member
VIP
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.
 

BenTen

Administrative
Staff
VIP
@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.
 

cat4scotty

New member
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,
 

MerryDay

Well-known member
VIP
@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
 

cat4scotty

New member
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.
 

MerryDay

Well-known member
VIP
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:

cat4scotty

New member
Hi HTH, Just what I was looking for. It's great, but as you said it needs some tweaking to get more responses. I have a few questions:

1) Do you have a link that can help me with the different script definitions? For example, Does ."Diff" refer to the current bar, while ."Diff"(1) refers to the previous bar and presumably "Diff"(2) would be the 2nd previous.
2) Can I test the combination of scripts on a much earlier pattern. e.g. the initial one I referred to for SMPL on 12/3? ....https://tos.mx/PVPRuEg
3) Can I test for a breakout from a Flag formation "pattern", such as TSLA on 11/17?
4) With so many scripts, is there a way to put a particular script on "hold", without deleting it, and then having to reinstall?
5) For TSEM, which you presented earlier, it showed a clear breakout, or possible change in direction of the stock's movement from -ve slope to +ve. While the TRIT example shows the studies selecting a move up for the stock, the Scan only finds TRIT and doesn't find TSEM and TRIT doesn't show a change in slope from -ve to +ve. TDA educators, I understand, use a script for CAHOLD to do this. Is there a way to include this in the Scan?
6) not sure why these give different results:

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

Thanks again.
 

YungTraderFromMontana

Well-known member
I just wanted to give my two cents on why trading breakouts is a powerful strategy for any novices trying to find their way.

One of the most valuable things you can find as a trader is a area that you know the price of the stock respects; An area where a break below or above has a higher chance then normal of creating a trending move. This key factor gives you the perfect opportunity to skyrocket your risk/reward ratio into a trade. As we see with fractal energy, most of the time stocks movements are extremely random and undecipherable. If you can find a catalyst that makes these moves less random and more trending you can exploit it with proper order strategies. On any given day if you place a stop loss 2 percent below the current price and a take profits at 8 percent above price there is an extremely low chance that your take profits gets hit because of the usual erratic movement of prices. If you do the same thing but trade a breakout such as a breakout to ath or a 200sma breakout your chances go up drastically because price becomes more trendy, there is a higher chance the result of the next candle is the same as the one before it. Of course every breakout will not pan out, but if you enter soon after an area respected by price is broken, you can set a tight stop slightly below the resistance and set a take profits much higher. Because of this you can be wrong 2/3 times and still make lots of money over time.

The key to this strategy is making sure that you choose resistances that the stock truly respects, not places it respects some of the time. On stocks I would only consider resistance levels where price has been squeezed for the last 10+ bars or a ma like 200sma or possibly 50sma. If you get good at seeing when a certain level is important where the chance of getting faked out is low. You have a much statistically higher chance of having price trend your way. Fortunately it is not hard to find many of these levels, It does not take an expert.

For anyone looking for examples of heavily respected levels I'll give you a few.
Recent examples of respected area breakouts could be ODFL at around 151.6 or TEAM at around 159.

If you're looking for a good old examples look at /gc from November of 2005 to September of 2011. Look at how many ath breakouts took place where you could have risked under 2% to make 10-30%. I counted 11 times with only 8 failed breakouts. That's a 5-15 time win/risk ratio with a positive win rate.
 

yaarione

New member
Please check this image, I see a very strong move after this scenario.

Shake bulls --> failed breakout
Shake bulls --> failed breakdowns

Os2uavL.png


How can we scan for this scenario?
 

yaarione

New member
@yaarione Do you have any additional information other than just highlighted candles...???
This script pointing all lows and highs!! but, what I am looking for is to find
1. a recent HIGH and LOW(price is here marked on the image)
2. previous LOW and previous HIGH
3. if previous HIGH and LOW are inside recent HIGH and LOW then look for price crossing above previous LOW

wRmIEIl.png


Code:
def swinghigh = if high > high[1] and high > high[2] and high > high[-1] and high > high[-2] then 1 else 0;

Def swinglow = if low < low[1] and low < low[2] and low < low[-1] and low < low[-2] then 1 else 0;


Plot sh = if swinghigh then high else double.nan;
Plot sl = if swinglow then low else double.nan;
sh.setstyle(curve.points);
sl.setstyle(curve.points);
 

westsail5000

New member
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.
 

Printsora

New member
Can someone kindly help me write a study that scans for breakout stocks of the day? The Criteria is simply to scan for stocks crossing above its 5 day range by one standard deviation. I'm new to TOS and don't know anything about writing scripts.
 

Similar threads

Top