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?



  • 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!)



  • 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.



  • 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.



  • @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.



  • @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!


Log in to reply
 

});