Skip to content

Commit

Permalink
update essential properties when loading from envrionment
Browse files Browse the repository at this point in the history
  • Loading branch information
rjra2611 committed Jan 19, 2024
1 parent eb76a0c commit 1e473c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lean/commands/live/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from lean.components.util.live_utils import get_last_portfolio_cash_holdings, configure_initial_cash_balance, configure_initial_holdings,\
_configure_initial_cash_interactively, _configure_initial_holdings_interactively
from lean.models.data_providers import all_data_providers
from lean.components.util.json_modules_handler import build_and_configure_modules, get_and_build_module
from lean.components.util.json_modules_handler import build_and_configure_modules, get_and_build_module, update_essential_properties_available

_environment_skeleton = {
"live-mode": True,
Expand All @@ -54,11 +54,19 @@ def _get_configurable_modules_from_environment(lean_config: Dict[str, Any], envi

brokerage = environment["live-mode-brokerage"]
data_queue_handlers = environment["data-queue-handler"]
[brokerage_configurer] = [local_brokerage for local_brokerage in all_local_brokerages if local_brokerage.get_live_name(environment_name) == brokerage]
[brokerage_configurer] = [local_brokerage for local_brokerage in all_local_brokerages if _getBrokerageBaseName(local_brokerage.get_live_name(environment_name)) == _getBrokerageBaseName(brokerage)]
data_feed_configurers = [local_data_feed for local_data_feed in all_local_data_feeds if local_data_feed.get_live_name(environment_name) in data_queue_handlers]
return brokerage_configurer, data_feed_configurers


def _getBrokerageBaseName(brokerage: str) -> str:
"""Returns the base name of the brokerage.
:param brokerage: the name of the brokerage
:return: the base name of the brokerage
"""
return brokerage.split('.')[-1]

def _install_modules(modules: List[LeanConfigConfigurer], user_kwargs: Dict[str, Any]) -> None:
"""Raises an error if any of the given modules are not installed.
Expand Down Expand Up @@ -326,6 +334,8 @@ def deploy(project: Path,
if environment is not None:
environment_name = environment
lean_config = lean_config_manager.get_complete_lean_config(environment_name, algorithm_file, None)
[update_essential_properties_available(all_local_brokerages, kwargs)]
[update_essential_properties_available(all_local_data_feeds, kwargs)]
elif brokerage is not None or len(data_feed) > 0:
ensure_options(["brokerage", "data_feed"])

Expand Down
9 changes: 9 additions & 0 deletions lean/components/util/json_modules_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ def get_and_build_module(target_module_name: str, module_list: List[JsonModule],
target_module.update_configs(required_properties_value)
logger.debug(f"json_module_handler.get_and_build_module(): non-interactive: required_properties_value with module {target_module_name}: {required_properties_value}")
return target_module


def update_essential_properties_available(module_list: List[JsonModule], properties: Dict[str, Any]) -> JsonModule:
for target_module in module_list:
# update essential properties from brokerage to datafeed
# needs to be updated before fetching required properties
essential_properties = [target_module.convert_lean_key_to_variable(prop) for prop in target_module.get_essential_properties()]
essential_properties_value = {target_module.convert_variable_to_lean_key(prop) : properties[prop] for prop in essential_properties}
target_module.update_configs(essential_properties_value)
4 changes: 2 additions & 2 deletions lean/models/json_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def get_configurations_env_values_from_name(self, target_env: str) -> List[Dict[
config._is_type_configurations_env and self.check_if_config_passes_filters(
config)
] or [None]
if env_config is not None and target_env in env_config._env_and_values.keys():
env_config_values = env_config._env_and_values[target_env]
if env_config is not None:
env_config_values = list(env_config._env_and_values.values())[0]
return env_config_values

def get_config_from_type(self, config_type: Configuration) -> str:
Expand Down

0 comments on commit 1e473c6

Please sign in to comment.