Relative Volume Indicator Using Current and Lagged Volume
Summary
This Backtrader indicator defines a relative-volume series by dividing the volume from a period earlier by current volume. Its default lookback is 20 bars, and the indicator is configured to appear in CSV output. The resulting value therefore compares lagged volume with the current bar’s volume; it is not the more common current-volume-to-average-volume ratio.
A parameter controls how missing or zero volume is handled. When missing volume is expected to appear as NaN, ordinary division is used so that the result also becomes NaN. Otherwise, a guarded division returns zero when the denominator is zero. The document provides implementation details but no trading rules, empirical evaluation, or evidence that the measure predicts returns. Users would need to verify that the lag direction matches their intended interpretation and determine how to handle exceptional volume data in their own workflow.
Key ideas
- The indicator divides volume from the configured number of bars earlier by current volume.
- The default comparison uses a lag of 20 bars.
- When missing volume is represented as NaN, the result is allowed to remain NaN.
- An alternate mode guards division by zero by returning zero.
- The document does not test the indicator as a trading signal.
Tags
Full text
# relativevolume.py
```py
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2023 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import backtrader as bt
import backtrader.indicators as btind
class RelativeVolume(bt.Indicator):
csv = True # show up in csv output (default for indicators is False)
lines = ('relvol',)
params = (
('period', 20),
('volisnan', True),
)
def __init__(self):
if self.p.volisnan:
# if missing volume will be NaN, do a simple division
# the end result for missing volumes will also be NaN
relvol = self.data.volume(-self.p.period) / self.data.volume
else:
# Else do a controlled Div with a built-in function
relvol = bt.DivByZero(
self.data.volume(-self.p.period),
self.data.volume,
zero=0.0)
self.lines.relvol = relvol
```Shown in full with attribution under the source's licence. Licence: GPL-3.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.