Skip to content

Commit 3723ec8

Browse files
committed
Fix order executor priority queue
1 parent 1b024ed commit 3723ec8

File tree

15 files changed

+203
-152
lines changed

15 files changed

+203
-152
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,5 @@ bumpversion.egg-info/
153153
**/databases/
154154
.vscode/
155155
.logs
156-
venv
156+
venv
157+
**/app_logs.log

investing_algorithm_framework/app/app.py

Lines changed: 154 additions & 116 deletions
Large diffs are not rendered by default.

investing_algorithm_framework/app/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ def add_stop_loss(
12051205
self,
12061206
trade,
12071207
percentage: float,
1208-
trade_risk_type: TradeRiskType = TradeRiskType.FIXED,
1208+
trade_risk_type = TradeRiskType.FIXED,
12091209
sell_percentage: float = 100,
12101210
):
12111211
"""
@@ -1248,7 +1248,7 @@ def add_take_profit(
12481248
self,
12491249
trade,
12501250
percentage: float,
1251-
trade_risk_type: TradeRiskType = TradeRiskType.FIXED,
1251+
trade_risk_type = TradeRiskType.FIXED,
12521252
sell_percentage: float = 100,
12531253
) -> None:
12541254
"""

investing_algorithm_framework/cli/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def cli():
2323
'--type',
2424
default="default",
2525
help="Type of app to create. "
26-
"Options are: 'default', 'default-web', 'azure-function'."
26+
"Options are: 'default', 'default_web', 'azure_function'."
2727
)
2828
@click.option(
2929
'--path', default=None, help="Path to directory to initialize the app in"
@@ -43,6 +43,8 @@ def init(type, path, replace):
4343
type (str): Type of app to create. Options are: 'default',
4444
'default-web', 'azure-function'.
4545
path (str): Path to directory to initialize the app in
46+
replace (bool): If True, existing files will be replaced.
47+
If False, existing files will not be replaced.
4648
4749
Returns:
4850
None

investing_algorithm_framework/cli/templates/app-web.py.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ load_dotenv()
99
logging.config.dictConfig(DEFAULT_LOGGING_CONFIG)
1010

1111
app = create_app(web=True)
12+
app.add_market(market="binance", initial_balance=1000, trading_symbol="EUR")
1213
algorithm = Algorithm(name="MyTradingBot")
1314
algorithm.add_strategy(MyTradingStrategy)
1415
app.add_algorithm(algorithm)

investing_algorithm_framework/cli/templates/app.py.template

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ load_dotenv()
99
logging.config.dictConfig(DEFAULT_LOGGING_CONFIG)
1010

1111
app = create_app()
12-
app.add_portfolio_configuration(
13-
PortfolioConfiguration(
14-
initial_balance=1000,
15-
trading_symbol="EUR",
16-
market="bitvavo"
17-
)
18-
)
12+
app.add_market(market="binance", initial_balance=1000, trading_symbol="EUR")
1913
algorithm = Algorithm(name="MyTradingBot")
2014
algorithm.add_strategy(MyTradingStrategy)
2115
app.add_algorithm(algorithm)

investing_algorithm_framework/cli/templates/app_azure_function.py.template

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ from strategies.strategy import MyTradingStrategy
88
load_dotenv()
99

1010
app = create_app()
11-
app.add_portfolio_configuration(
12-
PortfolioConfiguration(
13-
initial_balance=1000,
14-
trading_symbol="EUR",
15-
market="bitvavo"
16-
)
17-
)
11+
app.add_market(market="binance", initial_balance=1000, trading_symbol="EUR")
1812
algorithm = Algorithm(name="MyTradingBot")
1913
algorithm.add_strategy(MyTradingStrategy)
2014
app.add_algorithm(algorithm)

investing_algorithm_framework/cli/templates/app_web.py.template

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ load_dotenv()
99
logging.config.dictConfig(DEFAULT_LOGGING_CONFIG)
1010

1111
app = create_app(web=True)
12-
app.add_portfolio_configuration(
13-
PortfolioConfiguration(
14-
initial_balance=1000,
15-
trading_symbol="EUR",
16-
market="bitvavo"
17-
)
18-
)
12+
app.add_market(market="binance", initial_balance=1000, trading_symbol="EUR")
1913
algorithm = Algorithm(name="MyTradingBot")
2014
algorithm.add_strategy(MyTradingStrategy)
2115
app.add_algorithm(algorithm)

investing_algorithm_framework/domain/order_executor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ def supports_market(self, market):
8585
raise NotImplementedError(
8686
"Subclasses must implement this method."
8787
)
88+
89+
def __repr__(self):
90+
"""
91+
Returns a string representation of the order executor.
92+
"""
93+
return f"{self.__class__.__name__}(priority={self._priority})"

investing_algorithm_framework/domain/portfolio_provider.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def priority(self):
2626
"""
2727
return self._priority
2828

29+
@priority.setter
30+
def priority(self, value: int):
31+
"""
32+
Sets the priority of the portfolio provider.
33+
"""
34+
self._priority = value
35+
2936
@abstractmethod
3037
def get_order(
3138
self, portfolio, order, market_credential
@@ -87,3 +94,6 @@ def supports_market(self, market) -> bool:
8794
bool: True if the market is supported, False otherwise
8895
"""
8996
raise NotImplementedError("Subclasses must implement this method.")
97+
98+
def __repr__(self):
99+
return f"{self.__class__.__name__}(priority={self.priority})"

investing_algorithm_framework/services/market_data_source_service/market_data_source_service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ class MarketDataSourceService:
2020
source that matches the symbol, market and time frame provided by the user.
2121
If there is, it will use that market data source to get the data. If there
2222
is not, it will use the MarketService to get the data.
23+
24+
Attributes:
25+
market_service: MarketService - The market service to use to get
26+
the data if there is no market data source that matches the
27+
symbol, market and time frame provided by the user.
28+
market_credential_service: MarketCredentialService - The market
29+
credential service to use to get the credentials for the market
30+
data sources.
31+
configuration_service: ConfigurationService - The configuration
32+
service to use to get the configuration for the market data
33+
sources.
34+
market_data_sources: List[MarketDataSource] - The list of market
35+
data sources to use to get the data.
2336
"""
2437
_market_data_sources: List[MarketDataSource] = []
2538

investing_algorithm_framework/services/portfolios/portfolio_sync_service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def sync_unallocated(self, portfolio):
8787

8888
portfolio_provider = self.portfolio_provider_lookup\
8989
.get_portfolio_provider(portfolio.market)
90-
9190
position = portfolio_provider.get_position(
9291
portfolio, portfolio.trading_symbol, market_credential
9392
)

tests/resources/stubs/order_executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self):
1515
self.order_status = None
1616

1717
def execute_order(self, portfolio, order, market_credential) -> Order:
18-
order.external_id = random_string(10)
18+
order.external_id = random_string(10)
1919
order.status = OrderStatus.OPEN
2020

2121
if self.order_amount is not None:

tests/resources/test_base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ def create_app(self):
198198
self.iaf_app.add_market_credential(market_credential)
199199

200200
if len(self.portfolio_configurations) > 0:
201-
for portfolio_configuration in self.portfolio_configurations:
202-
self.iaf_app.add_portfolio_configuration(
203-
portfolio_configuration
204-
)
201+
for portfolio_configuration in self.portfolio_configurations:
202+
self.iaf_app.add_portfolio_configuration(
203+
portfolio_configuration
204+
)
205205

206-
# Add all market credentials
207-
if len(self.market_credentials) > 0:
208-
for market_credential in self.market_credentials:
209-
self.iaf_app.add_market_credential(market_credential)
206+
# Add all market credentials
207+
if len(self.market_credentials) > 0:
208+
for market_credential in self.market_credentials:
209+
self.iaf_app.add_market_credential(market_credential)
210210

211211
if self.initialize:
212212
self.iaf_app.initialize_config()

tests/services/test_trade_service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3238,7 +3238,6 @@ def test_add_stop_loss_100_percent_and_take_profit_sell(self):
32383238
)
32393239

32403240
sell_order_data = trade_service.get_triggered_take_profit_orders()
3241-
print(sell_order_data)
32423241
self.assertEqual(2, len(sell_order_data[0]['take_profits']))
32433242

32443243
for order_data in sell_order_data:

0 commit comments

Comments
 (0)