Skip to content

Commit f5c97ef

Browse files
committed
Refactor app initialization
1 parent 2f0ca45 commit f5c97ef

File tree

64 files changed

+2067
-1487
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2067
-1487
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ The following algorithm connects to binance and buys BTC every 5 seconds. It als
3838

3939
```python
4040
import logging
41+
import logging.config
42+
4143
from investing_algorithm_framework import create_app, PortfolioConfiguration, \
4244
TimeUnit, CCXTOHLCVMarketDataSource, Algorithm, \
4345
CCXTTickerMarketDataSource, MarketCredential, DEFAULT_LOGGING_CONFIG
@@ -59,7 +61,6 @@ bitvavo_btc_eur_ticker = CCXTTickerMarketDataSource(
5961
symbol="BTC/EUR",
6062
)
6163
app = create_app()
62-
algorithm = Algorithm()
6364
# Bitvavo market credentials are read from .env file
6465
app.add_market_credential(MarketCredential(market="bitvavo"))
6566
app.add_portfolio_configuration(
@@ -69,18 +70,16 @@ app.add_portfolio_configuration(
6970
initial_balance=400
7071
)
7172
)
72-
app.add_algorithm(algorithm)
7373

7474
# Run every two hours and register the data sources
75-
@algorithm.strategy(
75+
@app.strategy(
7676
time_unit=TimeUnit.HOUR,
7777
interval=2,
7878
market_data_sources=[bitvavo_btc_eur_ticker, bitvavo_btc_eur_ohlcv_2h]
7979
)
8080
def perform_strategy(algorithm: Algorithm, market_data: dict):
8181
# Access the data sources with the indentifier
8282
polars_df = market_data["BTC-ohlcv"]
83-
8483
# Convert the polars dataframe to a pandas dataframe
8584
pandas_df = polars_df.to_pandas()
8685
ticker_data = market_data["BTC-ticker"]
@@ -91,13 +90,19 @@ def perform_strategy(algorithm: Algorithm, market_data: dict):
9190
closed_trades = algorithm.get_closed_trades()
9291

9392
# Create a buy oder
94-
algorithm.create_limit_order(
93+
trade = algorithm.create_limit_order(
9594
target_symbol="BTC/EUR",
9695
order_side="buy",
9796
amount=0.01,
9897
price=ticker_data["ask"],
9998
)
10099

100+
# Add a stop loss percentage of 5%
101+
algorithm.add_stop_loss(trade.id, percentage=5)
102+
103+
# Add a take profit percentage of 10%
104+
algorithm.add_take_profit(trade.id, percentage=5)
105+
101106
# Close a trade
102107
algorithm.close_trade(trades[0].id)
103108

investing_algorithm_framework/app/algorithm.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ def start(self, number_of_iterations: int = None):
164164
number_of_iterations=number_of_iterations
165165
)
166166

167+
def stop(self) -> None:
168+
"""
169+
Function to stop the algorithm. This function will stop the
170+
algorithm by stopping all jobs in the strategy orchestrator
171+
service.
172+
173+
Returns:
174+
None
175+
"""
176+
self.strategy_orchestrator_service.stop()
177+
167178
@property
168179
def name(self):
169180
return self._name
@@ -186,6 +197,14 @@ def config(self):
186197
"""
187198
return self.configuration_service.get_config()
188199

200+
def get_config(self):
201+
"""
202+
Function to get a config instance. This allows users when
203+
having access to the algorithm instance also to read the
204+
configs of the app.
205+
"""
206+
return self.configuration_service.get_config()
207+
189208
@property
190209
def description(self):
191210
"""
@@ -1222,7 +1241,7 @@ def get_open_trades(self, target_symbol=None, market=None) -> List[Trade]:
12221241
is specified, the open trades with the specified market will be
12231242
returned.
12241243
1225-
Parameters:
1244+
Args:
12261245
target_symbol: The symbol of the asset
12271246
market: The market of the asset
12281247
@@ -1238,7 +1257,7 @@ def close_trade(self, trade, market=None, precision=None) -> None:
12381257
parameter is specified, the amount of the order will be rounded
12391258
down to the specified precision.
12401259
1241-
Parameters:
1260+
Args:
12421261
trade: Trade - The trade to close
12431262
market: str - The market of the trade
12441263
precision: float - The precision of the amount
@@ -1400,3 +1419,9 @@ def get_unfilled_sell_value(self):
14001419
[order.get_amount() * order.get_price()
14011420
for order in pending_orders]
14021421
)
1422+
1423+
def get_trade_service(self):
1424+
return self.trade_service
1425+
1426+
def get_market_data_source_service(self):
1427+
return self._market_data_source_service

0 commit comments

Comments
 (0)