diff --git a/investing_algorithm_framework/app/app.py b/investing_algorithm_framework/app/app.py index 7b79a35..0fe5ee7 100644 --- a/investing_algorithm_framework/app/app.py +++ b/investing_algorithm_framework/app/app.py @@ -39,10 +39,9 @@ def on_run(self, app, algorithm: Algorithm): class App: - def __init__(self, state_handler=None, web=False): + def __init__(self, state_handler=None): self._flask_app: Optional[Flask] = None self.container = None - self._web = web self._algorithm: Optional[Algorithm] = None self._started = False self._tasks = [] @@ -134,13 +133,8 @@ def initialize_config(self): config = configuration_service.get_config() - if APP_MODE not in config or config[APP_MODE] is None: - if self._web: - configuration_service.add_value(APP_MODE, AppMode.WEB.value) - else: - configuration_service.add_value( - APP_MODE, AppMode.DEFAULT.value - ) + if APP_MODE not in config: + configuration_service.add_value(APP_MODE, AppMode.DEFAULT.value) def initialize(self): """ @@ -198,10 +192,13 @@ def initialize(self): for strategy in self.algorithm.strategies: - for market_data_source in strategy.market_data_sources: - market_data_source_service.add(market_data_source) + if strategy.market_data_sources is not None: + for market_data_source in strategy.market_data_sources: + market_data_source_service.add(market_data_source) + + config = self.container.configuration_service().get_config() - if self._web: + if config[APP_MODE] == AppMode.WEB.value: self._configuration_service.add_value( APP_MODE, AppMode.WEB.value ) @@ -322,6 +319,15 @@ def _initialize_app_for_backtest( configuration_service.add_value( BACKTESTING_END_DATE, backtest_date_range.end_date ) + configuration_service.add_value( + DATABASE_NAME, "backtest-database.sqlite3" + ) + configuration_service.add_value( + DATABASE_DIRECTORY_PATH, + os.path.join( + configuration_service.config[RESOURCE_DIRECTORY], "backtest_databases" + ) + ) if pending_order_check_interval is not None: configuration_service.add_value( @@ -330,7 +336,7 @@ def _initialize_app_for_backtest( ) # Create resource dir if not exits - self._create_resource_directory_if_not_exists() + self._create_resources_if_not_exists() def _create_backtest_database_if_not_exists(self): """ @@ -644,10 +650,6 @@ def add_portfolio_configuration(self, portfolio_configuration): .portfolio_configuration_service() portfolio_configuration_service.add(portfolio_configuration) - @property - def web(self): - return self._web - @property def running(self): return self.algorithm.running diff --git a/investing_algorithm_framework/create_app.py b/investing_algorithm_framework/create_app.py index 991c32c..054fd58 100644 --- a/investing_algorithm_framework/create_app.py +++ b/investing_algorithm_framework/create_app.py @@ -3,14 +3,15 @@ from .app import App from .dependency_container import setup_dependency_container +from .domain import APP_MODE, AppMode logger = logging.getLogger("investing_algorithm_framework") def create_app( config: dict = None, - web=False, - state_handler=None + state_handler=None, + web: bool = False ) -> App: """ Factory method to create an app instance. @@ -26,7 +27,7 @@ def create_app( # Load the environment variables load_dotenv() - app = App(web=web, state_handler=state_handler) + app = App(state_handler=state_handler) app = setup_dependency_container( app, ["investing_algorithm_framework"], @@ -38,5 +39,8 @@ def create_app( if config is not None: app.set_config_with_dict(config) + if web: + app.set_config("APP_MODE", AppMode.WEB.value) + logger.info("Investing algoritm framework app created") return app diff --git a/tests/resources/test_base.py b/tests/resources/test_base.py index cdfaf62..485593d 100644 --- a/tests/resources/test_base.py +++ b/tests/resources/test_base.py @@ -70,6 +70,8 @@ def setUp(self) -> None: for market_credential in self.market_credentials: self.app.add_market_credential(market_credential) + self.app.initialize_config() + if self.initialize: self.app.initialize() diff --git a/tests/services/test_portfolio_sync_service.py b/tests/services/test_portfolio_sync_service.py index 1ba3a38..d03c17d 100644 --- a/tests/services/test_portfolio_sync_service.py +++ b/tests/services/test_portfolio_sync_service.py @@ -1,8 +1,5 @@ -from datetime import datetime - from investing_algorithm_framework import PortfolioConfiguration, Algorithm, \ - MarketCredential, OperationalException, RESERVED_BALANCES, APP_MODE, \ - Order, SYMBOLS, AppMode + MarketCredential, OperationalException, RESERVED_BALANCES from tests.resources import TestBase @@ -36,6 +33,7 @@ def test_sync_unallocated(self): ) self.market_service.balances = {"EUR": 1000} self.app.add_algorithm(Algorithm()) + self.app.initialize_config() self.app.initialize() portfolio = self.app.container.portfolio_service()\ @@ -76,12 +74,11 @@ def test_sync_unallocated_with_no_balance(self): self.app.add_algorithm(Algorithm()) with self.assertRaises(OperationalException) as context: + self.app.initialize_config() self.app.initialize() self.assertEqual( - "The initial balance of the portfolio configuration is more than" " the available balance on the exchange. Please make sure" - " that the initial balance of the portfolio configuration" - " is less than the available balance on the exchange.", + "The initial balance of the portfolio configuration (1000.0 EUR) is more than the available balance on the exchange. Please make sure that the initial balance of the portfolio configuration is less than the available balance on the exchange 0.0 EUR.", str(context.exception) ) @@ -105,6 +102,7 @@ def test_sync_unallocated_with_reserved(self): ) self.market_service.balances = {"EUR": 1200} self.app.add_algorithm(Algorithm()) + self.app.initialize_config() self.app.initialize() portfolio = self.app.container.portfolio_service() \ @@ -129,6 +127,7 @@ def test_sync_unallocated_with_initial_size(self): ) self.market_service.balances = {"EUR": 1200} self.app.add_algorithm(Algorithm()) + self.app.initialize_config() self.app.initialize() portfolio = self.app.container.portfolio_service() \