Skip to content

Commit

Permalink
Add experiment multiple algorithms support
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Mar 19, 2024
1 parent 430d8bf commit e87ac63
Show file tree
Hide file tree
Showing 84 changed files with 2,436 additions and 633 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</a>
</p>


# [Investing Algorithm Framework](https://github.com/coding-kitties/investing-algorithm-framework)

The Investing Algorithm Framework is a Python tool that enables swift and
Expand All @@ -31,14 +30,11 @@ Features:
* Stateless running for cloud function deployments
* Polars dataframes support out of the box for fast data processing [pola.rs](https://pola.rs/)



## 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.
```python
import pathlib
from datetime import datetime, timedelta
from investing_algorithm_framework import create_app, PortfolioConfiguration, \
RESOURCE_DIRECTORY, TimeUnit, CCXTOHLCVMarketDataSource, Algorithm, \
CCXTTickerMarketDataSource, MarketCredential
Expand All @@ -58,6 +54,8 @@ bitvavo_btc_eur_ticker = CCXTTickerMarketDataSource(
symbol="BTC/EUR",
)
app = create_app({RESOURCE_DIRECTORY: pathlib.Path(__file__).parent.resolve()})
algorithm = Algorithm()

app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)
app.add_market_data_source(bitvavo_btc_eur_ticker)
app.add_market_credential(MarketCredential(
Expand All @@ -72,18 +70,25 @@ app.add_portfolio_configuration(
initial_balance=400
)
)
app.add_algorithm(algorithm)

@app.strategy(
@algorithm.strategy(
# Run every two hours
time_unit=TimeUnit.HOUR,
interval=2,
# Specify market data sources that need to be passed to the strategy
market_data_sources=["BTC-ticker", "BTC-ohlcv"]
)
def perform_strategy(algorithm: Algorithm, market_data: dict):
# By default data is passed as polars dataframe https://pola.rs/
df = market_data["BTC-ohlcv"].to_pandas()
# By default ohlcv data is passed as polars df in the form of
# {"<identifier>": <dataframe>} https://pola.rs/,
# call to_pandas() to convert to pandas
polars_df = market_data["BTC-ohlcv"]
print(f"I have access to {len(polars_df)} candles of ohlcv data")

# Ticker data is passed as {"<identifier>": <ticker dict>}
ticker_data = market_data["BTC-ticker"]

algorithm.create_limit_order(
target_symbol="BTC/EUR",
order_side="buy",
Expand Down Expand Up @@ -257,7 +262,8 @@ app.add_portfolio_configuration(
```

## Performance
We continiously are working on improving the performance of the framework.
We are continuously working on improving the performance of the framework. If
you have any suggestions, please let us know.

## Download
You can download the framework with pypi.
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions examples/expirement/app.py → examples/backtest/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
from data_sources import bitvavo_btc_eur_ohlcv_2h, bitvavo_dot_eur_ohlcv_2h, \
bitvavo_dot_eur_ticker, bitvavo_btc_eur_ticker
from investing_algorithm_framework import create_app, RESOURCE_DIRECTORY
from strategy import CrossOverStrategy

app = create_app(
config={RESOURCE_DIRECTORY: pathlib.Path(__file__).parent.resolve()}
)
app.add_strategy(CrossOverStrategy)
app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)
app.add_market_data_source(bitvavo_dot_eur_ohlcv_2h)
app.add_market_data_source(bitvavo_btc_eur_ticker)
Expand Down
Empty file added examples/backtest/backtest.py
Empty file.
File renamed without changes.
Empty file.
10 changes: 10 additions & 0 deletions examples/backtest_expirement/algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .algorithm_one import algorithm as algorithm_one
from .algorithm_two import algorithm as algorithm_two
from .algorithm_three import algorithm as algorithm_three


__all__ = [
"algorithm_one",
"algorithm_two",
"algorithm_three",
]
15 changes: 15 additions & 0 deletions examples/backtest_expirement/algorithms/algorithm_one.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from investing_algorithm_framework import TradingStrategy, Algorithm, TimeUnit

algorithm = Algorithm()


class Strategy(TradingStrategy):
strategy_id = "strategy_one"
time_unit = TimeUnit.MINUTE
interval = 30

def run_strategy(self, algorithm: Algorithm, market_data):
pass


algorithm.add_strategy(Strategy())
16 changes: 16 additions & 0 deletions examples/backtest_expirement/algorithms/algorithm_three.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from investing_algorithm_framework import TradingStrategy, Algorithm, TimeUnit


algorithm = Algorithm()


class Strategy(TradingStrategy):
strategy_id = "strategy_three"
time_unit = TimeUnit.MINUTE
interval = 30

def run_strategy(self, algorithm: Algorithm, market_data):
pass


algorithm.add_strategy(Strategy())
15 changes: 15 additions & 0 deletions examples/backtest_expirement/algorithms/algorithm_two.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from investing_algorithm_framework import TradingStrategy, Algorithm, TimeUnit

algorithm = Algorithm()


class Strategy(TradingStrategy):
strategy_id = "strategy_two"
time_unit = TimeUnit.MINUTE
interval = 30

