diff --git a/investing_algorithm_framework/app/app.py b/investing_algorithm_framework/app/app.py index 34f8450e..f42741cb 100644 --- a/investing_algorithm_framework/app/app.py +++ b/investing_algorithm_framework/app/app.py @@ -148,9 +148,10 @@ def initialize(self, sync=False): if not portfolio_service.exists( {"identifier": portfolio_configuration.identifier} ): - portfolio = portfolio_service.create_portfolio_from_configuration( - portfolio_configuration - ) + portfolio = portfolio_service\ + .create_portfolio_from_configuration( + portfolio_configuration + ) self.sync(portfolio) synced_portfolios.append(portfolio) @@ -162,9 +163,6 @@ def initialize(self, sync=False): if portfolio not in synced_portfolios: self.sync(portfolio) - - - def sync(self, portfolio): """ Sync the portfolio with the exchange. This method should be called @@ -259,8 +257,9 @@ def _initialize_app_for_backtest( configuration_service.config[BACKTESTING_START_DATE] = \ backtest_start_date configuration_service.config[BACKTESTING_END_DATE] = backtest_end_date - configuration_service.config[BACKTESTING_PENDING_ORDER_CHECK_INTERVAL] \ - = pending_order_check_interval + configuration_service.config[ + BACKTESTING_PENDING_ORDER_CHECK_INTERVAL + ] = pending_order_check_interval # Create resource dir if not exits self._create_resource_directory_if_not_exists() diff --git a/investing_algorithm_framework/domain/models/market/market_credential.py b/investing_algorithm_framework/domain/models/market/market_credential.py index 1166a5b5..3ea70dfa 100644 --- a/investing_algorithm_framework/domain/models/market/market_credential.py +++ b/investing_algorithm_framework/domain/models/market/market_credential.py @@ -31,5 +31,4 @@ def secret_key(self): def __repr__(self): return f"MarketCredential(" \ - f"{self.market}, {self.api_key}, {self.secret_key}" \ - f")" \ No newline at end of file + f"{self.market}, {self.api_key}, {self.secret_key}" diff --git a/investing_algorithm_framework/domain/services/portfolios/__init__.py b/investing_algorithm_framework/domain/services/portfolios/__init__.py index ee2626c4..0366e9a5 100644 --- a/investing_algorithm_framework/domain/services/portfolios/__init__.py +++ b/investing_algorithm_framework/domain/services/portfolios/__init__.py @@ -2,4 +2,4 @@ __all__ = [ "AbstractPortfolioSyncService" -] \ No newline at end of file +] diff --git a/investing_algorithm_framework/domain/services/portfolios/portfolio_sync_service.py b/investing_algorithm_framework/domain/services/portfolios/portfolio_sync_service.py index 81c8a534..c6784683 100644 --- a/investing_algorithm_framework/domain/services/portfolios/portfolio_sync_service.py +++ b/investing_algorithm_framework/domain/services/portfolios/portfolio_sync_service.py @@ -12,4 +12,4 @@ def sync_orders(self, portfolio): pass def sync_trades(self, portfolio): - pass \ No newline at end of file + pass diff --git a/investing_algorithm_framework/infrastructure/repositories/order_repository.py b/investing_algorithm_framework/infrastructure/repositories/order_repository.py index 7835d04d..656a9f25 100644 --- a/investing_algorithm_framework/infrastructure/repositories/order_repository.py +++ b/investing_algorithm_framework/infrastructure/repositories/order_repository.py @@ -33,9 +33,6 @@ def _apply_query_params(self, db, query, query_params): order_by_created_at_asc = self.get_query_param( "order_by_created_at_asc", query_params ) - order_by_created_at_desc = self.get_query_param( - "order_by_created_at_desc", query_params - ) if portfolio_query_param is not None: portfolio = db.query(SQLPortfolio).filter_by( diff --git a/investing_algorithm_framework/services/__init__.py b/investing_algorithm_framework/services/__init__.py index 35f7582f..01c4e0ff 100644 --- a/investing_algorithm_framework/services/__init__.py +++ b/investing_algorithm_framework/services/__init__.py @@ -1,3 +1,8 @@ +from .backtesting import BacktestService, BacktestReportWriterService +from .configuration_service import ConfigurationService +from .market_credential_service import MarketCredentialService +from .market_data_source_service import MarketDataSourceService, \ + BacktestMarketDataSourceService from .order_service import OrderService, OrderBacktestService from .portfolios import PortfolioService, BacktestPortfolioService, \ PortfolioConfigurationService, PortfolioSyncService, \ @@ -6,14 +11,8 @@ from .position_snapshot_service import PositionSnapshotService from .repository_service import RepositoryService from .strategy_orchestrator_service import StrategyOrchestratorService -from .market_data_source_service import MarketDataSourceService, \ - BacktestMarketDataSourceService -from .backtesting import BacktestService, BacktestReportWriterService -from .configuration_service import ConfigurationService -from .market_credential_service import MarketCredentialService from .trade_service import TradeService - __all__ = [ "StrategyOrchestratorService", "OrderService", @@ -26,6 +25,7 @@ "BacktestReportWriterService", "OrderBacktestService", "ConfigurationService", + "PortfolioSyncService", "PortfolioSnapshotService", "PositionSnapshotService", "MarketCredentialService", diff --git a/investing_algorithm_framework/services/portfolios/portfolio_sync_service.py b/investing_algorithm_framework/services/portfolios/portfolio_sync_service.py index 86ed7c96..bc06defa 100644 --- a/investing_algorithm_framework/services/portfolios/portfolio_sync_service.py +++ b/investing_algorithm_framework/services/portfolios/portfolio_sync_service.py @@ -11,16 +11,17 @@ class PortfolioSyncService(AbstractPortfolioSyncService): """ Service to sync the portfolio with the exchange. """ + def __init__( - self, - trade_service: TradeService, - configuration_service, - order_repository, - position_repository, - portfolio_repository, - portfolio_configuration_service, - market_credential_service, - market_service + self, + trade_service: TradeService, + configuration_service, + order_repository, + position_repository, + portfolio_repository, + portfolio_configuration_service, + market_credential_service, + market_service ): self.trade_service = trade_service self.configuration_service = configuration_service @@ -88,11 +89,10 @@ def sync_unallocated(self, portfolio): unallocated = unallocated - reserved_unallocated - if portfolio.unallocated is not None and\ + if portfolio.unallocated is not None and \ unallocated != portfolio.unallocated: if unallocated < portfolio.unallocated: - raise OperationalException( "There seems to be a mismatch between " "the portfolio configuration and the balances on" @@ -112,12 +112,12 @@ def sync_unallocated(self, portfolio): # create the portfolio with the initial balance if unallocated > portfolio.unallocated and \ not self.portfolio_repository.exists( - {"identifier": portfolio.identifier} - ): + {"identifier": portfolio.identifier} + ): unallocated = portfolio.unallocated if not self.portfolio_repository.exists( - {"identifier": portfolio.identifier} + {"identifier": portfolio.identifier} ): create_data = { "identifier": portfolio.get_identifier(), @@ -138,7 +138,8 @@ def sync_unallocated(self, portfolio): def sync_positions(self, portfolio): """ - Method to sync the portfolio balances with the balances on the exchange. + Method to sync the portfolio balances with the balances + on the exchange. This method will retrieve the balances from the exchange and update the portfolio balances accordingly. @@ -249,14 +250,14 @@ def sync_orders(self, portfolio): if portfolio_configuration.track_from is not None: ordered_external_order_list = [ order for order in ordered_external_order_list - if order.created_at >= - portfolio_configuration.track_from + if order.created_at >= portfolio_configuration + .track_from ] for external_order in ordered_external_order_list: if self.order_repository.exists( - {"external_id": external_order.external_id} + {"external_id": external_order.external_id} ): logger.info("Updating existing order") order = self.order_repository.find( diff --git a/investing_algorithm_framework/services/trade_service/trade_service.py b/investing_algorithm_framework/services/trade_service/trade_service.py index 59b2b410..c9b1e718 100644 --- a/investing_algorithm_framework/services/trade_service/trade_service.py +++ b/investing_algorithm_framework/services/trade_service/trade_service.py @@ -353,7 +353,7 @@ def close_trades(self, sell_order: Order, amount_to_close: float) -> None: remaining = amount_to_close - to_be_closed cost = buy_order.get_price() * to_be_closed net_gain = (sell_order.get_price() - buy_order.get_price()) \ - * to_be_closed + * to_be_closed amount_to_close = remaining self.order_service.repository.update( buy_order.id, @@ -367,7 +367,7 @@ def close_trades(self, sell_order: Order, amount_to_close: float) -> None: else: to_be_closed = amount_to_close net_gain = (sell_order.get_price() - buy_order.get_price()) \ - * to_be_closed + * to_be_closed cost = buy_order.get_price() * amount_to_close closed_amount = buy_order.get_trade_closed_amount()