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/

    Position size on previous day [-1]

    General Code/Help
    2
    2
    345
    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.
    • J
      jf last edited by

      Dear all,

      I would like to extract the position size of the "previous day" (using daily data as datafeeds).

      To obtain the position of "today" I can use the following, which works well:

      self.getposition(data=self.datas[i]).size

      As I have several datafeeds I need the [i] to refer to the individual feeds.

      However, if I would like to refer to the "previous day" I struggle...

      I have tried:

      self.getposition(data=self.datas[i][-1]).size
      as well as
      self.getposition(data=self.datas[i]).size[-1]

      but both dont work and I couldn't find any hints in the documentation/the forum.

      Would be great if someone could point me to a solution!

      Thanks!
      jf

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Position doesn't store the actual values of previous days. It only holds the current values of price and size.

        If you want to reference past positions, you need to keep them yourself.

        def __init__(self):
            self.posis = collections.defaultdict(list)
        
        def next(self):
            [self.posis[d].append(self.getposition(data=d) for d in self.datas]
        

        Of course you can also use a for loop and avoid the useless list full of None items which is generated with the one liner

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