Package: investing-algorithm-framework
If you like what we do, consider starring, sharing and contributing!SponsorsInvesting Algorithm FrameworkThe Investing Algorithm Framework is a Python tool that enables swift and
elegant development of trading bots. It comes with all the necessary
components for creating algorithms, including data provisioning,
portfolio management, and order execution.Features:Order executionBroker and exchange connections throughccxtBacktesting and performance analysis reportsPortfolio managementWeb API for interacting with your deployed trading botData persistence through sqlite db or an in-memory dbStateless running for cloud function deploymentsPolars dataframes support out of the box for fast data processingpola.rsExample implementationThe 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.importpathlibfromdatetimeimportdatetime,timedeltafrominvesting_algorithm_frameworkimportcreate_app,PortfolioConfiguration,\RESOURCE_DIRECTORY,TimeUnit,CCXTOHLCVMarketDataSource,Algorithm,\CCXTTickerMarketDataSource,MarketCredential# Define market data sourcesbitvavo_btc_eur_ohlcv_2h=CCXTOHLCVMarketDataSource(identifier="BTC-ohlcv",market="BITVAVO",symbol="BTC/EUR",timeframe="2h",window_size=200)# Ticker data for orders, trades and positionsbitvavo_btc_eur_ticker=CCXTTickerMarketDataSource(identifier="BTC-ticker",market="BITVAVO",symbol="BTC/EUR",)app=create_app({RESOURCE_DIRECTORY:pathlib.Path(__file__).parent.resolve()})app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)app.add_market_data_source(bitvavo_btc_eur_ticker)app.add_market_credential(MarketCredential(market="bitvavo",api_key="<your api key>",secret_key="<your secret key>",))app.add_portfolio_configuration(PortfolioConfiguration(market="bitvavo",trading_symbol="EUR",initial_balance=400))@app.strategy(# Run every two hourstime_unit=TimeUnit.HOUR,interval=2,# Specify market data sources that need to be passed to the strategymarket_data_sources=["BTC-ticker","BTC-ohlcv"])defperform_strategy(algorithm:Algorithm,market_data:dict):# By default data is passed as polars dataframe https://pola.rs/df=market_data["BTC-ohlcv"].to_pandas()ticker_data=market_data["BTC-ticker"]algorithm.create_limit_order(target_symbol="BTC/EUR",order_side="buy",amount=0.01,price=ticker_data["ask"],)if__name__=="__main__":app.run()You can find more examplesherefolder.BacktestingThe framework also supports backtesting. You can use the same code as above,
but instead of running the algorithm, you can run a backtest.importpathlibfromdatetimeimportdatetime,timedeltafrominvesting_algorithm_frameworkimportcreate_app,RESOURCE_DIRECTORY,\TimeUnit,CCXTOHLCVMarketDataSource,Algorithm,pretty_print_backtest,\CCXTTickerMarketDataSource,PortfolioConfiguration# Define market data sourcesbitvavo_btc_eur_ohlcv_2h=CCXTOHLCVMarketDataSource(identifier="BTC-ohlcv",market="bitvavo",symbol="BTC/EUR",timeframe="2h",window_size=200)bitvavo_btc_eur_ticker=CCXTTickerMarketDataSource(identifier="BTC-ticker",market="bitvavo",symbol="BTC/EUR",backtest_timeframe="2h"# We want the ticker data to# be sampled every 2 hours, inline with the strategy interval)app=create_app({RESOURCE_DIRECTORY:pathlib.Path(__file__).parent.resolve()})app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)app.add_market_data_source(bitvavo_btc_eur_ticker)app.add_portfolio_configuration(PortfolioConfiguration(initial_balance=400,market="bitvavo",trading_symbol="EUR",))@app.strategy(time_unit=TimeUnit.HOUR,interval=2,market_data_sources=["BTC-ticker","BTC-ohlcv"])defperform_strategy(algorithm:Algorithm,market_data:dict):print(f"Performing trading strategy on market "+f"data{market_data['BTC-ohlcv']andmarket_data['BTC-ticker']}")if__name__=="__main__":backtest_report=app.backtest(start_date=datetime(2023,11,12)-timedelta(days=10),end_date=datetime(2023,11,12),)pretty_print_backtest(backtest_report)For more examples, check out theexamplesfolder.Backtesting reportYou can use thepretty_print_backtestfunction to print a backtest report.
For example if you run themoving average example trading botyou will get the following backtesting report:====================Backtestreport===============================*Startdate:2023-08-2400:00:00
*Enddate:2023-12-0200:00:00
*Numberofdays:100*Numberofruns:1201====================Portfoliooverview============================*Numberoforders:40*Initialbalance:400.0000EUR
*Finalbalance:428.2434EUR
*Totalnetgain:28.2434EUR
*Totalnetgainpercentage:7.0609%
*Growthrate:7.0609%
*Growth28.2434EUR====================Positionsoverview========================╭────────────┬──────────┬──────────────────┬──────────────┬───────────────┬───────────────────────────┬────────────────┬───────────────╮
│Position│Amount│Pendingamount│Cost(EUR)│Value(EUR)│Percentageofportfolio│Growth(EUR)│Growth_rate│
├────────────┼──────────┼──────────────────┼──────────────┼───────────────┼───────────────────────────┼────────────────┼───────────────┤
│EUR│428.243│0│428.243│428.243│100.0000%│0│0.0000%│
╰────────────┴──────────┴──────────────────┴──────────────┴───────────────┴───────────────────────────┴────────────────┴───────────────╯====================Tradesoverview===========================*Numberoftradesclosed:20*Numberoftradesopen:0*Percentageofpositivetrades:30.0%
*Percentageofnegativetrades:70.0%
*Averagetradesize:100.9692EUR
*Averagetradeduration:83.6hours
╭─────────┬─────────────────────┬─────────────────────┬────────────────────┬──────────────┬──────────────────┬───────────────────────┬────────────────────┬─────────────────────╮
│Pair│Opendate│Closedate│Duration(hours)│Size(EUR)│Netgain(EUR)│Netgainpercentage│Openprice(EUR)│Closeprice(EUR)│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-11-2412:00:00│2023-11-2714:00:00│74│107.55│-1.9587│-1.8212%│4.777│4.69│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-11-2000:00:00│2023-11-2108:00:00│32│109.39│-4.5949│-4.2005%│4.9875│4.778│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│BTC-EUR│2023-11-1922:00:00│2023-11-2200:00:00│50│109.309│-2.7624│-2.5272%│34159.1│33295.9│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│BTC-EUR│2023-11-0612:00:00│2023-11-1314:00:00│170│107.864│6.1015│5.6567%│32685.9│34534.9│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-10-2012:00:00│2023-10-2708:00:00│164│99.085│10.9799│11.0813%│3.5465│3.9395│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│BTC-EUR│2023-10-1404:00:00│2023-10-2722:00:00│330│97.4278│24.137│24.7742%│25638.9│31990.7│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-10-1404:00:00│2023-10-1714:00:00│82│99.5572│-1.8877│-1.8961%│3.56│3.4925│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-10-0708:00:00│2023-10-0808:00:00│24│99.9498│-1.5708│-1.5716%│3.8815│3.8205│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│BTC-EUR│2023-09-2710:00:00│2023-10-0520:00:00│202│98.2888│3.433│3.4927%│25202.2│26082.5│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-09-2710:00:00│2023-10-0320:00:00│154│98.7893│1.2085│1.2233%│3.842│3.889│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-09-2512:00:00│2023-09-2704:00:00│40│98.9193│-0.5194│-0.5251%│3.809│3.789│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-09-1416:00:00│2023-09-1802:00:00│82│98.9419│-0.0912│-0.0921%│3.799│3.7955│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│BTC-EUR│2023-09-0706:00:00│2023-09-1016:00:00│82│98.6093│0.3412│0.3460%│24051│24134.3│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-09-0700:00:00│2023-09-0902:00:00│50│98.9158│-0.2358│-0.2383%│3.986│3.9765│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-09-0514:00:00│2023-09-0612:00:00│22│99.2132│-1.1909│-1.2003%│3.999│3.951│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-09-0416:00:00│2023-09-0422:00:00│6│99.355│-0.5671│-0.5708%│3.942│3.9195│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-09-0410:00:00│2023-09-0414:00:00│4│99.4774│-0.4889│-0.4914%│3.968│3.9485│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│BTC-EUR│2023-08-2610:00:00│2023-08-2618:00:00│8│99.0829│-0.03│-0.0302%│24166.6│24159.3│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-08-2510:00:00│2023-08-2810:00:00│72│99.659│-0.6975│-0.6999%│4.1435│4.1145│
├─────────┼─────────────────────┼─────────────────────┼────────────────────┼──────────────┼──────────────────┼───────────────────────┼────────────────────┼─────────────────────┤
│DOT-EUR│2023-08-2400:00:00│2023-08-2500:00:00│24│99.9999│-1.3626│-1.3626%│4.1465│4.09│
╰─────────┴─────────────────────┴─────────────────────┴────────────────────┴──────────────┴──────────────────┴───────────────────────┴────────────────────┴─────────────────────╯==================================================================Broker/Exchange configurationThe framework has by default support forccxt.
This should allow you to connect to a lot of brokers/exchanges.frominvesting_algorithm_frameworkimportPortfolioConfiguration,\MarketCredential,create_appapp=create_app()app.add_market_credential(MarketCredential(market="<your market>",api_key="<your api key>",secret_key="<your secret key>",))app.add_portfolio_configuration(PortfolioConfiguration(market="<your market>",initial_balance=400,track_from="01/01/2022",trading_symbol="EUR"))PerformanceWe continiously are working on improving the performance of the framework.DownloadYou can download the framework with pypi.pipinstallinvesting-algorithm-frameworkDisclaimerIf 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.DocumentationAll the documentation can be found online
at thedocumentation webstieIn 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 Slackorjoin us on redditAcknowledgementsWe want to thank all contributors to this project. A full list of all
the people that contributed to the project can be
foundhereBugs / IssuesIf you discover a bug in the framework, pleasesearch our issue trackerfirst. If it hasn't been reported, pleasecreate a new issue.ContributingThe investing algorithm framework is a community driven project.
We welcome you to participate, contribute and together help build
the future trading bots developed in pyhton.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 theproject board.
You can pick up a task by assigning yourself to it.Notebefore 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 thedevelopbranch, notmaster.