backtrader.analyzers.logreturnsrolling module¶
LogReturnsRolling Analyzer Module - Rolling log returns calculation.
This module provides the LogReturnsRolling analyzer for calculating rolling log returns over a specified timeframe.
- Classes:
LogReturnsRolling: Analyzer that calculates rolling log returns.
示例
>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.LogReturnsRolling)
- class backtrader.analyzers.logreturnsrolling.LogReturnsRolling[源代码]¶
-
This analyzer calculates rolling returns for a given timeframe and compression
Params:
timeframe(default:None) IfNonethetimeframeof the first data in the system will be usedPass
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
data(default:None)Reference asset to track instead of the portfolio value.
- .note: this data must have been added to a
cerebroinstance with addata,resampledataorreplaydata
- .note: this data must have been added to a
firstopen(default:True)When tracking the returns of data the following is done when crossing a timeframe boundary, for example,
Years:Last
closethe previous year is used as the reference price to see the return in the current year
The problem is the first calculation, because the data has** no previous** closing price.As such, and when this parameter is True, the opening price will be used for the first calculation.
This requires the data feed to have an
openprice (forclosethe standard [0] notations will be used without a reference to a field price)Else the initial close will be used.
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
- params = (('data', None), ('firstopen', True), ('fund', None))¶
- __init__(*args, **kwargs)[源代码]¶
Initialize the LogReturnsRolling analyzer.
- 参数:
*args -- Positional arguments.
**kwargs -- Keyword arguments for analyzer parameters.
- start()[源代码]¶
Initialize the analyzer at the start of the backtest.
Sets the fund mode and initializes the rolling value queue with size controlled by compression parameter.
- notify_fund(cash, value, fundvalue, shares)[源代码]¶
Update current value from fund notification.
- 参数:
cash -- Current cash amount.
value -- Current portfolio value.
fundvalue -- Current fund value.
shares -- Number of fund shares.