#!/usr/bin/env python
"""Broker Observer Module - Cash and value tracking.
This module provides observers for tracking broker cash and portfolio value.
Classes:
Cash: Observer that tracks current cash level.
Value: Observer that tracks portfolio value.
Example:
>>> cerebro = bt.Cerebro()
>>> cerebro.addobserver(bt.observers.Broker)
"""
from ..observer import Observer
from ._strategy_scope import cash_value, fund_shares, observe, portfolio_value
# Get cash
[文档]
class Cash(Observer):
"""This observer keeps track the current amount of cash in the broker
Params: None
"""
_stclock = True
lines = ("cash",)
plotinfo = {"plot": True, "subplot": True}
[文档]
def next(self):
"""Update the cash value for the current period.
Gets current cash amount from the broker.
"""
self.lines[0][0] = observe(self, "cash", cash_value, self._owner)
# Get value
[文档]
class Value(Observer):
"""This observer keeps track of the current portfolio value in the broker
including the cash
Params:
- ``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_fundmode`` in the broker
documentation
Set it to ``True`` or ``False`` for a specific behavior
"""
_stclock = True
params = (("fund", None),)
lines = ("value",)
plotinfo = {"plot": True, "subplot": True}
def __init__(self):
"""Initialize the Value observer.
Sets up fund mode tracking variable.
"""
self._fundmode = None
[文档]
def start(self):
"""Start the Value observer and determine fund mode.
Detects or sets fund mode for value calculation.
"""
if self.p.fund is None:
self._fundmode = self._owner.broker.fundmode
else:
self._fundmode = self.p.fund
[文档]
def next(self):
"""Update the portfolio value for the current period.
Gets value from broker based on fund mode setting.
"""
if not self._fundmode:
self.lines[0][0] = observe(self, "value", portfolio_value, self._owner)
else:
self.lines[0][0] = observe(self, "value", portfolio_value, self._owner, fund=True)
# Get both cash and value
[文档]
class Broker(Observer):
"""This observer keeps track of the current cash amount and portfolio value in
the broker (including the cash)
Params: None
"""
_stclock = True
params = (("fund", None),)
alias = ("CashValue",)
lines = ("cash", "value")
plotinfo = {"plot": True, "subplot": True}
def __init__(self):
"""Initialize the Broker observer.
Sets up fund mode tracking variable.
"""
self._fundmode = None
[文档]
def start(self):
"""Start the Broker observer and configure plotting.
Determines fund mode and configures plot settings.
"""
if self.p.fund is None:
self._fundmode = self._owner.broker.fundmode
else:
self._fundmode = self.p.fund
if self._fundmode:
self.plotlines.cash._plotskip = True
self.plotlines.value._name = "FundValue"
[文档]
def next(self):
"""Update cash and value for the current period.
Gets current cash and portfolio value from the broker.
"""
if not self._fundmode:
value = observe(self, "value", portfolio_value, self._owner)
else:
value = observe(self, "value", portfolio_value, self._owner, fund=True)
cash = observe(self, "cash", cash_value, self._owner)
self.lines.value[0] = value
self.lines.cash[0] = cash
# fundvalue
[文档]
class FundValue(Observer):
"""This observer keeps track of the current fund-like value
Params: None
"""
_stclock = True
alias = ("FundShareValue", "FundVal")
lines = ("fundval",)
plotinfo = {"plot": True, "subplot": True}
[文档]
def next(self):
"""Update the fund value for the current period.
Gets current fund value from the broker.
"""
self.lines.fundval[0] = observe(self, "fundvalue", portfolio_value, self._owner, fund=True)
# Fund shares
[文档]
class FundShares(Observer):
"""This observer keeps track of the current fund-like shares
Params: None
"""
_stclock = True
lines = ("fundshares",)
plotinfo = {"plot": True, "subplot": True}
[文档]
def next(self):
"""Update the fund shares for the current period.
Gets current fund shares from the broker.
"""
self.lines.fundshares[0] = observe(self, "fundshares", fund_shares, self._owner)