Koding Books

Professional, free coding tutorials

Accumulation/Distribution Line indicator

The Accumulation/Distribution Line (ADL) indicator. It is a technical analysis indicator that uses volume and price data to assess whether a stock is being accumulated or distributed.

ADL Calculation

The Accumulation/Distribution Line indicator is calculated using the following formula:

ADL = Previous ADL + [(Current Period’s Money Flow Volume / Current Period’s Volume) x Multiplier]

Where:

  • Previous ADL is the ADL value from the previous period
  • The current Close is the closing price of the current period
  • The previous Close is the closing price of the previous period
  • High is the highest price of the current period
  • Low is the lowest price of the current period
  • Volume is the volume of the current period

The ADL is cumulative, meaning that each period’s ADL value is added to the previous period’s ADL value.

Trading

The Accumulation/Distribution Line (ADL) indicator is used in trading to identify potential buy or sell signals. The ADL is a volume-based indicator that measures money flow into and out of a security.

When the ADL rises, it indicates that the security is being accumulated, meaning buyers are more aggressive than sellers. This is a bullish signal, and traders may consider buying the security.

Conversely, when the ADL falls, it indicates that the security is being distributed, which means sellers are more aggressive than buyers. This is a bearish signal, and traders may consider selling the security.

Traders may also look for divergences between the ADL and the security price. For example, if the price of the security is rising, but the ADL is falling, it may indicate that the buying pressure is weakening, and a reversal may be imminent. Conversely, if the price of the security is falling, but the ADL is rising, it may indicate that the selling pressure is weakening, and a reversal may be imminent.

It is important to note that the ADL is just one tool in a trader’s toolbox and should be used with other technical indicators and analysis techniques to make informed trading decisions.

Algorithm

Algorithm for calculating the Accumulation/Distribution Line indicator:

  1. Initialize the ADL value for the first period to 0.
  2. For each subsequent period: a. Calculate the money flow multiplier using the formula: ((Close – Previous Close) / (High – Low)) Volume b. Calculate the money flow volume using the formula: Money Flow Multiplier Volume c. Add the money flow volume to the previous period’s ADL value to get the current period’s ADL value. d. Repeat steps a-c for each period in the data set.
  3. The resulting list of ADL values represents the Accumulation/Distribution Line indicator for the given data set.

Note that this algorithm assumes you have access to the necessary data for each period, including the closing price, high price, low price, and volume.

Python

Test

The test_calculate_adl function tests the calculate_adl function using a sample data set. The expected ADL values for the sample data set are pre-calculated and stored in the expected_adl list. The test asserts that the output of the calculate_adl function matches the expected_adl list.

def test_calculate_adl():
    close_prices = [10, 12, 14, 16, 18]
    high_prices = [11, 13, 15, 17, 19]
    low_prices = [9, 11, 13, 15, 17]
    volumes = [100, 200, 300, 400, 500]
    expected_adl = [0, 10000, 30000, 60000, 100000]
    assert calculate_adl(close_prices, high_prices, low_prices, volumes) == expected_adl

Code

This implementation uses NumPy’s array operations to calculate each period’s money flow multiplier and volume. It also uses NumPy’s cumsum function to calculate the cumulative sum of the money flow volume array, which gives us the ADL values for each period.

Note that we set the first value of the money_flow_multiplier array to 0 since there is no previous period to compare it to. We then calculate the money_flow_volume array and the adl array using NumPy’s array operations.

import numpy as np

def calculate_adl(close_prices, high_prices, low_prices, volumes):
    money_flow_multiplier = ((close_prices - np.roll(close_prices, 1)) / (high_prices - low_prices)) * volumes
    money_flow_multiplier[0] = 0 # set the first value to 0
    money_flow_volume = money_flow_multiplier * volumes
    adl = np.cumsum(money_flow_volume)
    return adl.tolist()

C++

Test

#include <iostream>
#include <vector>
#include <numeric>
#include <cassert>

void test_calculate_adl() {
    std::vector<double> close_prices = {10, 12, 14, 16, 18};
    std::vector<double> high_prices = {11, 13, 15, 17, 19};
    std::vector<double> low_prices = {9, 11, 13, 15, 17};
    std::vector<double> volumes = {100, 200, 300, 400, 500};
    std::vector<double> expected_adl = {0, 10000, 30000, 60000, 100000};
    std::vector<double> adl(close_prices.size());
    calculate_adl(close_prices, high_prices, low_prices, volumes, adl);
    assert(adl == expected_adl);
}

