I know this should be fairly easy to do but I'm stumped. I want to get the account value by bar from a cerebro instance so I can do analysis on a spreadsheet.
For some reason I can't seem to figure out how to make this happen. I have this observer:
class AcctValue(bt.Observer):
alias = ('Value',)
lines = ('value',)
plotinfo = {"plot": True, "subplot": True}
def next(self):
self.lines.value[0] = self._owner.broker.getvalue() # Get today's account value (cash + stocks)
which I call with this code block:
# Create an instance of cerebro
cerebro = bt.Cerebro()
# Input a starting broker account value
cerebro.broker.setcash(startcash)
# Add our strategy
cerebro.addstrategy(Boll)
comminfo = forexSpreadCommisionScheme(spread = spread, leverage = leverage)
comminfo.show_spread_leverage()
cerebro.broker.addcommissioninfo(comminfo)
# Add the data to Cerebro
cerebro.adddata(data)
# Add a sizer
cerebro.addsizer(ForexSizer,risk_per_trade=.0025,ATR_mult=6)
#cerebro.addobserver(OrderObserver)
cerebro.addobserver(AcctValue)
cerebro.addobservermulti(bt.observers.BuySell, markersize=4.0) # Plots up/down arrows
# Add the analyzers we are interested in
#cerebro.addanalyzer(bt.analyzers.GrossLeverage, _name="gl")
cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="ta")
cerebro.addanalyzer(bt.analyzers.SQN, _name="sqn")
cerebro.addanalyzer(bt.analyzers.Returns, _name="ret")
cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='mysharpe')
cerebro.addanalyzer(bt.analyzers.AnnualReturn, _name='ann')
cerebro.addanalyzer(bt.analyzers.DrawDown, _name='dd')
cerebro.addanalyzer(bt.analyzers.Transactions, _name = 'transactions')
#cerebro.addanalyzer(AcctStats)
# Run over everything
strategies = cerebro.run()
firstStrat = strategies[0]
result=strategies[0]
#Get final portfolio Value
portvalue = cerebro.broker.getvalue()
pnl = portvalue - startcash
#Print out the final result
print('Final Portfolio Value: ${}'.format(round(portvalue,2)))
print('P/L: ${}'.format(round(pnl,2)))
printSharpe(firstStrat.analyzers.mysharpe.get_analysis())
printTradeAnalysis(firstStrat.analyzers.ta.get_analysis())
printSQN(firstStrat.analyzers.sqn.get_analysis())
printAnnualReturns(result.analyzers.ann.get_analysis())
printReturns(result.analyzers.ret.get_analysis())
printDD(result.analyzers.dd.get_analysis())
using
b= cerebro.Observer.AccountValue()
b
doesn't work.
When I look at the vars(result) I see:
<main.AcctValue at 0x7f931b97a610>
but I don't know how to access this.
Can anyone help me?