# Technical Analysis with Python Contributor:
TheAutomatic.net
Visit: TheAutomatic.net Blogger, TheAutomatic.net, and Senior Data Scientist

In this post, we will introduce how to do technical analysis with Python. Python has several libraries for performing technical analysis of investments. We’re going to compare three libraries – tapandas_ta, and bta-lib.

## The ta library for technical analysis

One of the nicest features of the ta package is that it allows you to add dozens of technical indicators all at once. To get started, install the ta library using pip:

pip install ta

Next, let’s import the packages we need. We’ll be using yahoo_fin to pull in stock price data. Now, data contains the historical prices for AAPL.

import yahoo_fin.stock_info as si
import pandas as pd

# pull data from Yahoo Finance
data = si.get_data(“aapl”)

Next, let’s use ta to add in a collection of technical features. Below, we just need to specify what fields correspond to the open, high, low, close, and volume. This single call automatically adds in over 80 technical indicators, including RSI, stochastics, moving averages, MACD, ADX, and more.

data, open=”open”, high=”high”, low=”low”, close=”adjclose”, volume=”volume”)

For example, here’s the RSI values (using the standard 14-day calculation):

ta also has several modules that can calculate individual indicators rather than pulling them all in at once. These modules allow you to get more nuanced variations of the indicators. For example, if you want to calculate the 21-day RSI, rather than the default 14-day calculation, you can use the momentum module.

from ta.momentum import RSIIndicator

rsi_21 = RSIIndicator(close = data.adjclose, window = 21)

data[“rsi_21”] = rsi_21.rsi()

Similarly, we could use the trend module to calculate MACD.

from ta.trend import macd

data[“macd”] = macd(data.adjclose, window_slow = 26, window_fast = 12)