Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    2
    2
    866
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      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[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 = cerebro.run()
      result[0].datas[0].position_size.get(size = dataframe.shape[0])
      

      where dataframe is the original data fed to cerebro.

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @kav 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.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors