Technical Analysis in Python Made Easy with Pandas TA
Key Takeaways
The video demonstrates how to perform technical analysis in Python using the Pandas TA library, covering installation, data loading, indicator calculation, and trading signal generation. It also showcases the library's functionality and ease of use for finance professionals working with pandas data frames.
Full Transcript
Today we're going to learn how to easily do technical analysis in Python using pandas dataf frames. And what I mean by that is we're not going to leave the world of pandas. We're going to just use pandas dataf frames, but we're going to install an additional package called pandas ta. So pandas technical analysis to extend the functionality of data frames and allow us to easily calculate indicators, generate trading signals and more. If you like this video, let me know by hitting a like button and subscribing. But now, let us get right into it. >> [music] >> All right. So, I think this video is going to be super interesting and we're going to start off right away with some drama. So, as far as I understand it, and I'm not an expert in the history of this package. There was an original package called Pandis TA. And there's still a guy here who forked it from the original here, Pandis TA. And this package is no longer available for free. It's still being developed somehow as a paid version, as a subscription. and people are kind of uh annoyed by that. So they did a community fork of that which is pandasa classic. So this is now the open-source free community-based version of pandasa and this is what we're actually going to talk about today. So this is the free version. We're not going to use the paid version or anything like that. We're going to use pandista classic. It also has a documentation here. And they also nowadays differ in terms of some of their features and also in terms of some of their phrasing. So I think the original version talks about studies and in the classic community- based version here we talk about strategies and strategy systems. So that is the little historical journey here. So we're going to use pandasa classic for that. We're going to start by setting up our environment. So we're going to navigate to a directory that we want to be working in. In my case the tutorial directory. And here now we're going to set up the environment. Now you can either use your Python installation that you use for everything. So if you just have Python on your system, you don't want to think about isolated environments, you can just say pip or pip 3 install pandas ta classic like this. And we're also going to use jupiter lab. So these two packages, you can install them like this. If you want to use a virtual environment, you can create it using vn or virtual enth. In my case, since I'm using uv the rustbased python package manager, what I'm going to do is I'm going to say uvinit. You don't have to do that if you don't want to. UV init. This is creating a new project. And now I'm going to basically do the same thing that you do. Uh either UV pip install or UV add. It's the same command basically. And I'm also going to install Pandis TA Classic and Jupiter Lab. So these are the two packages we're going to need today. But since we're also going to work with financial data, we need to also install a package that allows us to get financial data. So we can say UV add or pip install. Same thing. Y finance. This is going to allow us to access the Yahoo Finance API to get financial information to get stock data onto our system. All right. Now, once we have all of this, you can just go and run Jupiter space lab. So, Jupiter Whitespace Lab or if you're using UV like me, you have to do UV run Jupyter Lab. This is going to open up the development environment of today's video in the browser. And this now, for those of you who don't know what Jupyter Lab is, is an environment that allows us to work with interactive Python notebooks. For those of you who don't know what interactive Python notebooks are, it's quite simple. We can click on this button here to create one. And the idea is we can run individual cells of code. So if you've only worked with ordinary Python files up until this point, the idea there is you run the whole script from top to bottom. You just execute all the code and whenever you want to change something, you have to execute the whole script again. With Jupyter Lab or with interactive IPython notebooks or with interactive Python notebooks, the I stands for interactive. What we do is we run individual cells. So I can define a variable here. I can print it down here. And then I can do something else below that. I can also set the value here to be equal to 20. And then I can go back up and just run this cell, which means it took the value that was set here. So we don't have to run it in the same order. We also don't have to run everything because the state is kept here in memory. So we don't have to uh restart everything from scratch if we want to just add an additional plot for example. Now, what we're going to do first here is we're going to load some stock data. How are we going to do that? We're going to do that with the Yahoo Finance API. So, I'm going to say import Y Finance as YIF. And then I'm going to say data is equal to YIF ticker. And we're going to choose a stock. Let's go with a classic AAPL Apple. And I'm going to say I'm interested in historical data. The period I'm interested in is the last 5 years, for example. That is going to load now a data frame from the Yahoo Finance API which shows me open, high, low, close values. So the opening price and the closing price which is the beginning and the end of the trading day, high and low, the maximum and minimum of this day, volume, the amounts of the amount of shares that changed hands. And then we have also the dividends and stock splits, which is not too important for us. We're going to mainly focus on close anyways. But now what we can do is with pandas ta or with pandas ta classic just by importing the module just by importing the library we are able to extend the functionality of our data frame. So to show you what I mean if I go ahead now and try to calculate the simple moving average. So if I say data.ta for technical analysis SMA and I say this is automatically going to take the closing price. If I say length is 50 so 50-day simple moving average. it's going to tell me that this attribute TA doesn't exist. But just by importing pandas classic uh pandas ta classic. So if I say import pandas ta classic sta just by doing that it's going to extend the functionality of the data frame. So now I have the simple moving average here with a length of 50 with a window of 50. So a 50-day SMA and you can do it either like this or you can also do it as a function call. So you can say TA SMA and then we can provide data close here as input and still length is equal to 50. That is going to produce the same result. As you can see these are just two different ways to do that. This is a function call and this is an extended version of the data frame. And like this you can do now a bunch of different things. So this is just a simple moving average. I can do the same thing with the exponential moving average. So I can just I think I have caps lock enabled. I can just do EMA. then I get the exponential moving average. And I can do this also with stuff like the MACD with the relative strength index. So let's go and do an RSI with this. I'm going to go with the length is equal to 14. And now I have the RSI, the relative strength index. I can also do MACD where I set fast is equal to 8 and slow is equal to 21. This now returns even three columns. So it returns the signal and the histogram and everything. Um and the cool thing now is I can also just easily integrate this. So what I can do is I can say data and then SMA 50 is equal to data ta SMA length 50 and I can do that with the EMA. Now if I want to do that with the MACD I would have to join. So I would have to do data and then join like this and then I would have to set this to data. But I can do that and then I have a data frame that now has also indicators. Now of course the first uh n days where n is the length are going to be nan because I cannot get the 50-day simple moving average if I don't have the 50 days before this time period. But um you get the idea. I can now easily extend my data frame to include more information. I can also say start after the first 50 entries. Then I don't have nan values here. And that is super convenient to just do this as a data frame method. Now obviously I'm not going to cover all the indicators but there is a documentation and in here if you go to indicators reference you can see all the stuff that is available a lot of momentum stuff and overlap stuff and trending stuff. I'm not a technical analysis expert by the way. I'm not really a finance guy. I am interested in it and I do know some things but I'm definitely not an expert. So don't ask me about the specific indicators. I'm just teaching you how to code here and how to use this in Python. You have a bunch of different indicators that you can easily use by just calling these functions either by doing ta do function calling it on a specific series or just calling it on the data frame as a method. The cool thing now is that you don't have to do this manually like this line by line. You can also define something called a strategy. So I can say I want to define a strategy. Let's call this my neural 9 strategy. And that is going to be a ta strategy object. And in here now I can define multiple indicators that I'm interested in. So let's name it first. Let's call it something like momentum and stuff. Not very professional I know. And then all I have to do is I have to say TA is equal to a list of dictionaries. And one such dictionary includes the indicator that I want to use and also some parameters. So for example I can start here by saying kind or actually let me use single quotations. Kind is going to be SMA for example. And since SMA takes a parameter length, I can also say now length as a keyword here and point it to 50. So now I can take that I can do the same thing with the EMA and I can do the same thing with the MACD. In this case I would do uh not length but I would do slow 21 and fast [snorts] 8. Then I could also go and do something like RSI and Ballinger bands. So BB bands like this. Here I would go with 20. Here I would go with 14. And that is now my strategy. And instead of doing everything here manually like this, I can just go and apply the strategy to the data frame. So for that maybe let's load the data again. Ticker APL then uh history period 5 years to load it from scratch here. And then I can just use data ta strategy and I can pass here my neural line strategy like this. Sorry it's lowercase s like this. And now I can see that my data frame was extended with all these indicators. I didn't even have to assign it. I didn't have to do anything. It happened in place just by calling this. this strategy was applied and I now have all these indicators uh in my data frame. So that is how we work with the indicators. How we can easily get the indicators. Now let's say I want to generate trading signals based on certain rules. For example, I have the exponential moving average with 20 days and with 50 days and when they cross I want to generate a signal. I can also easily do that with pandas. So let me get this here again to start from scratch. We always want to have a clean fresh data frame here. And now what I can do is I can just say EMA 20 is going to be equal to data TA EMA length is equal to 20. Same thing as before. Now I can do the same thing with 50. And now I can use a function from TA called cross. So I can say data and then EMA 20 x EMA 50 for example. And I can now say is equal to TA. And I can pass data EMA 20 and data EMA50 like this. And that now generates signals. So now if I print or if I show my data set, you can see I have the indicator if that happened or not. So if I go and say data EMA 20 m E ma50 and then I look at the value counts here and there it's going to happen and when it happens I get a one. So I get 11 times a one. Most of the time the cross does not happen but maybe I want to do some action here. Now this is very basic. This is just a yes or no thing but we can also design more complex um signals. For example, maybe I want to work with RSI thresholds. So I want to define a range and when something goes out of that range, when the RSI goes out of that range, I want to have certain actions being taken. So let me take the data frame here again. Let's start from scratch. And now I'm going to define the RSI. So I'm going to say RSI 14. Let's call it is going to be equal to data. TA RSI length is equal to 14. And now I want to say there is a limit. The lower limit is 30. The upper limit is 70 and when we cross these boundaries I want to generate signals. So for that I can say data is equal to data join because that's going to generate multiple columns. Now TAX signals and I can specify here data RSI 14 and then I can specify the lower and upper boundary. So I can say XA is equal to 30 and XB is equal to 70. And that is now going to generate multiple columns. If of course we don't get the value error. I had this before this. If you get this error, it just means that they're using uh an outdated version of pandas. So to fix that, what you need to do is you need to make sure that you have an older version of pandas. That is not a problem on your system. This is not something we did wrong. They're just using a function that is deprecated in pandas 2. And if you use pandas 3, this function no longer exists. So you cannot use it. So you would either have to change the source code or yeah, basically have to downgrade. So, we're going to go into into our tutorial directory and we're going to pin the version. So, you either say pip install or you say uv at or let's go with uv pip install to keep it simple. So, uv pip install or just pip install pandas and we want to put this in quotation marks here is equal to or let's say it's just less than 3 uh 3.0 that is going to force an older pandas version. You can see 3.0 was removed and 2.3 was installed. So now this function will exist. I'm not sure if I have to reload the kernel though. Let's see. Yeah, I have to reload the kernel. So let's run all of this again from top to bottom. And now this actually works. But you can see this is the issue in pandas 2. It says here future warning that this is deprecated and will not um work in future versions. These future versions now exist. So they need to update this. But essentially it worked with an older pandas version and now I get the trading signals here which are TS trends TS trades and then we have entries and exits. So this is basically buying and selling. So we can now take these signals that are generated by just calling the TAX signals function with the threshold here for the RSI and we can easily visualize that. Now since this is not a MAT plot lip tutorial, I don't want to waste the time and code the plotting logic from scratch. I'm going to copy paste it. However, at the link in the description down below, you will find the code on my GitHub. You can just copy paste it from there if you want to. I just want to show you how easily this works. That whole thing here is just mattlip stuff. This is the magic from pandas ta. So, we just say here entries is where this is one, exits is where this is one. And then we just um yeah, basically plot the entries and plot the exits as uh different markers with green and red color and buy and sell labels. That's basically it. I assume you will have to install mattplot lip. So let me go to tutorial let's say UV add or pip install mattplot lip and then we can just run this and this is going to produce a plot but of course only if I put the right column here. So RSI4 RSI_14 not just RSI 14 and this produces this beautiful plot here. Let me zoom out one more time. we get here the AAPL trading signals. So depending on our or based on our logic here we had the threshold at 30 and 70 and when we cross it here when we go above the 30 we buy. When we go above the um 70 and then back below 70 we sell. And again here we have a buy we have a sell we have a buy sell by sell. These are the signals. You can see if this would make sense. I mean here we would have uh won some money here as well. Here as well here as well here as well. And then here, I don't know, maybe you should buy right now. Of course, none of this is financial advice. This is very important. This is just a programming tutorial. Don't use this in any way here as uh investment advice. But yeah, this is how you can easily do that. Generate trading signals and visualize them. Again, the code will be on my GitHub. You can just copy paste it from there if you want to. If I may for a second, I would like to plug myself in as the sponsor of my own video. If you go to my website, neuralign.com, you will find a tab services and a tab tutoring. Here you can hire me for all sorts of stuff like data science, machine learning, web development. If you need help with something in a project, here you can book me for one-on-one tutoring. If you want me to teach you personally something that you don't understand, if you like my teaching style, on both pages at the bottom, you can contact me via mail and also via LinkedIn. Just wanted to let you know about this. And then finally, the last thing I want to show you here is something that is in beta. I can actually give you the link to this here as well in the documentation, which is performance metrics. I recently made a video on how to analyze performance and risk of a portfolio. We learned about certain ratios here like the sharp ratio and certino ratio and kmar ratio. Uh you can also use this package to calculate this although this is in a beta and they might not be entirely accurate and also you cannot use this as an extension. So you actually have to do the function call. But how would this for example work? If you want to have the sharp ratio you would go and do something like print and then ta.sharp sharp ratio and you would just provide the close price like this. That's essentially it. This will print the sharp ratio. You can do the same thing with the colar ratio and with all the other performance metrics that you can find in the documentation. But that's basically it. This is a very convenient package if you're someone in finance, if you want to do technical analysis, if you're working with pandas data frames a lot and you want to have an easy way to calculate all these indicators and generate signals. So that's it for this video today. I hope you enjoyed it and hope you learned something. If so, let me know by hitting a like button and leaving a comment in the comment section down below. Also, in case you're interested, on my website, you will find a services tab and a tutoring tab. There you can contact me if you need some help with a project, if you need a freelancer, or if you don't understand something and you need some tutoring, you can contact me at the bottom via LinkedIn or via mail. Besides that, don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free. Other than that, thank you much for watching. See you in the next video and bye.
Original Description
In this video, we learn how to easily do technical analysis in Python using Pandas TA.
Code: https://github.com/NeuralNine/youtube-tutorials/tree/main/PandasTA
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🖥️ Setup & Gear 🖥️: https://neuralnine.com/extras/
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Data Literacy
View skill →Related Reads
📰
📰
📰
📰
O(N) Manacher's Algorithm with Mirror Boundary Optimization
Dev.to · Dipaditya Das
Building a Power Grid Inside Minecraft with BFS Algorithms
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Medium · Programming
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Medium · AI
🎓
Tutor Explanation
DeepCamp AI