Rapidly build, backtest, and deploy quantitative strategies and trading bots
📖 View Documentation | 🚀 Getting StartedIf you like what we do, consider starring, sharing and contributing!
- Python 3.10+: Cross-platform support for Windows, macOS, and Linux.
- Backtesting: Simulate strategies with detailed performance reports.
- Live Trading: Execute trades in real-time with support for multiple exchanges via ccxt.
- Portfolio Management: Manage portfolios, trades, and positions with persistence via SQLite.
- Market Data Sources: Fetch OHLCV, ticker, and custom data with support for Polars and Pandas.
- Azure Functions Support: Deploy stateless trading bots to Azure.
- Web API: Interact with your bot via REST API.
- PyIndicators Integration: Perform technical analysis directly on your dataframes.
- Extensibility: Add custom strategies, data providers, order executors so you can connect your trading bot to your favorite exchange or broker.
Installation Install the framework via PyPI:
- First install the framework using
pip
. The Investing Algorithm Framework is hosted on [PyPi].
pip install investing-algorithm-framework
Run the following command to set up your project:
investing-algorithm-framewor init
For a web-enabled version:
investing-algorithm-framework init --web
This will create:
- app.py: The entry point for your bot.
- strategy.py: A sample strategy file to get started.
Note: Keep the app.py file as is. You can modify strategy.py and add additional files to build your bot. You can always change the app to the web version by changing the
app.py
file.
The following example connects to Binance and buys BTC every 2 hours.
import logging.config
from dotenv import load_dotenv
from investing_algorithm_framework import create_app, PortfolioConfiguration, \
TimeUnit, CCXTOHLCVMarketDataSource, Context, CCXTTickerMarketDataSource, \
MarketCredential, DEFAULT_LOGGING_CONFIG, Algorithm, Context
load_dotenv()
logging.config.dictConfig(DEFAULT_LOGGING_CONFIG)
# OHLCV data for candles
bitvavo_btc_eur_ohlcv_2h = CCXTOHLCVMarketDataSource(
identifier="BTC-ohlcv",
market="BITVAVO",
symbol="BTC/EUR",
time_frame="2h",
window_size=200
)
# Ticker data for orders, trades and positions
bitvavo_btc_eur_ticker = CCXTTickerMarketDataSource(
identifier="BTC-ticker",
market="BITVAVO",
symbol="BTC/EUR",
)
app = create_app()
# Bitvavo market credentials are read from .env file, or you can
# set them manually as params
app.add_market_credential(MarketCredential(market="bitvavo"))
app.add_portfolio_configuration(
PortfolioConfiguration(
market="bitvavo", trading_symbol="EUR", initial_balance=40
)
)
algorithm = Algorithm(name="test_algorithm")
# Define a strategy for the algorithm that will run every 10 seconds
@algorithm.strategy(
time_unit=TimeUnit.SECOND,
interval=10,
market_data_sources=[bitvavo_btc_eur_ticker, bitvavo_btc_eur_ohlcv_2h]
)
def perform_strategy(context: Context, market_data: dict):
# Access the data sources with the indentifier
polars_df = market_data["BTC-ohlcv"]
ticker_data = market_data["BTC-ticker"]
unallocated_balance = context.get_unallocated()
positions = context.get_positions()
trades = context.get_trades()
open_trades = context.get_open_trades()
closed_trades = context.get_closed_trades()
app.add_algorithm(algorithm)
if __name__ == "__main__":
app.run()
You can find more examples here folder.
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.
To run a single backtest you can use the example code that can be found here. Simply run:
Its assumed here that you have cloned the repository, installed the framework and are in the root of the project.
python examples/backtest_example/run_backtest.py
You can use the pretty_print_backtest
function to print a backtest report.
For example if you run the moving average example trading bot
you will get the following backtesting report:
:%%%#+- .=*#%%% 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: 15
.:=+#%%%%%*###%%%%#*+#%%%%%%*+-: Initial balance: 400.0
+%%%%%%%%%%%%%%%%%%%= Final balance: 453.07
:++ .=#%%%%%%%%%%%%%*- Total net gain: 18.85 4.7%
:++: :+%%%%%%#-. Growth: 53.07 13.27%
:++: .%%%%%#= Number of trades closed: 2
:++: .#%%%%%#*= Number of trades open(end of backtest): 2
:++- :%%%%%%%%%+= Percentage positive trades: 75.0%
.++- -%%%%%%%%%%%+= Percentage negative trades: 25.0%
.++- .%%%%%%%%%%%%%+= Average trade size: 98.79 EUR
.++- *%%%%%%%%%%%%%*+: Average trade duration: 189.0 hours
.++- %%%%%%%%%%%%%%#+=
=++........:::%%%%%%%%%%%%%%*+-
.=++++++++++**#%%%%%%%%%%%%%++.
Positions overview
╭────────────┬──────────┬──────────────────────┬───────────────────────┬──────────────┬───────────────┬───────────────────────────┬────────────────┬───────────────╮
│ Position │ Amount │ Pending buy amount │ Pending sell amount │ Cost (EUR) │ Value (EUR) │ Percentage of portfolio │ Growth (EUR) │ Growth_rate │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ EUR │ 253.09 │ 0 │ 0 │ 253.09 │ 253.09 EUR │ 55.86% │ 0.00 EUR │ 0.00% │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ BTC │ 0.0028 │ 0 │ 0 │ 97.34 │ 99.80 EUR │ 22.03% │ 2.46 EUR │ 2.52% │
├────────────┼──────────┼──────────────────────┼───────────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│ DOT │ 19.9521 │ 0 │ 0 │ 100 │ 100.18 EUR │ 22.11% │ 0.18 EUR │ 0.18% │
╰────────────┴──────────┴──────────────────────┴───────────────────────┴──────────────┴───────────────┴───────────────────────────┴────────────────┴───────────────╯
Trades overview
╭───────────────────┬────────────────┬───────────────────────┬───────────────────────────┬──────────────────┬──────────────────┬─────────────┬────────────────────┬──────────────────────────────┬─────────────────────────────────╮
│ Pair (Trade id) │ Status │ Amount (remaining) │ Net gain (EUR) │ Open date │ Close date │ Duration │ Open price (EUR) │ Close price's (EUR) │ High water mark │
├───────────────────┼────────────────┼───────────────────────┼───────────────────────────┼──────────────────┼──────────────────┼─────────────┼────────────────────┼──────────────────────────────┼─────────────────────────────────┤
│ BTC/EUR (1) │ CLOSED │ 0.0040 (0.0000) BTC │ 1.98 (2.02%) │ 2023-09-13 14:00 │ 2023-09-22 12:00 │ 214.0 hours │ 24490.4 │ 24984.93 │ 25703.77 EUR (2023-09-19 14:00) │
├───────────────────┼────────────────┼───────────────────────┼───────────────────────────┼──────────────────┼──────────────────┼─────────────┼────────────────────┼──────────────────────────────┼─────────────────────────────────┤
│ DOT/EUR (2) │ CLOSED, SL, TP │ 24.7463 (0.0000) DOT │ 13.53 (13.53%) │ 2023-10-30 04:00 │ 2023-11-15 02:00 │ 382.0 hours │ 4.04 │ 4.23, 4.38, 4.24, 4.25, 4.79 │ 5.45 EUR (2023-11-12 10:00) │
├───────────────────┼────────────────┼───────────────────────┼───────────────────────────┼──────────────────┼──────────────────┼─────────────┼────────────────────┼──────────────────────────────┼─────────────────────────────────┤
│ BTC/EUR (3) │ CLOSED │ 0.0030 (0.0000) BTC │ -0.20 (-0.20%) │ 2023-11-06 14:00 │ 2023-11-06 16:00 │ 2.0 hours │ 32691.5 │ 32625.87 │ 32625.87 EUR (2023-11-06 16:00) │
├───────────────────┼────────────────┼───────────────────────┼───────────────────────────┼──────────────────┼──────────────────┼─────────────┼────────────────────┼──────────────────────────────┼─────────────────────────────────┤
│ BTC/EUR (4) │ CLOSED, TP │ 0.0030 (0.0000) BTC │ 3.54 (3.56%) │ 2023-11-07 22:00 │ 2023-11-14 12:00 │ 158.0 hours │ 33126.6 │ 34746.64, 33865.42 │ 34967.12 EUR (2023-11-10 22:00) │
├───────────────────┼────────────────┼───────────────────────┼───────────────────────────┼──────────────────┼──────────────────┼─────────────┼────────────────────┼──────────────────────────────┼─────────────────────────────────┤
│ BTC/EUR (5) │ OPEN │ 0.0028 (0.0028) BTC │ 2.46 (2.52%) (unrealized) │ 2023-11-29 12:00 │ │ 60.0 hours │ 34765.9 │ │ 35679.63 EUR (2023-12-01 16:00) │
├───────────────────┼────────────────┼───────────────────────┼───────────────────────────┼──────────────────┼──────────────────┼─────────────┼────────────────────┼──────────────────────────────┼─────────────────────────────────┤
│ DOT/EUR (6) │ OPEN │ 19.9521 (19.9521) DOT │ 0.18 (0.18%) (unrealized) │ 2023-11-30 18:00 │ │ 30.0 hours │ 5.01 │ │ 5.05 EUR (2023-11-30 20:00) │
╰───────────────────┴────────────────┴───────────────────────┴───────────────────────────┴──────────────────┴──────────────────┴─────────────┴────────────────────┴──────────────────────────────┴─────────────────────────────────╯
Stop losses overview
╭────────────────────┬───────────────┬──────────┬──────────┬─────────────────────────────────┬──────────────┬────────────────┬───────────────────────────────┬──────────────┬───────────┬───────────────╮
│ Trade (Trade id) │ Status │ Active │ Type │ Stop Loss (Initial Stop Loss) │ Open price │ Sell price's │ High water mark │ Percentage │ Size │ Sold amount │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────┼──────────────┼────────────────┼───────────────────────────────┼──────────────┼───────────┼───────────────┤
│ BTC/EUR (1) │ NOT TRIGGERED │ False │ TRAILING │ 24418.58 (23265.85) (5.0)% EUR │ 24490.37 EUR │ None │ 25703.77 EUR 2023-09-19 14:00 │ 50.0% │ 0.00 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────┼──────────────┼────────────────┼───────────────────────────────┼──────────────┼───────────┼───────────────┤
│ DOT/EUR (2) │ TRIGGERED │ False │ TRAILING │ 4.28 (3.84) (5.0)% EUR │ 4.04 EUR │ 4.239,4.254 │ 4.51 EUR 2023-11-01 20:00 │ 50.0% │ 12.37 DOT │ 12.3732 DOT │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────┼──────────────┼────────────────┼───────────────────────────────┼──────────────┼───────────┼───────────────┤
│ BTC/EUR (3) │ NOT TRIGGERED │ False │ TRAILING │ 31056.93 (31056.93) (5.0)% EUR │ 32691.51 EUR │ None │ 32691.51 EUR │ 50.0% │ 0.00 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────┼──────────────┼────────────────┼───────────────────────────────┼──────────────┼───────────┼───────────────┤
│ BTC/EUR (4) │ NOT TRIGGERED │ False │ TRAILING │ 33218.76 (31470.27) (5.0)% EUR │ 33126.60 EUR │ None │ 34967.12 EUR 2023-11-10 22:00 │ 50.0% │ 0.00 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────┼──────────────┼────────────────┼───────────────────────────────┼──────────────┼───────────┼───────────────┤
│ BTC/EUR (5) │ NOT TRIGGERED │ True │ TRAILING │ 33895.65 (33027.62) (5.0)% EUR │ 34765.92 EUR │ None │ 35679.63 EUR 2023-12-01 16:00 │ 50.0% │ 0.00 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────┼──────────────┼────────────────┼───────────────────────────────┼──────────────┼───────────┼───────────────┤
│ DOT/EUR (6) │ NOT TRIGGERED │ True │ TRAILING │ 4.80 (4.76) (5.0)% EUR │ 5.01 EUR │ None │ 5.05 EUR 2023-11-30 20:00 │ 50.0% │ 9.98 DOT │ │
╰────────────────────┴───────────────┴──────────┴──────────┴─────────────────────────────────┴──────────────┴────────────────┴───────────────────────────────┴──────────────┴───────────┴───────────────╯
Take profits overview
╭────────────────────┬───────────────┬──────────┬──────────┬─────────────────────────────────────┬──────────────┬────────────────┬─────────────────────────────────┬──────────────┬─────────────┬───────────────╮
│ Trade (Trade id) │ Status │ Active │ Type │ Take profit (Initial Take Profit) │ Open price │ Sell price's │ High water mark │ Percentage │ Size │ Sold amount │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (1) │ NOT TRIGGERED │ False │ TRAILING │ 25714.89 (25714.89) (5.0)% EUR │ 24490.37 EUR │ None │ │ 50.0% │ 0.0020 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (1) │ NOT TRIGGERED │ False │ TRAILING │ 26939.41 (26939.41) (10.0)% EUR │ 24490.37 EUR │ None │ │ 20.0% │ 0.0008 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ DOT/EUR (2) │ TRIGGERED │ False │ TRAILING │ 4.24 (4.24) (5.0)% EUR │ 4.04 EUR │ 4.233 │ 4.30 EUR (2023-10-31 00:00) │ 50.0% │ 12.3732 DOT │ 12.3732 DOT │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ DOT/EUR (2) │ TRIGGERED │ False │ TRAILING │ 4.45 (4.45) (10.0)% EUR │ 4.04 EUR │ 4.377 │ 4.51 EUR (2023-11-01 20:00) │ 20.0% │ 4.9493 DOT │ 4.9493 DOT │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (3) │ NOT TRIGGERED │ False │ TRAILING │ 34326.09 (34326.09) (5.0)% EUR │ 32691.51 EUR │ None │ │ 50.0% │ 0.0015 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (3) │ NOT TRIGGERED │ False │ TRAILING │ 35960.66 (35960.66) (10.0)% EUR │ 32691.51 EUR │ None │ │ 20.0% │ 0.0006 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (4) │ TRIGGERED │ False │ TRAILING │ 34782.93 (34782.93) (5.0)% EUR │ 33126.60 EUR │ 34746.64 │ 34967.12 EUR (2023-11-10 22:00) │ 50.0% │ 0.0015 BTC │ 0.0015 BTC │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (4) │ NOT TRIGGERED │ False │ TRAILING │ 36439.26 (36439.26) (10.0)% EUR │ 33126.60 EUR │ None │ │ 20.0% │ 0.0006 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (5) │ NOT TRIGGERED │ True │ TRAILING │ 36504.22 (36504.22) (5.0)% EUR │ 34765.92 EUR │ None │ │ 50.0% │ 0.0014 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ BTC/EUR (5) │ NOT TRIGGERED │ True │ TRAILING │ 38242.51 (38242.51) (10.0)% EUR │ 34765.92 EUR │ None │ │ 20.0% │ 0.0006 BTC │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ DOT/EUR (6) │ NOT TRIGGERED │ True │ TRAILING │ 5.26 (5.26) (5.0)% EUR │ 5.01 EUR │ None │ │ 50.0% │ 9.9761 DOT │ │
├────────────────────┼───────────────┼──────────┼──────────┼─────────────────────────────────────┼──────────────┼────────────────┼─────────────────────────────────┼──────────────┼─────────────┼───────────────┤
│ DOT/EUR (6) │ NOT TRIGGERED │ True │ TRAILING │ 5.51 (5.51) (10.0)% EUR │ 5.01 EUR │ None │ │ 20.0% │ 3.9904 DOT │ │
╰────────────────────┴───────────────┴──────────┴──────────┴─────────────────────────────────────┴──────────────┴────────────────┴─────────────────────────────────┴──────────────┴─────────────┴───────────────╯
Prerequisites
Ensure Azure Functions Core Tools are installed:
npm install -g azure-functions-core-tools@4 --unsafe-perm true
Deploying to Azure Use the provided deployment script:
This script:
investing-algorithm-framework deploy
This script:
- Creates Azure resources (e.g., storage accounts, function apps).
- Sets environment variables for the Azure Function.
- Deploys your bot to Azure.
Comprehensive documentation is available at investing-algorithm-framework.com.
Local Development
Clone the repository and install dependencies using Poetry:
The framework is built with poetry. To install the framework for local development, you can run the following commands:
Make sure you have poetry installed. If you don't have poetry installed, you can find installation instructions here
git clone http
cd investing-algorithm-framework
poetry install
To run the tests, you can run the following command:
# In the root of the project
python -m unittest discover -s tests
If you use this framework for your investments, do not risk money which you are afraid to lose, until you have clear understanding how the framework works. We can't stress this enough:
BEFORE YOU START USING MONEY WITH THE FRAMEWORK, MAKE SURE THAT YOU TESTED YOUR COMPONENTS THOROUGHLY. USE THE SOFTWARE AT YOUR OWN RISK. THE AUTHORS AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR INVESTMENT RESULTS.
Also, make sure that you read the source code of any plugin you use or implementation of an algorithm made with this framework.
We welcome contributions! Check out the project board and issues to get started.
All the documentation can be found online at the documentation webstie
In most cases, you'll probably never have to change code on this repo directly if you are building your algorithm/bot. But if you do, check out the contributing page at the website.
If you'd like to chat with investing-algorithm-framework users and developers, join us on Slack or join us on reddit
The investing algorithm framework is a community driven project. We welcome you to participate, contribute and together help build the future trading bots developed in python.
Feel like the framework is missing a feature? We welcome your pull requests! If you want to contribute to the project roadmap, please take a look at the project board. You can pick up a task by assigning yourself to it.
Note before starting any major new feature work, please open an issue describing what you are planning to do. This will ensure that interested parties can give valuable feedback on the feature, and let others know that you are working on it.
Important: Always create your feature or hotfix against the develop
branch, not main
.
- Slack Community
- Reddit Community
We want to thank all contributors to this project. A full list of all the people that contributed to the project can be found here
If you discover a bug in the framework, please search our issue tracker first. If it hasn't been reported, please create a new issue.