Technical Analysis of Stocks using TA-Lib | by Himanshu Sharma | Towards Data Science

Technical Performance Indicators are the mathematical calculations that are performed on different stock parameters like Volume, Price, etc. They help us in identifying different patterns that a stock follows or will follow in a certain given time. These performance indicators are generally visualized using charts and graphs.
TA-Lib is an open-source python library that is used in analyzing the stock market’s historical data like share price, volume, etc. in order to predict the future price or the market direction so that we can make our investments accordingly.
Ta-Lib contains a large variety of technical indicators that are used to study the market. We can visualize a large number of indicators in order to decide our future strategy. In this article, we will explore how to use TA-Lib to create different technical indicators.
Ta-lib installation is different from other python libraries as it is not available to install directly using pip install. officially available. First, we need to visit the link and download the whl file of Ta-Lib according to our windows version. After that, we can install it using pip install as given below.
pip install <filename.whl>
We will be working on technical analysis of the stock data so we will import ta-lib, other than that we need to import yfinance a python library to download historical stock data, and pandas also to load the data into a dataframe. We will also use matplotlib for visualization.
import yfinance as yfimport talib as taimport pandas as pdimport matplotlib.pyplot as plt
We will download the historical data of “Powergrid Corporation” a company listed in NSE, India. For downloading we will finance and then store the downloaded data into a dataframe using pandas.
power = yf.Ticker("POWERGRID.NS")df = power.history(start="2020-01-01", end='2020-09-04')df.head()
Now we will start creating different technical indicators using Ta-Lib. Although there are a large number of technical indicators, we will only analyze the most important ones or ones that are highly used by professionals.
Simple Moving Average
Simple Moving Average
A simple moving average (SMA) calculates the average of a selected range of closing prices, by the number of periods in that range.
df['MA'] = ta.SMA(df['Close'],20)df[['Close','MA']].plot(figsize=(12,12))plt.show()
2. Exponential Moving Average
An exponential moving average (EMA) is a type of moving average (MA) that places a greater weight and significance on the most recent data points. That is it is generally known as Exponentially Weighted Moving Average.
df['EMA'] = ta.EMA(df['Close'], timeperiod = 20)df[['Close','EMA']].plot(figsize=(12,10))plt.show()
3. Average Directional Movement Index(Momentum Indicator)
ADX can be used to help measure the overall strength of a trend. The ADX indicator is an average of expanding price range values.
df['avg'] = ta.ADX(df['High'],df['Low'], df['Close'], timeperiod=20)df[['avg']].plot(figsize=(12,10))
4. Bollinger Bands
Bollinger Bands are a type of statistical chart characterizing the prices and volatility over time of a financial instrument or commodity, using a formulaic method propounded by John Bollinger.
df['up_band'], df['mid_band'], df['low_band'] =                            ta.BBANDS(df['Close'], timeperiod =20)df[['Close','up_band','mid_band','low_band']].plot(figsize=                                     (12,10))plt.show()
5. Relative Strength Index(RSI)
The relative strength index is a technical indicator used in the analysis of financial markets. It is intended to chart the current and historical strength or weakness of a stock or market based on the closing prices of a recent trading period.
df['Relative'] = ta.RSI(df['Close'],14)df['Relative'].plot(figsize=(12,10))plt.show()
These are some of the most used Technical Indicators which we can use to visualize and analyze the market trend and future prediction
In this article, we saw how we can install Ta-Lib using a whl file. After that, we saw how to load the data using yfinace and store it into a dataframe. Then we used this data for technical analysis and created different technical indicators using Ta-Lib. This is just an introduction to the basic indicators there are many more indicators defined under Ta-Lib which we can explore.
towardsdatascience.com
medium.com
towardsdatascience.com
Thanks for reading! If you want to get in touch with me, feel free to reach me on hmix13@gmail.com or my LinkedIn Profile. You can also view my Github profile for different projects on data science.