backtrader.analyzers.vwr module¶
VWR Analyzer Module - Variability-Weighted Return calculation.
This module provides the VWR (Variability-Weighted Return) analyzer, an alternative to the Sharpe ratio using log returns.
- Classes:
VWR: Analyzer that calculates VWR metric.
Example
>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.VWR, _name='vwr')
>>> results = cerebro.run()
>>> print(results[0].analyzers.vwr.get_analysis())
- class backtrader.analyzers.vwr.VWR[source]¶
Bases:
TimeFrameAnalyzerBaseVariability-Weighted Return: Better SharpeRatio with Log Returns
Alias:
VariabilityWeightedReturn
See:
Params:
timeframe(default:None) IfNonethen the complete return over the entire backtested period will be reportedPass
TimeFrame.NoTimeFrameto consider the entire dataset with no time constraintscompression(default:None)Only used for sub-day timeframes to, for example, work on an hourly timeframe by specifying “TimeFrame.Minutes” and 60 as compression
If None, then the compression of the first data in the system will be used
tann(default:None)Number of periods to use for the annualization (normalization) of the average returns. If
None, then standardtvalues will be used, namely:days: 252weeks: 52months: 12years: 1
tau(default:2.0)Factor for the calculation (see the literature)
sdev_max(default:0.20)Max standard deviation (see the literature)
fund(default:None)If None, the actual mode of the broker (fundmode - True/False) will be autodetected to decide if the returns are based on the total net asset value or on the fund value. See
set_fundmodein the broker documentationSet it to
TrueorFalsefor a specific behavior
- - Get_analysis
Returns a dictionary with returns as values and the datetime points for each return as keys
The returned dict contains the following keys:
vwr: Variability-Weighted Return
- params = (('tann', None), ('tau', 0.2), ('sdev_max', 2.0), ('fund', None))¶
- __init__(*args, **kwargs)[source]¶
Initialize the VWR analyzer.
- Parameters:
*args – Positional arguments.
**kwargs – Keyword arguments for analyzer parameters.
- start()[source]¶
Initialize the analyzer at the start of the backtest.
Sets the fund mode and initializes lists to track period start and end values.
- stop()[source]¶
Calculate the VWR metric when backtest ends.
VWR = rnorm100 * (1 - (sdev_p / sdev_max)^tau) where sdev_p is the standard deviation of period returns.