Skip to main content

A framework for creating an investment algorithm

Project description

Build Tests Downloads Current Version

Sponsors

Finterion

Investing Algorithm Framework

The Investing Algorithm Framework is a Python tool that enables swift and elegant development of investment algorithms and trading bots. It comes with all the necessary components for creating algorithms, including data provisioning, portfolio management, and order execution.

Example implementation

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.

import pathlib
from datetime import datetime, timedelta
from investing_algorithm_framework import create_app, PortfolioConfiguration, \
    RESOURCE_DIRECTORY, TimeUnit, CCXTOHLCVMarketDataSource, Algorithm

# Define market data sources
bitvavo_btc_eur_ohlcv_2h = CCXTOHLCVMarketDataSource(
    identifier="BTC",
    market="BITVAVO",
    symbol="BTC/EUR",
    timeframe="2h",
    start_date_func=lambda : datetime.utcnow() - timedelta(days=17)
)
app = create_app({RESOURCE_DIRECTORY: pathlib.Path(__file__).parent.resolve()})
app.add_portfolio_configuration(
    PortfolioConfiguration(
        market="BITVAVO",
        api_key="<your api key>",
        secret_key="<your secret key>",
        trading_symbol="EUR"
    )
)


@app.strategy(
    time_unit=TimeUnit.HOUR, 
    interval=2, 
    market_data_sources=[bitvavo_btc_eur_ohlcv_2h]
)
def perform_strategy(algorithm: Algorithm, market_data):
    print(
        f"Performing trading strategy on market " +
        f"data {market_data[bitvavo_btc_eur_ohlcv_2h.get_identifier()]}"
    )

if __name__ == "__main__":
    app.run()

You can find more examples here folder.

Backtesting

The framework also supports backtesting. You can use the same code as above, but instead of running the algorithm, you can run a backtest.

import pathlib
from datetime import datetime, timedelta
from investing_algorithm_framework import create_app, RESOURCE_DIRECTORY, \
    TimeUnit, CCXTOHLCVMarketDataSource, Algorithm, pretty_print_backtest

# Define market data sources
bitvavo_btc_eur_ohlcv_2h = CCXTOHLCVMarketDataSource(
    identifier="BTC",
    market="BITVAVO",
    symbol="BTC/EUR",
    timeframe="2h",
    start_date_func=lambda : datetime.utcnow() - timedelta(days=17)
)
app = create_app({RESOURCE_DIRECTORY: pathlib.Path(__file__).parent.resolve()})

@app.strategy(
    time_unit=TimeUnit.HOUR, 
    interval=2, 
    market_data_sources=[bitvavo_btc_eur_ohlcv_2h]
)
def perform_strategy(algorithm: Algorithm, market_data):
    print(
        f"Performing trading strategy on market " +
        f"data {market_data[bitvavo_btc_eur_ohlcv_2h.get_identifier()]}"
    )

if __name__ == "__main__":
    backtest_report = app.backtest(
        start_date=datetime(2023, 11, 12) - timedelta(days=10),
        end_date=datetime(2023, 11, 12),
        unallocated=400,
        trading_symbol="EUR"
    )
    pretty_print_backtest(backtest_report)

For more examples, check out the examples folder.

Broker/Exchange configuration

The framework has by default support for ccxt. This should allow you to connect to a lot of brokers/exchanges.

from investing_algorithm_framework import App, PortfolioConfiguration
app = App()
app.add_portfolio_configuration(
    PortfolioConfiguration(
        market="BITVAVO", 
        api_key="xxxx", 
        secret_key="xxxx", 
        track_from="01/01/2022",
        trading_symbol="EUR"
    )
)

Download

You can download the framework with pypi.

pip install investing-algorithm-framework

Disclaimer

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.

For further information regarding usage and licensing we recommend going to the licensing page at the website.

Documentation

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

Acknowledgements

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

Bugs / Issues

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.

Contributing

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 master.

Project details


Release history Release notifications | RSS feed

This version

1.7.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

investing_algorithm_framework-1.7.2.tar.gz (67.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

investing_algorithm_framework-1.7.2-py3-none-any.whl (108.3 kB view details)

Uploaded Python 3

File details

Details for the file investing_algorithm_framework-1.7.2.tar.gz.

File metadata

File hashes

Hashes for investing_algorithm_framework-1.7.2.tar.gz
Algorithm Hash digest
SHA256 3c452c54ec3163a6e07a9002a9190b1817f0f33afbcffa023609b88c06f24a54
MD5 81432ee55810c7d0aad1ecb201c0a097
BLAKE2b-256 257d073d5c9f03df73090712fb685f39725245f56cdab94b44aaf7acbadf319a

See more details on using hashes here.

File details

Details for the file investing_algorithm_framework-1.7.2-py3-none-any.whl.

File metadata

File hashes

Hashes for investing_algorithm_framework-1.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3c88cac9c910c1e0feacb58a68a870f48f3b98e02b504ddbe54390b45c769a05
MD5 00a3d7621e0fad2949743d92e9601077
BLAKE2b-256 d545f4deb886a92a4da1364aee5079638e4229ecbd242abb2b6602ffc8079352

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page