order_target_percent does not work properly. It is always calculating position target value with the wrong basis using the previous close, regardless if you look-ahead or using the new
If you look at
broker.py line 365
def _get_value(), its very clear that the portfolio values is only calculated at
order_target_percent thenh calculates account/position value with the current close (see line 1301,
target *= self.broker.getvalue()).
What should've happened, for
cheat_on_open, is that
broker.get_value() should return the value of the account at the current
open for calculating sizing but instead it uses previous
close for sizing.
It is not obvious if you trade only a single security at 10% sizing but errors will show up all over the place if you trade more than one security with more than 95% of the portfolio invested.
The intellectually lazy way to bypass this bug is to never allocate over 90-95% of the whole portfolio to prevent this problem from showing up. The downside of salvaging backtesting this way is that it is difficult to verify
backtrader output verified against Excel or simple vectorized backtests.