Technical indicators On-balance Volume (OBV)


Background

On-balance volume is a technical analysis indicator intended to relate price and volume, and it is based on a cumulative total volume.

OBV is generally used to confirm price moves. The idea is that volume is higher on days where the price move is in the dominant direction, for example in a strong uptrend there is more volume on up days than down days.

On-balance volume is calculated by using the following formula:

On-balance volume

Python Implementation

import pandas as pd
import sqlite3

# load S&P500 data from stored database
sp500_db = sqlite3.connect(database="sp500_data.sqlite")

df = pd.read_sql_query(sql="SELECT * FROM SP500",
                       con=sp500_db,
                       parse_dates={"Date"})
df.head()

level_0indexDateTickerAdj CloseCloseHighLowOpenVolume...rsiatrmacdmacd signalbb_lowbb_midbb_highsmaemaad
0002014-04-29A35.04953438.12589338.30472237.17453438.1187404688612.0...NaNNaNNaNNaNNaNNaNNaNNaNNaN3.204858e+06
1112014-04-29AAL33.47674235.50999835.65000234.97000135.2000018994200.0...NaNNaNNaNNaNNaNNaNNaNNaNNaN5.290623e+06
2222014-04-29AAPL18.63333321.15464221.28500021.05392821.205000337377600.0...NaNNaNNaNNaNNaNNaNNaNNaNNaN-4.328196e+07
3332014-04-29ABBV34.03498551.36999951.52999950.75999850.9399995601300.0...NaNNaNNaNNaNNaNNaNNaNNaNNaN3.273491e+06
4442014-04-29ABT31.83151838.54000138.72000138.25999838.3699994415600.0...NaNNaNNaNNaNNaNNaNNaNNaNNaN9.599290e+05

5 rows × 21 columns

# remove irrelevant columns
df = df.drop('index', axis=1)
#df = df.drop('level_0', axis=1)
df.head()

level_0DateTickerAdj CloseCloseHighLowOpenVolumegarmin_klass_volrsiatrmacdmacd signalbb_lowbb_midbb_highsmaemaad
002014-04-29A35.04953438.12589338.30472237.17453438.1187404688612.0-0.002274NaNNaNNaNNaNNaNNaNNaNNaNNaN3.204858e+06
112014-04-29AAL33.47674235.50999835.65000234.97000135.2000018994200.0-0.000788NaNNaNNaNNaNNaNNaNNaNNaNNaN5.290623e+06
222014-04-29AAPL18.63333321.15464221.28500021.05392821.205000337377600.0-0.006397NaNNaNNaNNaNNaNNaNNaNNaNNaN-4.328196e+07
332014-04-29ABBV34.03498551.36999951.52999950.75999850.9399995601300.0-0.062705NaNNaNNaNNaNNaNNaNNaNNaNNaN3.273491e+06
442014-04-29ABT31.83151838.54000138.72000138.25999838.3699994415600.0-0.013411NaNNaNNaNNaNNaNNaNNaNNaNNaN9.599290e+05
import pandas_ta

df = df.set_index(['Date','Ticker'])

# compute OBV

def compute_obv(stock_data):
    obv = pandas_ta.obv(close=stock_data['Close'],
                        volume=stock_data['Volume'],
                        length=14)
    return obv

df['obv'] = df.groupby(level=1, group_keys=False).apply(compute_obv)
df.tail()

level_0Adj CloseCloseHighLowOpenVolumegarmin_klass_volrsiatrmacdmacd signalbb_lowbb_midbb_highsmaemaadobv
DateTicker
2024-04-25XYL1232347130.610001130.610001131.199997128.100006129.619995963600.00.00026461.4827450.6653060.3551460.2552764.8455914.8635024.881413128.482000128.5826951.381747e+0842359600.0
YUM1232348141.559998141.559998142.169998140.389999141.9799961693100.00.00007665.6681630.3225660.5508300.2857844.9134824.9379684.962454138.497000138.6585638.902989e+07253995207.0
ZBH1232349119.750000119.750000121.349998118.769997120.7099991078800.00.00020635.636078-0.350196-0.889200-0.6464804.7728464.8358884.898931125.012999123.5655697.870077e+07-62096220.0
ZBRA1232350292.529999292.529999293.290009271.630005274.359985674700.00.00135556.1722720.500501-0.391439-0.2424565.5887305.6663875.744044288.205502284.2623625.349014e+0737474700.0
ZTS1232351153.360001153.360001153.589996150.039993150.9700014567200.00.00017839.8096191.374957-3.202379-3.5843534.9674085.0650655.162723157.579352157.0035212.636284e+08255609600.0
# update the database
df = df.reset_index()
df.to_sql(name="SP500",
          con=sp500_db,
          if_exists="replace",
          index=True)
sp500_db.close()
import matplotlib.pyplot as plt
from datetime import datetime

# select AAPL
aapl = df[df['Ticker'] == 'AAPL'].set_index('Date')

# only select the data from 2022-01-01
aapl_new = aapl[aapl.index > datetime(2022,1,1)]

# set the theme of the chart
plt.style.use('fivethirtyeight')
plt.rcParams['figure.figsize'] = (20,16)

# create two charts on the same figure
ax1 = plt.subplot2grid((10,1),(0,0), rowspan=4, colspan=1)
ax2 = plt.subplot2grid((10,1),(5,0), rowspan=4, colspan=1)

# plot the closing price on the first chart
ax1.plot(aapl_new['Adj Close'])
ax1.set_title('AAPL Adjust Close Price')

# plot the OBV on the second chart
ax2.plot(aapl_new['obv'], color='orange', linewidth=1)
ax2.set_title('AAPL On-balance Volume')
Text(0.5, 1.0, 'AAPL On-balance Volume')

AAPL On-balance Volume

Reference

On-Balance Volume (OBV): Definition, Formula, and Uses As Indicator by Adam Hayes on Investopedia.


Author: wenvenn
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source wenvenn !