From e8c0b62f3028e4afbb2ca058695207f5e321deff Mon Sep 17 00:00:00 2001 From: Romazes Date: Fri, 3 May 2024 21:10:15 +0300 Subject: [PATCH 1/3] feat: back compatibility data_type == Open Interest --- lean/commands/data/download.py | 10 ++++++++-- lean/models/api.py | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lean/commands/data/download.py b/lean/commands/data/download.py index 007291da..4ab8ecee 100644 --- a/lean/commands/data/download.py +++ b/lean/commands/data/download.py @@ -195,7 +195,6 @@ def _select_products_interactive(organization: QCFullOrganization, datasets: Lis while True: category = logger.prompt_list("Select a category", category_options) - available_datasets = sorted((d for d in datasets if category in d.categories), key=lambda d: d.name) dataset: Dataset = logger.prompt_list("Select a dataset", [Option(id=d, label=d.name) for d in available_datasets]) @@ -215,7 +214,6 @@ def _select_products_interactive(organization: QCFullOrganization, datasets: Lis for dataset_option in dataset.options: if dataset_option.condition is None or dataset_option.condition.check(option_results): option_results[dataset_option.id] = dataset_option.configure_interactive() - products.append(Product(dataset=dataset, option_results=option_results)) logger.info("Selected data:") @@ -582,6 +580,14 @@ def download(ctx: Context, if data_provider_historical is None: data_provider_historical = _get_historical_data_provider() + # use "Open Interest" with spacing for ata_provider_historical == 'QuantConnect' (back compatibility) + user_input = ctx.params.get('data_type') + if user_input and dataset and user_input == QCDataType.OpenInterest: + ctx.params['data_type'] = QCDataType.Open_Interest + # use "OpenInterest" without spacing for data_provider_historical != 'QuantConnect' + elif user_input and data_type == QCDataType.Open_Interest: + data_type = QCDataType.OpenInterest + if data_provider_historical == 'QuantConnect': is_interactive = dataset is None if not is_interactive: diff --git a/lean/models/api.py b/lean/models/api.py index 6ec432df..70c25f6a 100644 --- a/lean/models/api.py +++ b/lean/models/api.py @@ -412,6 +412,7 @@ class QCDataType(str, Enum): Quote = "Quote" Bulk = "Bulk" OpenInterest = "OpenInterest" + Open_Interest = "Open Interest" @classmethod def get_all_members(cls): From ff712c8d74e5db36c6934f098dc413d13f7c23fd Mon Sep 17 00:00:00 2001 From: Romazes Date: Fri, 3 May 2024 23:12:55 +0300 Subject: [PATCH 2/3] feat: custom choice class for QCDataType --- lean/commands/data/download.py | 11 ++++++++++- lean/models/api.py | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lean/commands/data/download.py b/lean/commands/data/download.py index 4ab8ecee..9a9d63be 100644 --- a/lean/commands/data/download.py +++ b/lean/commands/data/download.py @@ -494,6 +494,14 @@ def _configure_date_option(date_value: str, option_id: str, option_label: str) - return date_option.configure_non_interactive(date_value) +class QCDataTypeCustomChoice(Choice): + def get_metavar(self, param) -> str: + choices_str = "|".join(QCDataType.get_all_members_except('Open Interest')) + + # Use square braces to indicate an option or optional argument. + return f"[{choices_str}]" + + @command(cls=LeanCommand, requires_lean_config=True, allow_unknown_options=True, name="download") @option("--data-provider-historical", type=Choice([data_downloader.get_name() for data_downloader in cli_data_downloaders], case_sensitive=False), @@ -504,7 +512,8 @@ def _configure_date_option(date_value: str, option_id: str, option_label: str) - @option("--force", is_flag=True, default=False, hidden=True) @option("--yes", "-y", "auto_confirm", is_flag=True, default=False, help="Automatically confirm payment confirmation prompts") -@option("--data-type", type=Choice(QCDataType.get_all_members(), case_sensitive=False), help="Specify the type of historical data") +@option("--data-type", type=QCDataTypeCustomChoice(QCDataType.get_all_members(), case_sensitive=False), + help="Specify the type of historical data") @option("--resolution", type=Choice(QCResolution.get_all_members(), case_sensitive=False), help="Specify the resolution of the historical data") @option("--security-type", type=Choice(QCSecurityType.get_all_members(), case_sensitive=False), diff --git a/lean/models/api.py b/lean/models/api.py index 70c25f6a..9edb4ddd 100644 --- a/lean/models/api.py +++ b/lean/models/api.py @@ -429,6 +429,10 @@ def get_all_members(cls): """ return list(cls.__members__.values()) + @classmethod + def get_all_members_except(cls, skip_value:str): + return [value for value in QCDataType.__members__.values() if value != skip_value] + class QCSecurityType(str, Enum): Equity = "Equity" Index = "Index" From 243fe868a3b468006ee177c603e9bcf5a2dd5caf Mon Sep 17 00:00:00 2001 From: Romazes Date: Fri, 3 May 2024 23:36:54 +0300 Subject: [PATCH 3/3] feat: use option callback to replace data type name --- lean/commands/data/download.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lean/commands/data/download.py b/lean/commands/data/download.py index 9a9d63be..a6c9e79b 100644 --- a/lean/commands/data/download.py +++ b/lean/commands/data/download.py @@ -502,6 +502,17 @@ def get_metavar(self, param) -> str: return f"[{choices_str}]" +def _replace_data_type(ctx, param, value): + dataset = ctx.params.get('dataset') + if dataset: + if value == QCDataType.OpenInterest: + return QCDataType.Open_Interest + return value + elif value == QCDataType.Open_Interest: + return QCDataType.OpenInterest + return value + + @command(cls=LeanCommand, requires_lean_config=True, allow_unknown_options=True, name="download") @option("--data-provider-historical", type=Choice([data_downloader.get_name() for data_downloader in cli_data_downloaders], case_sensitive=False), @@ -512,7 +523,8 @@ def get_metavar(self, param) -> str: @option("--force", is_flag=True, default=False, hidden=True) @option("--yes", "-y", "auto_confirm", is_flag=True, default=False, help="Automatically confirm payment confirmation prompts") -@option("--data-type", type=QCDataTypeCustomChoice(QCDataType.get_all_members(), case_sensitive=False), +@option("--data-type", callback=_replace_data_type, + type=QCDataTypeCustomChoice(QCDataType.get_all_members(), case_sensitive=False), help="Specify the type of historical data") @option("--resolution", type=Choice(QCResolution.get_all_members(), case_sensitive=False), help="Specify the resolution of the historical data") @@ -589,14 +601,6 @@ def download(ctx: Context, if data_provider_historical is None: data_provider_historical = _get_historical_data_provider() - # use "Open Interest" with spacing for ata_provider_historical == 'QuantConnect' (back compatibility) - user_input = ctx.params.get('data_type') - if user_input and dataset and user_input == QCDataType.OpenInterest: - ctx.params['data_type'] = QCDataType.Open_Interest - # use "OpenInterest" without spacing for data_provider_historical != 'QuantConnect' - elif user_input and data_type == QCDataType.Open_Interest: - data_type = QCDataType.OpenInterest - if data_provider_historical == 'QuantConnect': is_interactive = dataset is None if not is_interactive: