Finding Breakout Candidates with Python and Pandas

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back in this video i'm going to show you how to programmatically find stocks that are poised to break out now there are many different setups you can use to find stocks that are going to break out in this particular video i want to focus on stocks that were in an uptrend and then are consolidating in a very narrow range and so we'll often see as a stock is in in an uptrend and then there there'll be a few week period when that stock really trades within a very narrow range of a few percent and it trades in that range for a few weeks and then once it finally exits that range to the upside it takes off in the same direction as the uptrend before so you can often capture a really powerful move if you're able to purchase the stock right when it's exiting this tight consolidation and to illustrate what i mean i have a few charts and articles pulled up here that i'll show you real quick um the first is the the investopedia article and it talks about this exact setup says a tight consolidation is when a stock moves sideways in the narrow range eventually either the buyers or sellers went out and the price breaks from consolidation into a trending move and so the example shown here it shows this example of allstate where there's an uptrend tight consolidation and then once it breaks the consolidation there's a powerful uptrend continuation and then it consolidates again and the reason i was thinking about this setup and wanting to do a video on it is because uh i was actually looking at bitcoin data a few weeks ago and was noticing this exact setup and it actually came to be so if you look at the chart for bitcoin here you'll notice that bitcoin crashed with everything else and then it up trended for a while in a recovery and then you'll notice here throughout the entire month of may throughout the entire month of june entire month of july it just got tighter and tighter in this range and then i was watching it here where it was just uh just right around 9000 to 9100 or 9200 right it was in this very narrow range and i could just tell once it broke out of that range that it was going to explode and sure enough if you look what happened once it exited this range you had this giant candlestick here and we got our what a roughly 30 percent move from 9000 uh to i believe it traded over 12 000 per uh bitcoin so that was a great in a great example recently of when a uh an instrument was tightly consolidating consolidating and then if you uh placed a trade right when it broke out uh you would have got a nice gain there another example i was looking at was recently the russell 2000 and this was about a week ago i think and yeah i was watching the russell 2000 it was very range bound for a while if you zoom in here you can see for the last a few weeks couple weeks of july it was in a very narrow range here and then i was wondering whether the russell 2000 was going to eventually participate as much in the rally as for instance the qqq or the s p 500 and then it actually outperformed the last week or two and you'll see when it broke out it's consolidation you had some pretty large gains here so uh it broke out above uh 149 here and it quickly ran to 156 so there was a nice quick gain there in the russell 2000 and so uh what i want to do now is show you how to actually programmatically uh find these neural consolidations uh using pandas data frames and we're going to piggyback off of our last video where we uh captured candlestick data for all of the stocks in the s p 500 and we're going to continue on our scanner and scan through all those stocks to find stocks that are either one either they're tightly consolidating now so they're in a narrow range or two they are breaking out so they were in a tightly consolidating uh uh trading in a narrow consolidating pattern and then the most recent candlestick broke out so we want to find stocks that are won poised for a breakout and two stocks that are currently breaking out if we anticipate we get a little better entry but also you know we don't have that confirmation or if we wait till it's actually breaking breaking out uh then we have that confirmation but we have to pay a little bit more uh for that privilege so uh let's let's dive into the code and let me show you how to do that from the command line using the exact same data sets that we already captured in our previous video so what i'm going to do here is clone the repository that we created previously so if you have been following along with the last lesson we created this repository and we wrote the code under hacking the market so github.com hacking the markets slash candlestick stick screener and what i'm going to do is clone this to my desktop and so if you want to start from here you can clone this and see what we're doing so i'm on my command line right and let me remove any remnants i have of that all right and so i'm going to clone it fresh so i'm going to do git clone and i'm going to clone that repository and get all of that source code right and then i'm going to get a new window going here and i'm going to open our candlestick screener so i'm going to open that up and let's just run it real quick so this is right out of the repository i have my application and i have like my pattern detection function and so i i showed you how to scan the s p 500 for a different candlestick pattern so i'm going to run that real quick right and that's how we did it from the command line and then we have this flask application in app.pi and i can run that with flask run and that'll run a little local web server and then we built this little application here so i go to local host 5000 and i can scan for engulfing patterns for instance right and so i scan that it goes through all of the different csv files we had for that we retrieved from yahoo finance and we scan for bullish engulfing patterns and we pull up the charts and find all the charts that match that signal all right so this is using data sets under data sets daily and i've actually committed all these data sets to the repository so you can just follow along starting here and so what we have even though it's uh it's a week into august right now so these data sets right now are for january through july so this ends on july 31st so there's actually been a week of trading after july 31st uh but that's okay we're going to apply it to this july data and then so this that'll let us look at the current week so we don't know what let's pretend i don't really know what happened last week i can see how well my scanner would have worked on this data and then see if it successfully detected any uh any breakouts for the following week so let's pretend uh this is the end of july and we're looking for breakout candidates for the first week of august right and then after we're done with this we can actually update these snapshots for the first week of august and let's scan for breakouts for next week right so this is going to serve as a good uh test of what would happen if we use the system um at the end of july right right so cool i got my data sets uh let's start writing some code to make this happen so what i'm going to do here is i'm going to stop this web app so we're going to first do this from the command line just to scan and filter and then once we can do it from the command line we can just make those just reusable functions and then we can import those reusable functions into our flask web app and then use that as part of our web-based technical screener so uh first things first let's see if we can get this working from the command line so i'm gonna create a new uh a new python file and i'm going to call this chartlib dot pi and the reason i'm calling it chartlib i'm going to run it from the command line and test it but then later on we can import the functions in this file and use them on the web okay so the first thing i'm going to do is i'm going to borrow from app.pi for now and what i want to do is just read in data frames for all of these s p 500 symbols and since we're already doing this in the index file here i just want to copy a couple lines of code from here and so it's going to be these two lines of code where we loop through all the files in the data set directory and then read them into data frames so i'm going to paste this in here and i'm going to indent it okay so we have four file name and os dot list directory so we got to import os since we're using the os library and then we're going to say data frame equals pandas read csv so we're reading through all the csv files in this daily directory and we're reading them into a pandas data frame so since we're using pandas we need to import pandas right we'll import the pandas package there and we'll have these data frames and i'll just print df as we're looping through and i'm going to click this play on chartlab.pi and run it and you'll see it's printing out a bunch of data frames on my screen probably 500 of them right because we're scanning through the s p 500 right and so you see we have the date open hi low close uh the adjusted close and the volume of each of those symbols and this is 147 days of trading so from the first trading day january 2nd of the year through july 31st where our data set ends we have all this ohlc data for these symbols right so what do we want to do well we want to find uh stocks where it the stocks have recently been trading in a narrow range and so what that means is all of the closes we're gonna use the closing price and we want the closing prices to be relatively close to each other for an entire uh period for a long period of time let's say a couple of weeks right and so let's find um stocks where the closing price is uh in a narrow range for the past couple weeks so let's write a function to do that so i'm going to write a function called is consolidating and basically what we want to do to do is accept a data frame of ohlc data and look at the close for some parameter for 14 days for instance and uh find tell us whether or not it's been trading in a narrow range and return true or false so let's write that function so i'm going to define a new function and i'll call it is consolidating right it's going to take a pandas data frame as a parameter to start and we'll make this more customized as we go all right so it takes a pandas data frame okay and so what do we want to know about that data frame so let's think about how this would logically work um let's say we have a stock that's closing in a narrow range let's say it's trading between 98 and 100 right or it could be a 300 and 295 for instance it's just trading in that range so we want the closes to all be within a few points of each other now if it's a 400 stock you know it can be a few more points so what we want to do is use a percentage so let's say two or three percent right so we don't want to be so narrow that we can't find anything we want a couple percent i think is a good number so if there's a hundred dollar stock that's been trading between 98 and 100 that's a pretty good narrow range we can look at or 97 100. if it's 400 stock let's say between four 390 and 400 right and so let's find a stocks that have been closing within a couple percentage range so for our is consolidating function right we're getting a data frame of all of the closes we're really only interested in the most recent closes so let's say the last uh 14 days right or let's say since it's three weeks three weeks it closes let's use 15 days right and so uh we can use uh recent closes so let's get the recent closes from our data frame the last 15 data points and we have this array indexing that we use on a pandas data frame right so if we want the last 15 records from this data frame here right we could use this negative indexing so we can go negative 15 right to the end right so this will start at 15 records ago or 15 rows ago till until the last row right and so if i do recent closes and print that let's just print it right and that or let's just do the last five closes just to show you what this looks like right and then we'll call our our function so i'll type uh is consolidating df so we're looping through them we're pressing passing the data frame to our function and we're going to print the most five recent closes so if i run that right you'll see as it runs right we have the most five recent closes the the 27th through the 31st so that's the last week of closes right and so let's say we want the last 15 we'll do 15 right so we run that and this will show from what looks like july 13th through july 31st it excludes weekends because the stock market is not open so we have a way to isolate our last 15 closes now right and so the next thing we want to do is check if these closes are in a narrow range if it's been oscillating around a lot we don't care about it we want narrow a narrow range of closes here and so let's let's do this um so i'm going to call this recent candlesticks because this isn't actually the recent closes these this is the most recent candlesticks right so a good way to determine whether the closes are close to each other is we can get the maximum and minimum close value from this series right and we can use pandas max and min functions to do that so i can do max close equals recent candlesticks and then since the column is called close i'll type close as the key here and then i'll type dot max and then i can do min close equals recent candlesticks close dot min right so let's print out the max and min close so print uh the max close was and the min close was right and then i can just do dot format dot format and then i'll pass it max close and min close right so do that run it again right and so for this particular stock the max closed was 115 and then closed was 107. so that those are a bit too far apart in our few percent range that we were interested in so i'm gonna use uh let's start with a two percent range right so let's think about this if the maximum close for the stock was 100 let's say we want the minimum close to be 98 right and so let's check if the minimum close is greater than the max close times 0.98 right and so if we're interested in stocks that have traded in a 2 range right if the maximum close was 100 then the minimum close must be at least greater than 98 right to be within 2 because if it was like 96 right it'd be four percent away right so we want all of our closes to be between a min and max of 98 to 100 right so we'll just say if the min close is greater than max close times 0.98 then we'll say print is consolidating right and so let's do that so i'll run it again and instead of saying is consolidating there i want to return true and so i only want to print the data frames here that we're scanning through if uh it is consolidating is true right and so let's do that so i'm going to delete that line i'm going to delete the recent candlesticks and this should be our full function right so we're going to loop through all the csv files read them into a data frame and say if it's consolidating for that data frame then print is consolidating right and then if you look at the print the brackets there let's just put the file name so i'll do dot format file name and let's just print the ones where it's consolidating in a two percent range right if i scan it you'll see that costco is currently consolidating that's the only one that came up in that two percent range for the last uh 15 candles uh in july so let's pull up costco let's see what happened so if i go to trading view for instance so if we look here at the end of july you see indeed costco was consolidating in this narrow range here right and the closest were all in this a couple percent range here right until july 31st that's july 31st and then actually look what happened it broke out of that range right afterwards so if we look at the following week of data that's not part of our data set you'll see that costco indeed had a pretty explosive move out of this tight consolidation here and gained you know a few more a few more percent so that would have been a pretty good trade and if you look at the setup for costco here had a nice uptrend and it had this narrow consolidation and then busted out again upwards so costco from here at least it looks like our scanner this simple formula found a pretty good candidate for a continued uptrend and this is confirmed by the fact that we had the july data we scanned for a narrow consolidation and then we actually had confirmation the following week that wasn't part of our data set and you see it continued upward right but it's possible it would have continued consolidating for longer or you know maybe it didn't break out at all and for some reason it gapped down but in this particular case it was a pretty good breakout candidate now uh that was a pretty narrow range and we only found one stock let's see if we can expand our parameters a little bit to find a few more examples so i'm going to go beyond two percent let's say 2.5 percent and so for this threshold here right let's make this adjustable in our function and so let's have a percentage as a parameter and let's make that percentage adjustable so we'll have the default be 98 percent like that or let's have the default percentage be two let's use the number two and so to calculate this dynamically based on a parallel parameter let's say uh threshold equals percentage divided by 100 so 2 divided by 100 would be 0.02 right and we'll do one minus that percentage and that'll get us our number 0.98 dynamically right so we'll call that threshold and we run it we should get the same number right and so now we can make uh use this in our library and let's say we want to pass a specific a different percentage when we call the function you know from our chart right if we wanted to have a little input input box where the user could specify the percentage we could let them specify 2.5 now and that 2.5 would override this too and if i run it now let's see if we find any more candidates all right so you see we got another one we got this walmart consolidating so let's look at walmart what happened so a walmart was a similar setup so it looks like walmart was in a pretty narrow range here but it doesn't look like it's quite broke broken out yet it's just in this narrow range here still right it just kept consolidating so i don't think walmart so that'd be an example of one that's still consolidating and then let's look at cpb right oh and that one is really narrow right now right and it's still consolidating for another week so that looks like a fairly good candidate for a potential breakout maybe next week and if we zoom this out further yeah you can see yeah campbell's soup get in on that campbell soup trade uh we have this uptrend here and then it looks like it has this triangle here right and it's gotten uh tighter and tighter here and so eventually uh we think this is gonna have an explosive move and then when we're anticipating this breakout right uh we can get some more um we can focus in on this and use some of our other techniques to look at real-time data like starting next week and be really watching the stock closely and place our trade automatically whenever it breaks out of this range right so these were examples of stocks where we didn't necessarily have any confirmation right you saw campbell soup still consolidating walmart still consolidating it looks like costco actually did break out but let's see if we can find some additional stocks that are were already breaking out on the very last candlestick but were consolidating before so let's make another function called is breaking out so i'm going to do uh define a new function called is breaking out right and this will also accept a data frame and then let's have this have a percentage that's customizable as well right and so if we do that what do we want to do here right we want to check the previous 15 days but exclude the very last candlestick so we're gonna use our indexing again on our pandas data frame but we're gonna go all the way up to the pre next to last candlestick so how do we do that right so we want to get the last close right so let's get the last closed first right so the last close of our data frame is going to be negative one until the end that's the last row right so it's the last row and then till the end so this is actually only one record right and then we want the close and then with pandas we have to do this dot values at zero just to get uh the value only and so this will give us the last close so let's let's see what that looks like so i'm just going to print that i just print these along the way to make sure we're going okay so let's go ahead and call our is breaking out function so we'll just call is breaking out and we'll pass in our data frame and it should print the last close so we'll see one number a bunch of times and we just see a bunch of last closes getting printed to our screen so what we want to see if we want to see if the last close is actually breaking out we want to detect if all the candlesticks before that last close were in a tight consolidation and that last close went outside of that consolidation or it went above the maximum of the consolidating days before all right so how do we check if the last 15 candlesticks before the last close were consolidating well we already have the is consolidating function that accepts a data frame right so let's just reuse that function and pass it a data frame so we'll say if is consolidating right data frame and we don't want to pass it the last candlestick so let's pass it a data frame that's a subset of the entire data frame so we want to pass it the data frame all the way up to the last candlestick so we'll use indexing where we start at the beginning colon till negative one and so this will start at the beginning and go to up to but exclude the last candlestick right so if we know it's consolidating the next thing we want to know is if it's breaking out so we want to check if the last candlestick is greater than the maximum of the consolidation period so the consolidation period are the recent closes so let's create a new a variable called recent closes and this will be a subset of the data frame as well and this will be from negative 16 to negative one right and so this will be the previous 15 and then this will be this last close it's the very last one and we'll make this negative 16 and negative 15. we'll add parameters to make that more dynamic as well since we want to be able to adjust the number of days right so i'm hard coding that now but we'll we'll update that and make it a little bit better so that'll be our recent closes and so we want to check if the last close is greater than the recent closes close dot max right so that's the maximum of the prior 15 days when it was in consolidation we know it's consolidating because our ins consolidating or is consolidating function said true right and we'll also put a return false here so that the cases where uh none of this was true uh then we know it's not consolidating so if it returned true and the min close was greater than that threshold then we know we're consolidating and then we want to check if the last close is clear that greater than the maximum of the recent closes that were in the consolidation period and if that's the case we can return true and if none of that happened then we can return false at the end so this will return true uh first if we hit this condition otherwise it's not breaking out at all and so now we in our main loop here where we're looping through all the files we'll say if it's consulting and then we'll also do an if is if is breaking out and then we'll do percentage or we don't need a percentage here it'll be the default and then we'll say print is breaking out format file name so i'm going to run this again let's see what happens we still have just consolidating and the reason for that is uh we still have percentage as just two here um so the default is two so what i want to do here is set a percentage default on is breaking out to 2.5 and our 2.5 is the one that actually found more candidates so since i have is breaking out calling is consolidating i'm going to set percentage equal percentage and so let's try it with 2.5 and see what we get okay so that actually got us some value so you see we have costco's consolidating uh campbell's consolidating we have these two new ones clx which is clorox and then we have a tmus which i believe is t-mobile and so let's look at what happened there so we have clx is breaking out so if we look there uh you see clorox which has really taken off this year i guess with the virus and all of that uh so we have in july towards the end of july um it was consolidating for a long time and then it looks like here uh right here is where it actually breaks out so july 30 or july 31st right here we got that huge candle uh it did a quick pullback and then continued breaking out so our code has detected that clorox is recently as of july 31st is broken out of a tight consolidation and so this looks like a good candidate to continue this uptrend here and if we look at the other one we have uh t-mobile right we pull that up and we look at the end of july indeed it was tightly consolidating here and then july 31st the last candle we looked at right it had this large green candle here and it broke out of this consolidation and if we look what happened the following week uh looks like that did continue and then there was another pop looks like uh friday friday had this huge uh up candle here and our code has detected uh this breakout and look sure enough that led to some news probably so if we look up t-mobile i don't know what happened so t-mobile august 7th let's see if there's any news on t-mobile so if we check a t-mobile stock on uh yeah so you see t-mobile had this big pop why so let's see what happened right shares of the wireless provider search by more than eight percent to a new 52-week high following better than expected quarterly earnings so t-mobile earned nine cents revenue compared with estimates right and so this happened but a week prior to that we detected that it was exiting that consolidation for whatever reason so using the technicals we were able to detect that something was happening right under the surface and we would be able to make that entry and then sure enough we would have gained a great profit the earnings report was good uh this wouldn't always happen obviously but looked like this was a good case where we detected the stock was breaking out and we're able to place a trade and we're able to capture a quick move there so we could have bought a call option for instance as soon as it was breaking out of this range uh so yeah um i thought i think that's a pretty good lesson right we looked through all the different uh stocks in the s p 500 we loaded them into panos data frames and we used this indexing on the data frames to analyze particular ranges of candlesticks inside of this data frame so we did this on our data up through the end of july and now that's the end of the first week of august what we could do is in our app.pi right we could click a button update our snapshot for the latest week and we could scan for consolidation and breakouts for the following week and start all over again so that's it for this video i just wanted to focus on that one particular setup stay tuned for future videos and if you like what you see subscribe like the video participate in the forum follow me on twitter all that stuff so see you next time
Info
Channel: Part Time Larry
Views: 29,262
Rating: 4.986711 out of 5
Keywords: python, breakouts, stocks, trading, robinhood, alpaca, algorithmic trading, automated trading, consolidation, technical analysis
Id: exGuyBnhN_8
Channel Id: undefined
Length: 29min 32sec (1772 seconds)
Published: Sun Aug 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.