backtrader.analyzers.annualreturn module¶
Annual Return Analyzer Module - Annual return calculation.
This module provides the AnnualReturn analyzer for calculating year-by-year returns of a strategy.
- Classes:
AnnualReturn: Analyzer that calculates annual returns.
示例
>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.AnnualReturn, _name='annret')
>>> results = cerebro.run()
>>> print(results[0].analyzers.annret.get_analysis())
- class backtrader.analyzers.annualreturn.AnnualReturn[源代码]¶
基类:
AnalyzerThis analyzer calculates the AnnualReturns by looking at the beginning and end of the year
Params:
(None)
Member Attributes:
rets: list of calculated annual returnsret: dictionary (key: year) of annual returns
get_analysis:
Returns a dictionary of annual returns (key: year)
- __init__()[源代码]¶
Initialize the AnnualReturn analyzer.
Initializes cache lists for storing dates and values during backtesting.
- next()[源代码]¶
Cache current date and account value on each bar.
Stores the current datetime and portfolio value for later annual return calculation.
- stop()[源代码]¶
Calculate annual returns from cached data.
Iterates through cached date-value pairs to calculate returns for each calendar year. Stores results in self.rets (list) and self.ret (dictionary keyed by year).
- class backtrader.analyzers.annualreturn.MyAnnualReturn[源代码]¶
基类:
AnalyzerThis analyzer calculates the AnnualReturns by looking at the beginning and end of the year
Params:
(None)
Member Attributes:
rets: list of calculated annual returnsret: dictionary (key: year) of annual returns
get_analysis:
Returns a dictionary of annual returns (key: year)
- stop()[源代码]¶
Calculate annual returns using pandas.
Uses pandas DataFrame operations to group data by year and calculate annual returns based on beginning and ending values for each year.
备注
This method requires pandas to be installed.