backtesting.py
backtesting.py copied to clipboard
calculation of `Buy & Hold Return`
this is how Buy & Hold Return is calculated:
c = data.Close.values
s.loc['Buy & Hold Return [%]'] = (c[-1] - c[0]) / c[0] * 100 # long-only return
so it's calced use day one and the day last.
Expected Behavior
Buy & Hold Return is used for compare with strategy gain. Therefore, I guess they should started at same time, since the strategy get enough data to work on.
Take cross ma strategy example, use 5 days for short line and 10 days for long line, the strategy can only works from 10th trade day. It's better to make comparision since this day
- Backtesting version: 0.3
One thing that is nice about the backtesting.py framework is its generic nature. I have a number of strategies and many of them rely on the entire period for a representative benchmark.
Having said that, it would be nice if it were easier to specify a "warming period" for both the strategy and the benchmark. I haven't come up with a clean way to do that as of yet, though I haven't really dug into it.
As a feature, it would be kind of nice if there were a way to specify a warming_period as a number of candles to ignore for the plot and stats and thus the benchmark, etc.
eg.
bt = Backtest(df, smacross, warming_period=10)
Full disclosure, I've been away for a bit so this might have been implemented in a different fashion. I'm not sure.
a way to specify a
warming_periodas a number of candles to ignore for the plot and stats
Done automatically like this for backtest and plots:
https://github.com/kernc/backtesting.py/blob/4f21a652d5292f93d5a717cc25f068fd2c35dcc2/backtesting/backtesting.py#L1146-L1149
Therefore, you can extend the warming period by containing an indicator with sufficient leading np.nan values.
I like that it's done automatically. We should extended it more consistently into stats (Buy&Hold computation) — PR welcome.
I am also having a similar issue because of non configurable warming up period explained in #477
Has the feature "warming_period" been added to make sure that the strategy tested and the buy and hold strategy are calculated on the same timeframe?