Skip to content

Commit

Permalink
Fix issue with data sources parameters passing
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Dec 28, 2024
1 parent a8eaf67 commit 73b997b
Show file tree
Hide file tree
Showing 18 changed files with 652 additions and 116 deletions.
79 changes: 45 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The Investing Algorithm Framework is a Python framework that enables swift and e

Features:

* Indicators module: A collection of indicators and utility functions that can be used in your trading strategies.
* Indicators module: A collection of indicators and utility functions that can be used in your trading strategies.
* Order execution and tracking
* Broker and exchange connections through [ccxt](https://github.com/ccxt/ccxt)
* Backtesting and performance analysis reports [example](./examples/backtest_example)
Expand All @@ -41,8 +41,8 @@ from investing_algorithm_framework import create_app, PortfolioConfiguration, \
RESOURCE_DIRECTORY, TimeUnit, CCXTOHLCVMarketDataSource, Algorithm, \
CCXTTickerMarketDataSource, MarketCredential, SYMBOLS

# Define the symbols you want to trade for optimization, otherwise the
# algorithm will check if you have orders and balances on all available
# Define the symbols you want to trade for optimization, otherwise the
# algorithm will check if you have orders and balances on all available
# symbols on the market
symbols = ["BTC/EUR"]

Expand All @@ -52,6 +52,13 @@ config = {
SYMBOLS: symbols
}

state_manager = AzureBlobStorageStateManager(
account_name="<your account name>",
account_key="<your account key>",
container_name="<your container name>",
blob_name="<your blob name>",
)

# Define market data sources
# OHLCV data for candles
bitvavo_btc_eur_ohlcv_2h = CCXTOHLCVMarketDataSource(
Expand All @@ -67,7 +74,11 @@ bitvavo_btc_eur_ticker = CCXTTickerMarketDataSource(
market="BITVAVO",
symbol="BTC/EUR",
)
app = create_app(config=config)
app = create_app(
config=config,
sync_portfolio=True,
state_manager=state_manager
)
algorithm = Algorithm()
app.add_market_credential(MarketCredential(
market="bitvavo",
Expand All @@ -85,16 +96,16 @@ app.add_algorithm(algorithm)

@algorithm.strategy(
# Run every two hours
time_unit=TimeUnit.HOUR,
interval=2,
time_unit=TimeUnit.HOUR,
interval=2,
# Specify market data sources that need to be passed to the strategy
market_data_sources=[bitvavo_btc_eur_ticker, bitvavo_btc_eur_ohlcv_2h]
)
def perform_strategy(algorithm: Algorithm, market_data: dict):
# By default, ohlcv data is passed as polars df in the form of
# {"<identifier>": <dataframe>} https://pola.rs/,
# {"<identifier>": <dataframe>} https://pola.rs/,
# call to_pandas() to convert to pandas
polars_df = market_data["BTC-ohlcv"]
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>}
Expand All @@ -104,21 +115,21 @@ def perform_strategy(algorithm: Algorithm, market_data: dict):
trades = algorithm.get_trades()
open_trades = algorithm.get_open_trades()
closed_trades = algorithm.get_closed_trades()
# Create a buy oder

# Create a buy oder
algorithm.create_limit_order(
target_symbol="BTC/EUR",
order_side="buy",
amount=0.01,
price=ticker_data["ask"],
)

# Close a trade
algorithm.close_trade(trades[0].id)

# Close a position
algorithm.close_position(positions[0].get_symbol())

if __name__ == "__main__":
app.run()
```
Expand All @@ -136,14 +147,14 @@ To run a single backtest you can use the example code that can be found [here](.
You can use the ```pretty_print_backtest``` function to print a backtest report.
For example if you run the [moving average example trading bot](./examples/crossover_moving_average_trading_bot)
you will get the following backtesting report:

```bash

:%%%#+- .=*#%%% Backtest report
*%%%%%%%+------=*%%%%%%%- ---------------------------
*%%%%%%%%%%%%%%%%%%%%%%%- Start date: 2023-08-24 00:00:00
.%%%%%%%%%%%%%%%%%%%%%%# End date: 2023-12-02 00:00:00
#%%%####%%%%%%%%**#%%%+ Number of days: 100
#%%%####%%%%%%%%**#%%%+ Number of days: 100
.:-+*%%%%- -+..#%%%+.+- +%%%#*=-: Number of runs: 1201
.:-=*%%%%. += .%%# -+.-%%%%=-:.. Number of orders: 40
.:=+#%%%%%*###%%%%#*+#%%%%%%*+-: Initial balance: 400.0
Expand All @@ -156,10 +167,10 @@ you will get the following backtesting report:
.++- -%%%%%%%%%%%+= Percentage negative trades: 70.0%
.++- .%%%%%%%%%%%%%+= Average trade size: 100.9692 EUR
.++- *%%%%%%%%%%%%%*+: Average trade duration: 83.6 hours
.++- %%%%%%%%%%%%%%#+=
=++........:::%%%%%%%%%%%%%%*+-
.=++++++++++**#%%%%%%%%%%%%%++.
.++- %%%%%%%%%%%%%%#+=
=++........:::%%%%%%%%%%%%%%*+-
.=++++++++++**#%%%%%%%%%%%%%++.

Price noise

Positions overview
Expand Down Expand Up @@ -220,8 +231,8 @@ Trades overview

### Backtest experiments

The framework also supports backtest experiments. Backtest experiments allows you to
compare multiple algorithms and evaluate their performance. Ideally,
The framework also supports backtest experiments. Backtest experiments allows you to
compare multiple algorithms and evaluate their performance. Ideally,
you would do this by parameterizing your strategy and creating a factory function that
creates the algorithm with the different parameters. You can find an example of this
in the [backtest experiments example](./examples/backtest_experiment).
Expand All @@ -237,14 +248,14 @@ from investing_algorithm_framework import PortfolioConfiguration, \
app = create_app()
app.add_market_credential(
MarketCredential(
market="<your market>",
market="<your market>",
api_key="<your api key>",
secret_key="<your secret key>",
)
)
app.add_portfolio_configuration(
PortfolioConfiguration(
market="<your market>",
market="<your market>",
initial_balance=400,
track_from="01/01/2022",
trading_symbol="EUR"
Expand All @@ -267,27 +278,27 @@ 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
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.
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
Also, make sure that you read the source code of any plugin you use or
implementation of an algorithm made with this framework.

## Documentation

All the documentation can be found online
All the documentation can be found online
at the [documentation webstie](https://investing-algorithm-framework.com)

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
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
If you'd like to chat with investing-algorithm-framework users
and developers, [join us on Slack](https://inv-algo-framework.slack.com) or [join us on reddit](https://www.reddit.com/r/InvestingBots/)

## Acknowledgements
Expand All @@ -302,12 +313,12 @@ first. If it hasn't been reported, please [create a new issue](https://github.co

### Contributing

The investing algorithm framework is a community driven project.
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](https://github.com/coding-kitties/investing-algorithm-framework/projects?query=is%3Aopen).
You can pick up a task by assigning yourself to it.
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.
Expand Down
11 changes: 8 additions & 3 deletions investing_algorithm_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
pretty_print_backtest_reports_evaluation, load_backtest_reports, \
RESERVED_BALANCES, APP_MODE, AppMode, DATETIME_FORMAT, \
load_backtest_report, BacktestDateRange, convert_polars_to_pandas, \
DateRange
DateRange, get_backtest_report
from investing_algorithm_framework.infrastructure import \
CCXTOrderBookMarketDataSource, CCXTOHLCVMarketDataSource, \
CCXTTickerMarketDataSource, CSVOHLCVMarketDataSource, \
CSVTickerMarketDataSource
from .create_app import create_app
from investing_algorithm_framework.indicators import get_rsi, get_peaks, \
is_uptrend, is_downtrend, is_crossover, is_crossunder, is_above, \
is_below, has_crossed_upward, get_sma, get_up_and_downtrends, \
get_rsi, get_ema, get_adx, has_crossed_downward, get_willr, \
is_divergence

__all__ = [
"Algorithm",
Expand Down Expand Up @@ -83,6 +88,6 @@
"get_adx",
"has_crossed_downward",
"get_willr",
"is_bearish_divergence",
"is_bullish_divergence",
"is_divergence",
"get_backtest_report"
]
16 changes: 9 additions & 7 deletions investing_algorithm_framework/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def initialize(self, sync=False):
Also, it initializes all required services for the algorithm.
:return: None
Args:
sync (bool): Whether to sync the portfolio with the exchange
Returns:
None
"""
if self.algorithm is None:
raise OperationalException("No algorithm registered")
Expand Down Expand Up @@ -162,8 +165,8 @@ def initialize(self, sync=False):
.create_portfolio_from_configuration(
portfolio_configuration
)
self.sync(portfolio)
synced_portfolios.append(portfolio)
# self.sync(portfolio)
# synced_portfolios.append(portfolio)

if sync:
portfolios = portfolio_service.get_all()
Expand Down Expand Up @@ -494,16 +497,15 @@ def run(
separate thread.
Args:
payload: The payload to handle if the app is running in
payload (dict): The payload to handle if the app is running in
stateless mode
number_of_iterations: The number of iterations to run the
number_of_iterations (int): The number of iterations to run the
algorithm for
sync: Whether to sync the portfolio with the exchange
sync (bool): Whether to sync the portfolio with the exchange
Returns:
None
"""

# Run all on_initialize hooks
for hook in self._on_after_initialize_hooks:
hook.on_run(self, self.algorithm)
Expand Down
6 changes: 4 additions & 2 deletions investing_algorithm_framework/domain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
add_column_headers_to_csv, get_total_amount_of_rows, \
load_backtest_report, convert_polars_to_pandas, \
csv_to_list, StoppableThread, pretty_print_backtest_reports_evaluation, \
pretty_print_backtest, load_csv_into_dict, load_backtest_reports
pretty_print_backtest, load_csv_into_dict, load_backtest_reports, \
get_backtest_report
from .metrics import get_price_efficiency_ratio

__all__ = [
Expand Down Expand Up @@ -117,5 +118,6 @@
"load_backtest_report",
"get_price_efficiency_ratio",
"convert_polars_to_pandas",
"DateRange"
"DateRange",
"get_backtest_report"
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
.backtesting.backtest_date_range import BacktestDateRange
from investing_algorithm_framework.domain.models.base_model import BaseModel
from investing_algorithm_framework.domain.models.time_unit import TimeUnit
from investing_algorithm_framework.domain.models.position import Position
from investing_algorithm_framework.domain.models.trade import Trade
from investing_algorithm_framework.domain.models.order import Order

logger = getLogger(__name__)

Expand Down Expand Up @@ -451,7 +454,7 @@ def from_dict(data):
data["backtest_end_date"], DATETIME_FORMAT)
)

return BacktestReport(
report = BacktestReport(
name=data["name"],
strategy_identifiers=data["strategy_identifiers"],
number_of_runs=data["number_of_runs"],
Expand All @@ -477,6 +480,23 @@ def from_dict(data):
average_trade_size=float(data["average_trade_size"]),
)

positions = data["positions"]

if positions is not None:
report.positions = [Position.from_dict(position) for position in positions]

trades = data["trades"]

if trades is not None:
report.trades = [Trade.from_dict(trade) for trade in trades]

orders = data["orders"]

if orders is not None:
report.orders = [Order.from_dict(order) for order in orders]

return report

def get_trades(self, symbol=None):
"""
Function to get trades. If a symbol is provided, it will
Expand Down
6 changes: 6 additions & 0 deletions investing_algorithm_framework/domain/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ def update(self, data):

if value is not None:
setattr(self, attr, value)

@staticmethod
def from_dict(data):
instance = BaseModel()
instance.update(data)
return instance
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


class Position(BaseModel):
"""
This class represents a position in a portfolio.
"""

def __init__(
self,
Expand Down
4 changes: 3 additions & 1 deletion investing_algorithm_framework/domain/models/time_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def from_string(value: str):
return entry

raise ValueError(
f"Could not convert value {value} to time unit"
f"Could not convert value {value} to time unit," +
" please make sure that the value is either of type string or" +
f"TimeUnit. Its current type is {type(value)}"
)

def equals(self, other):
Expand Down
Loading

0 comments on commit 73b997b

Please sign in to comment.