# Technical analysis with Python

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 – ta, pandas_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 dozen 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)

## The pandas_ta library

An alternative to ta is the pandas_ta library. Let’s get started with pandas_ta by installing it with pip:

pip install pandas_ta

When you import pandas_ta, it lets you add new indicators in a nice object-oriented fashion. pandas_ta does this by adding an extension to the pandas data frame. Here’s an example calculating TSI (True Strength Index).

import pandas_ta

data = si.get_data(“aapl”)

data.ta.tsi()

Note that by default, pandas_ta will use the “close” column in the data frame. To change this to adjusted close, we add the line above data.ta.adjusted = “adjclose”.

Here’s a couple other examples:

data.ta.sma(length = 10)

## bta-lib for technical analysis

A third package you can use for technical analysis is the bta-lib package. This library was created for several reasons, including having easy-to-ready technical indicators and making the creation of new indicators simple. Like the ones above, you can install this one with pip:

pip install bta-lib

btalib is imported like below.

import btalib

Here’s an example calculating stochastics:

data = si.get_data(“aapl”)

stoch = btalib.stochastic(data)

print(stoch.df)

You can get the default values for each indicator by looking at doc. A nice feature of btalib is that the doc strings of the indicators provide descriptions of what they do.

print(btalib.stochastic.__doc__)

By Dr. George Lane in the 50s. It compares a closing price to the price
range and tries to show convergence if the closing prices are close to the
extremes

– It will go up if closing prices are close to the highs
– It will go down if closing prices are close to the lows

It shows divergence if the extremes keep on growing/decreasing but closing
prices do not in the same manner (distance to the extremes increases)

Formula:
– hh = highest(high, period)
– ll = lowest(low, period)
– kfast = 100 * (close – ll) / (hh – ll)
– k = MovingAverage(kfast, pfast)
– d = MovingAverage(k, pslow)

See:
– http://en.wikipedia.org/wiki/Stochastic_oscillator

Aliases: stoch, Stochastic, STOCHASTIC, STOCH

Inputs: high, low, close

Outputs: k, d

Params:
– period (default: 14)
– pfast (default: 3)
– _ma (default: sma)
– pslow (default: 3)
Slow moving average period
– _maslow (default: None)
Slow moving average (if `None`, use same as fast)

TA-LIB (with compatibility flag “_talib=True”):

Change period to 5