How to Install Ta-Lib in Python – Part V

Ishan Shah, Rekhit Pachanekar and Gaurav Singh demonstrate how to install Ta-Lib across platforms using Anaconda prompt in Part I, and showcase how to install it on Windows PC in Part II, on MacOS in Part III and in Linux in Part IV.

Technical Indicators using Python Ta-Lib

We will first import the Python Ta-Lib library since we are using it to work out different indicators. Along with that, we use the python matplotlib to draw their graphs for analysis. Since we are going to be working on the stock prices, we will import the data from Yahoo Finance.

Thus, the code will be as follows:

import talib as ta import matplotlib.pyplot as plt plt.style.use('bmh')
import yfinance as yf aapl = yf.download('AAPL', '2019-1-1','2019-12-27')

In the above example, we have imported the stock price data of Apple from 1 January 2019 to 27 December 2019.

Let us start off with something simple. We will find the Simple Moving Average and the Exponential Moving Average of Apple stock data. We will use the following code:

aapl['Simple MA'] = ta.SMA(aapl['Close'],14)
aapl['EMA'] = ta.EMA(aapl['Close'], timeperiod = 14)

# Plot aapl
[['Close','Simple MA','EMA']].plot(figsize=(15,15)) plt.show()

The output is as follows:

That was simple, wasn’t it?

Recall that at the start of this tutorial on Installing Python Ta-Lib, we talked about Bollinger Bands. Well, let’s try that one now.

# Bollinger Bands aapl['upper_band'], aapl['middle_band'], aapl['lower_band'] = ta.BBANDS(aapl['Close'], timeperiod =20)

# Plot aapl[['Close','upper_band','middle_band','lower_band']].plot(figsize=(15,15)) plt.show()

Wasn’t it easy? We are sure you are getting it but let us break down the line and see how Python Ta-Lib worked out the Bollinger Bands.

To create the Bollinger Bands, we simply passed the Closing Price ie the “Close” column and defined a time period of 20 for the moving average. This is done using the “ta.BBANDS()” function.

As the Bollinger Bands consist of three bands, we store the data in the following three columns, “upper_band”, “middle_band” and “lower_band.

aapl['upper_band'], aapl['middle_band'], aapl['lower_band'] = ta.BBANDS(aapl['Close'], timeperiod =20)

Now, can you figure out from the code below, what are the parameters passed?

aapl['RSI'] = ta.RSI(aapl['Close'],14) aapl['RSI'].plot(figsize=(15,15)) plt.show()

As a bonus, we plotted the Stochastic Oscillators as well, the code is given below:

aapl['slowk'], aapl['slowd'] = ta.STOCH(aapl['High'], aapl['Low'], aapl['Close'], fastk_period=14, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0) aapl[['slowk','slowd']].plot(figsize=(15,15)) plt.show()
aapl['fastk'], aapl['fastd'] = ta.STOCHF(aapl['High'], aapl['Low'], aapl['Close'], fastk_period=14, fastd_period=3, fastd_matype=0) aapl[['fastk','fastd']].plot(figsize=(15,15)) plt.show()

Visit QuantInsti for additional insight on this topic:
https://blog.quantinsti.com/install-ta-lib-python/

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.