For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
getposition().size inside bt.observer
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.
backtrader administrators last edited by
@kav said in getposition().size inside bt.observer:
either in an observer
Look at the examples which take the broker value. The same technique should do it.