From 9a36ca4b5e234b75efc2319fc5061c0faf15ca90 Mon Sep 17 00:00:00 2001 From: marcvanduyn Date: Thu, 4 Jan 2024 12:54:41 +0100 Subject: [PATCH] Fix backtesting example --- README.md | 164 +++++++++++++----- .../backtesting/backtesting_moving_average.py | 116 +++++++++++++ .../backtest_market_data_source_service.py | 5 +- version.py | 2 +- 4 files changed, 244 insertions(+), 43 deletions(-) create mode 100644 examples/backtesting/backtesting_moving_average.py diff --git a/README.md b/README.md index 572dd8d7..a2b8b54f 100644 --- a/README.md +++ b/README.md @@ -137,82 +137,164 @@ For more examples, check out the [examples](./examples/backtesting) folder. ### Backtesting report You can use the ```pretty_print_backtest``` function to print a backtest report. -For example if you run the [moving average example trading bot](./examples/backtesting/moving_average.py) +For example if you run the [moving average example trading bot](./examples/backtesting/backtesting_moving_average.py) you will get the following backtesting report: ```bash ====================Backtest report=============================== -* Start date: 2023-08-24 00:00:00 -* End date: 2023-12-02 00:00:00 -* Number of days: 100 -* Number of runs: 1201 +* Start date: 2023-01-01 00:00:00 +* End date: 2023-12-31 00:00:00 +* Number of days: 364 +* Number of runs: 4369 ====================Portfolio overview============================ -* Number of orders: 41 +* Number of orders: 126 * Initial balance: 400.0000 EUR -* Final balance: 440.9081 EUR -* Total net gain: 40.4219 EUR -* Total net gain percentage: 10.1055% -* Growth rate: 10.2270% -* Growth 40.9081 EUR +* Final balance: 470.5919 EUR +* Total net gain: 70.5919 EUR +* Total net gain percentage: 17.6480% +* Growth rate: 17.6480% +* Growth 70.5919 EUR ====================Positions overview======================== ╭────────────┬──────────┬──────────────────┬──────────────┬───────────────┬───────────────────────────┬────────────────┬───────────────╮ │ Position │ Amount │ Pending amount │ Cost (EUR) │ Value (EUR) │ Percentage of portfolio │ Growth (EUR) │ Growth_rate │ ├────────────┼──────────┼──────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤ -│ EUR │ 330.317 │ 0 │ 330.317 │ 330.317 │ 74.9174% │ 0 │ 0.0000% │ -├────────────┼──────────┼──────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤ -│ DOT │ 22.0034 │ 0 │ 110.105 │ 110.591 │ 25.0826% │ 0.4863 │ 0.4416% │ +│ EUR │ 470.592 │ 0 │ 470.592 │ 470.592 │ 100.0000% │ 0 │ 0.0000% │ ╰────────────┴──────────┴──────────────────┴──────────────┴───────────────┴───────────────────────────┴────────────────┴───────────────╯ ====================Trades overview=========================== -* Number of trades closed: 20 -* Number of trades open: 1 -* Percentage of positive trades: 39.02439024390244% -* Percentage of negative trades: 58.536585365853654% -* Average trade size: 101.5585 EUR -* Average trade duration: 92.5 hours +* Number of trades closed: 63 +* Number of trades open: 0 +* Percentage of positive trades: 31.746031746031743% +* Percentage of negative trades: 68.25396825396825% +* Average trade size: 108.7449 EUR +* Average trade duration: 78.34920634920636 hours ╭─────────┬─────────────────────┬─────────────────────┬────────────────────┬──────────────┬──────────────────┬───────────────────────┬────────────────────┬─────────────────────╮ │ Pair │ Open date │ Close date │ Duration (hours) │ Size (EUR) │ Net gain (EUR) │ Net gain percentage │ Open price (EUR) │ Close price (EUR) │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-11-30 16:00:00 │ │ 259.148 │ 110.105 │ 0 │ 0.0000% │ 5.004 │ │ +│ BTC-EUR │ 2023-12-19 04:00:00 │ 2023-12-24 12:00:00 │ 128 │ 113.924 │ 1.3369 │ 1.1735% │ 39284 │ 39745 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-12-16 12:00:00 │ 2023-12-17 08:00:00 │ 20 │ 116.856 │ -1.431 │ -1.2246% │ 38952 │ 38475 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-12-14 04:00:00 │ 2023-12-16 00:00:00 │ 44 │ 117.978 │ -2.457 │ -2.0826% │ 39326 │ 38507 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-11-28 22:00:00 │ 2023-12-11 04:00:00 │ 294 │ 113.53 │ 16.2756 │ 14.3360% │ 34403 │ 39335 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-11-23 00:00:00 │ 2023-11-26 20:00:00 │ 92 │ 113.371 │ -0.3795 │ -0.3347% │ 34355 │ 34240 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-11-19 22:00:00 │ 2023-11-22 00:00:00 │ 50 │ 113.144 │ -4.9665 │ -4.3895% │ 34286 │ 32781 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-11-16 04:00:00 │ 2023-11-16 22:00:00 │ 18 │ 114.154 │ -4.5936 │ -4.0241% │ 34592 │ 33200 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-11-06 10:00:00 │ 2023-11-13 14:00:00 │ 172 │ 114.765 │ 6.132 │ 5.3431% │ 32790 │ 34542 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-11-05 10:00:00 │ 2023-11-05 22:00:00 │ 12 │ 114.635 │ -1.365 │ -1.1907% │ 32753 │ 32363 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-10-31 20:00:00 │ 2023-11-03 08:00:00 │ 60 │ 114.184 │ -0.588 │ -0.5150% │ 32624 │ 32456 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-10-29 16:00:00 │ 2023-10-31 16:00:00 │ 48 │ 114.632 │ -0.9905 │ -0.8641% │ 32752 │ 32469 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-10-14 04:00:00 │ 2023-10-27 22:00:00 │ 330 │ 107.44 │ 27.0732 │ 25.1984% │ 25581 │ 32027 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-10-06 18:00:00 │ 2023-10-09 16:00:00 │ 70 │ 108.244 │ -1.4104 │ -1.3030% │ 26401 │ 26057 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-09-27 10:00:00 │ 2023-10-05 20:00:00 │ 202 │ 106.424 │ 3.0408 │ 2.8573% │ 25339 │ 26063 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-09-12 16:00:00 │ 2023-09-21 18:00:00 │ 218 │ 106.304 │ 3.6784 │ 3.4603% │ 24160 │ 24996 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-09-07 02:00:00 │ 2023-09-10 16:00:00 │ 86 │ 105.943 │ 0.1144 │ 0.1080% │ 24078 │ 24104 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-09-06 00:00:00 │ 2023-09-06 12:00:00 │ 12 │ 105.772 │ -0.4136 │ -0.3910% │ 24039 │ 23945 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-09-04 18:00:00 │ 2023-09-04 22:00:00 │ 4 │ 107.924 │ -0.9765 │ -0.9048% │ 23983 │ 23766 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-08-29 14:00:00 │ 2023-09-01 02:00:00 │ 60 │ 108.627 │ -5.6072 │ -5.1619% │ 25262 │ 23958 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-08-26 10:00:00 │ 2023-08-26 20:00:00 │ 10 │ 108.617 │ -0.072 │ -0.0663% │ 24137 │ 24121 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-08-23 14:00:00 │ 2023-08-26 08:00:00 │ 66 │ 108.243 │ 0.324 │ 0.2993% │ 24054 │ 24126 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-08-14 06:00:00 │ 2023-08-15 18:00:00 │ 36 │ 107.344 │ -0.044 │ -0.0410% │ 26836 │ 26825 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-08-13 02:00:00 │ 2023-08-14 02:00:00 │ 24 │ 107.48 │ -0.476 │ -0.4429% │ 26870 │ 26751 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-08-08 06:00:00 │ 2023-08-11 04:00:00 │ 70 │ 108.876 │ 0.779 │ 0.7155% │ 26555 │ 26745 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-08-02 02:00:00 │ 2023-08-04 18:00:00 │ 64 │ 107.752 │ -1.8 │ -1.6705% │ 26938 │ 26488 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-07-31 00:00:00 │ 2023-08-01 00:00:00 │ 24 │ 109.536 │ -0.656 │ -0.5989% │ 26716 │ 26556 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-07-27 12:00:00 │ 2023-07-31 00:00:00 │ 84 │ 109.614 │ -0.7298 │ -0.6658% │ 26735 │ 26557 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-07-20 10:00:00 │ 2023-07-24 12:00:00 │ 98 │ 108.184 │ -2.668 │ -2.4662% │ 27046 │ 26379 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-07-13 22:00:00 │ 2023-07-14 22:00:00 │ 24 │ 109.415 │ -4.0872 │ -3.7355% │ 28055 │ 27007 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-07-11 00:00:00 │ 2023-07-12 22:00:00 │ 46 │ 111.212 │ -1.904 │ -1.7120% │ 27803 │ 27327 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-07-01 10:00:00 │ 2023-07-05 16:00:00 │ 102 │ 111.9 │ -0.148 │ -0.1323% │ 27975 │ 27938 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-06-29 16:00:00 │ 2023-07-01 04:00:00 │ 36 │ 112.316 │ -1.032 │ -0.9188% │ 28079 │ 27821 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-06-28 02:00:00 │ 2023-06-28 06:00:00 │ 4 │ 111.28 │ 0.008 │ 0.0072% │ 27820 │ 27822 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-06-17 00:00:00 │ 2023-06-26 08:00:00 │ 224 │ 107.564 │ 17.892 │ 16.6339% │ 23903 │ 27879 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-06-09 12:00:00 │ 2023-06-10 04:00:00 │ 16 │ 106.386 │ -1.075 │ -1.0105% │ 24741 │ 24491 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-06-07 04:00:00 │ 2023-06-07 22:00:00 │ 18 │ 108.33 │ -3.1906 │ -2.9453% │ 25193 │ 24451 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-06-03 10:00:00 │ 2023-06-05 10:00:00 │ 48 │ 108.996 │ -1.2943 │ -1.1875% │ 25348 │ 25047 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-05-26 22:00:00 │ 2023-05-31 14:00:00 │ 112 │ 107.113 │ 2.021 │ 1.8868% │ 24910 │ 25380 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-05-23 02:00:00 │ 2023-05-24 14:00:00 │ 36 │ 108.807 │ -3.1175 │ -2.8652% │ 25304 │ 24579 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-05-20 22:00:00 │ 2023-05-21 18:00:00 │ 20 │ 107.883 │ -0.9933 │ -0.9207% │ 25089 │ 24858 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-05-18 00:00:00 │ 2023-05-19 04:00:00 │ 28 │ 108.283 │ -1.1051 │ -1.0206% │ 25182 │ 24925 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-05-14 16:00:00 │ 2023-05-17 00:00:00 │ 56 │ 109.327 │ 0.1804 │ 0.1650% │ 24847 │ 24888 │ +├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ +│ BTC-EUR │ 2023-05-04 06:00:00 │ 2023-05-07 04:00:00 │ 70 │ 110.3 │ -0.2142 │ -0.1942% │ 26262 │ 26211 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-11-24 12:00:00 │ 2023-11-27 14:00:00 │ 74 │ 110.725 │ -2.4793 │ -2.2392% │ 4.7964 │ 4.689 │ +│ BTC-EUR │ 2023-04-25 22:00:00 │ 2023-05-01 04:00:00 │ 126 │ 108.339 │ 0.7056 │ 0.6513% │ 25795 │ 25963 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-11-20 00:00:00 │ 2023-11-21 08:00:00 │ 32 │ 111.695 │ -3.8798 │ -3.4735% │ 4.9805 │ 4.8075 │ +│ BTC-EUR │ 2023-04-18 20:00:00 │ 2023-04-19 14:00:00 │ 18 │ 110.756 │ -3.552 │ -3.2070% │ 27689 │ 26801 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-11-16 20:00:00 │ 2023-11-20 00:00:00 │ 76 │ 109.263 │ 2.5245 │ 2.3105% │ 33110 │ 33875 │ +│ BTC-EUR │ 2023-04-09 20:00:00 │ 2023-04-17 02:00:00 │ 174 │ 109.578 │ 5.2332 │ 4.7758% │ 26090 │ 27336 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-11-06 10:00:00 │ 2023-11-13 14:00:00 │ 172 │ 107.996 │ 5.7387 │ 5.3138% │ 32726 │ 34465 │ +│ BTC-EUR │ 2023-04-05 10:00:00 │ 2023-04-06 02:00:00 │ 16 │ 109.654 │ -1.3776 │ -1.2563% │ 26108 │ 25780 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-10-20 12:00:00 │ 2023-10-27 08:00:00 │ 164 │ 100.652 │ 11.2336 │ 11.1608% │ 3.5526 │ 3.9491 │ +│ BTC-EUR │ 2023-03-29 10:00:00 │ 2023-04-02 22:00:00 │ 108 │ 109.507 │ -0.861 │ -0.7863% │ 26073 │ 25868 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-10-14 04:00:00 │ 2023-10-27 22:00:00 │ 330 │ 99.957 │ 24.6753 │ 24.6859% │ 25630 │ 31957 │ +│ BTC-EUR │ 2023-03-26 14:00:00 │ 2023-03-27 16:00:00 │ 26 │ 108.788 │ -3.9018 │ -3.5866% │ 25902 │ 24973 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-10-14 04:00:00 │ 2023-10-17 14:00:00 │ 82 │ 100.946 │ -1.1753 │ -1.1643% │ 3.5643 │ 3.5228 │ +│ BTC-EUR │ 2023-03-24 02:00:00 │ 2023-03-25 00:00:00 │ 22 │ 109.628 │ -2.1252 │ -1.9385% │ 26102 │ 25596 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-10-07 08:00:00 │ 2023-10-08 06:00:00 │ 22 │ 101.211 │ -1.0607 │ -1.0480% │ 3.874 │ 3.8334 │ +│ BTC-EUR │ 2023-03-12 22:00:00 │ 2023-03-23 00:00:00 │ 242 │ 105.391 │ 22.9143 │ 21.7421% │ 20665 │ 25158 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-09-27 10:00:00 │ 2023-10-05 20:00:00 │ 202 │ 97.4688 │ 4.0365 │ 4.1413% │ 24992 │ 26027 │ +│ BTC-EUR │ 2023-03-01 06:00:00 │ 2023-03-02 06:00:00 │ 24 │ 104.664 │ -1.0951 │ -1.0463% │ 22269 │ 22036 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-09-27 10:00:00 │ 2023-10-03 22:00:00 │ 156 │ 99.7074 │ 1.9774 │ 1.9832% │ 3.8271 │ 3.903 │ +│ BTC-EUR │ 2023-02-28 14:00:00 │ 2023-02-28 22:00:00 │ 8 │ 106.464 │ -1.368 │ -1.2849% │ 22180 │ 21895 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-09-25 12:00:00 │ 2023-09-27 04:00:00 │ 40 │ 99.7994 │ -0.3672 │ -0.3679% │ 3.8052 │ 3.7912 │ +│ BTC-EUR │ 2023-02-28 02:00:00 │ 2023-02-28 08:00:00 │ 6 │ 106.402 │ -1.104 │ -1.0376% │ 22167 │ 21937 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-09-14 18:00:00 │ 2023-09-18 02:00:00 │ 80 │ 99.4298 │ -0.1612 │ -0.1621% │ 3.8244 │ 3.8182 │ +│ BTC-EUR │ 2023-02-27 06:00:00 │ 2023-02-27 22:00:00 │ 16 │ 106.603 │ -0.8352 │ -0.7835% │ 22209 │ 22035 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-09-12 16:00:00 │ 2023-09-21 18:00:00 │ 218 │ 98.008 │ 1.64 │ 1.6733% │ 24502 │ 24912 │ +│ BTC-EUR │ 2023-02-14 18:00:00 │ 2023-02-22 02:00:00 │ 176 │ 103.52 │ 10.055 │ 9.7131% │ 20704 │ 22715 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-09-07 02:00:00 │ 2023-09-10 16:00:00 │ 86 │ 98.4656 │ 0.4592 │ 0.4664% │ 24016 │ 24128 │ +│ BTC-EUR │ 2023-02-07 22:00:00 │ 2023-02-09 04:00:00 │ 30 │ 103.973 │ -3.2112 │ -3.0885% │ 21661 │ 20992 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-09-07 00:00:00 │ 2023-09-09 02:00:00 │ 50 │ 99.3915 │ -0.307 │ -0.3089% │ 3.982 │ 3.9697 │ +│ BTC-EUR │ 2023-02-02 04:00:00 │ 2023-02-05 18:00:00 │ 86 │ 105.894 │ -2.1658 │ -2.0453% │ 21611 │ 21169 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-09-06 00:00:00 │ 2023-09-06 12:00:00 │ 12 │ 98.5558 │ -0.3116 │ -0.3162% │ 24038 │ 23962 │ +│ BTC-EUR │ 2023-01-26 04:00:00 │ 2023-01-30 22:00:00 │ 114 │ 106.13 │ -1.245 │ -1.1731% │ 21226 │ 20977 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-09-05 12:00:00 │ 2023-09-06 16:00:00 │ 28 │ 99.6655 │ -0.7846 │ -0.7872% │ 3.9759 │ 3.9446 │ +│ BTC-EUR │ 2023-01-20 18:00:00 │ 2023-01-25 02:00:00 │ 104 │ 104.924 │ 4.6004 │ 4.3845% │ 19797 │ 20665 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ BTC-EUR │ 2023-09-04 18:00:00 │ 2023-09-04 22:00:00 │ 4 │ 98.2565 │ -0.0164 │ -0.0167% │ 23965 │ 23961 │ +│ BTC-EUR │ 2023-01-08 22:00:00 │ 2023-01-19 02:00:00 │ 244 │ 99.6278 │ 19.2324 │ 19.3043% │ 16069 │ 19171 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-09-04 08:00:00 │ 2023-09-04 22:00:00 │ 14 │ 99.9766 │ -1.2274 │ -1.2277% │ 3.9748 │ 3.926 │ +│ BTC-EUR │ 2023-01-08 14:00:00 │ 2023-01-08 20:00:00 │ 6 │ 98.9644 │ -0.5146 │ -0.5200% │ 15962 │ 15879 │ ├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤ -│ DOT-EUR │ 2023-08-25 02:00:00 │ 2023-08-25 10:00:00 │ 8 │ 99.9996 │ -0.0928 │ -0.0928% │ 4.0968 │ 4.093 │ +│ BTC-EUR │ 2023-01-02 04:00:00 │ 2023-01-08 14:00:00 │ 154 │ 99.5136 │ 2.1376 │ 2.1480% │ 15549 │ 15883 │ ╰─────────┴─────────────────────┴─────────────────────┴────────────────────┴──────────────┴──────────────────┴───────────────────────┴────────────────────┴─────────────────────╯ ================================================================== ``` diff --git a/examples/backtesting/backtesting_moving_average.py b/examples/backtesting/backtesting_moving_average.py new file mode 100644 index 00000000..34c65b5a --- /dev/null +++ b/examples/backtesting/backtesting_moving_average.py @@ -0,0 +1,116 @@ +import pathlib +from datetime import datetime, timedelta +from investing_algorithm_framework import create_app, RESOURCE_DIRECTORY, \ + TimeUnit, CCXTOHLCVMarketDataSource, Algorithm, pretty_print_backtest, \ + CCXTTickerMarketDataSource, PortfolioConfiguration + +import tulipy as tp +import pandas as pd + +# This trading strategy uses tulip indicators https://pypi.org/project/tulipy/ +# and pandas https://pypi.org/project/pandas/ + +# Define market data sources +bitvavo_btc_eur_ohlcv_2h = CCXTOHLCVMarketDataSource( + identifier="BTC/EUR-ohlcv", + market="bitvavo", + symbol="BTC/EUR", + timeframe="2h", + start_date_func=lambda : datetime.utcnow() - timedelta(days=17) +) +bitvavo_btc_eur_ticker = CCXTTickerMarketDataSource( + identifier="BTC/EUR-ticker", + market="bitvavo", + symbol="BTC/EUR", + backtest_timeframe="2h" # We want the ticker data to + # be sampled every 2 hours, inline with the strategy interval +) +app = create_app({RESOURCE_DIRECTORY: pathlib.Path(__file__).parent.resolve()}) +app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h) +app.add_market_data_source(bitvavo_btc_eur_ticker) +app.add_portfolio_configuration(PortfolioConfiguration( + initial_balance=400, + market="bitvavo", + trading_symbol="EUR", +)) +symbols = ["BTC/EUR"] + + +def is_crossover(fast_series, slow_series): + """ + Expect two numpy series (fast and slow). + With the given series it will check if the fast has a crossover with the + slow series + """ + + return fast_series[-2] <= slow_series[-2] \ + and fast_series[-1] > slow_series[-1] + + +def is_crossunder(fast_series, slow_series): + """ + Expect two numpy series (fast and slow). + With the given series it will check if the fast has a crossunder with the + slow series + """ + + return fast_series[-2] >= slow_series[-2] \ + and fast_series[-1] < slow_series[-1] + + +def is_below_trend(fast_series, slow_series): + return fast_series[-1] < slow_series[-1] + + +def is_above_trend(fast_series, slow_series): + return fast_series[-1] > slow_series[-1] + + +def is_within_rsi_bounds(rsi_series, lower_bound, upper_bound): + return lower_bound <= rsi_series[-1] <= upper_bound + + +@app.strategy( + time_unit=TimeUnit.HOUR, + interval=2, + market_data_sources=["BTC/EUR-ticker", "BTC/EUR-ohlcv"] +) +def perform_strategy(algorithm: Algorithm, market_data: dict): + + for symbol in symbols: + target_symbol = symbol.split('/')[0] + + # Don't open a new order when we already have an open order + if algorithm.has_open_orders(target_symbol): + continue + + ohlcv_data = market_data[f"{symbol}-ohlcv"] + df = pd.DataFrame( + ohlcv_data, + columns=['Datetime', 'Open', 'High', 'Low', 'Close', 'Volume'] + ) + fast = tp.sma(df["Close"].to_numpy(), period=9) + slow = tp.sma(df["Close"].to_numpy(), period=50) + price = market_data[f"{symbol}-ticker"]["bid"] + + if algorithm.has_position(target_symbol) \ + and is_crossunder(fast, slow): + algorithm.close_position(target_symbol) + elif not algorithm.has_position(target_symbol) and is_crossover( + fast, slow): + algorithm.create_limit_order( + target_symbol=target_symbol, + order_side="BUY", + price=price, + percentage_of_portfolio=25, + precision=4 + ) + + +if __name__ == "__main__": + backtest_report = app.backtest( + start_date=datetime(2023, 1, 1), + end_date=datetime(2023, 12, 31), + pending_order_check_interval="2h", + ) + pretty_print_backtest(backtest_report) \ No newline at end of file diff --git a/investing_algorithm_framework/services/market_data_source_service/backtest_market_data_source_service.py b/investing_algorithm_framework/services/market_data_source_service/backtest_market_data_source_service.py index ab645571..5f38bad2 100644 --- a/investing_algorithm_framework/services/market_data_source_service/backtest_market_data_source_service.py +++ b/investing_algorithm_framework/services/market_data_source_service/backtest_market_data_source_service.py @@ -57,6 +57,7 @@ def __init__( ) def get_data(self, identifier): + for backtest_market_data_source in self._market_data_sources: if backtest_market_data_source.identifier == identifier: backtest_market_data_source.market_credentials_service = \ @@ -66,7 +67,9 @@ def get_data(self, identifier): .config[BACKTESTING_INDEX_DATETIME], ) - return None + raise OperationalException( + f"Backtest market data source not found for {identifier}" + ) def get_ticker(self, symbol, market): market_data_source = self.get_ticker_market_data_source( diff --git a/version.py b/version.py index 1322a83f..5e2e075f 100644 --- a/version.py +++ b/version.py @@ -1,4 +1,4 @@ -VERSION = (2, 1, 0, 'alpha', 0) +VERSION = (2, 2, 0, 'alpha', 0) def get_version(version=None):