From 62529cc4bcdd22885cac6534bc0ea80efdb084e8 Mon Sep 17 00:00:00 2001 From: DUYN Date: Thu, 25 Jun 2020 10:37:29 +0200 Subject: [PATCH] Rearrange core package --- bin/investing-algorithm-framework-admin | 2 +- investing_algorithm_framework/__main__.py | 4 +- .../{core => }/configuration/__init__.py | 6 +- .../configuration/config_constants.py | 0 .../configuration/setup/__init__.py | 1 + .../setup/default_template_creators.py | 4 +- .../setup}/template.py | 0 .../configuration/setup/template_creator.py | 2 +- .../core/configuration/setup/__init__.py | 1 - .../core/context/context.py | 12 +- .../core/data_providers/__init__.py | 1 - .../core/executors/executor.py | 2 +- .../core/management/__init__.py | 2 - .../core/management/command_manager.py | 73 ------------ .../core/order_executors/__init__.py | 1 - .../core/resolvers/__init__.py | 3 +- .../{class_collector.py => class_resolver.py} | 3 +- .../core/resolvers/database_resolver.py | 5 +- .../core/resolvers/module_loaders/__init__.py | 1 - .../module_loaders/data_providers_loader.py | 37 ------- .../core/state/__init__.py | 2 + .../{states/bot_state.py => state/state.py} | 6 +- .../core/states/__init__.py | 2 - .../core/strategies/__init__.py | 1 - .../core/validators/__init__.py | 1 + .../state_validators.py} | 0 .../management/__init__.py | 3 + .../{core => }/management/command.py | 51 +-------- .../management/command_manager.py | 104 ++++++++++++++++++ .../management/commands/__init__.py | 0 .../commands/create_standard_algo.py} | 6 +- .../{core => }/management/commands/help.py | 6 +- .../management/commands/run_algorithm.py | 6 +- .../{core => }/management/utils.py | 11 +- .../{core/states => }/templates/__init__.py | 0 .../templates/data_providers/__init__.py | 1 + .../data_providers/data_provider.py | 0 .../templates/order_executors/__init__.py | 1 + .../order_executors/order_executor.py | 0 .../configuration/__init__.py-template | 0 .../configuration/context.py-template | 8 +- .../configuration/settings.py-template | 0 .../data_providers/__init__.py-template | 0 .../data_providers/data_providers.py-template | 2 +- .../order_executors/__init__.py-template | 0 .../order_executors.py-template | 2 +- .../strategies/__init__.py-template | 0 .../strategies/strategies.py-template | 2 +- .../manage.py-template | 2 +- .../templates/states}/__init__.py | 0 .../states}/data_providing_state.py | 12 +- .../states}/ordering_state.py | 8 +- .../states}/setup_state.py | 12 +- .../states}/strategy_state.py | 8 +- .../templates/strategies/__init__.py | 1 + .../strategies/strategy.py | 0 .../core/resolvers/test_database_resolver.py | 2 +- tests/core/test_context.py | 23 ++++ tests/{core => }/management/__init__.py | 0 .../test_create_standard_algo.py} | 4 +- tests/templates/__init__.py | 0 tests/templates/data_providers/__init__.py | 0 .../data_providers/resources.py | 2 +- .../data_providers/test_data_providers.py} | 2 +- 64 files changed, 210 insertions(+), 241 deletions(-) rename investing_algorithm_framework/{core => }/configuration/__init__.py (94%) rename investing_algorithm_framework/{core => }/configuration/config_constants.py (100%) create mode 100644 investing_algorithm_framework/configuration/setup/__init__.py rename investing_algorithm_framework/{core => }/configuration/setup/default_template_creators.py (95%) rename investing_algorithm_framework/{core/configuration => configuration/setup}/template.py (100%) rename investing_algorithm_framework/{core => }/configuration/setup/template_creator.py (92%) delete mode 100644 investing_algorithm_framework/core/configuration/setup/__init__.py delete mode 100644 investing_algorithm_framework/core/data_providers/__init__.py delete mode 100644 investing_algorithm_framework/core/management/__init__.py delete mode 100644 investing_algorithm_framework/core/management/command_manager.py delete mode 100644 investing_algorithm_framework/core/order_executors/__init__.py rename investing_algorithm_framework/core/resolvers/{class_collector.py => class_resolver.py} (98%) delete mode 100644 investing_algorithm_framework/core/resolvers/module_loaders/__init__.py delete mode 100644 investing_algorithm_framework/core/resolvers/module_loaders/data_providers_loader.py create mode 100644 investing_algorithm_framework/core/state/__init__.py rename investing_algorithm_framework/core/{states/bot_state.py => state/state.py} (91%) delete mode 100644 investing_algorithm_framework/core/states/__init__.py delete mode 100644 investing_algorithm_framework/core/strategies/__init__.py create mode 100644 investing_algorithm_framework/core/validators/__init__.py rename investing_algorithm_framework/core/{context/state_validator.py => validators/state_validators.py} (100%) create mode 100644 investing_algorithm_framework/management/__init__.py rename investing_algorithm_framework/{core => }/management/command.py (67%) create mode 100644 investing_algorithm_framework/management/command_manager.py rename investing_algorithm_framework/{core => }/management/commands/__init__.py (100%) rename investing_algorithm_framework/{core/management/commands/create_algorithm.py => management/commands/create_standard_algo.py} (92%) rename investing_algorithm_framework/{core => }/management/commands/help.py (83%) rename investing_algorithm_framework/{core => }/management/commands/run_algorithm.py (76%) rename investing_algorithm_framework/{core => }/management/utils.py (69%) rename investing_algorithm_framework/{core/states => }/templates/__init__.py (100%) create mode 100644 investing_algorithm_framework/templates/data_providers/__init__.py rename investing_algorithm_framework/{core => templates}/data_providers/data_provider.py (100%) create mode 100644 investing_algorithm_framework/templates/order_executors/__init__.py rename investing_algorithm_framework/{core => templates}/order_executors/order_executor.py (100%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/configuration/__init__.py-template (100%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/configuration/context.py-template (63%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/configuration/settings.py-template (100%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/data_providers/__init__.py-template (100%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/data_providers/data_providers.py-template (77%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/order_executors/__init__.py-template (100%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/order_executors/order_executors.py-template (52%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/strategies/__init__.py-template (100%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/algorithm_project_template/strategies/strategies.py-template (78%) rename investing_algorithm_framework/templates/{ => projects}/algorithm_project_directory/manage.py-template (87%) rename {tests/core/data_providers => investing_algorithm_framework/templates/states}/__init__.py (100%) rename investing_algorithm_framework/{core/states/templates => templates/states}/data_providing_state.py (91%) rename investing_algorithm_framework/{core/states/templates => templates/states}/ordering_state.py (73%) rename investing_algorithm_framework/{core/states/templates => templates/states}/setup_state.py (61%) rename investing_algorithm_framework/{core/states/templates => templates/states}/strategy_state.py (92%) create mode 100644 investing_algorithm_framework/templates/strategies/__init__.py rename investing_algorithm_framework/{core => templates}/strategies/strategy.py (100%) create mode 100644 tests/core/test_context.py rename tests/{core => }/management/__init__.py (100%) rename tests/{core/management/test_create_algorithm.py => management/test_create_standard_algo.py} (92%) create mode 100644 tests/templates/__init__.py create mode 100644 tests/templates/data_providers/__init__.py rename tests/{core => templates}/data_providers/resources.py (88%) rename tests/{core/data_providers/data_provider.py => templates/data_providers/test_data_providers.py} (88%) diff --git a/bin/investing-algorithm-framework-admin b/bin/investing-algorithm-framework-admin index d76a586c..99647ba4 100644 --- a/bin/investing-algorithm-framework-admin +++ b/bin/investing-algorithm-framework-admin @@ -1,6 +1,6 @@ #!/usr/bin/env python -from investing_algorithm_framework.core.management import execute_from_command_line +from investing_algorithm_framework.management import execute_from_command_line if __name__ == "__main__": execute_from_command_line() diff --git a/investing_algorithm_framework/__main__.py b/investing_algorithm_framework/__main__.py index 000d890e..e50b38f1 100644 --- a/investing_algorithm_framework/__main__.py +++ b/investing_algorithm_framework/__main__.py @@ -1,9 +1,9 @@ """ Invokes investing_algorithm_framework-admin when the investing_algorithm_framework framework module is run as a script. -Example: python -m investing_algorithm_framework createalgorithm SampleAlgorithm +Example: python -m investing_algorithm_framework create_standard_algo SampleAlgorithm """ -from investing_algorithm_framework.core.management import execute_from_command_line +from investing_algorithm_framework.management import execute_from_command_line if __name__ == "__main__": execute_from_command_line() diff --git a/investing_algorithm_framework/core/configuration/__init__.py b/investing_algorithm_framework/configuration/__init__.py similarity index 94% rename from investing_algorithm_framework/core/configuration/__init__.py rename to investing_algorithm_framework/configuration/__init__.py index e3b9b05b..12a9b94e 100644 --- a/investing_algorithm_framework/core/configuration/__init__.py +++ b/investing_algorithm_framework/configuration/__init__.py @@ -5,13 +5,11 @@ from enum import Enum from investing_algorithm_framework.core.exceptions import ImproperlyConfigured, OperationalException -from investing_algorithm_framework.core.configuration.template import Template -from investing_algorithm_framework.core.configuration.config_constants import SETTINGS_MODULE_PATH_ENV_NAME, \ - SETTINGS_STRATEGY_REGISTERED_APPS, SETTINGS_DATA_PROVIDER_REGISTERED_APPS, BASE_DIR, SETTINGS_LOGGING_CONFIG +from investing_algorithm_framework.configuration.config_constants import SETTINGS_MODULE_PATH_ENV_NAME, \ + SETTINGS_STRATEGY_REGISTERED_APPS, SETTINGS_DATA_PROVIDER_REGISTERED_APPS, SETTINGS_LOGGING_CONFIG class TimeUnit(Enum): - SECOND = 'SEC', MINUTE = 'MIN', HOUR = 'HR', diff --git a/investing_algorithm_framework/core/configuration/config_constants.py b/investing_algorithm_framework/configuration/config_constants.py similarity index 100% rename from investing_algorithm_framework/core/configuration/config_constants.py rename to investing_algorithm_framework/configuration/config_constants.py diff --git a/investing_algorithm_framework/configuration/setup/__init__.py b/investing_algorithm_framework/configuration/setup/__init__.py new file mode 100644 index 00000000..5369c0f7 --- /dev/null +++ b/investing_algorithm_framework/configuration/setup/__init__.py @@ -0,0 +1 @@ +from investing_algorithm_framework.configuration.setup.default_template_creators import DefaultProjectCreator diff --git a/investing_algorithm_framework/core/configuration/setup/default_template_creators.py b/investing_algorithm_framework/configuration/setup/default_template_creators.py similarity index 95% rename from investing_algorithm_framework/core/configuration/setup/default_template_creators.py rename to investing_algorithm_framework/configuration/setup/default_template_creators.py index e5eb9e05..877de4c0 100644 --- a/investing_algorithm_framework/core/configuration/setup/default_template_creators.py +++ b/investing_algorithm_framework/configuration/setup/default_template_creators.py @@ -3,11 +3,11 @@ import investing_algorithm_framework from investing_algorithm_framework.core.exceptions import ImproperlyConfigured -from investing_algorithm_framework.core.configuration.setup.template_creator import TemplateCreator +from investing_algorithm_framework.configuration.setup.template_creator import TemplateCreator class DefaultProjectCreator(TemplateCreator): - TEMPLATE_ROOT_DIR = 'templates/algorithm_project_directory' + TEMPLATE_ROOT_DIR = 'templates/projects/algorithm_project_directory' PROJECT_NAME_PLACEHOLDER = '{{ project_name }}' PROJECT_TEMPLATE_DIR_NAME = 'algorithm_project_template' diff --git a/investing_algorithm_framework/core/configuration/template.py b/investing_algorithm_framework/configuration/setup/template.py similarity index 100% rename from investing_algorithm_framework/core/configuration/template.py rename to investing_algorithm_framework/configuration/setup/template.py diff --git a/investing_algorithm_framework/core/configuration/setup/template_creator.py b/investing_algorithm_framework/configuration/setup/template_creator.py similarity index 92% rename from investing_algorithm_framework/core/configuration/setup/template_creator.py rename to investing_algorithm_framework/configuration/setup/template_creator.py index b26519dc..2bfb62d3 100644 --- a/investing_algorithm_framework/core/configuration/setup/template_creator.py +++ b/investing_algorithm_framework/configuration/setup/template_creator.py @@ -2,7 +2,7 @@ import stat from abc import ABC, abstractmethod -from investing_algorithm_framework.core.configuration import Template +from investing_algorithm_framework.configuration.setup.template import Template class TemplateCreator(Template, ABC): diff --git a/investing_algorithm_framework/core/configuration/setup/__init__.py b/investing_algorithm_framework/core/configuration/setup/__init__.py deleted file mode 100644 index 467e9860..00000000 --- a/investing_algorithm_framework/core/configuration/setup/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from investing_algorithm_framework.core.configuration.setup.default_template_creators import DefaultProjectCreator diff --git a/investing_algorithm_framework/core/context/context.py b/investing_algorithm_framework/core/context/context.py index 4cfa33e5..f7f78ef2 100644 --- a/investing_algorithm_framework/core/context/context.py +++ b/investing_algorithm_framework/core/context/context.py @@ -1,9 +1,9 @@ from typing import Type -from investing_algorithm_framework.core.configuration import settings +from investing_algorithm_framework.configuration import settings from investing_algorithm_framework.core.exceptions import OperationalException from investing_algorithm_framework.core.utils import Singleton -from investing_algorithm_framework.core.states import BotState +from investing_algorithm_framework.core.state import State class Context(metaclass=Singleton): @@ -13,15 +13,15 @@ class Context(metaclass=Singleton): """ # A reference to the current state of the context. - _state: BotState = None + _state: State = None # Settings reference settings = settings - def register_initial_state(self, bot_state: Type[BotState]) -> None: - self._state = bot_state(context=self) + def register_initial_state(self, state: Type[State]) -> None: + self._state = state(context=self) - def transition_to(self, bot_state: Type[BotState]) -> None: + def transition_to(self, bot_state: Type[State]) -> None: """ Function to change the running BotState at runtime. """ diff --git a/investing_algorithm_framework/core/data_providers/__init__.py b/investing_algorithm_framework/core/data_providers/__init__.py deleted file mode 100644 index 4206751c..00000000 --- a/investing_algorithm_framework/core/data_providers/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from investing_algorithm_framework.core.data_providers.data_provider import DataProvider diff --git a/investing_algorithm_framework/core/executors/executor.py b/investing_algorithm_framework/core/executors/executor.py index cced4296..2d4ca153 100644 --- a/investing_algorithm_framework/core/executors/executor.py +++ b/investing_algorithm_framework/core/executors/executor.py @@ -8,7 +8,7 @@ from investing_algorithm_framework.core.utils import StoppableThread from investing_algorithm_framework.core.events.observer import Observer from investing_algorithm_framework.core.events.observable import Observable -from investing_algorithm_framework.core.configuration.config_constants import DEFAULT_MAX_WORKERS +from investing_algorithm_framework.configuration.config_constants import DEFAULT_MAX_WORKERS class Executor(Observable, Observer, ABC): diff --git a/investing_algorithm_framework/core/management/__init__.py b/investing_algorithm_framework/core/management/__init__.py deleted file mode 100644 index 52bab28b..00000000 --- a/investing_algorithm_framework/core/management/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from investing_algorithm_framework.core.management.command import BaseCommand -from investing_algorithm_framework.core.management.command_manager import execute_from_command_line diff --git a/investing_algorithm_framework/core/management/command_manager.py b/investing_algorithm_framework/core/management/command_manager.py deleted file mode 100644 index ea8a3d75..00000000 --- a/investing_algorithm_framework/core/management/command_manager.py +++ /dev/null @@ -1,73 +0,0 @@ -import os -import sys - -from investing_algorithm_framework.core.management import BaseCommand -from investing_algorithm_framework.core.resolvers import ClassCollector -from investing_algorithm_framework.core.management.utils import get_commands -from investing_algorithm_framework.core.management.commands.help import HelpCommand - - -class ManagementUtility: - """ - Encapsulate the logic of the django-admin and manage.py utilities. - """ - - def __init__(self, argv=None) -> None: - self.argv = argv or sys.argv[:] - self.program_name = os.path.basename(self.argv[0]) - - if self.program_name == '__main__.py': - self.program_name = 'python -m investing-investing_algorithm_framework' - self.settings_exception = None - - def execute(self) -> None: - """ - Given the command-line arguments, figure out which sub command is being - run, create a parser appropriate to that command, and run it. - """ - - try: - sub_command = self.argv[1] - except IndexError: - sub_command = 'help' # Display help by default if no arguments are given. - - # Run the command if it is not a help command - if sub_command != 'help': - response = self.fetch_command(sub_command).run_from_argv(self.argv) - else: - # Help for sub command - if len(self.argv) > 2: - - # make the first argument the sub command and the second argument the help option - sub_command = self.argv[2] - option = '--help' - - self.argv[1] = sub_command - self.argv[2] = option - response = self.fetch_command(sub_command).run_from_argv(self.argv) - else: - # Show general help command - command = HelpCommand() - response = command.run_from_argv(self.argv) - - sys.stdout.write(response) - - def fetch_command(self, sub_command: str) -> BaseCommand: - commands = get_commands() - app_name = commands[sub_command] - - collector = ClassCollector( - '{}.management.commands'.format(app_name), - class_type=BaseCommand, - module_name=sub_command - ) - - command_instance = collector.instances[0] - return command_instance - - -def execute_from_command_line(argv=None): - - """Run the ManagementUtility.""" - utility = ManagementUtility(argv) - utility.execute() \ No newline at end of file diff --git a/investing_algorithm_framework/core/order_executors/__init__.py b/investing_algorithm_framework/core/order_executors/__init__.py deleted file mode 100644 index 6352e5b6..00000000 --- a/investing_algorithm_framework/core/order_executors/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from investing_algorithm_framework.core.order_executors.order_executor import OrderExecutor diff --git a/investing_algorithm_framework/core/resolvers/__init__.py b/investing_algorithm_framework/core/resolvers/__init__.py index 06ed3192..63d2bf7c 100644 --- a/investing_algorithm_framework/core/resolvers/__init__.py +++ b/investing_algorithm_framework/core/resolvers/__init__.py @@ -1,3 +1,4 @@ -from investing_algorithm_framework.core.resolvers.class_collector import ClassCollector from investing_algorithm_framework.core.resolvers.database_resolver import DatabaseResolver +from investing_algorithm_framework.core.resolvers.class_resolver import ClassResolver + diff --git a/investing_algorithm_framework/core/resolvers/class_collector.py b/investing_algorithm_framework/core/resolvers/class_resolver.py similarity index 98% rename from investing_algorithm_framework/core/resolvers/class_collector.py rename to investing_algorithm_framework/core/resolvers/class_resolver.py index e7dbceaf..725a75cc 100644 --- a/investing_algorithm_framework/core/resolvers/class_collector.py +++ b/investing_algorithm_framework/core/resolvers/class_resolver.py @@ -4,7 +4,7 @@ from importlib import import_module -class ClassCollector: +class ClassResolver: """ Class ClassCollector: This class will load all the classes of a specific type given the package_path. You can specify a module name if you know where the class is located @@ -55,4 +55,3 @@ def _find_classes(self, package_path: str, module_name: str = None) -> None: @property def instances(self) -> List[Any]: return self._class_instances - diff --git a/investing_algorithm_framework/core/resolvers/database_resolver.py b/investing_algorithm_framework/core/resolvers/database_resolver.py index 2042f316..4afef0d0 100644 --- a/investing_algorithm_framework/core/resolvers/database_resolver.py +++ b/investing_algorithm_framework/core/resolvers/database_resolver.py @@ -8,9 +8,8 @@ from sqlalchemy.orm.exc import DetachedInstanceError from sqlalchemy.exc import DatabaseError - -from investing_algorithm_framework.core.configuration import settings -from investing_algorithm_framework.core.configuration.config_constants import BASE_DIR, DATABASE_NAME +from investing_algorithm_framework.configuration import settings +from investing_algorithm_framework.configuration.config_constants import BASE_DIR, DATABASE_NAME from investing_algorithm_framework.core.exceptions import DatabaseOperationalException diff --git a/investing_algorithm_framework/core/resolvers/module_loaders/__init__.py b/investing_algorithm_framework/core/resolvers/module_loaders/__init__.py deleted file mode 100644 index fe4c826b..00000000 --- a/investing_algorithm_framework/core/resolvers/module_loaders/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from investing_algorithm_framework.core.resolvers.module_loaders.data_providers_loader import DataProvidersLoader diff --git a/investing_algorithm_framework/core/resolvers/module_loaders/data_providers_loader.py b/investing_algorithm_framework/core/resolvers/module_loaders/data_providers_loader.py deleted file mode 100644 index 6fa51886..00000000 --- a/investing_algorithm_framework/core/resolvers/module_loaders/data_providers_loader.py +++ /dev/null @@ -1,37 +0,0 @@ -from typing import List - -from investing_algorithm_framework.core.exceptions import ImproperlyConfigured -from investing_algorithm_framework.core.configuration import settings -from investing_algorithm_framework.core.resolvers import ClassCollector -from investing_algorithm_framework.core.data_providers import DataProvider - - -class DataProvidersLoader: - - def __init__(self): - """ - Constructor will load the settings and check if the settings are configured. In the case the settings are not - configured it will throw an exception. - """ - self._settings = settings - - def load_modules(self) -> List[DataProvider]: - """ - Function that will load the specified DataProvider instances. It will throw an exception if no - DataProviders are specified. If third party plugins are used, it will try to load them. When it fails to load - the plugins it will throw an error. - """ - - bot_name = self._settings.BOT_PROJECT_NAME - - class_collector = ClassCollector(bot_name, DataProvider) - - if len(class_collector.instances) == 0: - raise ImproperlyConfigured( - "There are no data_providers providers configured. Make sure you implement data_providers providers " - "or use a plugin" - ) - - return class_collector.instances - - diff --git a/investing_algorithm_framework/core/state/__init__.py b/investing_algorithm_framework/core/state/__init__.py new file mode 100644 index 00000000..7e6bd75e --- /dev/null +++ b/investing_algorithm_framework/core/state/__init__.py @@ -0,0 +1,2 @@ +from investing_algorithm_framework.core.state.state import State + diff --git a/investing_algorithm_framework/core/states/bot_state.py b/investing_algorithm_framework/core/state/state.py similarity index 91% rename from investing_algorithm_framework/core/states/bot_state.py rename to investing_algorithm_framework/core/state/state.py index a576b20e..ecd2510f 100644 --- a/investing_algorithm_framework/core/states/bot_state.py +++ b/investing_algorithm_framework/core/state/state.py @@ -1,12 +1,12 @@ from abc import ABC, abstractmethod from typing import List -from investing_algorithm_framework.core.context.state_validator import StateValidator +from investing_algorithm_framework.core.validators import StateValidator -class BotState(ABC): +class State(ABC): """ - Represents a state of the Bot, these states are use by the BotContext. Each implemented state represents a work + Represents a state of the Bot, these state are use by the BotContext. Each implemented state represents a work mode for the investing_algorithm_framework. """ diff --git a/investing_algorithm_framework/core/states/__init__.py b/investing_algorithm_framework/core/states/__init__.py deleted file mode 100644 index 7b4016c6..00000000 --- a/investing_algorithm_framework/core/states/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from investing_algorithm_framework.core.states.bot_state import BotState - diff --git a/investing_algorithm_framework/core/strategies/__init__.py b/investing_algorithm_framework/core/strategies/__init__.py deleted file mode 100644 index 3851b14d..00000000 --- a/investing_algorithm_framework/core/strategies/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from investing_algorithm_framework.core.strategies.strategy import Strategy diff --git a/investing_algorithm_framework/core/validators/__init__.py b/investing_algorithm_framework/core/validators/__init__.py new file mode 100644 index 00000000..e4c3db4f --- /dev/null +++ b/investing_algorithm_framework/core/validators/__init__.py @@ -0,0 +1 @@ +from investing_algorithm_framework.core.validators.state_validators import StateValidator diff --git a/investing_algorithm_framework/core/context/state_validator.py b/investing_algorithm_framework/core/validators/state_validators.py similarity index 100% rename from investing_algorithm_framework/core/context/state_validator.py rename to investing_algorithm_framework/core/validators/state_validators.py diff --git a/investing_algorithm_framework/management/__init__.py b/investing_algorithm_framework/management/__init__.py new file mode 100644 index 00000000..3da4365c --- /dev/null +++ b/investing_algorithm_framework/management/__init__.py @@ -0,0 +1,3 @@ +from investing_algorithm_framework.management.command import BaseCommand +from investing_algorithm_framework.management.command_manager import execute_from_command_line + diff --git a/investing_algorithm_framework/core/management/command.py b/investing_algorithm_framework/management/command.py similarity index 67% rename from investing_algorithm_framework/core/management/command.py rename to investing_algorithm_framework/management/command.py index d165ad8e..d8e20a8a 100644 --- a/investing_algorithm_framework/core/management/command.py +++ b/investing_algorithm_framework/management/command.py @@ -77,37 +77,10 @@ def execute(self, *args, **options) -> Any: """ Try to execute this command. """ + response = self.handle(*args, **options) - try: - return self.handle_command_success(self.handle(*args, **options)) - except CommandError as e: - return self.handle_command_error("Command error: " + e.__str__()) - except ImproperlyConfigured as e: - return self.handle_command_error("Configuration error: " + e.__str__()) - - def handle_command_error(self, error_message: str = None) -> str: - """ - Handling of errors as output to the user - """ - - if error_message is None: - return self.format_error_message("Command ended with error") - - return self.format_error_message(error_message) - - def handle_command_success(self, message: str = None) -> str: - """ - Handling of successful command execution as output to the user - """ - - if message is None: - - if self.success_message is not None: - return self.format_success_message(self.success_message) - else: - return self.format_success_message("Command finished") - else: - return self.format_success_message(message) + if response is None and self.success_message is not None: + return self.success_message @abstractmethod def handle(self, *args, **options) -> Any: @@ -129,21 +102,3 @@ def run_from_argv(self, argv) -> Any: args = cmd_options.pop('args', ()) return self.execute(*args, **cmd_options) - - @staticmethod - def format_success_message(message: str) -> str: - """ - Utility function to format a success message - """ - - message = Fore.GREEN + message + '\n' - return message - - @staticmethod - def format_error_message(message: str) -> str: - """ - Utility function to format an error message - """ - - message = Fore.RED + message + '\n' - return message diff --git a/investing_algorithm_framework/management/command_manager.py b/investing_algorithm_framework/management/command_manager.py new file mode 100644 index 00000000..d6d13a89 --- /dev/null +++ b/investing_algorithm_framework/management/command_manager.py @@ -0,0 +1,104 @@ +import os +import sys +from colorama import Fore + +from investing_algorithm_framework.management import BaseCommand +from investing_algorithm_framework.core.resolvers import ClassResolver +from investing_algorithm_framework.management.utils import get_commands +from investing_algorithm_framework.management.commands.help import HelpCommand +from investing_algorithm_framework.core.exceptions import OperationalException + + +class ManagementUtility: + """ + Encapsulate the logic of the django-admin and manage.py-template utilities. + """ + + def __init__(self, argv=None) -> None: + self.argv = argv or sys.argv[:] + self.program_name = os.path.basename(self.argv[0]) + + if self.program_name == '__main__.py': + self.program_name = 'python -m investing-investing_algorithm_framework' + self.settings_exception = None + + def execute(self) -> None: + """ + Given the command-line arguments, figure out which sub command is being + run, create a parser appropriate to that command, and run it. + """ + + try: + sub_command = self.argv[1] + except IndexError: + sub_command = 'help' # Display help by default if no arguments are given. + + try: + # Run the command if it is not a help command + if sub_command.lower() not in ['help', '-help', '--help', 'h', '-h']: + response = self.fetch_command(sub_command).run_from_argv(self.argv) + else: + # Help for sub command + if len(self.argv) > 2: + + # make the first argument the sub command and the second argument the help option + sub_command = self.argv[2] + option = '--help' + + self.argv[1] = sub_command + self.argv[2] = option + response = self.fetch_command(sub_command).run_from_argv(self.argv) + else: + # Show general help command + command = HelpCommand() + response = command.run_from_argv(self.argv) + except Exception as e: + response = format_error_message(str(e)) + + sys.stdout.write(response) + + def fetch_command(self, sub_command: str) -> BaseCommand: + commands = get_commands() + + if sub_command not in commands: + raise OperationalException("Command was not found") + + app_name = commands[sub_command] + + collector = ClassResolver( + '{}.commands'.format(app_name), + class_type=BaseCommand, + module_name=sub_command + ) + + command_instance = collector.instances[0] + return command_instance + + +def format_success_message(message: str) -> str: + """ + Utility function to format a success message + """ + + if message is None: + return Fore.GREEN + "Process finished" + '\n' + + return Fore.GREEN + message + '\n' + + +def format_error_message(message: str) -> str: + """ + Utility function to format an error message + """ + + if message is None: + return Fore.RED + 'Error occurred' + '\n' + + return Fore.RED + message + '\n' + + +def execute_from_command_line(argv=None): + + """Run the ManagementUtility.""" + utility = ManagementUtility(argv) + utility.execute() \ No newline at end of file diff --git a/investing_algorithm_framework/core/management/commands/__init__.py b/investing_algorithm_framework/management/commands/__init__.py similarity index 100% rename from investing_algorithm_framework/core/management/commands/__init__.py rename to investing_algorithm_framework/management/commands/__init__.py diff --git a/investing_algorithm_framework/core/management/commands/create_algorithm.py b/investing_algorithm_framework/management/commands/create_standard_algo.py similarity index 92% rename from investing_algorithm_framework/core/management/commands/create_algorithm.py rename to investing_algorithm_framework/management/commands/create_standard_algo.py index ef6d4277..be3e5582 100644 --- a/investing_algorithm_framework/core/management/commands/create_algorithm.py +++ b/investing_algorithm_framework/management/commands/create_standard_algo.py @@ -3,11 +3,11 @@ from importlib import import_module from investing_algorithm_framework.core.exceptions import ImproperlyConfigured -from investing_algorithm_framework.core.management.command import BaseCommand, CommandError -from investing_algorithm_framework.core.configuration.setup.default_template_creators import DefaultProjectCreator +from investing_algorithm_framework.management.command import BaseCommand, CommandError +from investing_algorithm_framework.configuration.setup.default_template_creators import DefaultProjectCreator -class CreateAlgorithmCommand(BaseCommand): +class CreateStandardAlgorithmCommand(BaseCommand): help = ( "Creates a project directory structure for the given investing_algorithm_framework instance in the current " "directory or optionally in the given directory." diff --git a/investing_algorithm_framework/core/management/commands/help.py b/investing_algorithm_framework/management/commands/help.py similarity index 83% rename from investing_algorithm_framework/core/management/commands/help.py rename to investing_algorithm_framework/management/commands/help.py index 90be70ea..6f13b596 100644 --- a/investing_algorithm_framework/core/management/commands/help.py +++ b/investing_algorithm_framework/management/commands/help.py @@ -1,9 +1,9 @@ from typing import Any from collections import defaultdict -from investing_algorithm_framework.core.management import BaseCommand -from investing_algorithm_framework.core.management.utils import get_commands -from investing_algorithm_framework.core.configuration.config_constants import FRAMEWORK_CORE_MODULE_NAME, FRAMEWORK_NAME +from investing_algorithm_framework.management import BaseCommand +from investing_algorithm_framework.management.utils import get_commands +from investing_algorithm_framework.configuration.config_constants import FRAMEWORK_CORE_MODULE_NAME, FRAMEWORK_NAME class HelpCommand(BaseCommand): diff --git a/investing_algorithm_framework/core/management/commands/run_algorithm.py b/investing_algorithm_framework/management/commands/run_algorithm.py similarity index 76% rename from investing_algorithm_framework/core/management/commands/run_algorithm.py rename to investing_algorithm_framework/management/commands/run_algorithm.py index 984bd01e..2591bb1f 100644 --- a/investing_algorithm_framework/core/management/commands/run_algorithm.py +++ b/investing_algorithm_framework/management/commands/run_algorithm.py @@ -1,9 +1,9 @@ from typing import Any -from investing_algorithm_framework.core.management.command import BaseCommand, CommandError +from investing_algorithm_framework.management.command import BaseCommand from investing_algorithm_framework.core.context import Context -from investing_algorithm_framework.core.configuration.config_constants import SETTINGS_CONTEXT_CONFIGURATION -from investing_algorithm_framework.core.configuration import settings +from investing_algorithm_framework.configuration.config_constants import SETTINGS_CONTEXT_CONFIGURATION +from investing_algorithm_framework.configuration import settings class RunAlgorithmCommand(BaseCommand): diff --git a/investing_algorithm_framework/core/management/utils.py b/investing_algorithm_framework/management/utils.py similarity index 69% rename from investing_algorithm_framework/core/management/utils.py rename to investing_algorithm_framework/management/utils.py index 5aa3146e..9991b0b1 100644 --- a/investing_algorithm_framework/core/management/utils.py +++ b/investing_algorithm_framework/management/utils.py @@ -3,10 +3,9 @@ import functools from typing import Dict, List -from investing_algorithm_framework.core.resolvers import ClassCollector -from investing_algorithm_framework.core.management.command import BaseCommand -from investing_algorithm_framework.core.configuration import settings -from investing_algorithm_framework.core.configuration.config_constants import SETTINGS_MODULE_PATH_ENV_NAME +from investing_algorithm_framework.core.resolvers import ClassResolver +from investing_algorithm_framework.management.command import BaseCommand +from investing_algorithm_framework.configuration import settings # Load the settings settings = settings @@ -30,7 +29,7 @@ def get_commands() -> Dict[str, str]: """ # Base commands - commands = {name: 'investing_algorithm_framework.core' for name in find_commands(os.path.join(os.path.dirname(__file__)))} + commands = {name: 'investing_algorithm_framework.management' for name in find_commands(os.path.join(os.path.dirname(__file__)))} if not settings.configured: return commands @@ -45,7 +44,7 @@ def load_command(app_dir: str, sub_command: str) -> BaseCommand: class instance. """ - collector = ClassCollector( + collector = ClassResolver( '{}.management.commands'.format(app_dir), class_type=BaseCommand, module_name=sub_command diff --git a/investing_algorithm_framework/core/states/templates/__init__.py b/investing_algorithm_framework/templates/__init__.py similarity index 100% rename from investing_algorithm_framework/core/states/templates/__init__.py rename to investing_algorithm_framework/templates/__init__.py diff --git a/investing_algorithm_framework/templates/data_providers/__init__.py b/investing_algorithm_framework/templates/data_providers/__init__.py new file mode 100644 index 00000000..bf357f48 --- /dev/null +++ b/investing_algorithm_framework/templates/data_providers/__init__.py @@ -0,0 +1 @@ +from investing_algorithm_framework.templates.data_providers.data_provider import DataProvider diff --git a/investing_algorithm_framework/core/data_providers/data_provider.py b/investing_algorithm_framework/templates/data_providers/data_provider.py similarity index 100% rename from investing_algorithm_framework/core/data_providers/data_provider.py rename to investing_algorithm_framework/templates/data_providers/data_provider.py diff --git a/investing_algorithm_framework/templates/order_executors/__init__.py b/investing_algorithm_framework/templates/order_executors/__init__.py new file mode 100644 index 00000000..f67e6d7e --- /dev/null +++ b/investing_algorithm_framework/templates/order_executors/__init__.py @@ -0,0 +1 @@ +from investing_algorithm_framework.templates.order_executors.order_executor import OrderExecutor diff --git a/investing_algorithm_framework/core/order_executors/order_executor.py b/investing_algorithm_framework/templates/order_executors/order_executor.py similarity index 100% rename from investing_algorithm_framework/core/order_executors/order_executor.py rename to investing_algorithm_framework/templates/order_executors/order_executor.py diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/configuration/__init__.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/configuration/__init__.py-template similarity index 100% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/configuration/__init__.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/configuration/__init__.py-template diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/configuration/context.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/configuration/context.py-template similarity index 63% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/configuration/context.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/configuration/context.py-template index 330630d8..7d613df5 100644 --- a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/configuration/context.py-template +++ b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/configuration/context.py-template @@ -1,8 +1,8 @@ from investing_algorithm_framework.core.context import Context -from investing_algorithm_framework.core.states.templates.setup_state import SetupState -from investing_algorithm_framework.core.states.templates.data_providing_state import DataProvidingState -from investing_algorithm_framework.core.states.templates.strategy_state import StrategyState -from investing_algorithm_framework.core.states.templates.ordering_state import OrderingState +from investing_algorithm_framework.templates.states.setup_state import SetupState +from investing_algorithm_framework.templates.states.data_providing_state import DataProvidingState +from investing_algorithm_framework.templates.states.strategy_state import StrategyState +from investing_algorithm_framework.templates.states.ordering_state import OrderingState # Import custom components from {{ project_name }}.data_providers.data_providers import MyDataProvider diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/configuration/settings.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/configuration/settings.py-template similarity index 100% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/configuration/settings.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/configuration/settings.py-template diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/data_providers/__init__.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/data_providers/__init__.py-template similarity index 100% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/data_providers/__init__.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/data_providers/__init__.py-template diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/data_providers/data_providers.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/data_providers/data_providers.py-template similarity index 77% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/data_providers/data_providers.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/data_providers/data_providers.py-template index ac4816d9..1f5cc051 100755 --- a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/data_providers/data_providers.py-template +++ b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/data_providers/data_providers.py-template @@ -1,4 +1,4 @@ -from investing_algorithm_framework.core.data_providers import DataProvider +from investing_algorithm_framework.templates.data_providers import DataProvider from investing_algorithm_framework.core.utils import TimeUnit """ diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/order_executors/__init__.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/order_executors/__init__.py-template similarity index 100% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/order_executors/__init__.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/order_executors/__init__.py-template diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/order_executors/order_executors.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/order_executors/order_executors.py-template similarity index 52% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/order_executors/order_executors.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/order_executors/order_executors.py-template index 65037855..2a04fba2 100644 --- a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/order_executors/order_executors.py-template +++ b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/order_executors/order_executors.py-template @@ -1,4 +1,4 @@ -from investing_algorithm_framework.core.order_executors import OrderExecutor +from investing_algorithm_framework.templates.order_executors import OrderExecutor class MyOrderExecutor(OrderExecutor): diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/strategies/__init__.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/strategies/__init__.py-template similarity index 100% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/strategies/__init__.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/strategies/__init__.py-template diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/strategies/strategies.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/strategies/strategies.py-template similarity index 78% rename from investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/strategies/strategies.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/strategies/strategies.py-template index ee42313c..557c61c1 100644 --- a/investing_algorithm_framework/templates/algorithm_project_directory/algorithm_project_template/strategies/strategies.py-template +++ b/investing_algorithm_framework/templates/projects/algorithm_project_directory/algorithm_project_template/strategies/strategies.py-template @@ -1,4 +1,4 @@ -from investing_algorithm_framework.core.strategies import Strategy +from investing_algorithm_framework.templates.strategies import Strategy from investing_algorithm_framework.core.utils import TimeUnit """ diff --git a/investing_algorithm_framework/templates/algorithm_project_directory/manage.py-template b/investing_algorithm_framework/templates/projects/algorithm_project_directory/manage.py-template similarity index 87% rename from investing_algorithm_framework/templates/algorithm_project_directory/manage.py-template rename to investing_algorithm_framework/templates/projects/algorithm_project_directory/manage.py-template index 6de60b3d..697caa3c 100755 --- a/investing_algorithm_framework/templates/algorithm_project_directory/manage.py-template +++ b/investing_algorithm_framework/templates/projects/algorithm_project_directory/manage.py-template @@ -9,7 +9,7 @@ def main(): os.environ.setdefault('INVESTING_ALGORITHM_FRAMEWORK_SETTINGS_MODULE', '{{ project_name }}.configuration.settings') try: - from investing_algorithm_framework.core.management import execute_from_command_line + from investing_algorithm_framework.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import investing_bot_framework. Are you sure it's installed and available on your PYTHONPATH" diff --git a/tests/core/data_providers/__init__.py b/investing_algorithm_framework/templates/states/__init__.py similarity index 100% rename from tests/core/data_providers/__init__.py rename to investing_algorithm_framework/templates/states/__init__.py diff --git a/investing_algorithm_framework/core/states/templates/data_providing_state.py b/investing_algorithm_framework/templates/states/data_providing_state.py similarity index 91% rename from investing_algorithm_framework/core/states/templates/data_providing_state.py rename to investing_algorithm_framework/templates/states/data_providing_state.py index 74ca3616..33717f8b 100644 --- a/investing_algorithm_framework/core/states/templates/data_providing_state.py +++ b/investing_algorithm_framework/templates/states/data_providing_state.py @@ -6,12 +6,12 @@ from investing_algorithm_framework.core.events import Observer from investing_algorithm_framework.core.context.context import Context from investing_algorithm_framework.core.exceptions import OperationalException -from investing_algorithm_framework.core.states import BotState +from investing_algorithm_framework.core.state import State from investing_algorithm_framework.core.executors import ExecutionScheduler from investing_algorithm_framework.core.workers import Worker -from investing_algorithm_framework.core.data_providers import DataProvider +from investing_algorithm_framework.templates.data_providers import DataProvider from investing_algorithm_framework.core.executors import Executor -from investing_algorithm_framework.core.configuration.config_constants import DEFAULT_MAX_WORKERS, SETTINGS_MAX_WORKERS +from investing_algorithm_framework.configuration.config_constants import DEFAULT_MAX_WORKERS, SETTINGS_MAX_WORKERS logger = logging.getLogger(__name__) @@ -43,7 +43,7 @@ def configured(self): class DataProviderScheduler(ExecutionScheduler): """ - Data Provider scheduler that will function as a scheduler to make sure it keeps it state across multiple states, + Data Provider scheduler that will function as a scheduler to make sure it keeps it state across multiple state, it is defined as a Singleton. """ @@ -68,7 +68,7 @@ def configured(self) -> bool: return self._configured -class DataProvidingState(BotState, Observer): +class DataProvidingState(State, Observer): """ Represent the data_providers state of a bot. This state will load all the defined data_providers providers and will run them. @@ -78,7 +78,7 @@ class DataProvidingState(BotState, Observer): registered_data_providers: List = None - from investing_algorithm_framework.core.states.templates.strategy_state import StrategyState + from investing_algorithm_framework.templates.states.strategy_state import StrategyState transition_state_class = StrategyState data_provider_scheduler: DataProviderScheduler = None diff --git a/investing_algorithm_framework/core/states/templates/ordering_state.py b/investing_algorithm_framework/templates/states/ordering_state.py similarity index 73% rename from investing_algorithm_framework/core/states/templates/ordering_state.py rename to investing_algorithm_framework/templates/states/ordering_state.py index a69dc796..757a69ed 100644 --- a/investing_algorithm_framework/core/states/templates/ordering_state.py +++ b/investing_algorithm_framework/templates/states/ordering_state.py @@ -1,13 +1,13 @@ from typing import List -from investing_algorithm_framework.core.states import BotState +from investing_algorithm_framework.core.state import State from investing_algorithm_framework.core.exceptions import OperationalException -from investing_algorithm_framework.core.order_executors import OrderExecutor +from investing_algorithm_framework.templates.order_executors import OrderExecutor -class OrderingState(BotState): +class OrderingState(State): - from investing_algorithm_framework.core.states.templates.data_providing_state import DataProvidingState + from investing_algorithm_framework.templates.states.data_providing_state import DataProvidingState transition_state_class = DataProvidingState order_executors: List[OrderExecutor] = None diff --git a/investing_algorithm_framework/core/states/templates/setup_state.py b/investing_algorithm_framework/templates/states/setup_state.py similarity index 61% rename from investing_algorithm_framework/core/states/templates/setup_state.py rename to investing_algorithm_framework/templates/states/setup_state.py index 176bb3c4..fea8d1c6 100644 --- a/investing_algorithm_framework/core/states/templates/setup_state.py +++ b/investing_algorithm_framework/templates/states/setup_state.py @@ -1,14 +1,14 @@ import logging from investing_algorithm_framework.core.exceptions import ImproperlyConfigured -from investing_algorithm_framework.core.states import BotState +from investing_algorithm_framework.core.state import State logger = logging.getLogger(__name__) -class SetupState(BotState): +class SetupState(State): - from investing_algorithm_framework.core.states.templates.data_providing_state import DataProvidingState + from investing_algorithm_framework.templates.states.data_providing_state import DataProvidingState transition_state_class = DataProvidingState def __init__(self, context): @@ -26,9 +26,9 @@ def run(self) -> None: # Load the settings if not self.context.settings.configured: raise ImproperlyConfigured( - "Settings module is not specified, make sure you have setup a investing_algorithm_framework project and " - "the investing_algorithm_framework is valid or that you have specified the settings module in your " - "manage.py file" + "Settings module is not specified, make sure you have setup a investing_algorithm_framework project " + "and the investing_algorithm_framework is valid or that you have specified the settings module in your " + "manage.py-template file" ) diff --git a/investing_algorithm_framework/core/states/templates/strategy_state.py b/investing_algorithm_framework/templates/states/strategy_state.py similarity index 92% rename from investing_algorithm_framework/core/states/templates/strategy_state.py rename to investing_algorithm_framework/templates/states/strategy_state.py index 616594f1..8fe5c753 100644 --- a/investing_algorithm_framework/core/states/templates/strategy_state.py +++ b/investing_algorithm_framework/templates/states/strategy_state.py @@ -2,13 +2,13 @@ import time from typing import List -from investing_algorithm_framework.core.states import BotState +from investing_algorithm_framework.core.state import State from investing_algorithm_framework.core.executors import ExecutionScheduler from investing_algorithm_framework.core.exceptions import OperationalException from investing_algorithm_framework.core.workers import Worker from investing_algorithm_framework.core.executors import Executor from investing_algorithm_framework.core.events import Observer -from investing_algorithm_framework.core.configuration.config_constants import DEFAULT_MAX_WORKERS, SETTINGS_MAX_WORKERS +from investing_algorithm_framework.configuration.config_constants import DEFAULT_MAX_WORKERS, SETTINGS_MAX_WORKERS logger = logging.getLogger(__name__) @@ -64,7 +64,7 @@ def configured(self) -> bool: return self._configured -class StrategyState(BotState, Observer): +class StrategyState(State, Observer): registered_strategies: List = None strategy_scheduler: StrategyScheduler = None @@ -133,6 +133,6 @@ def register_strategies(strategies: List) -> None: StrategyState.registered_strategies = strategies def get_transition_state_class(self): - from investing_algorithm_framework.core.states.templates.ordering_state import OrderingState + from investing_algorithm_framework.templates.states.ordering_state import OrderingState return OrderingState diff --git a/investing_algorithm_framework/templates/strategies/__init__.py b/investing_algorithm_framework/templates/strategies/__init__.py new file mode 100644 index 00000000..5e8f00c2 --- /dev/null +++ b/investing_algorithm_framework/templates/strategies/__init__.py @@ -0,0 +1 @@ +from investing_algorithm_framework.templates.strategies.strategy import Strategy diff --git a/investing_algorithm_framework/core/strategies/strategy.py b/investing_algorithm_framework/templates/strategies/strategy.py similarity index 100% rename from investing_algorithm_framework/core/strategies/strategy.py rename to investing_algorithm_framework/templates/strategies/strategy.py diff --git a/tests/core/resolvers/test_database_resolver.py b/tests/core/resolvers/test_database_resolver.py index 35af0541..880a2c64 100644 --- a/tests/core/resolvers/test_database_resolver.py +++ b/tests/core/resolvers/test_database_resolver.py @@ -3,7 +3,7 @@ from tests.resources import BaseTestMixin from tests.resources import utils -from investing_algorithm_framework.core.configuration import settings +from investing_algorithm_framework.configuration import settings from investing_algorithm_framework.core.extensions import db diff --git a/tests/core/test_context.py b/tests/core/test_context.py new file mode 100644 index 00000000..070422f9 --- /dev/null +++ b/tests/core/test_context.py @@ -0,0 +1,23 @@ +from investing_algorithm_framework.core.context import Context +from investing_algorithm_framework.core.state import State + + +class TestState(State): + + def run(self) -> None: + pass + + +def test_singleton_instance() -> None: + context_one = Context() + context_two = Context() + assert context_one is context_two + + +def test_initial_state() -> None: + context = Context() + context.register_initial_state(TestState) + assert type(context._state) is TestState + + + diff --git a/tests/core/management/__init__.py b/tests/management/__init__.py similarity index 100% rename from tests/core/management/__init__.py rename to tests/management/__init__.py diff --git a/tests/core/management/test_create_algorithm.py b/tests/management/test_create_standard_algo.py similarity index 92% rename from tests/core/management/test_create_algorithm.py rename to tests/management/test_create_standard_algo.py index 8e1046d0..f78889b8 100644 --- a/tests/core/management/test_create_algorithm.py +++ b/tests/management/test_create_standard_algo.py @@ -1,7 +1,7 @@ import os from tempfile import TemporaryDirectory from tests.resources.utils import random_string -from investing_algorithm_framework.core.management.commands.create_algorithm import CreateAlgorithmCommand +from investing_algorithm_framework.management.commands.create_standard_algo import CreateStandardAlgorithmCommand class TestCreateAlgorithm: @@ -15,7 +15,7 @@ def test(self): with TemporaryDirectory() as tempdir: - command = CreateAlgorithmCommand() + command = CreateStandardAlgorithmCommand() command.handle(name=self.project_name, directory=tempdir) # Check if manage py is present diff --git a/tests/templates/__init__.py b/tests/templates/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/templates/data_providers/__init__.py b/tests/templates/data_providers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/core/data_providers/resources.py b/tests/templates/data_providers/resources.py similarity index 88% rename from tests/core/data_providers/resources.py rename to tests/templates/data_providers/resources.py index 045a532a..9088d614 100644 --- a/tests/core/data_providers/resources.py +++ b/tests/templates/data_providers/resources.py @@ -1,7 +1,7 @@ from typing import Dict, Any from investing_algorithm_framework.core.events import Observer -from investing_algorithm_framework.core.data_providers import DataProvider +from investing_algorithm_framework.templates.data_providers import DataProvider class TestDataProviderOne(DataProvider): diff --git a/tests/core/data_providers/data_provider.py b/tests/templates/data_providers/test_data_providers.py similarity index 88% rename from tests/core/data_providers/data_provider.py rename to tests/templates/data_providers/test_data_providers.py index 8e7aea76..902c773c 100644 --- a/tests/core/data_providers/data_provider.py +++ b/tests/templates/data_providers/test_data_providers.py @@ -1,4 +1,4 @@ -from tests.core.data_providers.resources import TestDataProviderOne, TestDataProviderTwo, TestObserver +from tests.templates.data_providers.resources import TestDataProviderOne, TestDataProviderTwo, TestObserver def test():