Skip to content

Commit db0e220

Browse files
authored
289 include unrealized gains in backtesting result (#292)
* Refactor app initialization * Add sql trade model * Refactor to persistent trade service * Fix flake8 issues * Fix windows path issue * Fix windows tests * Add windows poetry build * Add windows build to test workflow * Fix windows matrix tests * Fix python version pin * Fix venv reference * Remove windows * Fix logging config * Add python 3.9 to the test matrix * Add python 3.9 support * Add python 3.8 support * Remove python 3.8 support
1 parent d992dc2 commit db0e220

File tree

136 files changed

+19817
-26756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+19817
-26756
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ jobs:
4141
fail-fast: true
4242
matrix:
4343
os: [ "ubuntu-latest", "macos-latest" ]
44-
python-version: [ "3.8", "3.9", "3.10", "3.11" ]
44+
python-version: [ "3.9", "3.10", "3.11" ]
45+
defaults:
46+
run:
47+
shell: bash
4548
runs-on: ${{ matrix.os }}
4649
steps:
4750
#----------------------------------------------
@@ -99,7 +102,7 @@ jobs:
99102
#----------------------------------------------
100103
- name: Run tests
101104
run: |
102-
source .venv/bin/activate
105+
source $VENV
103106
coverage run -m unittest discover -s tests
104107
# #----------------------------------------------
105108
# # upload coverage stats

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Features:
3737
The following algorithm connects to binance and buys BTC every 5 seconds. It also exposes an REST API that allows you to interact with the algorithm.
3838

3939
```python
40-
import logging
4140
import logging.config
4241

4342
from investing_algorithm_framework import create_app, PortfolioConfiguration, \
@@ -61,7 +60,6 @@ bitvavo_btc_eur_ticker = CCXTTickerMarketDataSource(
6160
symbol="BTC/EUR",
6261
)
6362
app = create_app()
64-
algorithm = Algorithm()
6563
# Bitvavo market credentials are read from .env file
6664
app.add_market_credential(MarketCredential(market="bitvavo"))
6765
app.add_portfolio_configuration(
@@ -71,18 +69,16 @@ app.add_portfolio_configuration(
7169
initial_balance=400
7270
)
7371
)
74-
app.add_algorithm(algorithm)
7572

7673
# Run every two hours and register the data sources
77-
@algorithm.strategy(
74+
@app.strategy(
7875
time_unit=TimeUnit.HOUR,
7976
interval=2,
8077
market_data_sources=[bitvavo_btc_eur_ticker, bitvavo_btc_eur_ohlcv_2h]
8178
)
8279
def perform_strategy(algorithm: Algorithm, market_data: dict):
8380
# Access the data sources with the indentifier
8481
polars_df = market_data["BTC-ohlcv"]
85-
8682
# Convert the polars dataframe to a pandas dataframe
8783
pandas_df = polars_df.to_pandas()
8884
ticker_data = market_data["BTC-ticker"]
@@ -93,18 +89,21 @@ def perform_strategy(algorithm: Algorithm, market_data: dict):
9389
closed_trades = algorithm.get_closed_trades()
9490

9591
# Create a buy oder
96-
algorithm.create_limit_order(
92+
order = algorithm.create_limit_order(
9793
target_symbol="BTC/EUR",
9894
order_side="buy",
9995
amount=0.01,
10096
price=ticker_data["ask"],
10197
)
98+
trade = algorithm.get_trade(order_id=order.id)
99+
algorithm.add_trailing_stop_loss(trade=trade, percentage=5)
102100

103101
# Close a trade
104-
algorithm.close_trade(trades[0].id)
102+
algorithm.close_trade(trade=trade)
105103

106104
# Close a position
107-
algorithm.close_position(positions[0].get_symbol())
105+
position = algorithm.get_position(symbol="BTC/EUR")
106+
algorithm.close_position(position)
108107

109108
if __name__ == "__main__":
110109
app.run()
@@ -116,7 +115,14 @@ if __name__ == "__main__":
116115

117116
The framework also supports backtesting and performing backtest experiments. After a backtest, you can print a report that shows the performance of your trading bot.
118117

119-
To run a single backtest you can use the example code that can be found [here](./examples/backtest).
118+
To run a single backtest you can use the example code that can be found [here](./examples/backtest_example). Simply run:
119+
120+
> Its assumed here that you have cloned the repository, installed the framework and
121+
> are in the root of the project.
122+
123+
```bash
124+
python examples/backtest_example/run_backtest.py
125+
```
120126

121127
### Backtesting report
122128

examples/app.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

examples/backtest_example/__init__.py

Whitespace-only changes.

examples/backtest_example/algorithm/__init__.py

Whitespace-only changes.

examples/backtest_example/algorithm/algorithm.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/backtest_example/algorithm/data_sources.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/backtest_example/algorithm/strategy.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

examples/backtest_example/app.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)