Candlestick Pattern Recognition with Python and TA-Lib

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back today we're going to be discussing japanese candlestick charting techniques now chances are if you're watching this channel you already have some familiarity with candlestick charts you've probably traded stocks or crypto and you're looking at candlestick charts all the time in your trading platform but what you might not have done before is write programs in order to detect uh candlestick patterns so um if you're unfamiliar with this book or these different charting techniques this book describes a variety of candlestick patterns that you can look at in the price action in order to determine whether there might be a price reversal or a continuation and they often have interesting names like the dragonfly doji candle or the bullish engulfing candle which we've discussed in this channel before and so there's all these different patterns in the price action that have been documented in this book now as if you've heard about this book before you know that this guy steve nissan who wrote this book originally traveled to japan i believe in the 80s and at the time i believe in the united states people were primarily using bar charts but when he went over to japan he discovered that these different candlestick charting techniques had been developed in the past and i have the wikipedia article here on my screen and you can see this guy uh was apparently a i don't know how to pronounce his name but he was a rice trader and he started studied um price patterns in the rice market and was actually able to uh profit a lot uh from his own analysis and he eventually even wrote a book on market psychology in the 18th century and he even said quotes like when all are bearish there's cause for prices to rise which sounds very very familiar to some of the sayings we have like buying with uh when others are fearful so uh the japanese actually um studied a lot of this this price action uh hundreds of years ago and uh this guy studied uh in japan and brought a lot of those findings back to america and wrote this book and i bookmarked a couple pages here that we'll use as a launching point for our python programs so i bookmarked the section on the engulfing pattern and the star patterns like the morning star and i took a couple of pictures with my phone and i'll bring him up on the screen and you see um he described the engulfing pattern here so you see there's a down trend here and then there's this bearish candle and then following that there's a strong bullish candle that completely engulfs the previous candle and then also i uh i took a picture of this morningstar pattern that is on one of the pages in this book and you see how there's a downtrend here uh there's a long can bearish candle and then there's a smaller bearish candle but then there's this huge uh bullish candle and that indicates a reversal um from that downtrend and then it breaks out of this channel and then that downtrend is over so oftentimes as traders we're trying to detect inflections in the price action so that we can determine when there's a bottom because that's when the lowest price is right and we're trying to maximize our gains rather than just holding through a downtrend to wait for it to come back up right and so if you look at a chart for instance of the s p 500 as we usually do you can see there was a giant crash this year you know the s p 500 was down 35 maybe almost 40 percent here and obviously um if you held it this whole time you know you're still down but if you were somehow to detect a bottom here or a reversal you would know you would be able to find a good entry point so what we're going to do is get some historical data for the s p 500 from yahoo finance we're going to put it in a pandas data frame and then we're going to use talib a technical analysis library in order to scan this data and detect some of these patterns and so ta lib is actually a python package that has some of the built-in functionality for detecting candlestick patterns and so i'm going to show you how to use that today and we're going to look at the s p 500 and try to get a better understanding of these candlestick patterns and how we could use them in order to find actionable ideas for our trades programmatically and the reason we're doing it programmatically is because it saves us a lot of time you'll see still see a lot of people out there on twitter or different blog posts and things of people who just sit there all day and they reserve hours and hours of time to go through all these charts in order to in order to define these patterns but if we're able to mathematically describe these patterns you know based on open high low enclosed price data and numbers i should be able to detect with python these patterns like this bullish engulfing pattern or this morning star pattern and that's exactly what python's ta lib allows us to do so that we don't have to waste any time we can scan thousands of stocks every stock in the known universe in seconds and present us filter it down and give us a list of actionable ideas that meet our criteria so what i'm going to show you how to do in this series i'm going to show you how to analyze the s p 500 historical data for every candlestick pattern so we're going to do that from the command line just for one ticker symbol and then after we show and understand how to do that we're going to going to package this up in a small web application and it'll be very easy and i'll show you how to analyze data set a data set of all of the historical price data for every stock in the s p 500 so hundreds of different stocks and we'll scan and scan through we'll build a little tech build a little technical scanner and choose any candlestick work pattern we're interested in and scan and find those that meet uh the criteria and so we'll see as of the close of july 31st uh 2020 let's just see if we can come up with any actionable ideas so yeah let's go ahead and get started with some python code oh one last thing uh in case you're interested in this book and you want a hard copy of it for some reason it's kind of expensive but i went ahead and linked it in this little amazon shop so i noticed there was this amazon influencer program where you could create a shop for affiliates and i created this and i'll link it in the description but basically anytime i mention a book i'll just put it in the shop and so in case you wanted to buy this anyway you could click this and buy it and i'm sure i get some portion i didn't really read it but it doesn't hurt anything so i'll link this in the description uh okay so now that's out of the way let's go ahead and get started with writing some python code all right so i'm gonna go ahead and go over to my laptop here and i'm in visual studio code as always and i'm going to create a new window here and put it on my screen so that you can follow along so here we are i have a new window and then i'm going to open and create a new folder just for this project so i'm gonna do a new folder and let's do a japanese candlestick all right and i am going to open that folder and so all this is regular folder right nothing in it yet so now that we have an empty directory let's create a new file it's just going to be a simple command line script for now and then we'll make the web app in the next video so i'm going to do a new file and i'll do let's just call it uh pattern detect pattern detect dot pi and let's just uh have it print hello for now right and i'll just run it make sure this runs okay cool and you see i have a virtual environment already so if you want to create a virtual environment you can um so i run that in visual studio code and then let's go ahead and set up our requirements.txt and this will be all the different python packages that we're going to be using in this tutorial and so i'm going to install ta lib like that and then you have to spell it just like that flask pandas and why finance right and so talib is the technical analysis library that we're going to use to detect candlestick patterns we're going to use flask so we're going to start with a command line script but then flask is going to enable us to use a web framework so that we can show our build a small web application that lets us scan for candlesticks on the web pandas is a library for manipulating data sets in data frames so it's like a wrapper around numpy and then why finance is our package we've used in previous videos in order to get historical price data from yahoo finance so i'm going to install all of these so i'm going to do pip 3 install r requirements.txt and that will install every requirement in this file if you don't want to do it that way if you can just do pip install and then just the name of the package like pip install ta lib if you don't have a virtual environment so you can do it like that as well so i have all the requirements um gathered together now and now we have this empty script and we're not going to print hello we're going to do something else now let's go back to our python script pattern to tech and let's import the libraries that we're going to need so i'm in pattern detect.pi i'm going to import ta lib and i'm also going to import y finance as y f and then that's all we really need for now and then what we're going to do first let's go ahead and get a data set so i'm going to get the s p 500 for just the current year and so to do that i'm going to do a data equals yf dot download and in and i've used this elsewhere in the channel before but i'll just show it real quick again just so that uh this video is complete so you don't have to go back and watch the previous videos there's a package called why finance that you can google for it's in the python package index and what this person this author has done is create a python wrapper around yahoo finance essentially and so it can just make web requests yahoo finance and get anything you can find on yahoo finance and and it returns them in a nice useful format so you can get all that data inside of nice python dictionaries and lists and data structures and you can also download data and have it go directly into a data frame with just a single function call so it's really really handy and so if you look here uh it shows a little example here but what i'm going to use is a download function so this yf.download so let's do that so you just import y finance syf which we've done already and so we're going to do it just like this data equals yf.download and you can do multiple tickers i'm just going to start with one because we're going to talk about spy for the s p 500 just so we can compare it to the chart that we have pulled up in trading view right now so i got i download the ticker symbol spy and we just got to give it a start and end date and so i'm going to give it start date of 2020 so january 1st and we're going to give it august 1st which is today and that will make sure we get the closing price of friday july 31st so i'm going to do 2020 august 1st of 2020 and now if i just print data and you'll see how easy this is now i'm just going to run this so i can do python 3 pattern detect dot pi and if i do that you'll see look at that we have a data frame and so you see we have a date here so july or january so january 1st i believe was a weekend so january 2nd was the first trading day of the year and then you also see we have the uh july 31st which is the close of yesterday and so we have the open high low and close adjusted closed and the volume so we have that historical data all ready to go inside of a pandas data frame it's ready for analysis just like that just with this one line of python code thanks to this y finance package that's done a lot of the hard work for us so that's great so we already have our historical data in the data frame now how do we analyze that data using ta lib so let's talk about ta lib it's the ta technical analysis library for python the original ta lib i believe is written in c someone wrote all these useful functions for performing technical analysis and it's written in c so it'll be really fast but there's also a python wrapper around that source code and someone's created this python wrapper and makes it very easy to use from your python programs and so if you google python ta lib you'll run into this wrapper there's installation instructions i just installed it using pip there might be other instructions here if you want to like compile it from scratch or install it on windows i did on osx just now so i've already installed it and then it has some examples on how to use it now what i'm primarily interested in for right now there's a bunch of indicators like atr and stuff like that so it has a whole bunch of features that we'll talk about we've already talked about this in the bitcoin series and i believe i use the rsi indicator for as an example but we're going to discuss the candlestick patterns so you click here and there's this pattern recognition so these pattern recognition functions are available and you see these have various names that sound familiar to what's in our book so there's like the abandoned baby pattern three advancing white soldiers and so forth so instead of scanning for all these patterns now which we'll get to in the follow-up video let's focus on a couple of specific patterns specifically the ones that we mentioned from the book just the morning star pattern and the engulfing candle pattern and so i'm going to take this morning star pattern here and let's just copy this guy and then i'm going to put this in our text editor and then we'll also take the engulfing pattern right get that guy put it over here and let's just focus on one of them for now so we have integer so apparently we return some kind of integer indicating uh what happened so i'm going to call this uh num let's just call it num for now and then we'll do something with it later and these actually require ta lib dot in front of them since i'm importing ta lib tlib actually has all these functions as part of the package we can import individual technical indicators directly but i'm going to do a talib dot candle morning star and then instead of open so what's the open high low and close um we already have this data frame right and it has columns for open high low and close from our pandas data frame and so we can do is do data open and then we got data and then we're going to do data hi all right so i'll do data hi data low and data close close and then will you there's probably ways to uh adjust the settings a little bit but we don't need that we'll just do open high and low and close and let's see what number is returned so let's print number all right and if i print that you'll see we get a whole series of zeros here which yeah that's not very useful yet right so uh but let's let's see uh what happens let's filter this down so let's show um since this doesn't show all the days right it just says the beginning and end so now let's go ahead and print um num where um num is not equal to zero let's look at that and look at that so what i did this actually gives us another data frame and you can filter that data frame down by using um this indexing here and so what what i'm saying is give me this data frame but only show me the rows where a num does not equal zero and it filtered down and showed me those rows and you'll see there's one day on here where this is not zero and so you see it gives me a hundred right so that means it detected something and so what's significant about march 24th let's look at let's look at our chart here so i have s p 500 candlestick chart and let's look at what happened for the year we had this giant crash and if we zoom in all the way down march 24th right there and look at this very bottom here right there's two red candles down march 20th 20th march 23rd and there's this green reversal candle on march 24th right and then let's look back at our screenshot of the book right so i'm gonna minimize this one for a minute and look at that downtrend s p 500 and then here you have two bearish candles two bears candlesticks downtrend couple bearish candlesticks one long candlestick one shorter candlestick and then a large bullish candlestick and it describes this candlestick pattern here as a morning star and look at that there's a reversal there turns out you nearly nailed the bottom there so rather than holding this entire period you might have found this to be a good entry point i'm not saying this is always a good entry point like any technical analysis uh person will tell you you know this isn't always accurate but you know it turns out this morning star here was a sign of a bottoming formation the downtrend was broken and then look at that if you bought after this morningstar pattern was detected uh you made a good amount of money probably you know 30 percent at least uh 30 at least return rather than being breaking even for the year so and you might have been able to detect some kind of topping pattern over here as well so maybe you got out of the market by detecting some kind of topping pattern and then maybe you were able to detect some bottoming pattern here now we could go and back and back test all this to see if it's any better than random or if it has a slight edge but the point is here we scan for morningstar patterns and found one that match exactly one of the best days to enter the market other than the exact bottom here but that was just purely catching a falling knife right there all right so um yeah so we use talib we scan s p 500 day by day candlesticks open hilo and close we got that data frame we passed it ta lib and we detected the morning star pattern which is great so um what if we didn't want to enter on this morning star pattern right what if we wanted additional confirmation that this was indeed a bottom maybe something else happened here later right so let's go back to our analysis here and i've already tried this before so this reason i'm showing this example so let's try this engulfing pattern part as well right and so um what i'm going to do now is let's what let's go ahead and add those um columns to our existing data frame rather than creating a new one and so we can do is we can do morning stars right equals ta lib and then we'll also do one called uh engulfing equals talib dot cdl engulfing right and let's do the same thing so let's let's pass the engulfing function that same series and let's get our engulfing values right and so and now let's add these columns so these are like columns that we can add to this existing data frame and so we can do is do data morning stars all right morning star equals morningstar and then data engulfing right equals engulfing right now let's just print data now and remove this print up here and let's see what this looks like all right so i spelled it wrong so i'll put a g there run it again and look at this now we have our existing data frame with our open high low and close adjusted in volume and then we have new columns called morningstar and engulfing and i'll do it like this morningstar we can use whatever key we want so i'll do engulfing like that run it again and you see uh we could actually make columns for all of our different candlestick patterns and put them in the same data frame and we'll do that in a follow-up video so we have those and now let's see um let's see what happens with engulfing so now we're going to filter our data frame so i'm going to say engulfing days equals data where the data engulfing is not equal to zero and now let's print our engulfing days right if i do this you'll see we have some various engulfing days and one i wanted to highlight here so you see i believe it's bearish engulfing if it's a negative number and bullish engulfing if it's a positive number and one thing i want to focus on is this april second one here which is a positive 100 and then if you look at the chart for the s p 500 again so we had our morning star pattern here and now you know let's say you want some additional confirmation you'll remember and i remember almost every day so if you look here um right when this reversal happened there was a huge selloff again and people were worried oh is it just going to crash back down and make a lower low a lot of people thought the s p might end up at 1800 at the time that was a very common prediction at the time if you looked around and then look at this so on april 2nd you see this bearish candle and then this bullish candle that completely engulfs this april first bears candle so let me zoom in here you see how big of a bearish candle that is so the wick is like way up here so the market tanked and then the engulfing pattern happens here and you can see a whole bunch of buying uh stepped in so the sell-off did not continue and this i feel like was one of the actual best entries that to take so i actually added on this particular day on april 2nd here and highlighted this so look at this entry even though you didn't nail the exact bottom that was a great entry point here where um this little cup if you will this bottoming pattern and so that would have been a great entry point right here and you still would have captured what uh this was s p uh at about 250 it looks like here and so you would erode s p 500 from 250 to 325 or 327 wherever we are right now which would have been a great return as well even though you didn't capture the very bottom you know so not only did you get a one reversal pattern you got a second reversal pattern that confirmed that we weren't going lower and this would have been a great uh entry importance point as well so with about 10 lines of python code we were able to download a historical data set using the why finance package we were able to use python's ta lib in order to scan for morning star and engulfing patterns or any candlestick pattern we want and we're able to process a pandas data frame of our historical price data and show us possible actual actionable training ideas that we can use in the real world and we came away with a better understanding of our book japanese candlestick charting techniques and we're able to combine this old book with some modern python and able to bring it to life and come up with some ideas that might make us some money so i hope you like this lesson i hope it explained a lot for you uh thanks for watching and stay tuned for the next video and i'm going to show you how to scan for all the different candlestick patterns inside of a larger universe of stocks and come up with some actionable trading ideas in the present right now thanks a lot
Info
Channel: Part Time Larry
Views: 54,031
Rating: undefined out of 5
Keywords:
Id: QGkf2-caXmc
Channel Id: undefined
Length: 24min 11sec (1451 seconds)
Published: Sat Aug 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.