Skip to content

Commit ed8c1ee

Browse files
author
investing-algorithms
authored
Merge pull request #28 from investing-algorithms/hotfix_templates
Hotfix templates
2 parents e5f1e36 + 8440562 commit ed8c1ee

File tree

32 files changed

+171
-211
lines changed

32 files changed

+171
-211
lines changed

investing_algorithm_framework/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Invokes investing_algorithm_framework-admin when the investing_algorithm_framework framework module is run as a script.
3-
Example: python -m investing_algorithm_framework createbot SampleBot
3+
Example: python -m investing_algorithm_framework createalgorithm SampleAlgorithm
44
"""
55

66
from investing_algorithm_framework.core.management import execute_from_command_line

investing_algorithm_framework/core/configuration/config_constants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
FRAMEWORK_CORE_MODULE_NAME = 'investing_algorithm_framework.core'
44

55
# Setting related constants
6-
SETTINGS_BOT_PROJECT_NAME = 'BOT_PROJECT_NAME'
7-
SETTINGS_MODULE_PATH_ENV_NAME = 'INVESTING_BOT_FRAMEWORK_SETTINGS_MODULE'
6+
SETTINGS_ALGORITHM_PROJECT_NAME = 'ALGORITHM_PROJECT_NAME'
7+
SETTINGS_MODULE_PATH_ENV_NAME = 'INVESTING_ALGORITHM_FRAMEWORK_SETTINGS_MODULE'
88
SETTINGS_DATA_PROVIDER_REGISTERED_APPS = 'INSTALLED_DATA_PROVIDER_APPS'
99
SETTINGS_STRATEGY_REGISTERED_APPS = 'INSTALLED_STRATEGY_APPS'
1010
SETTINGS_MAX_WORKERS = 'DEFAULT_MAX_WORKERS'
11-
SETTINGS_BOT_CONTEXT_CONFIGURATION = 'BOT_CONTEXT_CONFIGURATION'
11+
SETTINGS_CONTEXT_CONFIGURATION = 'CONTEXT_CONFIGURATION'
1212
SETTINGS_LOGGING_CONFIG = 'LOGGING'
1313

1414
# Operational constants
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from investing_algorithm_framework.core.configuration.setup.default_template_creators import DefaultBotProjectCreator
1+
from investing_algorithm_framework.core.configuration.setup.default_template_creators import DefaultProjectCreator

investing_algorithm_framework/core/configuration/setup/default_template_creators.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
from investing_algorithm_framework.core.configuration.setup.template_creator import TemplateCreator
77

88

9-
class DefaultBotProjectCreator(TemplateCreator):
10-
TEMPLATE_ROOT_DIR = 'templates/bot_project_directory'
11-
BOT_PROJECT_NAME_PLACEHOLDER = '{{ bot_project_name }}'
12-
BOT_PROJECT_TEMPLATE_DIR_NAME = 'bot_project_template'
9+
class DefaultProjectCreator(TemplateCreator):
10+
TEMPLATE_ROOT_DIR = 'templates/algorithm_project_directory'
11+
PROJECT_NAME_PLACEHOLDER = '{{ project_name }}'
12+
PROJECT_TEMPLATE_DIR_NAME = 'algorithm_project_template'
1313

1414
def configure(self) -> None:
1515
bot_dir = os.path.join(self._bot_project_directory, self._bot_name)
1616

1717
if os.path.exists(bot_dir):
18-
raise ImproperlyConfigured("Bot destination directory {} already exists".format(self._bot_name))
18+
raise ImproperlyConfigured("Project destination directory {} already exists".format(self._bot_name))
1919

2020
def create(self) -> None:
2121

@@ -29,7 +29,7 @@ def create(self) -> None:
2929
path_rest = root[len(template_dir) + 1:]
3030

3131
# Replace template investing_algorithm_framework directory with given investing_algorithm_framework name
32-
path_rest = path_rest.replace(self.BOT_PROJECT_TEMPLATE_DIR_NAME, self._bot_name)
32+
path_rest = path_rest.replace(self.PROJECT_TEMPLATE_DIR_NAME, self._bot_name)
3333

3434
# Create the directories if they don't exist
3535
destination_dir = os.path.join(self._bot_project_directory, path_rest)
@@ -76,15 +76,15 @@ def create(self) -> None:
7676
)
7777

7878
# Format placeholders in file if needed
79-
if filename in ['manage.py-template', 'settings.py-template']:
79+
if filename in ['manage.py-template', 'settings.py-template', 'context.py-template']:
8080

8181
# Read the file
8282
with open(destination_path, 'r') as file:
8383

8484
file_data = file.read()
8585

8686
# Replace the placeholder with the investing_algorithm_framework name
87-
file_data = file_data.replace(self.BOT_PROJECT_NAME_PLACEHOLDER, self._bot_name)
87+
file_data = file_data.replace(self.PROJECT_NAME_PLACEHOLDER, self._bot_name)
8888

8989
# Write the file out again
9090
with open(destination_path, 'w') as file:

investing_algorithm_framework/core/configuration/template.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class Template:
1010

1111
def __init__(self, bot_project_directory: str, bot_name: str) -> None:
1212
"""
13-
investing_algorithm_framework project directory is the root directory of the given investing_algorithm_framework. The bot_name will be the same as the root project
14-
directory. For simplicity it is explicitly passed as a parameter
13+
investing_algorithm_framework project directory is the root directory of the given
14+
investing_algorithm_framework. The algorithm_name will be the same as the root project directory. For
15+
simplicity it is explicitly passed as a parameter
1516
"""
1617

1718
if bot_project_directory is None:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from investing_algorithm_framework.core.context.bot_context import BotContext
1+
from investing_algorithm_framework.core.context.context import Context

investing_algorithm_framework/core/context/bot_context.py renamed to investing_algorithm_framework/core/context/context.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from investing_algorithm_framework.core.states import BotState
77

88

9-
class BotContext(metaclass=Singleton):
9+
class Context(metaclass=Singleton):
1010
"""
11-
The BotContext defines the current state of the running bot. It also maintains a reference to an instance of a
12-
BotState subclass, which represents the current state of the BotContext.
11+
The Context defines the current state of the running algorithms. It also maintains a reference to an instance of a
12+
state subclass, which represents the current state of the context instance.
1313
"""
1414

15-
# A reference to the current state of the Bot Context.
15+
# A reference to the current state of the context.
1616
_state: BotState = None
1717

1818
# Settings reference
@@ -36,7 +36,7 @@ def _check_state(self, raise_exception: bool = False) -> bool:
3636

3737
if raise_exception:
3838
raise OperationalException(
39-
"Bot context doesn't have a state. Make sure that you set the state of bot either "
39+
"Context doesn't have a state. Make sure that you set the state either "
4040
"by initializing it or making sure that you transition to a new valid state."
4141
)
4242
else:

investing_algorithm_framework/core/data_providers/data_provider.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def __str__(self) -> str:
2323

2424
class DataProvider(ScheduledWorker):
2525
"""
26-
Class DataProvider: An entity which responsibility is to provide data_providers from an external data_providers
27-
source. Where a data_providers source is defined as any third party service that provides data_providers,
28-
e.g cloud storage, REST API, or website.
26+
Class DataProvider: An entity which responsibility is to provide data from an external source.
27+
Where a data_providers source is defined as any third party service that provides data e.g cloud storage,
28+
REST API, or website.
2929
30-
A data_providers provider must always be run with the start function from it´s super class. Otherwise depend
30+
A data_provider must always be run with the start function from it´s super class. Otherwise depended
3131
observers will not be updated.
3232
"""
3333

investing_algorithm_framework/core/data_providers/mixins/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

investing_algorithm_framework/core/data_providers/mixins/rest_api_mixin.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

investing_algorithm_framework/core/management/commands/createbot.py renamed to investing_algorithm_framework/core/management/commands/create_algorithm.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,43 @@
44

55
from investing_algorithm_framework.core.exceptions import ImproperlyConfigured
66
from investing_algorithm_framework.core.management.command import BaseCommand, CommandError
7-
from investing_algorithm_framework.core.configuration.setup import DefaultBotProjectCreator
7+
from investing_algorithm_framework.core.configuration.setup.default_template_creators import DefaultProjectCreator
88

99

10-
class CreateBotCommand(BaseCommand):
10+
class CreateAlgorithmCommand(BaseCommand):
1111
help = (
12-
"Creates a project directory structure for the given investing_algorithm_framework name in the current directory or optionally "
13-
"in the given directory."
12+
"Creates a project directory structure for the given investing_algorithm_framework instance in the current "
13+
"directory or optionally in the given directory."
1414
)
1515

16-
missing_args_message = "You must provide a investing_algorithm_framework name."
17-
success_message = "Bot created and initialized."
16+
missing_args_message = "You must provide a project name."
17+
success_message = "Algorithm created and initialized."
1818

1919
def add_arguments(self, parser):
20-
parser.add_argument('name', help='Name of the investing_algorithm_framework.')
20+
parser.add_argument('name', help='Name of the algorithm/project.')
2121
parser.add_argument('directory', nargs='?', help='Optional destination directory')
2222
parser.add_argument(
2323
'--template_creator',
2424
help='Optional template creator plugin, provided by third party libraries'
2525
)
2626

27-
def handle(self, **options) -> str:
27+
def handle(self, **options) -> None:
2828

2929
# Get all the default attributes
30-
bot_name = options.get('name', None)
30+
project_name = options.get('name', None)
3131
directory = options.get('directory', None)
3232
template_creator = options.get('template_creator', None)
3333

34-
self.validate_name(bot_name)
34+
self.validate_name(project_name)
3535

3636
# initialize the investing_algorithm_framework project directory
3737
if directory is None:
38-
directory = os.path.join(os.getcwd(), bot_name)
38+
directory = os.path.join(os.getcwd(), project_name)
3939

4040
if os.path.isdir(directory):
4141
raise ImproperlyConfigured(
42-
"Directory {} already exists. Please make sure that the investing_algorithm_framework project name does not correspond to "
43-
"an existing directory"
42+
"Directory {} already exists. Please make sure that the project "
43+
"name does not correspond to an existing directory".format(str(directory))
4444
)
4545

4646
os.mkdir(directory)
@@ -49,24 +49,26 @@ def handle(self, **options) -> str:
4949
directory = os.path.abspath(os.path.expanduser(directory))
5050

5151
if not os.path.exists(directory):
52-
raise CommandError("Destination directory {} does not exist, please create it first.".format(directory))
52+
raise CommandError(
53+
"Destination directory {} does not exist, please create it first.".format(str(directory))
54+
)
5355

5456
# Use default investing_algorithm_framework creator
5557
if not template_creator:
56-
bot_template_creator = DefaultBotProjectCreator(directory, bot_name)
58+
template_creator = DefaultProjectCreator(directory, project_name)
5759

5860
# Creates templates
59-
bot_template_creator.configure()
60-
bot_template_creator.create()
61+
template_creator.configure()
62+
template_creator.create()
6163

6264
@staticmethod
6365
def validate_name(name: str) -> None:
6466
"""
65-
Helper function to validate the name of a given investing_algorithm_framework
67+
Helper function to validate the name of a given project
6668
"""
6769

6870
if name is None:
69-
raise CommandError("you must provide a investing_algorithm_framework name")
71+
raise CommandError("you must provide a project name")
7072

7173
if not re.match("^[a-zA-Z]+\w*$", name):
7274
raise CommandError("{} is not allowed, value must begin with a letter and "
@@ -80,6 +82,6 @@ def validate_name(name: str) -> None:
8082
else:
8183
raise CommandError(
8284
"'{}' conflicts with the name of an existing Python "
83-
"module and cannot be used as a investing_algorithm_framework name. Please try "
85+
"module and cannot be used as a project name. Please try "
8486
"another name.".format(name)
8587
)

investing_algorithm_framework/core/management/commands/help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def add_arguments(self, parser) -> None:
1717
def handle(self) -> Any:
1818
usage = [
1919
"",
20-
"This is the command line management for the investing investing_algorithm_framework framework. \n"
20+
"This is the command line management for the investing algorithm framework. \n"
2121
"Type help <sub_command>' for help on a specific sub command.",
2222
"",
2323
"Available sub commands:",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Any
2+
3+
from investing_algorithm_framework.core.management.command import BaseCommand, CommandError
4+
from investing_algorithm_framework.core.context import Context
5+
from investing_algorithm_framework.core.configuration.config_constants import SETTINGS_CONTEXT_CONFIGURATION
6+
from investing_algorithm_framework.core.configuration import settings
7+
8+
9+
class RunAlgorithmCommand(BaseCommand):
10+
help_message = (
11+
"Runs a instance of an algorithm created with the investing_algorithm_framework, by default it will run "
12+
"until stopped, if cycles is specified it will run the according to the amount of cycles"
13+
)
14+
15+
success_message = (
16+
"Algorithm is finished running"
17+
)
18+
19+
def add_arguments(self, parser) -> None:
20+
pass
21+
22+
def handle(self, *args, **options) -> Any:
23+
24+
# configure settings
25+
settings.configure()
26+
27+
# Load the context configuration
28+
__import__(settings[SETTINGS_CONTEXT_CONFIGURATION])
29+
30+
# Create an investing_algorithm_framework context of the investing_algorithm_framework and run it
31+
context = Context()
32+
context.start()
33+
34+
35+
36+

investing_algorithm_framework/core/management/commands/runbot.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)