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!
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!
Upon further testing, I don't get notify_trade() called even upon closing a position fully.
Related to this question: https://community.backtrader.com/topic/937/notify_trade-not-firing-on-subsequent-trades-when-using-tradeid/3
I'm using my own live broker, and trying to figure out why I never see notify_trade() called when an order is submitted (and completed), and a position is opened.
According to the official reply in that thread, notify_trade() should get called (in the broker, strategy, analyzers) when the position is opened, but I'm only seeing order_notify() called. I can confirm a position was opened, which implies a trade (open) happened and should have been notified? I've never seen trade_notify() called at all when running live, in fact.
Any clues?
@overcash apologies, I didn't get a notification of your reply :)
I have that at the end of my stategy's next() method.
Agree - I ran into this as well.
@overcash
My solution was:
# if we're backtesting and out of data, close open positions so they get counted properly.
# must be done on the 2nd to last, as market orders wait to get another candle, to execute based on open price.
if self.position and len(self.data) == self.data.buflen() - 1 and "live" not in self.environment:
logger.debug("Closing open position to conclude backtesting")
self.close()
I'm curious if anyone has implemented anything, or knows of some good resources to share?
Looking to implement robust position management. To me, this means a few things:
Ideally all of that will happen by simply having a strategy set leverage between say -3 and +3, though I haven't looked into how to do that - I guess I can just remove and re-add the commission scheme to cerebro.
Thanks for any pointers before I embark on doing it all my from scratch :)
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!
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.
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!)