Pandas TA Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back glad you could make it today uh today i'm gonna be discussing pandas ta which is yet another technical analysis library for python i've covered quite a few of these before but i want to be comprehensive and cover the entire ecosystem of the different python packages that are out there and so i've come up with a few project ideas that we can use to learn and demonstrate this library the first thing i'm going to show you is just how to use this library so just how to get price data as usual and then how to apply some of these indicators and functions that are built in some of the features of pandas ta as you can see it provides 130 different indicators so i want to show you how to apply this library to a pandas data frame secondly what i want to do is build some projects on top of this so what i'll do is we're going to build this alerting functionality so a trading view as i've demonstrated if you use tradingview it has this alerting functionality where you can alert on different uh points of interest so it might be an on an indicator or on a strategy or maybe you just want to know when price crosses above a certain threshold or it moves down a certain amount and then you want to receive some type of notification and a lot of people have been asking me about setting up a discord and so one thing i thought would be kind of cool is to create a little discord notification bot here and so we're going to be using this indicator library in order to make notifications on any of these different indicators and just notify on discord and so what i have right here is a python script that's running on a server and it's periodically checking the uh price of ethereum and also applying uh some technical analysis to that price data and so it's using pandas ta to do that and here for instance i'm using the adx indicator and i'm trying to determine whether uh the price is in an uptrend or a downtrend or whether there's no clear trend as you can see right here uh it's posting every couple of minutes here and it's just saying no clear trend for ethereum so it's looking at price data it tells me the adx value along with these directional positive and negative directional indicator values right here and just saying there's no clear trend so if we were to look at ethereum right now and i look over here on my trading view yeah so you can see ethereum is kind of range bound right now right and so there's no clear trend yet whereas here you see it's in a clear uptrend and you see this adx value is high here and we'll we'll dive into that more in a moment and so based on whether a particular stock symbol or crypto currency is in an uptrend or whether it's range bound you might want to apply a different strategies depending on those conditions so for instance someone asked me to build a grid bot by request and i think there's yeah so there's a script here for that in trading view but some people like to specify a grid to trade around i don't know whether this is a good idea or not but this is just an example uh so i have this grid simulator here and what we can do is like where is ethereum trading now between uh 2050 and 2150 and so for this grid simulator what i can do it lets me say i want to trade uh in this range here right so if i put that there it'll draw these grid lines on the price right and then you can program your grid bot to execute place a bunch of buy and sell orders to trade this range and yeah we'll we'll look at this uh later on in a future tutorial and we can see whether whether this is useful to us or not so after we learn how to apply a variety of these indicators locally on our local machine and are able to send a discord notification i'm going to show you how to deploy this all to linode so i have a linode running right and so i have this linux server running it's just a small cpu and not much ram and but that's all we really need and this is like five dollars a month so i have a server running at all times i'm gonna show you how to get this up and running at all times and i've left this running you know for the last 24 hours so you can see this has been running non-stop issuing these alerts so i'll show you how to take our code and deploy it to a server okay and then the third thing i'm going to do is show you how to build a user interface for pandas ta and so what we can do here is i have a streamlit app open here and i'll show you how to build this web app here where i can type a symbol such as tesla and i'm going to show you how to make it draw this chart and it also pull in the price history for tesla and show it right there and then we'll dynamically pull in all the different indicators that are available in the library right and so i can click adx right here and then this will automatically use the default calculations for adx that are built into the library but then i can override any of those so if i want uh this to be length 25 here and i type that in you see how it'll recalculate that and the other thing you'll notice here is when i selected that first indicator you see how it instantly drew uh the second indicator box here and so let's say i want to combine uh this with the macd you see i can select macd and then you see it automatically calculates uh macd there and then i can enter different parameters here so let's say if i want to change these default values to like uh 6 15 and 28 and then uh 10 here or whatever you want to do and you see how that automatically updates and runs new calculations here and so what we're gonna do is show you how to do some more dynamic programming with python to dynamically call these different indicator functions based on what's selected inside of this user interface we'll be able to update these charts with whatever symbol we want so i can easily switch over to microsoft it'll switch over and run the same calculations and i can try out different indicator combinations so yeah these are the projects we're going to be building in this tutorial so before we can build something this complex we need to start off very simple with a command line script that can demonstrate some of this functionality that's built into pandit's ta so let's go ahead and get started talking about the features of pandas ta get a simple script going and then we're going to build on top of that so let's get started and so as you may have guessed if you've been following this channel for a while we've got a new visual studio code editor here and we're going to be writing this using the python programming language i'm going to create a requirements.txt file this where i show the packages i'll be using in this tutorial and so we're going to need pandas pandas ta pandas ta why finance we'll use that to just get some stock price data when we're demonstrating stocks and we'll use ccxt which we've been using to get crypto data from a variety of exchanges we don't need anything else yet but i'll go ahead and add these for now since i'm going to be building multiple projects we're going to end up using streamlit we're going to use matplotlib and we're going to use plotly actually i don't think we need matplotlib for now plotly and request okay and so those are our dependencies and i'm going to create a new file and i'm just going to call this ta.pi and then in here i'll import ccxt and y finance and then i'll import pandas ta as ta and i'll import pandas as pd and so to get started uh we'll need our price data to apply this analysis on as usual and so in cc ccxt uh just to look this up because i can't assume that everyone's watched every video right just to review uh cc xt here is a library for cryptocurrency data and then why finance is a package for uh getting uh data from yahoo finance and so ccxt i'm going to use this and you can see you just need an exchange class here and so your exchange might be called binance right and so what i can do is use binance we're not going to do any authentication or ordering here so you don't need any api keys or anything this should just work out of the box and so i'm going to do exchange equals ccxt dot and i can do dot binance like that and inside of the ccxt library there's actually a function for just getting a bars of data so i can do bars equals exchange dot fetch ohlcv just like that and then i just need to give this a symbol of some sort so since this is a binance i can do eusdt like that i can give it a time frame it looks like the default time frame is one minute here but i can do time frame equals let's say five minutes right and then also i can give it a limit on the number of bars that i want to retrieve and so let's just do limit equals let's get 500 for now right and so if this all works as expected this should just print out a data frame full of data okay and so you see uh this is actually a list of lists here so you see i have a ton of price data here so pandas ta actually works on a pandas data frame so this list of lists it's not going to know uh what to do with it so how do we get this list of lists and convert it to a pandas data frame well that's very easy we just do df so a data frame equals pd dot a data frame okay and then i could just give it these bars as the first parameter and then i need to give it a list of columns so uh so uh this panda's data frame will have columns and i just give it this list of columns in order so i'm going to do the columns and it looks like we have time and then open high low close here and then volume and so i'll just do columns so i'll do columns equals and then i have a list open high low close and volume here and those are the names of my pandas data frames columns and if i print the data frame here now and then remove the printing of bars it should be formatted a bit nicer and looks like i messed it up open high-low close volume oh and time that i deleted there and so the first column is actually time okay and so you see this i have now i have this nice data frame i requested 500 uh bars and so zero through 499 there's my time stamp open high low close and volume and so you see these are actually 300 seconds apart or five minutes so i requested a five minute time frame and this is actually a unix timestamp in milliseconds so i could actually divide that by a thousand and so yeah there's my uh price data for ethereum that we can operate on for the folks that are interested in trading stocks we can do the same thing get a data frame of stocks from yahoo finance we've done this many times we can do ticker equals yfinance.ticker okay and then we just give it a symbol like tesla for instance just like that and then we can do a data frame equals and then we do ticker dot history okay and then that history uh we just need to give it some period or we let's see if it gives us a default period so if i print that data frame for a tesla let's see what we get yeah so it gives us a data frame of tesla price data and it looks like 30 days is the default that it gives us but we could also specify we want more so we can say we want a period one year right and so if i did that you'd see we get a lot more data so we can pull price data for crypto and we can pull it for the dailies for stock and we can pull minute data for stock as well if we want to do that or you don't have to use the official finance if you want to use alpaca interactive brokers other data sources for that you can use that as long as you get in a data frame just like this and so now that we have these nice data frames with price data let's apply pandas ta let's just use some basic indicators that come out of the box so let's look at the documentation for pandas ta and by the way make sure you check out the y finance package here that's how i knew to do dot history but i've done this so many times on the channel that that's why i i know you know dot ticker dot history right so there's pandas ta and you can see the indicators are divided into different categories so if i look for momentum on this page right you see these are divided into different categories of indicators depending on what you're attempting to measure so for a moment momentum you can see there's like the macd right there you can see there's the rsi right there so those are some common ones but uh you know you might know about what a fischer transform is uh directional movement and so for awesome oscillator i don't even know what that is but you can see there's so many of these to choose from if you want to play around with these so there's um the set of momentum indicators uh here's the overlap category you see there's this double exponential moving average the ema fibonacci's weighted moving average a lot of things a super trend we built that from scratch when we're trying to figure it out uh it looks like that's just built in out of the box got linear regression here a whole bunch of stuff built in to this package uh there's some basic performance measurements you can do to log drawdowns and percent returns you got statistics here right if you just want a mean or a variance or standard deviation got trend indicators right here so uh adx is one we'll use in a moment to do our alert on and it also includes the directional movement indicators there volatility right if we want to do like our bollinger bands keltner channels that sort of thing and then there's volume indicators for accumulation distribution money flow index yeah uh on balance volume whole bunch of indicators built in uh across many different categories uh you can also see there are some other features of this library including including integration with vector bt and vector bt is a back testing library that i plan to do a tutorial on in the future um looks like there's some ways to build strategies full strategies using this and also there's some integration with a ta lib as well so if you want to use those candlestick pattern recognition that i did a video on like the stuff that detects a hanging man and uh and uh inverted hammer and that sort of thing if you have ta lib installed and this pandas ta installed then you can just call all these functions and recognize those patterns as well okay so let's take one of these functions so i'm going to use adx as an example so you see how you just call t a dot whatever the name of the indicator you want it so there's a function for each of these indicators like dpo long run parabolic stop and reverse so forth right and so i just go to my python code and since i have this data frame here i'm going to do it on crypto at first what i do is i have this data frame and so i can call since i imported pandas ta as ta i can do ta.adx right out of the box and you see the adx indicator built in except high low and close you need to pass it a series of high lows and closes and then optionally you can pass it a specific length so it has defaults for the length and all these parameters but you are required to pass it a series of high lows and closes and so since i have those i can pass df high df low low and then df close okay and this requires more than just a close you have to pass it high low and close because i think this will end up calculating the atr first which requires uh high low and close yeah so there's the atr calculation here and it uses the link you pass in and the default length let's see what the default length is i'm going to look inside of here and looks like there's a default length of 14 here and so yeah if we wanted to do a length of 10 for instance we could do that okay so there's a ta.adx so i'm going to say adx equals ta.dx pass it my data frame and then i'll print adx just like that and now if i print that out run it you see now i have a brand new data frame called adx and i printed that out and so you see i have values for adx14 and i have these directional movement indicators here right and then so that's my current adx value on ethereum and it looks like it's up to 26 now and then it says dmn 14 and that's greater than this positive directional index and so without looking at the ethereum chart i'm going to assume that ethereum went from a narrow range which it was in when i started when i hit the record button about 20 minutes ago or so um i'm going to assume it went into a downtrend so i don't have to look at the chart or any of the price history necessarily supposedly this indicator is going to tell me that ethereum is now in a downtrend and so let's see if that's the case i'm going to go over to my chart for ethereum and i have it open and i'm going to hide this because it's getting in the way and then i'm on the five minute chart and it looks like it's interpreting this yeah so you see um there's a mixture of candles that are choppy but then now look at that red red red red red red red down down right and so you see now ethereum went down to uh 2080 in the last 20 30 minutes or so um so from 2135 to 2080. and so you can see this um adx value right it was it was low here right it was narrow here and then since it started going down quickly you see this adx rises and if you look at our investopedia for instance right the article on this just to study this indicator a little more you can see there's significance to the value of uh 25 so so this is one possible indicator to try to study price and to detect uh whether a an instrument is trending strongly or not and so you see a value of zero to 25 is absent or weak trend but once it gets above 25 we're saying it's in a strong trend and it can get even stronger so if the price went like parabolic for instance then this value would go up even higher and so applying this to ethereum here you see how uh a downtrend started here right and so it's just now getting started basically once it exceeded 25 now we're saying oh it might be breaking down out of this this range here and and you see there's a bit of lag here right so it has to go down a while first and then this value will start going up right and so just to see how it looked uh historically right you see this downtrend accelerates here uh and then the adx value goes up and one thing to note anytime anyone covers this indicator is just this adx part alone right the adx alone here it doesn't tell you the direction it just tells you the strength of the trend right and so even though this is going down it's trending down the adx is going up and so this is where there's a strong uptrend and the adx value is going up right and that's why i added this other bit here um so let me pull this back up and this actually plots the adx along with those uh positive and negative directional values so if i zoom in here and then i've changed the colors on these it's so that the positive di here is green and the negative di is red and then i have the adx in blue and so you see this adx should match uh this one right and then you see that red how that's up here and this green is down so that means since the red is on top here that means it's a downtrend and if i look back to uh this other period of time right where the adx is high up here this is a strong uptrend you'll see this green line crosses above the red line right and so the green line is up here right so just basic uh statistics that are being applied here to try to catch this trend and i know uh this this thing isn't going to be perfect it's going to lag a bit things can easily reverse really quickly so it's not like this always knows the answers but uh just some general measurements you can do and you can see how i was able to know that you know ethereum was trending down because uh just looking at the numerical values that came out of pandas ta all right uh in addition to adx there's a variety of other indicators but before i dive into those what i want to mention is you see how to i had to remember the order of these this uh df high low and close what's kind of cool about pandas ta is that since all these generally have open high low close as the columns what you can do here is somehow it magically adds these functions to your existing data frame right and so what i can do here i'm going to comment out that adx right here and i'm going to say df right i already had a data frame so when i print the data frame by itself right it's just the open high low close volume but if i i can actually call t df.ta.adx just like that and then i don't even have to pass it that open hilo close and now i can do 80x equals that and if i print adx like that you can see how it was able to just magically work because it infers the name of the columns or it knows the names of the column already and then it it adds this ta function on top of just a basic pandas data frame so i can call it just like that and so that makes it so easy so now that we want to pull out like macd for instance we can do macd equals df.ta.macd and then we print the macd okay do that so you can see right there now we have our macd values calculated right there uh one downside for me for me running it this way it seems to mess with my intellisense that lets me see the parameters in line here in visual studio code but if i just type ta.macd like that you can see i can see what the parameters are or you can look at the source code of pandas ta to find out what all the different parameters are so it looks like i can pass in my own fast and slow in signal values here and so if i want to use a fast equals 14 and then slow equals 28 or something like that then i can run this and it'll calculate it with those values so you see it actually has a naming convention for all the columns here so there's another calculation there and likewise i can do any other indicator i want so you get the idea df.ta rsi and then print rsi right and then we can try you can try whatever you want okay and so there's the rsi as a single column so um what might you want to do here so you see how i have a new data frame here that just has those values what kind of sucks there is it's detached from the price right it returned a new data frame that just has the adx values and just has the macd values and so one thing i might want to do is put all this into one big data frame right and then in that big data frame i might want to filter it down uh the stock or stock symbol or the crypto asset that i'm trading i might want to filter that down to uh only rows that matches certain conditions so uh when it's overbought or oversold or when there's a certain an uptrend a strong uptrend for instance and i want to find adjust those periods of time or for the instance of our discord bot we want to look at the last a bar and see if it matches a certain conditions so we want all these calculations to happen in one place and so how do we combine these data frames well if you know your pandas you can create a new data frame right and you can do pd so pandas.concat and you're going to concatenate all these together so i have my original data frame and then i concatenate my adx data frame my macd data frame and my rsi data frame just like that and then i think i need to do axis equals one here okay so that knows how to concatenate them and then if i print the data frame again let's see what happens and then if i look down here i ran that and now you see i have all this price data and then i have separate columns for adx 14 dmp 14 dmn 14. macd and rsi altogether right and so if i want to just filter this down and just show the rows where um rsi is oversold for instance i could do like df equals df um and then df rsi 14 is less than 30 like that i think that's how that would work and if i do that you can see this just shows me all of the rows in the pandas data frame where our rsi is less than 30. so you see how all those are in the 20s and so it looks like on the 5 minute time frame um as of like 10 minutes ago it reached oversold right and so if you look at i guess like 7 10 or so and if i look at this i guess you'll see that eventually this led to an oversold condition right here and so if i were to plot the rsi on here right so relative strength index right uh yeah so you can see that's indeed the case so the sell-off happened you can see that the rsi went below 30 here and it's considered oversold and then you know maybe maybe you want to buy it and you think this first green candle is going to be a bounce right and as we know from our previous video i made that crypto trading bot uh just using the rsi on its own is not a good idea you know it's not going to give you any real edge because this could easily stay oversold and that's why you might want to combine with other indicators or other ideas you may have and you know what i think that's pretty good for this video we gave a pretty good overview of the different projects we're going to build i showed you how to use ccxt and why finance to get a stock and crypto price data we grabbed that data we applied a few indicators from different categories for momentum and trend did a brief discussion of adx uh we applied uh those indicators to this data frame of price data uh we concatenated that all together and we filtered it down so we have a good handle on using pandas ta to apply technical analysis to price data so in the next video we're going to focus on setting up a discord channel how to set up a discord web hook and how to create notifications that get sent as chat messages and i'll show you how to set up a linux server that way i can keep a script like this running at all times checking for price data running these indicator functions and sending notifications when appropriate when there's an event in time we're interested in so uh thanks a lot for watching stay tuned for the next video in the series thanks
Info
Channel: Part Time Larry
Views: 14,811
Rating: undefined out of 5
Keywords: pandas, technical analysis, python, adx, indicator, strategy, algotrading, stock trading, crypto bots, streamlit, linode, server, discord, notifications, technical alerts
Id: lij39o0_L2I
Channel Id: undefined
Length: 27min 0sec (1620 seconds)
Published: Fri Jul 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.