Skip to content

Commit

Permalink
Update with new backtest example
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Apr 12, 2024
1 parent 072c813 commit 8b7b2f8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
76 changes: 45 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,52 +139,66 @@ For example if you run the [moving average example trading bot](./examples/cross
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
====================Portfolio overview============================
* Number of orders: 10
* Initial balance: 400.0000 EUR
* Final balance: 431.8837 EUR
* Total net gain: 28.4171 EUR
* Total net gain percentage: 7.1043%
* Growth rate: 7.9709%
* Growth 31.8837 EUR
====================Positions overview========================

/&# #&( 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
.&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Number of orders: 10
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&. Initial balance: 400.0
&&&&&&&# /((( &&&&&&&&&&&&*((( .&&&&&&&. Final balance: 431.1499
&&&&&&&&&&&&&&&&&&& (((( &&&&&&&& (((( &&&&&&&&&&&&&&&&&&& Total net gain: 28.5542 7.139%
(((&&&&&&&& (((( &&&&&& (((( &&&&&&&&&(( Growth: 31.1499 7.787%
/((((((((((&&&&&&&&&& (((, &&&&&& (((**&&&&&&&&&((((((((((( Number of trades closed: 4
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Number of trades open(end of backtest): 2
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Percentage positive trades: 60.0%
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Percentage negative trades: 20.0%
((((( &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&( Average trade size: 98.9886 EUR
((((( &&&&&&&&&&&&&&&&&&&&&&&&, Average trade duration: 184.0 hours
((((( &&&&&&&&&&&&&#
((((( #&&&&&&&&&&###
((((( &&&&&&&&&&&###.
.((((( &&&&&&&&&&&&&&###(
((((( &&&&&&&&&&&&&&&&#(((/
((((( &&&&&&&&&&&&&&&&&&&@((((
((((( &&&&&&&&&&&&&&&&&&&&&&((((
((((( &&&&&&&&&&&&&&&&&&&&&&&&((((,
.((((( &&&&&&&&&&&&&&&&&&&&&&&&&&((((
((((( &&&&&&&&&&&&&&&&&&&&&&&&&&&&((((
((((( &&&&&&&&&&&&&&&&&&&&&&&&&&&&&((((
((((( &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&((((
((((( &&&&&&&&&&&&&&&&&&&&&&&&&&&&&#((((
(((((((((((((((((((#########&&&&&&&&&&&&&&&&&&&&&&&&&&&&(((((
(((((((((((((((((((((######&&&&&&&&&&&&&&&&&&&&&&&&&&&&((((
Positions overview
╭────────────┬──────────┬──────────────────────┬───────────────────────┬──────────────┬───────────────┬───────────────────────────┬────────────────┬───────────────╮
│ Position │ Amount │ Pending buy amount │ Pending sell amount │ Cost (EUR) │ Value (EUR) │ Percentage of portfolio │ Growth (EUR) │ Growth_rate │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ EUR │ 214.219 │ 0 │ 0 │ 214.219214.21949.6010% │ 0 │ 0.0000% │
│ EUR │ 217.04400217.044217.04450.3407% │ 00.0000% │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ BTC │ 0.0031 │ 0 │ 0 │ 107.095110.401 │ 25.5627% │ 3.30663.0875% │
│ BTC │ 0.003 00104.372106.8424.7802% │ 2.46782.3644% │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ DOT │ 21.3291 │ 0 │ 0 │ 107.104 │ 107.264 │ 24.8363% │ 0.16 │ 0.1494% │
│ DOT │ 21.329500107.138107.26624.8791% │ 0.128 0.1195% │
╰────────────┴──────────┴──────────────────────┴───────────────────────┴──────────────┴───────────────┴───────────────────────────┴────────────────┴───────────────╯
====================Trades overview===========================
* Number of trades closed: 4
* Number of trades open: 2
* Percentage of positive trades: 60.0%
* Percentage of negative trades: 20.0%
* Average trade size: 98.8728 EUR
* Average trade duration: 183.5 hours
Trades overview
╭─────────┬─────────────────────┬─────────────────────┬────────────────────┬──────────────┬──────────────────┬───────────────────────┬────────────────────┬─────────────────────╮
│ 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 20:00:00 │ │ 2976.65 │ 107.104 │ 0 │ 0.0000% │ 5.0215 │ │
│ DOT-EUR │ 2023-11-30 18:00:00 │ │ 3207.26107.13800.0000% │ 5.023 │ │
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-11-29 14:00:00 │ │ 3006.65107.095 │ 0 │ 0.0000% │ 34546.6 │ │
│ BTC-EUR │ 2023-11-29 12:00:00 │ │ 3237.26104.37200.0000% │ 34790.7 │ │
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-11-08 00:00:00 │ 2023-11-14 16:00:00 │ 160 │ 99.22651.33521.3456% │ 33075.533520.6
│ BTC-EUR │ 2023-11-07 22:00:002023-11-14 14:00:0016099.23372.53952.5591% │ 33077.933924.4
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-11-06 16:00:00 │ 2023-11-06 20:00:00 │ 4 │ 97.8607 │ -0.0026 │ -0.0026% │ 32620.232619.4
│ BTC-EUR │ 2023-11-06 14:00:002023-11-06 18:00:00498.2854 │ -0.4248 │ -0.4322% │ 32761.832620.2
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ DOT-EUR │ 2023-10-30 06:00:00 │ 2023-11-14 00:00:00 │ 354 │ 100.551 │ 24.8794 │ 24.7430% │ 4.0375 │ 5.0365 │
│ DOT-EUR │ 2023-10-30 04:00:002023-11-14 00:00:00356100.53724.288624.1588% │ 4.05655.0365
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│ BTC-EUR │ 2023-09-13 16:00:00 │ 2023-09-22 16:00:00 │ 216 │ 97.8529 │ 2.2051 │ 2.2534% │ 24463.225014.5
│ BTC-EUR │ 2023-09-13 14:00:002023-09-22 14:00:0021697.89762.15082.1970% │ 24474.425012.1
╰─────────┴─────────────────────┴─────────────────────┴────────────────────┴──────────────┴──────────────────┴───────────────────────┴────────────────────┴─────────────────────╯
==================================================================
```
### Backtest experiments
Expand Down
4 changes: 0 additions & 4 deletions examples/backtest/algorithm/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
if not algorithm.has_position(target_symbol) \
and is_crossover(fast, slow) \
and is_above_trend(fast, trend):
print(f"opening trade on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price}")

algorithm.create_limit_order(
target_symbol=target_symbol,
order_side=OrderSide.BUY,
Expand All @@ -88,7 +86,6 @@ def apply_strategy(self, algorithm: Algorithm, market_data):

if algorithm.has_position(target_symbol) \
and is_below_trend(fast, slow):
print(f"closing trade on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price}")
open_trades = algorithm.get_open_trades(
target_symbol=target_symbol
)
Expand All @@ -105,5 +102,4 @@ def apply_strategy(self, algorithm: Algorithm, market_data):
current_price=market_data[f"{symbol}-ticker"]["bid"],
stop_loss_percentage=self.stop_loss_percentage
):
print(f"stop los triggered on {algorithm.config['BACKTESTING_INDEX_DATETIME']} with price {price} ")
algorithm.close_trade(open_trade)
File renamed without changes.

0 comments on commit 8b7b2f8

Please sign in to comment.