Skip to content

Commit 369c796

Browse files
authored
Fix stop losses bug when using 100% of the portfolio value (#306)
* Fix stop losses bug when using 100% of the portfolio value * Add poetry.lock and pyproject.toml to the commit * Fix flake warnings
1 parent 05c26fd commit 369c796

37 files changed

+21461
-1454
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,6 @@ bumpversion.egg-info/
151151
**/backtest_reports/
152152
**/backtest_reports/*
153153
**/databases/
154-
.vscode/
154+
.vscode/
155+
.logs
156+
venv

README.md

+87-113
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
1-
<br/>
2-
<div align="center">
3-
<h1><a href="https://investing-algorithm-framework.com" target="_blank">Investing Algorithm Framework</a></h4>
4-
</div>
5-
<br/>
6-
7-
<div align="center">
8-
<b>Rapidly build and deploy quantitative strategies and trading bots</b>
9-
</div>
10-
<br/>
11-
12-
<p align="center">
13-
<a target="_blank" href="https://investing-algorithm-framework.com">View Docs</a>
14-
<a href="https://investing-algorithm-framework.com/Getting%20Started/installation)">Getting Started</a>
15-
</p>
1+
<div align="center"> <h1><a href="https://investing-algorithm-framework.com" target="_blank">Investing Algorithm Framework</a></h1> <p><b>Rapidly build, backtest, and deploy quantitative strategies and trading bots</b></p> <a target="_blank" href="https://investing-algorithm-framework.com">📖 View Documentation</a> | <a href="https://investing-algorithm-framework.com/Getting%20Started/installation">🚀 Getting Started</a> </div>
162

173
---
184

19-
<a href=https://investing-algorithm-framework.com><img src="https://img.shields.io/badge/docs-website-brightgreen"></a>
20-
[![Build](https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/publish.yml/badge.svg)](https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/publish.yml)
21-
[![Tests](https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/test.yml)
22-
[![Downloads](https://pepy.tech/badge/investing-algorithm-framework)](https://pepy.tech/badge/investing-algorithm-framework)
23-
[![Current Version](https://img.shields.io/pypi/v/investing_algorithm_framework.svg)](https://img.shields.io/pypi/v/investing_algorithm_framework.svg)
24-
<a href="https://www.reddit.com/r/InvestingBots/"><img src="https://img.shields.io/reddit/subreddit-subscribers/investingbots?style=social"></a> <br/>
25-
[![GitHub stars](https://img.shields.io/github/stars/coding-kitties/investing-algorithm-framework.svg?style=social&label=Star&maxAge=1)](https://github.com/SeaQL/sea-orm/stargazers/) If you like what we do, consider starring, sharing and contributing!
5+
<div align="center"> <a href="https://investing-algorithm-framework.com">
6+
<a target="_blank" href="https://discord.gg/dQsRmGZP"><img src="https://img.shields.io/discord/1345358169777635410.svg?color=7289da&label=TradeBotLab%20Discord&logo=discord&style=flat"></a>
7+
<img src="https://img.shields.io/badge/docs-website-brightgreen"></a> <a href="https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/publish.yml"><img src="https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/publish.yml/badge.svg"></a> <a href="https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/test.yml"><img src="https://github.com/coding-kitties/investing-algorithm-framework/actions/workflows/test.yml/badge.svg"></a> <a href="https://pepy.tech/project/investing-algorithm-framework"><img src="https://pepy.tech/badge/investing-algorithm-framework"></a> <a href="https://pypi.org/project/investing-algorithm-framework/"><img src="https://img.shields.io/pypi/v/investing-algorithm-framework.svg"></a> <a href="https://www.reddit.com/r/InvestingBots/"><img src="https://img.shields.io/reddit/subreddit-subscribers/investingbots?style=social"></a> <a href="https://github.com/coding-kitties/investing-algorithm-framework/stargazers"><img src="https://img.shields.io/github/stars/coding-kitties/investing-algorithm-framework.svg?style=social&label=Star"></a>
8+
</div>
9+
10+
> If you like what we do, consider starring, sharing and contributing!
2611
2712
## Sponsors
2813

@@ -34,66 +19,54 @@
3419
</picture>
3520
</a>
3621

37-
## Features and planned features:
38-
39-
- [x] **Based on Python 3.10+**: Windows, macOS and Linux.
40-
- [x] **Documentation**: [Documentation](https://investing-algorithm-framework.com)
41-
- [x] **Persistence of portfolios, orders, positions and trades**: Persistence is achieved through sqlite.
42-
- [x] **Limit orders**: Create limit orders for buying and selling.
43-
- [x] **Trade models**: Models and functionality for trades, trades stop losses (fixed and trailing) and take profits (fixed and trailing).
44-
- [x] **Market data sources**: Market data sources for OHLCV data and ticker data, and extendible with custom data and events.
45-
- [x] **Polars and Pandas dataframes support** Out of the box dataframes support for fast data processing ([pola.rs](https://pola.rs/), [pandas](https://pandas.pydata.org/)).
46-
- [x] **Azure Functions support**: Stateless running for cloud function deployments in Azure.
47-
- [x] **Live trading**: Live trading.
48-
- [x] **Backtesting and performance analysis reports** [example](./examples/backtest_example)
49-
- [x] **Backtesting multiple algorithms with different backtest date ranges** [example](./examples/backtests_example)
50-
- [x] **Backtesting and results evaluation**: Compare multiple backtests and run experiments. Save and load backtests. Save strategies as part of the backtest. [docs](https://investing-algorithm-framework.com/Getting%20Started/backtesting)
51-
- [x] **Order execution**: Currently support for a wide range of crypto exchanges through [ccxt](https://github.com/ccxt/ccxt) (Support for traditional asset brokers is planned).
52-
- [x] **Web API**: Rest API for interacting with your deployed trading bot
53-
- [x] **PyIndicators**: Works natively with [PyIndicators](https://github.com/coding-kitties/PyIndicators) for technical analysis on your Pandas and Polars dataframes.
54-
- [ ] **Builtin WebUI (Planned)**: Builtin web UI to manage your bot and evaluate your backtests.
55-
- [ ] **Manageable via Telegram (Planned)**: Manage the bot with Telegram.
56-
- [ ] **Performance status report via Web UI and telegram(Planned)**: Provide a performance status of your current trades.
57-
- [ ] **CI/CD integration (Planned)**: Tools for continuous integration and deployment (version tracking, comparison of backtests, automatic deployments).
58-
- [ ] **Tracing and replaying of strategies in backtests (Planned)**: Tracing and replaying of strategies in backtests for specific dates and date ranges so you can evaluate your strategy step by step.
59-
- [ ] **AWS Lambda support (Planned)**: Stateless running for cloud function deployments in AWS.
60-
- [ ] **Azure App services support (Planned)**: deployments in Azure app services with Web UI.
61-
62-
## Quickstart
63-
64-
1. First install the framework using `pip`. The Investing Algorithm Framework is hosted on [PyPi](https://pypi.org/project/investing-algorithm-framework/).
22+
23+
## 🌟 Features
24+
25+
- [x] Python 3.10+: Cross-platform support for Windows, macOS, and Linux.
26+
- [x] Backtesting: Simulate strategies with detailed performance reports.
27+
- [x] Live Trading: Execute trades in real-time with support for multiple exchanges via ccxt.
28+
- [x] Portfolio Management: Manage portfolios, trades, and positions with persistence via SQLite.
29+
- [x] Market Data Sources: Fetch OHLCV, ticker, and custom data with support for Polars and Pandas.
30+
- [x] Azure Functions Support: Deploy stateless trading bots to Azure.
31+
- [x] Web API: Interact with your bot via REST API.
32+
- [x] PyIndicators Integration: Perform technical analysis directly on your dataframes.
33+
- [x] Extensibility: Add custom strategies, data providers, order executors so you can connect your trading bot to your favorite exchange or broker.
34+
35+
## 🚀 Quickstart
36+
37+
Installation
38+
Install the framework via [PyPI](https://pypi.org/project/investing-algorithm-framework/):
39+
40+
1. First install the framework using `pip`. The Investing Algorithm Framework is hosted on [PyPi].
6541

6642
```bash
67-
$ pip install investing-algorithm-framework
43+
pip install investing-algorithm-framework
6844
```
6945

70-
2. Next, just run:
46+
Run the following command to set up your project:
7147

7248
```bash
73-
$ investing-algorithm-framewor init
49+
investing-algorithm-framewor init
7450
```
7551

76-
or if you want the web version:
52+
For a web-enabled version:
7753

7854
```bash
79-
$ investing-algorithm-framework init --web
55+
investing-algorithm-framework init --web
8056
```
81-
> You can always change the app to the web version by changing the `app.py` file.
8257

83-
The command will create the file `app.py` and an example script called `strategy.py`.
58+
This will create:
8459

85-
From there, you start building your trading bot in the `strategy.py`.
60+
* app.py: The entry point for your bot.
61+
* strategy.py: A sample strategy file to get started.
8662

87-
More information can be found on our [docs](https://investing-algorithm-framework.com/)
88-
89-
> Make sure you leave the `app.py` file as is, as it is the entry point for the framework.
90-
> You can change the `bot.py` file to your liking and add other files to the working directory.
91-
> The framework will automatically pick up the files in the working directory.
92-
```
63+
> Note: Keep the app.py file as is. You can modify strategy.py and add additional files to build your bot.
64+
> You can always change the app to the web version by changing the `app.py` file.
9365
94-
## Example implementation
66+
---
9567

96-
The following algorithm connects to binance and buys BTC every 2 hours.
68+
## 📈 Example: A Simple Trading Bot
69+
The following example connects to Binance and buys BTC every 2 hours.
9770

9871
```python
9972
import logging.config
@@ -157,7 +130,7 @@ if __name__ == "__main__":
157130

158131
> You can find more examples [here](./examples) folder.
159132
160-
## Backtesting and experiments
133+
## 🔍 Backtesting
161134

162135
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.
163136

@@ -270,45 +243,38 @@ Take profits overview
270243
╰────────────────────┴───────────────┴──────────┴──────────┴─────────────────────────────────────┴──────────────┴────────────────┴─────────────────────────────────┴──────────────┴─────────────┴───────────────╯
271244
```
272245
273-
### Backtest experiments
246+
## 🌐 Deployment to Azure
274247
275-
The framework also supports backtest experiments. Backtest experiments allows you to
276-
compare multiple algorithms and evaluate their performance. Ideally,
277-
you would do this by parameterizing your strategy and creating a factory function that
278-
creates the algorithm with the different parameters. You can find an example of this
279-
in the [backtest experiments example](./examples/backtest_experiment).
248+
Prerequisites
280249
281-
## Broker/Exchange configuration
250+
Ensure Azure Functions Core Tools are installed:
282251
283-
The framework has by default support for [ccxt](https://github.com/ccxt/ccxt).
284-
This should allow you to connect to a lot of brokers/exchanges.
252+
```bash
253+
npm install -g azure-functions-core-tools@4 --unsafe-perm true
254+
```
255+
Deploying to Azure
256+
Use the provided deployment script:
257+
258+
This script:
285259
286-
```python
287-
from investing_algorithm_framework import PortfolioConfiguration, \
288-
MarketCredential, create_app
289-
app = create_app()
290-
app.add_market_credential(
291-
MarketCredential(
292-
market="<your market>",
293-
api_key="<your api key>",
294-
secret_key="<your secret key>",
295-
)
296-
)
297-
app.add_portfolio_configuration(
298-
PortfolioConfiguration(
299-
market="<your market>",
300-
initial_balance=400,
301-
trading_symbol="EUR"
302-
)
303-
)
304260
```
261+
investing-algorithm-framework deploy
262+
```
263+
264+
This script:
265+
266+
* Creates Azure resources (e.g., storage accounts, function apps).
267+
* Sets environment variables for the Azure Function.
268+
* Deploys your bot to Azure.
305269
306-
## Performance
270+
## 📚 Documentation
271+
Comprehensive documentation is available at [investing-algorithm-framework.com](https://investing-algorithm-framework.com).
307272
308-
We are continuously working on improving the performance of the framework. If
309-
you have any suggestions, please let us know.
273+
## 🛠️ Development
310274
311-
## Installation for local development
275+
Local Development
276+
277+
Clone the repository and install dependencies using Poetry:
312278
313279
The framework is built with poetry. To install the framework for local development, you can run the following commands:
314280
@@ -329,11 +295,11 @@ To run the tests, you can run the following command:
329295
python -m unittest discover -s tests
330296
```
331297

332-
## Disclaimer
298+
## ⚠️ Disclaimer
299+
333300

334301
If you use this framework for your investments, do not risk money
335-
which you are afraid to lose, until you have clear understanding how
336-
the framework works. We can't stress this enough:
302+
which you are afraid to lose, until you have clear understanding how the framework works. We can't stress this enough:
337303
338304
BEFORE YOU START USING MONEY WITH THE FRAMEWORK, MAKE SURE THAT YOU TESTED
339305
YOUR COMPONENTS THOROUGHLY. USE THE SOFTWARE AT YOUR OWN RISK.
@@ -342,6 +308,8 @@ THE AUTHORS AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR INVESTMENT RESU
342308
Also, make sure that you read the source code of any plugin you use or
343309
implementation of an algorithm made with this framework.
344310
311+
We welcome contributions! Check out the project board and issues to get started.
312+
345313
## Documentation
346314
347315
All the documentation can be found online
@@ -354,17 +322,7 @@ contributing page at the website.
354322
If you'd like to chat with investing-algorithm-framework users
355323
and developers, [join us on Slack](https://inv-algo-framework.slack.com) or [join us on reddit](https://www.reddit.com/r/InvestingBots/)
356324
357-
## Acknowledgements
358-
359-
We want to thank all contributors to this project. A full list of all the people that contributed to the project can be
360-
found [here](https://github.com/investing-algorithms/investing-algorithm-framework/blob/master/AUTHORS.md)
361-
362-
### [Bugs / Issues](https://github.com/investing-algorithms/investing-algorithm-framework/issues?q=is%3Aissue)
363-
364-
If you discover a bug in the framework, please [search our issue tracker](https://github.com/investing-algorithms/investing-algorithm-framework/issues?q=is%3Aissue)
365-
first. If it hasn't been reported, please [create a new issue](https://github.com/investing-algorithms/investing-algorithm-framework/issues/new).
366-
367-
### Contributing
325+
## 🤝 Contributing
368326
369327
The investing algorithm framework is a community driven project.
370328
We welcome you to participate, contribute and together help build the future trading bots developed in python.
@@ -376,4 +334,20 @@ You can pick up a task by assigning yourself to it.
376334
**Note** before starting any major new feature work, *please open an issue describing what you are planning to do*.
377335
This will ensure that interested parties can give valuable feedback on the feature, and let others know that you are working on it.
378336
379-
**Important:** Always create your feature or hotfix against the `develop` branch, not `main`.
337+
**Important:** Always create your feature or hotfix against the `develop` branch, not `main`.
338+
339+
## 📬 Support
340+
341+
* Slack Community
342+
* Reddit Community
343+
344+
345+
## 🏆 Acknowledgements
346+
347+
We want to thank all contributors to this project. A full list of all the people that contributed to the project can be
348+
found [here](https://github.com/investing-algorithms/investing-algorithm-framework/blob/master/AUTHORS.md)
349+
350+
### [Bugs / Issues](https://github.com/investing-algorithms/investing-algorithm-framework/issues?q=is%3Aissue)
351+
352+
If you discover a bug in the framework, please [search our issue tracker](https://github.com/investing-algorithms/investing-algorithm-framework/issues?q=is%3Aissue)
353+
first. If it hasn't been reported, please [create a new issue](https://github.com/investing-algorithms/investing-algorithm-framework/issues/new).

examples/backtest_example/run_backtest.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,15 @@ def _is_buy_signal(self, data):
174174

175175

176176
app = create_app(name="GoldenCrossStrategy")
177+
app.add_market(
178+
market="BINANCE", trading_symbol="EUR", initial_balance=400,
179+
)
177180
app.add_strategy(CrossOverStrategy)
178181
app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)
179182
app.add_market_data_source(bitvavo_dot_eur_ohlcv_2h)
180183
app.add_market_data_source(bitvavo_btc_eur_ticker)
181184
app.add_market_data_source(bitvavo_dot_eur_ticker)
182185

183-
# Add a portfolio configuration of 400 euro initial balance
184-
app.add_portfolio_configuration(
185-
PortfolioConfiguration(
186-
market="BINANCE", trading_symbol="EUR", initial_balance=400,
187-
)
188-
)
189186

190187
if __name__ == "__main__":
191188
end_date = datetime(2023, 12, 2)

examples/test.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from investing_algorithm_framework import download
2+
3+
4+
if __name__ == "__main__":
5+
data = download(
6+
symbol="BTC/USDT",
7+
market="binance",
8+
data_type="ohlcv",
9+
start_date="2023-01-01",
10+
end_date="2023-10-01",
11+
window_size=200,
12+
pandas=True,
13+
save=True,
14+
storage_dir="./data"
15+
)
16+
print(data)

investing_algorithm_framework/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
CCXTTickerMarketDataSource, CSVOHLCVMarketDataSource, \
1919
CSVTickerMarketDataSource, AzureBlobStorageStateHandler
2020
from .create_app import create_app
21+
from .download_data import download
2122

2223
__all__ = [
2324
"Algorithm",
@@ -75,5 +76,6 @@
7576
"MarketDataType",
7677
"TradeRiskType",
7778
"Context",
78-
"APPLICATION_DIRECTORY"
79+
"APPLICATION_DIRECTORY",
80+
"download"
7981
]

0 commit comments

Comments
 (0)