getposition().size inside bt.observer
kav last edited by kav
I was wondering if there was a way to get
getposition().size, or the same information, either in an observer or somehow out of
cerebro.run()e.g. I want to get a vector the same length as the data with at each time point the number of position held by the strategy at that point in time.
One solution is to do something like this in the Strategy:
def next(self): contract = self.datas current_position = self.getposition(contract).size self.datas.position_size = current_position
where position_size is an 0 valued column I added to the data.
which can then be recovered from as:
result = cerebro.run() result.datas.position_size.get(size = dataframe.shape)
where dataframe is the original data fed to cerebro.
either in an observer
Look at the examples which take the broker value. The same technique should do it.