2121from  lean .models .errors  import  MoreInfoError 
2222from  lean .models .lean_config_configurer  import  LeanConfigConfigurer 
2323from  lean .models .logger  import  Option 
24- from  lean .models .configuration  import  InternalInputUserInput 
24+ from  lean .models .configuration  import  ConfigurationsEnvConfiguration ,  InternalInputUserInput 
2525from  lean .models .click_options  import  options_from_json , get_configs_for_options 
2626from  lean .models .json_module  import  LiveInitialStateInput 
2727from  lean .commands .live .live  import  live 
2828from  lean .components .util .live_utils  import  get_last_portfolio_cash_holdings , configure_initial_cash_balance , configure_initial_holdings ,\
2929                                            _configure_initial_cash_interactively , _configure_initial_holdings_interactively 
3030from  lean .models .data_providers  import  all_data_providers 
31- from  lean .components .util .json_modules_handler  import  build_and_configure_modules , get_and_build_module 
31+ from  lean .components .util .json_modules_handler  import  build_and_configure_modules , get_and_build_module ,  update_essential_properties_available 
3232
3333_environment_skeleton  =  {
3434    "live-mode" : True ,
@@ -54,11 +54,20 @@ def _get_configurable_modules_from_environment(lean_config: Dict[str, Any], envi
5454
5555    brokerage  =  environment ["live-mode-brokerage" ]
5656    data_queue_handlers  =  environment ["data-queue-handler" ]
57-     [brokerage_configurer ] =  [local_brokerage  for  local_brokerage  in  all_local_brokerages  if  local_brokerage .get_live_name (environment_name ) ==  brokerage ]
58-     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 ]
57+     [brokerage_configurer ] =  [local_brokerage  for  local_brokerage  in  all_local_brokerages  if  _get_brokerage_base_name (local_brokerage .get_live_name (environment_name )) ==  _get_brokerage_base_name (brokerage )]
58+     data_queue_handlers_base_names  =  [_get_brokerage_base_name (data_queue_handler ) for  data_queue_handler  in  data_queue_handlers ]
59+     data_feed_configurers  =  [local_data_feed  for  local_data_feed  in  all_local_data_feeds  if  _get_brokerage_base_name (local_data_feed .get_live_name (environment_name )) in  data_queue_handlers_base_names ]
5960    return  brokerage_configurer , data_feed_configurers 
6061
6162
63+ def  _get_brokerage_base_name (brokerage : str ) ->  str :
64+     """Returns the base name of the brokerage. 
65+ 
66+     :param brokerage: the name of the brokerage 
67+     :return: the base name of the brokerage 
68+     """ 
69+     return  brokerage .split ('.' )[- 1 ]
70+ 
6271def  _install_modules (modules : List [LeanConfigConfigurer ], user_kwargs : Dict [str , Any ]) ->  None :
6372    """Raises an error if any of the given modules are not installed. 
6473
@@ -326,6 +335,52 @@ def deploy(project: Path,
326335    if  environment  is  not None :
327336        environment_name  =  environment 
328337        lean_config  =  lean_config_manager .get_complete_lean_config (environment_name , algorithm_file , None )
338+         
339+         lean_environment  =  lean_config ["environments" ][environment_name ]
340+         for  key  in  ["live-mode-brokerage" , "data-queue-handler" ]:
341+             if  key  not  in lean_environment :
342+                 raise  MoreInfoError (f"The '{ environment_name } { key }  ,
343+                                     "https://www.lean.io/docs/v2/lean-cli/live-trading/algorithm-control" )
344+ 
345+         brokerage  =  lean_environment ["live-mode-brokerage" ]
346+         data_queue_handlers  =  lean_environment ["data-queue-handler" ]
347+         data_queue_handlers_base_names  =  [_get_brokerage_base_name (data_queue_handler ) for  data_queue_handler  in  data_queue_handlers ]
348+         data_feed_configurers  =  []
349+ 
350+         for  local_brokerage  in  all_local_brokerages :
351+             configuration_environments : List [ConfigurationsEnvConfiguration ] =  [config  for  config  in  local_brokerage ._lean_configs  if  config ._is_type_configurations_env ]
352+             for  configuration_environment  in  configuration_environments :
353+                 configuration_environment_values  =  list (configuration_environment ._env_and_values .values ())[0 ]
354+                 if  any (True  for  x  in  configuration_environment_values  if  x ["name" ] ==  "live-mode-brokerage"  and  _get_brokerage_base_name (x ["value" ]) ==  _get_brokerage_base_name (brokerage )):
355+                     brokerage_configurer  =  local_brokerage 
356+                     # fill essential properties 
357+                     for  condition  in  configuration_environment ._filter ._conditions :
358+                         if  condition ._type  !=  "exact-match" :
359+                             continue 
360+                         property_name_to_fill  =  local_brokerage .convert_lean_key_to_variable (condition ._dependent_config_id )
361+                         property_value_to_fill  =  condition ._pattern 
362+                         kwargs [property_name_to_fill ] =  property_value_to_fill 
363+                         lean_config [condition ._dependent_config_id ] =  property_value_to_fill 
364+                     break 
365+         
366+         for  local_data_feed  in  all_local_data_feeds :
367+             configuration_environments : List [ConfigurationsEnvConfiguration ] =  [config  for  config  in  local_data_feed ._lean_configs  if  config ._is_type_configurations_env ]
368+             for  configuration_environment  in  configuration_environments :
369+                 configuration_environment_values  =  list (configuration_environment ._env_and_values .values ())[0 ]
370+                 if  any (True  for  x  in  configuration_environment_values  if  x ["name" ] ==  "data-queue-handler"  and  _get_brokerage_base_name (x ["value" ]) in  data_queue_handlers_base_names ):
371+                     data_feed_configurers .append (local_data_feed )
372+                     # fill essential properties 
373+                     for  condition  in  configuration_environment ._filter ._conditions :
374+                         if  condition ._type  !=  "exact-match" :
375+                             continue 
376+                         property_name_to_fill  =  local_data_feed .convert_lean_key_to_variable (condition ._dependent_config_id )
377+                         property_value_to_fill  =  condition ._pattern 
378+                         kwargs [property_name_to_fill ] =  property_value_to_fill 
379+                         lean_config [condition ._dependent_config_id ] =  property_value_to_fill 
380+ 
381+         [update_essential_properties_available ([brokerage_configurer ], kwargs )]
382+         [update_essential_properties_available (data_feed_configurers , kwargs )]
383+ 
329384    elif  brokerage  is  not None  or  len (data_feed ) >  0 :
330385        ensure_options (["brokerage" , "data_feed" ])
331386
0 commit comments