def run_strategy(self, algorithm: Algorithm, market_data):
pass


algorithm.add_strategy(Strategy())
13 changes: 13 additions & 0 deletions examples/backtest_expirement/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pathlib

from data_sources import bitvavo_btc_eur_ohlcv_2h, bitvavo_dot_eur_ohlcv_2h, \
bitvavo_dot_eur_ticker, bitvavo_btc_eur_ticker
from investing_algorithm_framework import create_app, RESOURCE_DIRECTORY

app = create_app(
config={RESOURCE_DIRECTORY: pathlib.Path(__file__).parent.resolve()}
)
app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)
app.add_market_data_source(bitvavo_dot_eur_ohlcv_2h)
app.add_market_data_source(bitvavo_btc_eur_ticker)
app.add_market_data_source(bitvavo_dot_eur_ticker)
33 changes: 33 additions & 0 deletions examples/backtest_expirement/backtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from datetime import datetime, timedelta

from algorithms import algorithm_one, algorithm_two, algorithm_three
from app import app
from investing_algorithm_framework import PortfolioConfiguration

if __name__ == "__main__":
end_date = datetime(2023, 12, 2)
start_date = end_date - timedelta(days=100)

# Add a portfolio configuration of 400 euro initial balance
app.add_portfolio_configuration(
PortfolioConfiguration(
market="BINANCE",
trading_symbol="EUR",
initial_balance=400,
)
)

# Run the backtest for each algorithm
reports = app.run_backtests(
algorithms=[
algorithm_one,
algorithm_two,
algorithm_three,
],
start_date=start_date,
end_date=end_date,
pending_order_check_interval="2h",
)


# pretty_print_expirement(reports)
30 changes: 30 additions & 0 deletions examples/backtest_expirement/data_sources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from investing_algorithm_framework import CCXTOHLCVMarketDataSource, \
CCXTTickerMarketDataSource


bitvavo_btc_eur_ohlcv_2h = CCXTOHLCVMarketDataSource(
identifier="BTC/EUR-ohlcv",
market="BINANCE",
symbol="BTC/EUR",
timeframe="2h",
window_size=200
)
bitvavo_dot_eur_ohlcv_2h = CCXTOHLCVMarketDataSource(
identifier="DOT/EUR-ohlcv",
market="BINANCE",
symbol="DOT/EUR",
timeframe="2h",
window_size=200
)
bitvavo_dot_eur_ticker = CCXTTickerMarketDataSource(
identifier="DOT/EUR-ticker",
market="BINANCE",
symbol="DOT/EUR",
backtest_timeframe="2h",
)
bitvavo_btc_eur_ticker = CCXTTickerMarketDataSource(
identifier="BTC/EUR-ticker",
market="BINANCE",
symbol="BTC/EUR",
backtest_timeframe="2h",
)
30 changes: 20 additions & 10 deletions examples/bitvavo_trading_bot/bitvavo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from investing_algorithm_framework import MarketCredential, TimeUnit, \
CCXTOHLCVMarketDataSource, CCXTTickerMarketDataSource, TradingStrategy, \
create_app, PortfolioConfiguration
create_app, PortfolioConfiguration, Algorithm

"""
Bitvavo market data sources example. Bitvavo does not requires you
to have an API key and secret key to access their market data
Bitvavo trading bot example with market data sources of bitvavo.
Bitvavo does not requires you to have an API key and secret key to access
their market data
If you just want to backtest your strategy, you don't need to
add a market credential. If your runnning your strategy live,
add a market credential. If your running your strategy live,
you need to add a market credential to the app, that accesses your
account on bitvavo.
"""
Expand All @@ -31,6 +32,9 @@
symbol="BTC/EUR",
)

# Create an algorithm and link your trading strategy to it
algorithm = Algorithm()


class BitvavoTradingStrategy(TradingStrategy):
time_unit = TimeUnit.HOUR
Expand All @@ -44,17 +48,23 @@ def apply_strategy(self, algorithm, market_data):
print(market_data["BTC/EUR-ohlcv"])
print(market_data["BTC/EUR-ticker"])

algorithm.add_strategy(BitvavoTradingStrategy)

# Create an app and add the market data sources and market credentials to it
app = create_app()
app.add_market_credential(bitvavo_market_credential)
app.add_market_data_source(bitvavo_btc_eur_ohlcv_2h)
app.add_market_data_source(bitvavo_btc_eur_ticker)
app.add_strategy(BitvavoTradingStrategy)
app.add_portfolio_configuration(PortfolioConfiguration(
initial_balance=1000,
trading_symbol="EUR",
market="bitvavo"
))

# Register your algorithm and portfolio configuration to the app
app.add_algorithm(algorithm)
app.add_portfolio_configuration(
PortfolioConfiguration(
initial_balance=1000,
trading_symbol="EUR",
market="bitvavo"
)
)

if __name__ == "__main__":
app.run()
Expand Down
27 changes: 0 additions & 27 deletions examples/expirement/experiment.py

This file was deleted.

13 changes: 0 additions & 13 deletions examples/expirement/production.py

This file was deleted.

Loading

0 comments on commit e87ac63

Please sign in to comment.