What is broker.getvalue() supposed to return?
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
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"
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).size * datas.close) + 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.
btbacktesting broker returns sum of the cash and all open positions as a
From my experience with the backtesting broker target orders (% target orders in particular) it always open position for % of the broker value as exepcted.
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
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()