backtrader.analyzers.total_value 源代码
"""TotalValue Analyzer Module - Portfolio value tracking.
This module provides the TotalValue analyzer for tracking the total
portfolio value over time.
Classes:
TotalValue: Analyzer that records portfolio value at each step.
Example:
>>> cerebro = bt.Cerebro()
>>> cerebro.addanalyzer(bt.analyzers.TotalValue, _name='val')
>>> results = cerebro.run()
>>> print(results[0].analyzers.val.get_analysis())
"""
from collections import OrderedDict
from ..analyzer import Analyzer
[文档]
class TotalValue(Analyzer):
"""This analyzer will get total value from every next.
Params:
Methods:
- Get_analysis
Returns a dictionary with returns as values and the datetime points for
each return as keys
"""
params = ()
rets = None
[文档]
def start(self):
"""Initialize the analyzer at the start of the backtest.
Creates the ordered dictionary to store value history.
"""
super().start()
self.rets = OrderedDict()
# PERFORMANCE OPTIMIZATION: Cache broker reference
self._broker = self.strategy.broker
# PERFORMANCE OPTIMIZATION: Cache attribute access, called 522K+ times
[文档]
def next(self):
"""Record the total portfolio value for the current bar.
Gets the current portfolio value from the broker and stores it
keyed by datetime.
"""
# Calculate the return
super().next()
# Cache attribute access for performance
self.rets[self.datas[0].datetime.datetime()] = self._broker.getvalue()
[文档]
def get_analysis(self):
"""Return the total value analysis results.
Returns:
OrderedDict: Dictionary mapping datetimes to portfolio values.
"""
return self.rets