Skip to content

Commit

Permalink
Merge pull request #249 from rjra2611/feature-implement-addon-modules…
Browse files Browse the repository at this point in the history
…-support

Implement addon modules support
  • Loading branch information
Martin-Molinero authored Dec 6, 2022
2 parents 64fa1f6 + 5d8fcb7 commit d594954
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Options:
--python-venv TEXT The path of the python virtual environment to be used
--update Pull the LEAN engine image before running the backtest
--backtest-name TEXT Backtest name
--vscode Setup vscode extension related configurations.
--lean-config FILE The Lean configuration file that should be used (defaults to the nearest lean.json)
--verbose Enable debug logging
--help Show this message and exit.
Expand Down
24 changes: 21 additions & 3 deletions lean/commands/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


from pathlib import Path
from typing import Optional
from typing import List, Optional
from click import command, option, argument, Choice

from lean.click import LeanCommand, PathParameter
Expand All @@ -23,7 +23,8 @@
from lean.models.utils import DebuggingMethod
from lean.models.logger import Option
from lean.models.data_providers import QuantConnectDataProvider, all_data_providers

from lean.models.addon_modules import all_addon_modules
from lean.models.addon_modules.addon_module import AddonModule

# The _migrate_* methods automatically update launch configurations for a given debugging method.
#
Expand Down Expand Up @@ -280,6 +281,10 @@ def _select_organization() -> QCMinimalOrganization:
@option("--backtest-name",
type=str,
help="Backtest name")
@option("--vscode",
is_flag=True,
default=False,
help="Setup vscode extension related configurations.")
def backtest(project: Path,
output: Optional[Path],
detach: bool,
Expand All @@ -291,7 +296,8 @@ def backtest(project: Path,
image: Optional[str],
python_venv: Optional[str],
update: bool,
backtest_name: str) -> None:
backtest_name: str,
vscode: bool,) -> None:
"""Backtest a project locally using Docker.
\b
Expand All @@ -307,6 +313,7 @@ def backtest(project: Path,
Alternatively you can set the default engine image for all commands using `lean config set engine-image <image>`.
"""
from datetime import datetime
addon_modules: List[AddonModule] = []
logger = container.logger
project_manager = container.project_manager
algorithm_file = project_manager.find_algorithm_file(Path(project))
Expand Down Expand Up @@ -369,6 +376,17 @@ def backtest(project: Path,
if python_venv is not None and python_venv != "":
lean_config["python-venv"] = f'{"/" if python_venv[0] != "/" else ""}{python_venv}'

# prepare for vscode settings
if vscode:
cloud_messaging_handler = next((module for module in all_addon_modules if module.get_name() == "Cloud Messaging Handler"), None)
if cloud_messaging_handler:
addon_modules.append(cloud_messaging_handler)

# build and configure addon modules
for module in addon_modules:
module.build(lean_config, logger).configure(lean_config, "backtesting")
module.ensure_module_installed(container.organization_manager.try_get_working_organization_id())

lean_runner = container.lean_runner
lean_runner.run_lean(lean_config,
"backtesting",
Expand Down
22 changes: 22 additions & 0 deletions lean/models/addon_modules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List
from lean.models.addon_modules.addon_module import AddonModule
from lean.models import json_modules

all_addon_modules: List[AddonModule] = []

for json_module in json_modules:
if "addon-module" in json_module["type"]:
all_addon_modules.append(AddonModule(json_module))
20 changes: 20 additions & 0 deletions lean/models/addon_modules/addon_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from lean.models.lean_config_configurer import LeanConfigConfigurer

class AddonModule(LeanConfigConfigurer):
"""A JsonModule implementation for add on modules."""



2 changes: 0 additions & 2 deletions lean/models/json_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
from enum import Enum
from typing import Any, Dict, List, Type
from lean.components.util.logger import Logger
from lean.container import container
from lean.models.logger import Option
from lean.models.configuration import BrokerageEnvConfiguration, Configuration, InternalInputUserInput
from copy import copy
from abc import ABC
Expand Down

0 comments on commit d594954

Please sign in to comment.