This article explains how to obtain 40+ technical indicators for a stock using the Technical Analysis (TA) library in Python.
Abstract
Technical indicators are derived from a stock's price and volume and are used to explain and predict stock price movements. The article discusses the importance of technical indicators and momentum strategies in stock analysis and investing. It then provides a step-by-step guide on how to use the TA library in Python to obtain more than 40 technical indicators for any stock with just one line of code. The guide covers importing necessary libraries, obtaining historical data, and bringing in technical indicators. The article also mentions the limitations of Yahoo Finance's API and the benefits of using the TA library for obtaining technical indicators.
Bullet points
Technical indicators are used to explain and predict stock price movements.
The TA library in Python allows users to obtain more than 40 technical indicators for any stock with just one line of code.
The guide covers importing necessary libraries, obtaining historical data, and bringing in technical indicators.
Yahoo Finance's API has limitations, such as a call limit of 2,000 per hour or 48,000 per day.
The TA library is a useful tool for obtaining technical indicators and can save time and computational power.
The article also discusses the importance of technical indicators and momentum strategies in stock analysis and investing.
Obtain 40+ Technical Indicators for a Stock Using Python
Using the Technical Analysis (TA) library, we can acquire 40+ technical indicators for any stock.
A correlation of all the technical indicators using Microsoft’s stock data. (Photo by Author)
Technical indicators are exploratory variables usually derived from a stock’s price and volume. They are used to explain a stock’s price movements in hopes of predicting future swings. In other words, they are used to determine whether a stock is “overbought” or “oversold”. Though these indicators are widely exploited by both independent investors and hedge funds alike, many people do not have quick way of obtaining them. They have to resort to calculating each indicator one at a time. This process takes a great deal of time and computational power. Believe me. I’ve spent my fair share of time coding this process using python in the past (see proof in the articles below):
Calculating technical indicators takes time away from the modeling process and can therefore be a deterrent to building more complex statistical models. With the TA (technical analysis) library though, we can substantiate any stock’s historical price data with more than 40 different technical indicators using just one line of code. This is huge. Since I’ve never seen anyone using it, I decided to write this quick guide in hopes of encouraging more people to take advantage of it.
Outline of this guide:
The importance of technical indicators and utilizing momentum strategies in your stock analysis process.
Importing the necessary python libraries.
Obtaining a stock’s historical data.
Substantiating the historical data with 40+ technical indicators.
Why is This Important to You?
Computers are revolutionizing the way people invest. Now that 80% of every day trades are done by machines, easily accessible data is becoming vastly more important. Independent investors and hedge funds alike are looking for new ways in which they can leverage the speed of computers with their increasingly large troves of data to make more informed investing decisions. This is where technical analysis comes into play.
Technical analysis attempts to extract information about a stock from the patterns in it’s past movements. To do this, we use an assortment of variables calculated from the stock’s historical data to make predictions. If we have enough indicators, we can even leverage the power of our computers to search for patterns that are unique to each stock and may help us predict it’s future price swings. This code is a simple and fast way to enhance the amount of data (number of technical indicators) you have about a stock. You can then visualize the indicators if you would like or even run them through your own statistical models.
A study was conducted on more than a century of data, just to check its effectiveness and its results were astounding. The study found that Momentum trading was profitable since the last 137 years. After testing the entire sample from 1880 to 2016, the annualized excess return stood at 7%.
Momentum Trading Strategies by QuantInsti
If momentum trading has returned an average of 7% in annual returns over the last 137 years without todays computational power, imagine what it will return in the next 100 years given the growth in technology, automation, and statistical modeling techniques.
Import Necessary Libraries
In order to substantiate our stock’s historical data using python, we first need to import these libraries:
Yfinance: Gathers the historical data of the stock that we want to analyze.
Pandas: Allows us to work with large datasets in python.
Shutil, Glob, and OS: Access folders/files on your computer.
Now that we have imported the required libraries for this script, we can bring in the historical data for the stock that we want to analyze. To do this, we are going to make a call to Yahoo Finance. In this example, we are doing this with the stock Microsoft.
A couple of things to note:
In this example we chose to pull all of the historical data for a stock, but you can change the kind of data you want to import from YF. Check out their API to learn more.
Yahoo Finance may block your IP if you make more than 2,000 calls per hour or 48,000 calls per day. In this example we are only making one call, but if you’re planning on automating this code be cautious of the call limitation.
Let’s take a look at this data frame. As you can see below, Yahoo Finance provides us with the daily data for Microsoft going back to 03/13/1986. It provides us with the opening and closing prices as well as the day’s high, low, and trade volume.
Sample of the initial historical data.
In the next section, we are going to take advantage of the Technical Analysis (TA) library to substantiate this data with 40+ technical indicators.
Bring in the Technical Indicators
I have read a lot of algorithmic investing and quant articles, yet I have not seen anyone use this library before. Most people will calculate the technical indicator they want by hand and then analyze the stock. With this process, we can bring in every technical indicator with one line of code or specify just the one momentum indicator that we want to analyze. The ease of using this library is immense and should be utilized by more people.
Getting 40+ technical indicators:
After running this code, we can see that there is many more columns for us to analyze. A lot of which you may not recognize. This is why I mentioned that it can be a little overwhelming. To learn more about the new variables, you can visit the TA website. Also, to learn how to utilize momentum indicators be sure to check out Quantra’s course and take advantage of the Black Friday deals while you can.
Below is a list of all the new columns that we have brought in:
The list of technical indicators we have created.
Final Results
We now have the historical data for Microsoft going back to 1986 as well as 40+ technical indicators to analyze alongside it. A sample of our final results can be seen below.
Sample of the final output.
If you found this article helpful, Medium has a trove of other articles with similar code breakdowns and tutorials that I honestly use all of the time. If you don’t have one already, I would highly recommend creating an account.
Also, connect with me on LinkedIn here, I’m always happy to make a new connection!
* Disclaimer: I’m not a financial advisor. The content of this article is not financial advice.