Technical Analysis Library in Python Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back in this video i'm going to show you how to use ta the technical analysis library in python i got the documentation pulled up here um not a very creative name but it works so all you have to do is do pip install ta and you'll have this nice library available to you in the last video i talked about super trend a little bit and the atr being the first component of that and we calculated that by hand just to make sure we understood what we were doing but you don't want to calculate things by hand that's what the computer is for and that's what these libraries are for they solve these problems for you so you don't have to think about it but it's helpful to understand what goes on to these in in these calculations and in these scripts that way you're not just blindly copying things and you can like validate the numbers you're seeing make sure things seem right otherwise you know you might you might end up with some bugs so this is the technical analysis library in python there's a github page for it where you can check it out as you can see like any other technical analysis library provides a variety of indicators and they're broken up into modules so they're divided by type so we have the volume indicators volatility indicators which includes average two range bollinger bands keltner channels things we've discussed in previous videos you got your typical trend indicators like simple moving average exponential moving average you got adx you got your macd and then you also have a momentum like the stochastic relative strength some oscillators and so forth so just like any other indicator library however i like this one and the reason i want to try it is because you know i i've talked about this ta lib before which is very popular right and i've also tried out tulip indicators and backtraders technical analysis library but you know they all have their strengths and weaknesses and now this one's uh pretty popular but you know there's a couple of weaknesses to it the first is that the interface to it is a little weird you know see you can even tell how the functions are named that they're just there's just one big name space uh you got these capital letter functions here and so if you look at the candlestick functions that are built in here uh like these pattern recognitions ones uh you know it's just called cdl three black crows right and it's not really subdivided well and um it's also a little bit challenging to install on different operating systems right and so it's just not the interface isn't quite as pythonic it's not nicely divided up into classes so what i like about this one is if you look you can import a class so it's divided up so it's divided into like ta.volatility you can import bollinger bands and then you just construct your you instantiate bollinger bands here and you just pass it in a data frame of data and then run a couple of methods and you're done so it's very easy to use um so like i said it's divided up into a number of classes here and yeah let's go ahead and get started uh using some of them um so yeah let me let me install this first so i have a new blank a visual studio code editor and i'm just gonna click open here and i'm gonna create a new folder and i'm gonna call it uh super trend bot okay and i'll post source code shortly so i'll open this up and i have an empty folder and i'm going to have a config.pi as usual and i'm going to put my api keys so i'm using binance here so i'm going to use my binance key and i'm going to set that and i talked about that in the ccxt tutorial and then i'm going to have like a binance secret key and so i'll have that in there and i'll fill that in behind the scenes so to get started using this technical analysis library we need to first make sure we install it so i will get that installed so i've got a command line here and i'm just going to do pip 3 install ta and make sure you have it and then i'm also going to install ccxt and the schedule package right and so once all of that is installed i'll be ready to do this tutorial so i'm going to create a new script called bot.pi again and let's see if we can just import ta okay and we're also going to let's do from ta dot volatility import volunteer ban so we'll take this example right off here and see what we can do let's see if we can compute some bollinger bands so in order to perform some type of calculation we need some data and so this has some data built in so if you browse the github repository here and i'll link this below so there's like a test folder and under data they provide some some test data that you can use but i don't really want to use this test data it's super old so this looks like data from 2010 for like a month right so i'm i'm interested in what's going on now we want to make some trades now so let's get some current data and to get our current data we can use ccxt which we just discussed a couple of videos ago and so i'm going to use that in conjunction uh with my binance or coinbase api key or if you want to configure a different brokerage if you use kraken or one of these other things feel free to do that as well so let's go ahead and get that set up so i'll import ccxt and then i still have a snippet from the last video and all we really do is instantiate an exchange so i'm doing exchange equals ccxt.binance but you know if you want to do ccxt coinbase you know you could do it like that and then you just need to give it your import config and my config has my binance api key and my binance secret key and i'm going to fill that in behind the scenes real quick so this actually works and let me make sure i can actually grab some data so i'm going to do exchange.load markets and assign that to markets and let me see if i can actually get some data from here so i'm going to print markets like that okay and it looks like i got plenty of data back so i know my connection to binance has been established so now let me see if i can fetch some data that we can apply this bollinger bands indicator to so i'm going to do exchange dot fetch and there's one called a method called fetch ohl cv and let's see the parameters it just needs a symbol and a time frame by default it's going to fetch the one minute time frame yeah let's let's let it use the default and so let's do ethereum usdt right and then let's see if this works so let's say bars equals exchange fetches okay uh so yeah so let's do four bar in bars print a bar so this should return a list okay and look at that so we have time stamps and let's see by default how many did that return if i print print the length of bars okay it returns 500 by default um i should be able to set some additional parameters and i don't really need 500 minute bars right now uh we're going to do an atr of like 14 or 10 or some number like that so i'm just going to get the last uh let's just get the last 20 minutes right and then let's verify this to make sure it's all right and so i'm going to run this again and let's look at this much so you see only have 20 different bars here and the open high low close and so uh let's look at unix timestamp real quick and it's what time is it's 52 minutes past the hour over here and if i click that you see that this is indeed 52 minutes after the hour and then if i go back one more time stamp you know that should be the 51st minute right and it should go back 20 minutes right so this timestamp is the price 20 minutes ago so looks like indeed we have um some minute data coming in and we can also look at we can look at trading view is what i use a lot i'll leave a link below in case you want to sign up so uh trading view yeah so here's the last 20 minutes so 52 minutes so i'm gonna change the one minute time frame and let's see at the 51 minute mark you see the high of 19 90 32 and you see that 1990 32 right there and then the low 19 86 56 and the low here see that 19 1986 56 cool so we're all matched up there so we have 20 minutes of data so let's first see if we can apply this bollinger bands indicator that we imported from ta.volatility and just so you know if i go into volatility you'll see there's also a class for average to range bollinger bands you should see keltner channels and so forth right and so we imported one called bollinger bands but i can also improve average true range and i'll go ahead and do that now and we'll use that in a minute okay so let's see how we apply the bollinger bands calculate bollinger bands on these bars that we just fetched from binance so the first thing i need to do is create a new instance of the indicator so i'm just going to call it bb indicator equals and then i do new instance of bollinger bands and look at the parameters all i need is a series of closes right this is calculated on the closes right and then you give it the time the period so by default this is 20 periods and by default it uses a two standard deviations right so there are some built-in settings of 20 periods and 2 standard deviations so all i need to do is provide a series of closing values okay so how do we do that so this data at the moment is not in a pandas data frame and you'll notice this bollinger bands indicator requires pd.series so it's a a pandas data frame so how do we convert this series of data here into a pandas data frame so i forgot to mention we're also going to use pandas so i'll add that to my requirements text and you can also just install like pip3 install pandas and make sure you have it so at the top here i'll also import pandas as pd okay so what we'll do is we'll say uh let's create a data frame so i'll call it df and that's going to equal a pd.data frame okay so data frame okay and we need to just give it some data so our data is just in bars here so i'm going to give it the bars and then what i can do is specify a list of columns so what i'm allowed to do here is do columns equals and i can just tell it what the order of of these so i can say you know i can give this column a label so all of these timestamps i'm just going to call timestamp and i'm going to call that one open hi low and close so i'm making a new data frame from this data and i'm telling it what to label each of these columns so i'm going to say columns equals timestamp comma open comma high comma low comma close comma volume right okay and let's see what that looks like now so if i print df and i'll comment out this indicator okay let's print df and see what we did and look at that just like that i've connected to binance i fetched some open high low close data just the last 20 minutes of ethereum and i converted that list here to a pandas data frame and it has you know this nice index here and then it has time stamp open high low close in a nice easy to use pandas data frame format and so now that my indicator here i've instantiated it and all it really needs is a series of closes i can just say df close just like that and then on my indicator i need to call one of the functions that's built into bollinger bands so let's see what's in here so i'm going to jump in here and you just need to call the function you want to run so bollinger bands if you'll remember and let's pull some up okay so i'm going to go to indicators and i'm going to go to bollinger bands and let's put some on there okay and so you see bollinger bollinger bands have some parameters here 20 and 2 which are the parameters we already have by default use the close and the length is 20 and the standard deviation is two and what this means is there's a moving average here so a 20 period moving average and then this upper band here is two standard deviations away and this lower band is two standard deviations uh below that moving average right and so yeah that's what it looks like visually and so what do we need to do so this looks like this indicator has different methods you can call on this bollinger bands object one of them you call to calculate the moving average so if you just need the moving average you call bollinger moving average if you want the high band or the upper band you call this method if you want the lower band you call this method so let's call those and see what happens so now that i have an instance of this bollinger bands indicator i should be able to do bb indicator dot and then the name of the method method bollinger h band so bollinger h band okay and let's see if this works so i'll just say the upper band equals this calculation and print the upper band and let's see if that works and sure enough i have a series of values so hopefully that represents the upper bollinger band and then i'll also go ahead and call the other one so i'll do lower band equals bb indicator dot bollinger l band okay and then i'll say moving average equals bb indicator dot and it's called bollinger m-a-v-g so i'll do a bollinger m-a-v-g okay and yeah why don't we just assign those so let's just add them to the data frame that we already have right and so i'll just do uh data frame upper band right data frame lower band and so what we're doing is adding columns and we're just going to put these indicator values right next to our price data so it's nice and easy to read and we'll calculate them and store them right in there and now if i just print the whole data frame we have this new larger data frame that has our ethereum timestamps timestamps open high low close volume we have our upper band values lower band values and our moving average values and let's see if these make sense so if i look at the end here the most recent minute you see the high and the low and close and so we have this uh average here of 1989 and the lower band is below by about eight and then the upper band is above 1989 by about eight so that makes sense right so if i look at this the most recent data here you see we have the moving average here and let me hover over that so you can see what is that 19 yeah 1989 if you see in the top left there you see it says bb 20 close 2-0 and so the red line which is the moving average here says 1989 80 and then you go up by about eight point something and it's 1998.46 and you go below uh two standard deviations and you get 1981-13 and sure enough that looks very similar to our numbers down here okay so yeah looking good and we don't really need the bollinger bands here because we're we're working with super trend but yeah i'll just keep that around the source code that i post after this and so now since we're actually going to use the average true range let's go ahead and do atr indicator equals average true range okay and then this average to range is going to be of what does it need you remember in our calculation we need more than the close right in the last video i calculated average true range in detail and we needed highs lows and closes right and so if you look here in my visual studio code it needs a series of highs series of lows and series of closes and by default this n is 14 so it's uses 14 periods so it's the 14 period atr and so let's give it a high low and a close right we already have the high low enclosed in this data frame so i'm going to do a df high comma df low and then i'll do a comma df close okay and then now i just call the function so if i look at this average true range source code you'll see there's just a function called average true range that you call and so i call that after initializing it so i'll do average true range just like that and then let's set the atr and let's put that in the data frame so dfatr equals atr indicator.average2range and i'll take this print move it down below and look at that so now we have an atr column here and it gets filled in you'll notice the first uh 13 of these 0 through 12 right they're all zero right because it's a 14 period average shoot range and so we need 14 periods to actually occur before we have this atr value so you'll see at index 13 remember we index starting at zero you'll see that see the atr is 2.53 and you know you can go verify that if you want to but you see that looks nice and so we have the most recent atr value here at the end and uh one thing to also note is that uh so let's what observe these time stamps so i think it gives us a time stamp even though this candle might not be complete so you see that ends in sixty thousand there i'm gonna run it again and you see it says sixty thousand still and let's see if it's changed so see this timestamp i've run it twice you see it's the same time stamp but let's see so this high here you notice that it changed 1989 to 1966 right so there's still some different values here so this most recent candle is still evolving and it looks like binance still returns that to us and so we're not going to apply our our indicator to a bar that is not yet complete so let's say we only care about the bars that are already finished right we don't we don't care about this bar because it's still evolving we only want to use look at closing values for the day for instance let's pretend we're running this on the daily and you know it's still in the middle of the day and we're only interested in uh after the day closes right so how do we filter out this last bar well um let's say we're doing bollinger bands and we want the last 20 i'm going to go ahead and fetch 21 of them and just chop off the last one so in python if you remember you can access a list by its index so you can get the zeroth item the first item and so forth or if i want the zeroth and first item i can do zero colon two for instance and that would get get me you know the first and second item in the list so let's say i want all of the items in the list except for the last one what i can do is just do a colon negative one so what that says is start at the beginning all the way up till before the last one right and so that'll get us um 20 out of 21 right so let's let's let's run this real quick let me show you what that looks like so if i run bars here like this so i'm going to print the bars again you'll see how we have that 4000 one at the bottom but if i do bars negative one you'll see it filters out that one so you see this one ends in 80 000 and so now you see when i run it again with that negative one it goes all the way up till the last bar that is closed and so now we have 20 clean uh bars of data that have all been closed and we're running our indicator only on those closed bars so yeah i'm pretty happy with this right um i'll leave the scheduling until the next one when we put it all together but you'll see we're already fetching data from ccxt from binance and we've learned how to use python ta to apply uh some indicators and we've calculated bollinger bands and average true range and in the following videos i'll show you how to fetch this data like more and more data fresh data on a schedule and run these calculations on schedule we'll dive more into the super trend indicator and hook it up with some buys and sells so uh thanks a lot for watching and stay tuned for the next one and we'll keep going
Info
Channel: Part Time Larry
Views: 22,544
Rating: undefined out of 5
Keywords: python ta, technical analysis library in python, bollinger bands, average true range, indicator library, pandas dataframe, tutorial
Id: s3KIV-WaNuQ
Channel Id: undefined
Length: 22min 23sec (1343 seconds)
Published: Wed Apr 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.