Skip to content

Commit

Permalink
Fix flake8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Mar 27, 2024
1 parent 67ec6e6 commit c786d9b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 41 deletions.
15 changes: 7 additions & 8 deletions investing_algorithm_framework/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ def secret_key(self):

def __repr__(self):
return f"MarketCredential(" \
f"{self.market}, {self.api_key}, {self.secret_key}" \
f")"
f"{self.market}, {self.api_key}, {self.secret_key}"
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

__all__ = [
"AbstractPortfolioSyncService"
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def sync_orders(self, portfolio):
pass

def sync_trades(self, portfolio):
pass
pass
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions investing_algorithm_framework/services/__init__.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand All @@ -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",
Expand All @@ -26,6 +25,7 @@
"BacktestReportWriterService",
"OrderBacktestService",
"ConfigurationService",
"PortfolioSyncService",
"PortfolioSnapshotService",
"PositionSnapshotService",
"MarketCredentialService",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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(),
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()

Expand Down

0 comments on commit c786d9b

Please sign in to comment.