Technical indicators Accumulation Distribution Indicator (AD)


Background

The accumulation/distribution indicator (A/D) is a cumulative indicator that uses volume and price to assess whether a stock is being accumulated or distributed. The A/D measure seeks to identify divergences between the stock price and the volume flow. It provides a measure of the commitment of bulls and bears to the market and is used to detect divergences between volume and price action - signs that a trend is weakening.

If the price is rising but the indicator is falling, then it suggests that buying or accumulation volume may not be enough to support the price rise and a price decline could be forthcoming.

The Accumulation/Distribution is calculated using the following formula: AD = AD + ((Close - Low) - (High - Close)) / (High - Low) * Volume

If a security’s price is in a downtrend while the A/D line is in an uptrend, then the indicator shows there may be buying pressure and the security’s price may reverse to the upside. Conversely, if a security’s price is in an uptrend while the A/D line is in a downtrend, then the indicator shows there may be selling pressure, or higher distribution. This warns that the price may be due for a decline.

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()

indexDateTickerAdj CloseCloseHighLowOpenVolumegarmin_klass_volrsiatrmacdmacd signalbb_lowbb_midbb_highsmaema
002014-04-29A35.04953438.12589338.30472237.17453438.1187404688612.0-0.002274NaNNaNNaNNaNNaNNaNNaNNaNNaN
112014-04-29AAL33.47674235.50999835.65000234.97000135.2000018994200.0-0.000788NaNNaNNaNNaNNaNNaNNaNNaNNaN
222014-04-29AAPL18.63333321.15464221.28500021.05392821.205000337377600.0-0.006397NaNNaNNaNNaNNaNNaNNaNNaNNaN
332014-04-29ABBV34.03498551.36999951.52999950.75999850.9399995601300.0-0.062705NaNNaNNaNNaNNaNNaNNaNNaNNaN
442014-04-29ABT31.83151838.54000138.72000138.25999838.3699994415600.0-0.013411NaNNaNNaNNaNNaNNaNNaNNaNNaN
# remove irrelevant columns
df = df.drop('index', axis=1)
#df = df.drop('level_0', axis=1)
df.head()

DateTickerAdj CloseCloseHighLowOpenVolumegarmin_klass_volrsiatrmacdmacd signalbb_lowbb_midbb_highsmaema
02014-04-29A35.04953438.12589338.30472237.17453438.1187404688612.0-0.002274NaNNaNNaNNaNNaNNaNNaNNaNNaN
12014-04-29AAL33.47674235.50999835.65000234.97000135.2000018994200.0-0.000788NaNNaNNaNNaNNaNNaNNaNNaNNaN
22014-04-29AAPL18.63333321.15464221.28500021.05392821.205000337377600.0-0.006397NaNNaNNaNNaNNaNNaNNaNNaNNaN
32014-04-29ABBV34.03498551.36999951.52999950.75999850.9399995601300.0-0.062705NaNNaNNaNNaNNaNNaNNaNNaNNaN
42014-04-29ABT31.83151838.54000138.72000138.25999838.3699994415600.0-0.013411NaNNaNNaNNaNNaNNaNNaNNaNNaN
import pandas_ta

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

# compute AD

def compute_ad(stock_data):
    ad = pandas_ta.ad(high=stock_data['High'],
                        low=stock_data['Low'],
                        close=stock_data['Close'],
                        volume=stock_data['Volume'],
                        length=14)
    return ad

df['ad'] = df_new.groupby(level=1, group_keys=False).apply(compute_ad)
df.tail()

Adj CloseCloseHighLowOpenVolumegarmin_klass_volrsiatrmacdmacd signalbb_lowbb_midbb_highsmaemaad
DateTicker
2024-04-25XYL130.610001130.610001131.199997128.100006129.619995963600.00.00026461.4827450.6653060.3551460.2552764.8455914.8635024.881413128.482000128.5826951.381747e+08
YUM141.559998141.559998142.169998140.389999141.9799961693100.00.00007665.6681630.3225660.5508300.2857844.9134824.9379684.962454138.497000138.6585638.902989e+07
ZBH119.750000119.750000121.349998118.769997120.7099991078800.00.00020635.636078-0.350196-0.889200-0.6464804.7728464.8358884.898931125.012999123.5655697.870077e+07
ZBRA292.529999292.529999293.290009271.630005274.359985674700.00.00135556.1722720.500501-0.391439-0.2424565.5887305.6663875.744044288.205502284.2623625.349014e+07
ZTS153.360001153.360001153.589996150.039993150.9700014567200.00.00017839.8096191.374957-3.202379-3.5843534.9674085.0650655.162723157.579352157.0035212.636284e+08
# 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 AD on the second chart
ax2.plot(aapl_new['ad'], color='orange', linewidth=1)
ax2.set_title('AAPL Accumulation Distribution Indicator')
Text(0.5, 1.0, 'AAPL Accumulation Distribution Indicator')

AAPL Accumulation Distribution Indicator

Reference

Accumulation/Distribution Indicator (A/D): What it Tells You by Cory Mitchell on Investopedia
e


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 !