Description
Expected behavior
Hi after downloading the most recent version of Backtesting .py (0.6.1) and running it I noticed material increases in my stats generated. I currently use the backtest module with the trade_on_close set to True (see below). The expected behaviour based on earlier versions is that both the buy (entry price) and sell (exit price) trades are execute using the closing price on the day on which the respective signal is generated.
This functionality had been working as expected in previous versions as I use Backtesting.py to validate my own backtesting code.
Code sample
bt = Backtest(df, DIY_Signal_Generation, cash=10000, trade_on_close=True)
Actual behavior
After looking at the backtesting.py trades file and comparing this to my own prices dataframe it appears that when I sell/close a position, the price used is the following day's open price not the closing price of the day on which the sell signal was generated. This issue is exasperated due to the fact that in my price file I use adjusted prices for my close prices and all other prices (open, hi, lo) are left unadjusted. The result is that my buy trades are done with adjusted prices and my sales are done with unadjusted prices.
The trade_on_close functionality had been working in previous versions.
Additional info, steps to reproduce, full crash traceback, screenshots
Below are stats run with and without the trade_on_close set to False or True. The "false" setting (left side) appears to be working as both buy and sell trades are executed using the next day open. The "True" (right side) is showing the affect of buy trades done on the close (adjusted prices) and the sell done at the next day open (unadjusted prices).
With trade on close = False | With trade on close = True | |
---|---|---|
Start 2006-06-22 00:00:00 | Start 2006-06-22 00:00:00 | |
End 2023-07-21 00:00:00 | End 2023-07-21 00:00:00 | |
Duration 6238 days 00:00:00 | Duration 6238 days 00:00:00 | |
Exposure Time [%] 67.3645 | Exposure Time [%] 67.3645 | |
Equity Final [$] 86.7069k | Equity Final [$] 4.5889M | |
Equity Peak [$] 94.4488k | Equity Peak [$] 5.1869M | |
Return [%] 767.0694 | Return [%] 45.7890k | |
Buy & Hold Return [%] 716.6954 | Buy & Hold Return [%] 716.6954 | |
Return (Ann.) [%] 13.4977 | Return (Ann.) [%] 43.2270 | |
Volatility (Ann.) [%] 33.2193 | Volatility (Ann.) [%] 37.5969 | |
CAGR [%] 9.1177 | CAGR [%] 28.0933 | |
Sharpe Ratio 406.3219m | Sharpe Ratio 1.1497 | |
Sortino Ratio 667.6919m | Sortino Ratio 2.7208 | |
Calmar Ratio 347.0655m | Calmar Ratio 1.5193 | |
Max. Drawdown [%] -38.8910 | Max. Drawdown [%] -28.4511 | |
Avg. Drawdown [%] -3.9926 | Avg. Drawdown [%] -2.7242 | |
Max. Drawdown Duration 1064 days 00:00:00 | Max. Drawdown Duration 687 days 00:00:00 | |
Avg. Drawdown Duration 43 days 00:00:00 | Avg. Drawdown Duration 15 days 00:00:00 |
Trades 99 | | # Trades 99
Win Rate [%] 47.4747 | | Win Rate [%] 76.7677
Best Trade [%] 68.7149 | | Best Trade [%] 72.7316
Worst Trade [%] -18.1411 | | Worst Trade [%] -16.8448
Avg. Trade [%] 2.0143 | | Avg. Trade [%] 6.1904
Max. Trade Duration 357 days 00:00:00 | | Max. Trade Duration 357 days 00:00:00
Avg. Trade Duration 41 days 00:00:00 | | Avg. Trade Duration 41 days 00:00:00
Profit Factor 2.2587 | | Profit Factor 9.7871
Expectancy [%] 2.5082 | | Expectancy [%] 6.7451
SQN 1.4753 | | SQN 1.6812
Kelly Criterion 234.9810m | | Kelly Criterion 520.8895m
_strategy DIY_Signal_Gener... | | _strategy DIY_Signal_Gener...
_equity_curve Eq... | | _equity_curve Eq...
_trades Size EntryB... | | _trades Size Entry...
dtype: object | | dtype: object
Software versions
Currently running Backtesting.py version 0.6.1, Sambo version 1.25.0