void calculate_adl(const std::vector<double>& close_prices, const std::vector<double>& high_prices, const std::vector<double>& low_prices, const std::vector<double>& volumes, std::vector<double>& adl) {
    adl[0] = 0; // initialize the ADL value for the first period to 0
    for (int i = 1; i < close_prices.size(); i++) {
        double money_flow_multiplier = ((close_prices[i] - close_prices[i-1]) / (high_prices[i] - low_prices[i])) * volumes[i];
        double money_flow_volume = money_flow_multiplier * volumes[i];
        adl[i] = adl[i-1] + money_flow_volume;
    }
}

int main() {
    test_calculate_adl();
    std::cout << "All tests passed!\n";
    return 0;
}

Code

In this C++ implementation, the calculate_adl the function takes in four constant reference parameters (close_prices, high_prices, low_prices, and volumes) and one non-constant reference parameter (adl). The adl the function modifies parameters to store the ADL values for each period.

The test_calculate_adl function tests the calculate_adl function using a sample data set. The expected ADL values for the sample data set are pre-calculated and stored in the expected_adl vector. The test asserts that the output of the calculate_adl function matches the expected_adl vector.

Note that this implementation assumes that the input and output vectors have the same length. If this is not the case, you may need to add additional error checking to the function.

#include <iostream>
#include <vector>
#include <numeric>
#include <cassert>

void test_calculate_adl() {
    std::vector<double> close_prices = {10, 12, 14, 16, 18};
    std::vector<double> high_prices = {11, 13, 15, 17, 19};
    std::vector<double> low_prices = {9, 11, 13, 15, 17};
    std::vector<double> volumes = {100, 200, 300, 400, 500};
    std::vector<double> expected_adl = {0, 10000, 30000, 60000, 100000};
    std::vector<double> adl(close_prices.size());
    calculate_adl(close_prices, high_prices, low_prices, volumes, adl);
    assert(adl == expected_adl);
}

void calculate_adl(const std::vector<double>& close_prices, const std::vector<double>& high_prices, const std::vector<double>& low_prices, const std::vector<double>& volumes, std::vector<double>& adl) {
    adl[0] = 0; // initialize the ADL value for the first period to 0
    for (int i = 1; i < close_prices.size(); i++) {
        double money_flow_multiplier = ((close_prices[i] - close_prices[i-1]) / (high_prices[i] - low_prices[i])) * volumes[i];
        double money_flow_volume = money_flow_multiplier * volumes[i];
        adl[i] = adl[i-1] + money_flow_volume;
    }
}

int main() {
    test_calculate_adl();
    std::cout << "All tests passed!\n";
    return 0;
}

The last byte…

The Accumulation/Distribution Line is a technical analysis indicator that uses volume and price data to assess whether a stock is being accumulated or distributed. The ADL indicator is based on the idea that the volume of a stock is a measure of the buying and selling pressure on that stock.

The ADL indicator is calculated by adding the money flow volume for each period to the previous period’s ADL value. The money flow volume is calculated by multiplying the money flow multiplier by the volume for each period. The money flow multiplier is calculated using each period’s closing price, high price, low price, and volume.

The ADL indicator can identify buying and selling pressure trends for a stock. If the ADL is trending upwards, it suggests that the stock is being accumulated, while a downward trend suggests that the stock is being distributed. The ADL can also be used to confirm trends in the price of stock since a trend in the ADL that is consistent with the price trend suggests that the trend is likely to continue.

The ADL indicator is commonly used with other technical analysis indicators to confirm trends and identify potential trading opportunities. It is important to note that, like all technical analysis indicators, the ADL is not a perfect predictor of future price movements. It should be used in conjunction with other forms of analysis and risk management strategies.

Ali Kayani

https://www.linkedin.com/in/ali-kayani-silvercoder007/

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

The Quest for low latency in finance

Dual Thrust Trading Algorithm

Python Decorators

The Term Structure of Commodity Prices: A Mathematical and Computational Perspective