For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Custom observer to track average returns across data feeds
-
Hello community.. :)
I am new here and to the tool. I am trying to create a custom observer that tracks portfolio returns and mean return across the universe being considered in multi feed strategy.class AvgBench(bt.observer.Observer): lines = ('dreturn',) dreturn = collections.OrderedDict() def next(self): if self._owner._getminperstatus() < 0: self.lines.dreturn[0] = 0 # dummy line ret = 0 for d in self._owner.datas: ret += d.l.close[-1] - d.l.close[0] ret /= len(self._owner.datas) date = self._owner.data.datetime.date(0) self.dreturn[date] = ret
I have a few questions -
- Is this the best way to do this?
- I dont think
if self._owner._getminperstatus() < 0
is necessary here, please confirm. - How do I also add portfolio returns to the same observer?
I know I can get the current value of the portfolio usingself._owner.broker.get_value()
but how can i get the returns? - If I add the default
bt.observers.Benchmark
and access the returns asstgy_run[0].observers.benchmark.tbench.rets
I get a initial0.0
which I do not get with my custom observer. Any thought?