For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Backtrader Broker Value Calculation Always Uses Closing Price
-
My strategy uses cheat-on-open and occasionally runs out of margin.
It turns out that the broker value calculation is hardcoded to closing prices, which leads to a mismatch between the size of the position I am trying to enter (based on opening price) and what the broker thinks I have in equity (based on closing price of the same bar).
https://github.com/mementum/backtrader/blob/e2674b1690f6366e08646d8cfd44af7bb71b3970/backtrader/brokers/bbroker.py#L437-L442if not self.p.shortcash: dvalue = comminfo.getvalue(position, data.close[0]) else: dvalue = comminfo.getvaluesize(position.size, data.close[0]) dunrealized = comminfo.profitandloss(position.size, position.price, data.close[0])
Is anyone aware of a workaround for this?
-
I think this has already been answered here actually: https://community.backtrader.com/topic/726/question-on-cheat-on-open
I am going to try the custom sizer approach.