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/

    What is broker.getvalue() supposed to return?

    General Discussion
    3
    6
    1241
    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.
    • M
      manos last edited by

      Hello!

      If I call order_target_percent(0.1) with no existing position, it's often trying to short with 90% of the account value. Not every time, sometimes it goes long as well - but always it's calculating 90% instead of 10%.

      I'm using a custom broker, and after stepping through strategy.py, I've come to the conclusion that it's either calculating wrong (unlikely), or I misunderstood what the broker is supposed to be returning for getvalue().

      This is in the docs:
      Returns the portfolio value of the given datas (if datas is None, then the total portfolio value will be returned (alias: getvalue)
      Which reads to me like it's the sum of positions.. unless there is no position, in which case it will return the total cash.

      However I read this post a while back: https://community.backtrader.com/topic/1178/how-is-the-value-calculated/4 which stated "Broker value = Cash + Value of the open positions"

      And implemented getvalue() thusly:

          def getvalue(self, datas=None):
              """ Broker value = Cash + Value of the open positions
                  https://community.backtrader.com/topic/1178/how-is-the-value-calculated/4
              """
              if datas:
                  # Return the value of open positions (@last price) plus cash.
                  # Using Total cash here, as that represents account value accurately.
                  return (self.getposition(datas[0]).size * datas[0].close[0]) + self.wallet_balance["total"]
              else:
                  # Return the wallet total
                  return self.wallet_balance["total"]
      

      So if I have $100K and that's what getvalue() returns, the strategy.py logic is certainly trying to make the position $90K. (I won't explain the chain of logic here, because I assume it's right, and I'm holding the broker wrong).

      What am I misunderstanding here? (thanks!)

      M 1 Reply Last reply Reply Quote 0
      • M
        manos @manos last edited by

        To follow up - the really confusing bit is:

                      elif target < value:
                          size = comminfo.getsize(price, value - target)
                          return self.sell(data=data, size=size, price=price, **kwargs)
        

        in order_target_value(). The target should be less than the value, because I want a position that is 10% of the size of the account value. So.. it shouldn't be trying to sell in this scenario :)

        Further evidence that broker.getvalue() should return the value of positions, and never cash?
        And indeed looking at the ccxt broker, it seems it's only returning the value of positions.

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          bt backtesting broker returns sum of the cash and all open positions as a broker value.

          From my experience with the backtesting broker target orders (% target orders in particular) it always open position for % of the broker value as exepcted.

          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by

            @manos said in What is broker.getvalue() supposed to return?:

            To follow up - the really confusing bit is:

            This is not confusing. In this case value is defined as

            value = self.broker.getvalue(datas=[data])
            

            which is the value of the open position in data.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            M 1 Reply Last reply Reply Quote 1
            • M
              manos @ab_trader last edited by

              @ab_trader

              Ohhh. Facepalm.

              Now the docs make sense to me.
              Returns the portfolio value of the given datas (if datas is None, then the total portfolio value will be returned

              Thank you!

              1 Reply Last reply Reply Quote 1
              • Chen Xu
                Chen Xu last edited by

                I think it makes sense to have broker.getvalues(datas=None) to get all the position values plus the cash.

                But it is very confusing to have broker.getvalues([datas[j]]) return all the next position values from ticker j plus all the cash in the broker?

                IMHO, it is better designed in the way such coherence is expected that all_the_cash_in_the_broker + sum([broker.get_values(datas=[d]) for d in datas) == broker.get_values()

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