Build a TTM Squeeze Scanner in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back in this video i'm going to be discussing the ttm squeeze indicator we're going to be looking at a version of this indicator that's written in pine script on the trading view platform and then we're going to take that code and translate that into python and use it to scan for stocks that are about to take off so if you like the content on this channel subscribe and like this video and let's go ahead and get started so a couple of weeks ago i made this video called finding breakout candidates with python and pandas and i took a pretty simplistic approach to finding stocks that are trading in a narrow range and so i just took a percentage and said oh if this stock's been trading in this narrow range the past couple weeks so say it's 100 stock and it's been trading with between the range of 98 100 or 98.5 in 100 the past couple of weeks then it's been consolidating in a narrow range and then we looked for stocks that were coming out of that range as potential breakout candidates one viewer commented his name is craig i believe that a more sophisticated analysis of consolidation would be to use the ttm squeeze indicator found on the thinkorswim platform the squeeze indicator finds sections of the bollinger band study which fall inside the keltner's channels and when the market finishes a move the indicator turns off which corresponds to bands having pushed well outside the range of keltner's channels and so he talks about how this is a good way to detect consolidation and then a breakout of that consolidation so what i thought i'd do for this video since i've never used this before um that i would research this a little bit and learn how to code it and then demonstrate a new approach on this video so uh thank you craig for that comment i encourage anyone else to make suggestions because a lot of a lot of recording these videos is actually me learning myself and trying to communicate it to other people so part of this is actually me learning uh in public as as i go and so i'm going to learn about this and share it with everyone else so let's look at what this indicator is so i looked this up already um there's a number of pages on it and youtube videos already but no one's really talking about how to implement it in python and that's where i come into play so uh if you look on tradingview actually there's some people that use this indicator to trade i assume successfully um and so a lot of people seem to like this guy lazy bear writes a lot of scripts that he shares in the pine script language which us people that use tradingview some people know it some people just copy other people's scripts and if by the way if you want to sign up for trading view and you've never used it before then i'll leave a link to sign up for that it's really cool charting platform so i'll leave a link below so look for that but uh here's lazy bear's take at the indicator and i'm going to talk about the example chart he uses here to explain what this is doing and so you can see there's this indicator down here and you see this oscillator here and then if you look closely you'll see these black crosses and these gray crosses and what this is doing is when this these crosses are black right here that means the stock is going into a period of low volatility and so he used bitcoin as an example but we're gonna use stocks uh you can see right here uh bitcoin and this is back in 2014 when it was about 430 dollars per coin you see it gets in a very narrow range so it says black crosses means the squeeze is on so the bollinger bands are actually squeezed really close together they're inside of these channels called keltner's channels and i'll show you what all that looks like in a second but all it means is that during this period where these crosses are black there's very low volatility and it's trading in that very narrow range and then you see the crosses here eventually turn gray right and that means it's exiting it's entering a period of further uh volts more volatility right so the squeeze is on but once the squeeze is off you know the bollinger bands expand right and then so you see this large green candle and then this oscillator basically shows momentum increasing outside of this narrow range so it basically explodes out of this range right here and then you can see this shows momentum gradually increases increasing and so you want to place your trade right here as soon as the black cross goes to gray and it exits this narrow range and then you can ride that momentum there so you could trade it buy an option there or you could go long the stock and then you need to determine your own rules on when to exit that trade so you might want to here um we'll see when there's those green bars are showing increasing momentum and then you can see when the momentum decreases and so you could either wait till momentum starts to decrease or you know you could wait a little bit longer until it enters another period of of low volatility so maybe you wait all the way until until this next period of low volatility and exit the trade there so there's various ways you can exit exit the trade so you can just scalp a little bit of this move or you can try to write it longer but then there's some risks that you give back some of your gains all right so let's talk about how so how do we know when this squeeze happens so let's talk about the bollinger bands and the keltner channels and then yeah i'll show that visually and then we'll talk about the pine script code so he shared the pine script code here and a lot of people don't know know pinescript but i know enough to be able to translate this to python successfully and then we can use it ourself on our with our own tools if we want to so earlier today i wrote some python code to try to implement this squeeze indicator and i scanned for stocks where this occurred about a month ago so like the third week of july and so i'm going to show an example of what happened on trading view first and then we're going to look at the pine script and then we're going to translate that to python so the example uh one example that my screen came up with was target for the week ending july 24th and so you'll look here what i did was add a couple of indicators so i added the keltner channels and i've also added the bollinger bands and i'll talk about how to construct those in python in a moment but visually you can see there's these bands or envelopes around the price here so this is the chart of target stock and you can see these bollinger bands are in this light blue color and they're kind of expanding and contracting and then you see these blue this blue channel here that's actually the keltner channels and so if you remember the definition we gave earlier we want to look for a period where the bollinger bands are inside of the keltner channels and let's see where that happens and so if we go down to the prices here you'll see usually the bollinger bands these light blue ones are outside of these blue channels here and let me change the color of that just to make it um a little clearer when the bollinger bands go inside so i'm going to change this style to let's change this to like a red or orange right okay and i'll do that okay and so these red what we want to see is these red lines go inside and be completely contained inside these blue lines and so you'll see uh target is kind of an uptrend here but then you see this part where it's consolidating you'll see that's the period of low volatility and you'll see when we zoom in here eventually there's a period where these red lines here they're contained within the the blue lines right and so this is the period of low volatility and then you see the oscillator here at the bottom that in lazy bear script there's these gray pluses and then it becomes these black crosses here when it's a period of low volatility right and then eventually it becomes gray here around july 24th and you'll see that the red lines the bollinger bands are contained within the keltner's channels and then they break out right and then so this period july 24th is when uh we're saying the volatility is increasing and we're expecting a large momentum move and you see these green bars show that momentum and that's using a momentum indicator and there's a variety of momentum indicators that people use to us to show this oscillator and so there's an adx indicator but there's also i believe a lazy bear use some linear regression to determine the momentum of the move out so it moves out of this channel and you can see a target goes from around 122 a share right here and then closes above 150 so there's a huge momentum move coming out of that squeeze so it's squeezing right it's in this narrow volatility range and then we expect increased volatility and it takes off and that's over that's like a 25 gain i think that happened in target just in the last month which is a great a great move so that's what we're looking at visually now let's talk about the algorithm and coding this thing out so as usual i have a new visual studio code editor open and we're going to build this from scratch i'm going to create a new folder and open it so i'm going to click open folder and i'll create a new folder called squeeze and we'll open that up we'll create a couple of files i'm going to call one snapshot.pi because we're going to snapshot some price data again i'm going to create another file called squeeze.pi which will contain our code for scanning for these squeezes and then we're going to also need we'll just create a quick requirements.txt even though we don't need many requirements here so i'm not going to use ta lib here i'm just going to use pandas on its own and so all we really need is pandas and i'm going to also use plotly so that we can draw our own charts real quick and so that we can visually see that what we're doing is correct so we'll do that and so i'm going to go to snapshot.pi first and i'm going to import os and then i'm going to create a new folder for our data sets so i'm going to call it folder data sets and i'm going to go back to our github at hacking the markets and i'm just going to get the list of symbols that i need and so we have this repository called data sets and i'll also save this data set in this repository when i push it later and i just like to have a place with my s p 500 tickers and that'll just be our universe of stocks if you want to put a giant list of like every stock like the russell 2000 or any other stocks you can feel free to add those symbols there so i have this data set that just has some stock symbols so i'm going to copy those symbols and i'm just going to create a symbols.csv and this is this could really just be a text file because we're not really putting the company name or anything else in here and so what we're going to do first is use the y finance package which reminds me i also am going to use the y finance package and we're going to install these requirements and so i'm going to go to snapshot.pi i'm just going to run it real quick so that brings up my virtual environment and i'm going to do pip install r requirements text or you can install them individually so you just need to pip install pandas plotly and y finance and you'll have all the dependencies so now in my snapshot.pi i'm going to import y finance so i'll import y finance as y f and then all i'm going to do is open this symbols.csv and just loop through all of them and then fetch the daily time frame data from yahoo finance so i'm going to do with open symbols.csv as f so i'm going to get a file handle to symbols.csv and then i'll do lines equals f dot read dot split lines so i'm basically just reading in the contents of the file and splitting on the line breaks so each symbol will be on its own line and now let's do four symbol in lines so we're looping through those symbols and let's print the symbol and make sure everything's working so far print them out we're printing out all the stock symbols and then from y finance i've used this in many videos so i'm not going to go over it again all we need is to do is do y f dot and it has a download function that we can call and we just need to give it a symbol so i'm not going to download all the data for all of history i don't want to spend that much time downloading the data and i'm not running a back test i'm just going to get more recent data and show you how to implement this algorithm and so the first thing we'll do is download some data from a couple months ago and so i'm going to do start we're going to give it a start date and so i'll do 2020 0501 and let's say an end date of 2020 let's just do the end of june so we'll get some data for may and june we'll use it to build our scanner make sure it works as expected and we'll see what the results would have been for stocks that it finds we'll see what the results would have been for july and august and then once we're satisfied then we'll use this scanner again for the most recent snapshot of data for today is august 22nd so we'll try to get some data up till friday's close and then try to find some ideas for actionable actual actionable ideas for next week or the next few weeks after that so i'm going to download the data for may and june and then we're going to write all that data to a csv on the disk so i'll do data so this downloads this data closing data to a data frame for each symbol and we'll have a csv file for each of those symbols and so we'll save that to data sets slash and then we're going to leave a formatter and then we're going to use the symbol as the file name so i'll do two csv symbol and then we'll do dot format symbol and i'll run that again and we should see our data start downloading so i run that and you see how it's showing a progress bar and it's looping through all the symbols in the s p 500 and if i open my data sets directory you'll see data is flowing in and i'm going to let it run for a minute so while that's running we can go ahead and start on our squeeze dot pi to start implementing the algorithm here for the ttm squeeze indicator and so in this file i'm going to import os and pandas we're going to need and i'm also going to import this plotly and i'll wait on that since we're not going to do any graphing yet okay and i'm going to basically loop through all of the files in this data sets directory and then we're going to scan the price data for this ttm squeeze indicator so let's look at the pine script version of the indicator and translate it line by line and it's actually not that much code so if i scroll down here it says this is a derivative of john carter's ttm squeeze volatility indicator has discussed in his book mastering the trade chapter 11. black crosses on the midline show that the market just entered a squeeze bollinger bands are within the keltner channels this signif signifies low volatility and the market preparing itself for an explosive move up or down gray crosses signify the squeeze release which we just talked about so we need to calculate bollinger bands and we need to calculate keltner's channels so if you look down here at the implementation uh he stores a few variables that you can customize indicating the uh period for the bollinger band calculation and then the period for the kc the keltner channel calculation and there's this multiplier so these are really just variables that we need and then we also just need to calculate the bollinger bands so the upper bollinger band and the lower bollinger band and the upper keltner channel and the lower keltner channel and so let's look at the definition of bollinger bands and some of you probably know this already so so if we look at investopedia there's three lines there's a middle band which is just a simple moving average of our stock price we know how to calculate a simple moving average and then the upper and lower bands are typically two standard deviations plus or minus from the 20-day simple moving average right so all we need to do is find a standard deviation and a simple moving average and these functions are already built into pandas so we don't need ta lib or anything extra to do that and so all volunteer bands is are are these this envelope around the price this middle line is a moving average of that price and this top band is two standard deviations away from the moving average and then this bottom band is two standard deviations below and there's multiple ways of trading bollinger bands some people say when it hits the upper bollinger band that you're expecting a reverse into the mean so you're saying oh this is too many standard deviate this is too far away from the mean so we expect a mean reversal and so this is far far away from the moving average and we expect a return to the mean so you would short up here or let's say it crashes down below the moving average and gets extended further than usual away from the moving average you expect it to return to the moving average and so you buy the bottom bottom band here now the other way to approach this is uh saying that oh this went and pierced above the upper bollinger band and so that means it's strong momentum and you want to ride the upper bollinger bands so like right here um it hit the upper bollinger band and you could actually place a trade on this and say that indicates strong momentum and you're riding this this move right here along the top of the upper bollinger band so that's a description of the bollinger band calculation this is the visual and a couple of ways to trade that so let's just translate that description to python code in pandas if we go back to our snapshot here you'll see our snapshot is done and so we can start reading in all these files and calculating the upper and lower bollinger bands so what i'm going to do is loop through all these files that i have in data sets so i'll say for file name in os dot list directory so we're listing all the files in the data sets directory so i'll list the files in the datasets directory like that and let's print the file name let's make sure this works first and it's finding our files i run the program and it's finding all of my csv files which is great and so from the file name i can extract the symbol so i can do symbol equals filename dot split and i'll split it on the dot and then the first token of that will be the symbol so i'll just split it out and get the zeroth index of that list and we'll have the symbol by itself so i'll comment out the file name and we'll just print the symbol we're processing as we go and so now i'll use pandas and i'll read this csv file into a data frame so i'll do pandas dot read csv and i'll get data sets slash and then i'll use the file name i'm on so i'll read the file name i'm on into a data frame and if i print the data frame you should see price data in these pandas data frames so lots of price data printing out and you see we have data that stops at june 29th because we only downloaded may and june data one thing i'll also do is add a little bit of error handling because some people ran into some empty data frames on some of these symbols so we can do if df.empty we'll do continue and basically says if there's no data in the csv go to the next file and just keep going so we mentioned that the bollinger band here this middle line is based on just a simple moving average the 20-day simple moving average is a common default so let's calculate the 20-day simple moving average so to do that i'm going to say the data frames s20 sma so we'll give it a column name of 20 sma and we'll show what the 20 sma is on all of these and then we'll say we'll use the closing price so the headings are closed here so the pandas data frame will get the closing price and for each of these closing prices we'll have another column here over to the right that's the 20 sma and then going to be different it will have different values for uh the simple moving average as we go so we'll do df.close and then there's this function called rolling that we can use and we just give it a window size of 20 so that it's doing a rolling 20-day simple moving average and we'll do the mean so it's like a rolling mean so you know the the price the sma for this 40th row will be the mean of the previous 20 and then the sma for the 39th row here will be the mean of the previous 20 rows before that right very simple and so now when i print out my df again so i'll move my print df you should see that we have a simple moving average 20 column for all of these right and so let's let's show one symbol just for an example so i'll do symbols equals apple and i'll say uh if symbol in symbols print df so we'll just print one of them for apple just so that we can isolate one and look at it as we go all right and i won't print the symbol there all right so i'm gonna run that and i'll just print apple on its own right and you'll see the prices you'll see there's no 20 sma until 20 days pass right so it's not a number and then the first one here is 309 and that's the average of these 20 beforehand right and so the 40th here you see the average price was 346. easy right okay so we have a 20-day sma and then now we just need to calculate a standard deviation because the upper band is going to be two standard deviations above the simple moving average and then the lower bollinger band is just two standard deviations below uh the simple moving average and so let's calculate that standard deviation first so we'll say the data frames standard deviation is equal to the close and this is just going to be another rolling value window equals 20 and this will use the this function standard deviation right and if we do that and run it you'll see where we get a standard deviation and it looks like it's 13 here right 13 14 and so forth so this is uh this is what we're going to use to calculate those upper and lower bollinger bands and so now all we have to do is say that the lower band is the 20-day simple moving average moving average plus two times the standard deviation so we'll do plus two times our data frames standard deviation that we just calculated and then we'll say the upper band is equal to df 20 sma minus two times the standard deviation and then we'll have columns for the upper and lower bollinger band and that's very simple as well and if i run that again you'll see that this price was 346. if you add two times the standard deviation so that's like 27.5 you get this price of 373 and then if you subtract you know 27.5 ish from there you get this price of 318 321 and that's our upper band and our lower band and let's just graph that out to show what it looks like so let's import our plotly library and show that visually so we're going to use plotly candlestick charts in python it's very easy to create a candlestick chart using the plotly library and so it'll look like this and so if you look all we have to do is import plotly graph objects as go and then we want to read a csv which we've already done and we just need to pass it the date open high low and close which we have in a data frame so we give it the data frame and it'll create this nice candlestick chart for us so let's copy this code and show the figure so we'll say figure equals go figure data equals and then this is just a candlestick and then we need a date and it's called date already so it's actually working pretty conveniently already and but our symbols are called our columns are called open high low and close just like that so i'll do that and we need to just get apple we're looping through all of them right now so let's store the apple one separately so let's create a quick variable just to store apple the apple data frame on its own and chart it by itself just so that we can look at it so um if symbol and symbols so if we know the symbol is apple let's just say apple data frame equals df right and then so let's instead of df here i'll do apple df apple df all right let's run this and see if we get a nice chart so i'm going to chart that you see i have the chart of apple um so our chart looks good we have a candlestick chart we have this range slider which is a little funky so we're going to turn that range slider off and also you see we have these gaps where these gaps here right and that's actually the weekends we have no prices for that so we need a couple of settings to configure this how we want so we're going to turn off this weekend so that it compresses together nicely and also we need to add the bollinger bands to this chart so let me show you how to do that so first of all this data is actually a list of charts or figures to put on top of this chart so i'm going to take this first one which is the candlestick portion and just store it in its own variable and i'm just going to put this on one line so this just refers to the candlestick stick object here so i'm going to put this on its own line and then we're going to create one for the bollinger bands as well and then put that in its own object so candlestick equals go graph object candlestick and we're going to add that candlestick to the figure but we can also put additional figures in this list and so i'm going to create one for the upper and lower bollinger bands so i'm going to say upper band equals go.scatter so we need a scatter object and then we're going to do lower band equals go.scatter and we're going to append those to this figure some do upper band and lower band here right okay and then what what does this look like how do we use the scatter object i'm going to do x equals apple df date y equals apple df upper band and then i can give it a label if i want so i can say upper bollinger bands right do that and then actually i'm just going to copy this one since it's almost the same so do that and then for our lower band we're just going to do lower band equals that and then i'll use our lower band that we calculated and label it lower band and then let's see if we append it there and then also i'm at i'm configuring a couple options on this figure i'm going to say fig.layout.x axis dot type equals category and this is the magic this just type that in and that will get rid of the week the weekends where there's no data and then i can also do fig dot layout i'm not quite quite sure why that works but that's what i found so config.layout.x axis dot range slider we're going to turn off the range slider because it makes our behavior a little weird so i'm going to do that and now if i run it let's see what we get on our chart and you can see i named it lower and upper band so there's an error there i'm going to change it to include the underscore try it one more time and we run it so you see we don't have any bollinger bands for the first 20 days here the first 20 candles but then you start seeing these bands and then we have the upper band and it looks like my labels are misconfigured here so i'm going to do this one more time i'm going to say that the lower band okay so i did the lower band first the lower band is minus standard two standard deviations and the upper band is plus two standard deviations which is good and then i'm also going to add some color here so i can say line equals and i give it a dictionary and we can say color red and i'll do that twice so we're going to put these in red okay if i do that run it we get our chart again and you see that these are the bollinger bands for apple stock so you can see in this case apple never reverted to the mean and so this was a case of a stock with very strong upward momentum and it did not pay to bet against apple or short apple thinking it would revert back to the moving average so that's it for the bollinger bands part now we just have to do the keltner channel part so i'm going to actually download a little larger of a data set because i would like to show the band you know a little bit more visuals of these bands here so i'm going to run the snapshot program again and go all the way back to january so i'm running that now and i'm going to let that download in the background and so we'll go back to squeeze.pi and let's look at the calculation for the keltner channels now so i'll go back to this description so under our squeeze momentum pine script here we have this upper keltner channel and lower keltner channel a calculation it looks like it just used uses a moving average plus a range m a times a so so if you look here it looks like it calculate so if you look here he calculates the upper and lower keltner channels and it looks like it's just another moving average plus the sma of the true range so it uses the atr and then it just uses another multiplier so this is actually a very easy calculation as well so again let's look at the definition of keltner channels so we'll google that up so i'll use this version from stockcharts.com and then here's the calculation so it says the middle middle line is the 20-day exponential moving average in the other version i believe he's using just the simple moving average so he's using the sma here some people use the ema we'll just translate using the simple moving average since that's what lazy bear is using and we're trying to translate that so we need a middle line that's a moving average the upper channel line is the 20-day ema plus two times the atr 10 and the lower channel line is the 20 day ema minus two times the atr 10 right and so there's those atr lines and so this multiplier he makes variable in the script and so if you look back in trading view here he makes these mult these multipliers configurable so the default is 1.5 in his case this article uses two as a default so you can kind of decide what your settings here same way if you're doing an indicator like rsi or macd you know you have some customization of the moving average you're using the period you're using and what level indicates overbought and oversold and so forth so here they use an ema with two times the atr 10 and then here uh lazy bear is using 1.5 as the default setting but yeah the basic idea is instead of the moving average plus or minus two standard deviations you're using plus or minus two times the atr so let's do that calculation so let's go ahead and get this pine script code for the keltner channels and let's translate that into python so i'll copy this here and then we'll just get rid of it as we go so we just need a simple moving average of the source which is the close and the length of the keltner channel which we're using 20. so we already have the 20 day sma here and he's using the 20 sma for the keltner channels as well so we already have that column right the the range we're just saying is the high minus the low and so i can do that so i'm going to say the df's true range is equal to the absolute value of the data frames high minus the low so we're subtracting the high and the low and whatever the absolute value of that so if it's negative five it'll just be five right so we're getting that range so i'll do high minus low for the data frame and then i'll calculate the average to range the atr and i'll say that's equal to the true range and that's just a rolling mean again and so i'll just say window equals 20 and then calculate this mean and that'll be the atr and so the upper keltner channel is just the moving average portion this 20 sma so we'll just say that's the df 20 sma plus range ma times the keltner channel multiplier and he's using 1.5 as the default so we'll use 1.5 as the default and this range ma is what we're calling the atr this true range mean right here and so i'm going to say the df atr times 1.5 and times 1.5 there so we're going to have the upper channel is the 20 sma plus 1.5 times the atr and the lower keltner channel is the 20 sma minus 1.5 times that atr and then so i'm going to store this also in our data frame and we'll call it the lower keltner and then we'll call this one the upper keltner right so we'll do that and the same way we added the upper and lower band here we're just going to add this to the figure so we'll do upper keltner the lower keltner and swap these out keltner channel we'll put these in a different color so i'll do blue and then lower keltner channel upper band lower band so that'll be upper keltner here and lower keltner there okay so we should have lines for the upper and lower keltner channels upper and lower bollinger bands and we're adding these to our figure so i'm scanning through calculating these upper and lower bands these upper and lower keltners and i'm going to run it and show what it looks like on apple stock here and so you can see this is for the whole year now because i downloaded a newer data set we have our blue lines which are upper and lower keltner channels and our red lines which are upper and lower bollinger bands and you can see in the case of apple stock this indicator never actually triggered there wasn't this period of low volatility apple went straight down in march and then bounced back in a v-shaped reversal coming off of the covet crash there so let's look for another stock where we can show this actually happening so to do that let's actually just finish out our scanner so i'm going to hide i'm going to comment out this chart for a minute and let's see if we can finish up our scanner here so i'll comment this part and this part out so we're already doing our calculations for our upper and lower bollinger bands our upper and lower keltner channels and so let's define a little inner function here called in squeeze so i'll define a function called in squeeze and give it a data frame and when is it in the squeeze we said whenever the bollinger band the upper and lower bollinger bands are within completely contained within the keltner channel so the upper bollinger band needs to be less than the upper keltner channel and the lower bollinger band needs to be greater than the lower keltner channel so in squeeze i'm going to return true if the lower band so the lower band bollinger band is greater than the lower keltner channel df keltner lower and the upper band is less than the upper keltner channel right it's contained between so less than the keltner upper keltner all right and i'll call that lower keltner then we know we're in the squeeze right so i'll use pandas to apply this function to a new column so i'm going to do data frame i'm going to create a new column called squeeze on and i'll do df.apply and basically i can apply this function to other columns in the data frame so i'll do df.apply in squeeze which will actually call this function and look at the upper and lower bollinger bands and the upper and lower keltner channels and i'll return a true or false value and i do axis equals one and you'll see what this does so i'm going to run this and let's see if the last row of some of these are in a squeeze on position so i'll say if df dot iloke we can do we can use the last function or the tail function but i'm going to use i loc and i can do negative 1 and this will get us the last row of the data frame so the very last day and so we'll look at the last row using negative indexing and then squeeze on so we'll access this new squeeze on column so if the last row is squeeze on we can print that some symbol is in the squeeze so i'll format that and put the symbol so i run that to see who's in the squeeze right now and i should be getting some results but i'm not so that must mean i have a bug so i'm gonna look back and see this condition was never true apparently so lower band it looks like i made the same mistake here where i reversed my signs again so the lower keltner is actually minus and i'm not sure why i keep doing that so the upper this is minus the atr times 1.50 the lower and then the upper is the plus right so if i run this again let's see if we get some values so squeeze is on for a variety of stocks and let's see if any of these jump out at us so i saw targets i think i saw visa to start out with so let's look at visa and i think i saw amd and so i saw visa and amd so let's let's look at those that were in the squeeze for june 30th and so if i look at a couple of charts here or let's let's use our graph here so we'll say um let's let's get the amd chart so we'll say if symbol equals equals amd then we'll just store it in that data frame i know it's called apple but whatever i don't want to reconfigure this so i'm going to do that and let's just isolate amd to see it in the squeeze on june 30th so i'm going to run this and we'll run visa maybe a couple more okay so i run that and that should be the amd chart and so if you look here it's around 50 to 55 here it looks like there's a period of high volatility here the red is the bollinger bands and you can see they're way outside of the keltner channels during this period of volatility but then you can see they start to squeeze inwards and you see these bollinger bands these red lines are now inside of these blue lands so we're calling this the period of low volatility and that there's going to be eventually a point where this breaks out of this low volatility range and then indeed i believe if you look later on let's pull up the chart of amd after june 30th right so if i look at amd and pull that chart up you'll see if you look at june 30th here right and this is the same chart here to verify that our logic is good that it starts squeezing here um in june so this there's this period in june here of low volatility where our bollinger bands are come inside of this uh keltner channel and then you can see the volatility starts to expand these bollinger bands come out of that contraction and then you see amd pops from 53 to 85 like very quickly right and so we capture it coming out of this low volatility to a period of high volatility right and so yeah this successfully scanned and it looks like it stayed um it it stayed in the channel a little bit longer and you'll notice it actually was not very volatile for another week or two after that so it didn't really get any of this strong momentum on this histogram here until uh about a couple weeks later and that's when the real move happened but you see how it's in this narrow range and then it breaks out so it looks like our indicator is working pretty well and if we tried the same thing let's see for visa let's see what's going on there so we can run this for visa and see that it's in the squeeze on june 30th and let's see what that looks like so you see again visa bollinger bands outside of keltner channels and then they start to consult it starts to consolidate right here so you see these red lines inside right here uh for visa and if i looked at that for example you can see uh june 30th for and you see where it's still actually in this narrow range so eventually this is going to get more volatile and have some type of breakout so instead of showing something that's still in a squeeze i want to find something that wasn't a squeeze a couple of days ago and then is coming out of the squeeze because that's the trade i actually want to make so let's adjust this instead of just finding what is squeeze on we can see what was in squeeze on a couple days ago let's say the third three days ago or two days ago and then the last row let's say and not squeeze on so let's get the squeeze on we want to find something that would squeeze on a few days ago and then not squeeze on the last day so not squeeze on right and so we'll filter that down that should show less results oh and i'll change this text to say is coming out of the squeeze right and i'll run that and i'll do yesterday and today so run that find something that was squeeze on and then is no longer squeeze on so looks like mco and rol and i'll ignore that error because i commented out that data frame that's fine so let's look at mco so if we look at this mco here the smoothies corp there's june 30th you can see that it was in the squeeze here and then it was starting to come out right and then also let's compare i actually put in the pine script code for lazy bear script and you can see that our python function is functioning fairly similarly because these black crosses here are at this june 30th range and then it becomes gray here and it's coming out of the squeeze right and so and then let's do another example let's see if we can find one that really moved uh rol here you can see it was in the squeeze at the end of june here so at the bottom you see we have the black crosses and we also have this narrow consolidation and then you see that it comes out of the squeeze and this one actually had a nice move from uh 40 low 40s to mid 50s here so this was a good run here so it's upward trending there was this consolidation period and then it continued moving up so another good example right there so let's fast forward to the present day and let's see if we can use this to find some good ideas for next week so i'm going to get another snapshot here going all the way until 8 21 or 8 22 so i'll get the august data as well and let's run this so i let that run and now i have the data up until friday or yesterday and i'm looking i'm actually looking to allocate some money for next week so let's see if we find any ideas and if i like any of them i'm going to make some buys on monday so let's give this a shot i'm going to run the squeeze.pi again and see what comes up and it looks like it's flagging quite a few of them coming out of consolidation and that kind of makes sense i believe based on how the spy has been acting recently and let's see if any of these names jump out at us looks like uh google johnson and johnson are pretty big names so yeah let's see uh what google is doing so if i let's use our own chart for a minute so let's let this run and i'll say if symbol equals google let's chart what google looks like i'm gonna have to let this run again so let this figure generate real quick we'll look at google and then we'll look at a few more symbols that we recognize and so yeah you can see google here actually looks pretty good to me you see it had this nice upper trend coming off the bottom in march and then recently the uh the bar the bollinger bands here are squeezing here and it looks like it's just now coming out of that and so we're expecting a big move and so i actually like google and i might buy that here so the all-time high looks like it was just exceeded and then we have this narrow range after an uptrend so google looks like it's going to go to more new all-time highs so that looks like a good one if we look here we also have what else do we have i think i saw visa again so let's look at visa so we'll pull up visa so if i look at visa here i'll look at it and you'll see and that one looks really good as well i'm interested and i will buy visa had its all-time high in february fell off up trended and it's been and it's still consolidating this came up in the uh it this came up i think a month ago whenever we were showing this scan uh previously so it's still consolidating and it's just now coming out and so i would actually buy visa here expecting it to move to all-time high so visa and google i think would be good candidates to buy next week and let's also look i believe johnson and johnson is probably one to look at so so if we look here at johnson and johnson v shape but then a tight consolidation here and looks like those green candles it's just now breaking out again and so i would say johnson johnson looks like it is or is about to go to all-time highs and surpass it probably due to vaccine news and other things like that but also they have a whole ton of products so johnson johnson probably a good stock to buy as well and i believe i saw walgreens which seems kind of weird i probably wouldn't buy walgreens so let's see why it's in there so i'll see wba walgreens boot alliance now this one is actually a good example of the opposite so this could be used for going long or short and so looks like walgreens actually has been down trending right and then consolidated here at the end and you see this histogram here the bar is actually red indicating downward downward momentum so this one is a case of a stock that's consolidating at the lows in a downtrend and looks like it's about to break lower so if you're into shorting stocks walgreens could be a good example of a stock to short and so yeah that seems pretty good twitter i haven't had good luck with that i've got a hard time about that because i expect that i keep expecting that to be a stock that eventually runs to all-time highs but it's been stuck there in the 20s and 30s for years now ebay let's look at ebay that came up in our scan and ebay you know i haven't used ebay in a long time but actually it looks really good a really sharp uptrend here and then it's had this period of low volatility and it looks like it's busting out again and so ebay actually looks like it's going to go higher so we'll see what happens so let's write down those symbols and let's see if there's any merit to this ttm squeeze indicator so let's think about ebay google visa johnson and johnson and yeah any any other names on this list that you're interested in looking at you can try out this scanner and i think this is working pretty well and i don't really give individual stock picking advice on this channel i mostly talk about programming but i'm looking to invest some money and i'll probably actually buy i would buy those three stocks google johnson johnson and visa myself and i probably actually will allocate some money to uh those individual symbols uh they're you know there's those are solid large cap companies so i don't view that as a risky investment and also uh they look like they're reaching new all-time highs so that's a good sign and also the s p 500 just closed at an all-time high as well so i'm personally looking to put money on the long side because i like 52-week highs and then it gives me an easy place to place a stop loss if stocks begin to break down again i have an easy place to cut my losses so that's that's what i have for the ttm squeeze indicator we learned a good amount about pine script about the visual representation of the ttm squeeze indicator we learn to look at the bollinger bands and how to calculate those in python using pandas and also how to calculate the upper and lower keltner channels as well and how those are used individually but also how they're used as a whole in this ttm squeeze indicator to detect periods of low volatility and detect when stock is breaking out of this low volatility into a period of high volatility and expecting continued momentum or a continued uptrend so that's it i hope you learned a little bit more about the squeeze indicator leave a comment below if you have any suggestions this is the first time i've ever seen it or implemented it so let me know if there's any ways to improve upon this approach here or if i missed anything and i'll try to address it in a follow-up video i'm going to push the source code up here to github.com slash hacking the markets you can download it play around with it submit a pull request if you want to contribute some improvements and also leave a couple links if you want to sign up for trading view or binance or any of these other services that i've talked about it helps support the channel um that's it for now like and subscribe follow me on twitter all that good stuff and i'll see you around in the next video thanks a lot
Info
Channel: Part Time Larry
Views: 19,221
Rating: 4.9827337 out of 5
Keywords:
Id: YhkNoOqYp9A
Channel Id: undefined
Length: 53min 30sec (3210 seconds)
Published: Sun Aug 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.