For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

getposition().size inside

  • I was wondering if there was a way to get getposition().size, or the same information, either in an observer or somehow out of 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[0]
            current_position = self.getposition(contract).size
            self.datas[0].position_size[0] = current_position

    where position_size is an 0 valued column I added to the data.

    which can then be recovered from as:

    result =
    result[0].datas[0].position_size.get(size = dataframe.shape[0])

    where dataframe is the original data fed to cerebro.

  • administrators

    @kav said in getposition().size inside

    either in an observer

    Look at the examples which take the broker value. The same technique should do it.

Log in to reply