Skip to content

Commit 55e773d

Browse files
Add generic command support (#501)
- Add local and cloud generic commandn support
1 parent 3b3d0cc commit 55e773d

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ A locally-focused workflow (local development, local execution) with the CLI may
7272
- [`lean build`](#lean-build)
7373
- [`lean cloud backtest`](#lean-cloud-backtest)
7474
- [`lean cloud live`](#lean-cloud-live)
75+
- [`lean cloud live command`](#lean-cloud-live-command)
7576
- [`lean cloud live deploy`](#lean-cloud-live-deploy)
7677
- [`lean cloud live liquidate`](#lean-cloud-live-liquidate)
7778
- [`lean cloud live stop`](#lean-cloud-live-stop)
@@ -101,6 +102,7 @@ A locally-focused workflow (local development, local execution) with the CLI may
101102
- [`lean live`](#lean-live)
102103
- [`lean live add-security`](#lean-live-add-security)
103104
- [`lean live cancel-order`](#lean-live-cancel-order)
105+
- [`lean live command`](#lean-live-command)
104106
- [`lean live deploy`](#lean-live-deploy)
105107
- [`lean live liquidate`](#lean-live-liquidate)
106108
- [`lean live stop`](#lean-live-stop)
@@ -303,11 +305,30 @@ Options:
303305
--help Show this message and exit.
304306
305307
Commands:
308+
command Send a command to a running cloud live trading project.
306309
deploy Start live trading for a project in the cloud.
307310
liquidate Stops live trading and liquidates existing positions for a certain project.
308311
stop Stops live trading for a certain project without liquidating existing positions.
309312
```
310313

314+
### `lean cloud live command`
315+
316+
Send a command to a running cloud live trading project.
317+
318+
```
319+
Usage: lean cloud live command [OPTIONS] PROJECT
320+
321+
Send a command to a running cloud live trading project.
322+
323+
Options:
324+
--data TEXT The command to send, 'str' representation of a 'dict' e.g. "{ \"target\": \"BTCUSD\",
325+
\"$type\":\"MyCommand\" }"
326+
--verbose Enable debug logging
327+
--help Show this message and exit.
328+
```
329+
330+
_See code: [lean/commands/cloud/live/command.py](lean/commands/cloud/live/command.py)_
331+
311332
### `lean cloud live deploy`
312333

313334
Start live trading for a project in the cloud.
@@ -1165,6 +1186,7 @@ Options:
11651186
Commands:
11661187
add-security Represents a command to add a security to the algorithm.
11671188
cancel-order Represents a command to cancel a specific order by id.
1189+
command Send a command to a local running live trading project.
11681190
deploy Start live trading a project locally using Docker.
11691191
liquidate Liquidate the given symbol from the latest deployment of the given project.
11701192
stop Stop an already running local live trading project.
@@ -1216,6 +1238,25 @@ Options:
12161238

12171239
_See code: [lean/commands/live/cancel_order.py](lean/commands/live/cancel_order.py)_
12181240

1241+
### `lean live command`
1242+
1243+
Send a command to a local running live trading project.
1244+
1245+
```
1246+
Usage: lean live command [OPTIONS] PROJECT
1247+
1248+
Send a command to a local running live trading project.
1249+
1250+
Options:
1251+
--data TEXT The command to send, 'str' representation of a 'dict' e.g. "{ \"target\": \"BTCUSD\",
1252+
\"$type\":\"MyCommand\" }"
1253+
--lean-config FILE The Lean configuration file that should be used (defaults to the nearest lean.json)
1254+
--verbose Enable debug logging
1255+
--help Show this message and exit.
1256+
```
1257+
1258+
_See code: [lean/commands/live/command.py](lean/commands/live/command.py)_
1259+
12191260
### `lean live deploy`
12201261

12211262
Start live trading a project locally using Docker.

lean/commands/cloud/live/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
from lean.commands.cloud.live.live import live
1515
from lean.commands.cloud.live.deploy import deploy
1616
from lean.commands.cloud.live.stop import stop
17+
from lean.commands.cloud.live.command import command
1718
from lean.commands.cloud.live.liquidate import liquidate
1819

1920

2021
live.add_command(deploy)
2122
live.add_command(stop)
22-
live.add_command(liquidate)
23+
live.add_command(command)
24+
live.add_command(liquidate)

lean/commands/cloud/live/command.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
15+
from lean.container import container
16+
from click import command, argument, option
17+
from lean.click import LeanCommand
18+
19+
20+
@command(cls=LeanCommand)
21+
@argument("project", type=str)
22+
@option("--data", type=str, help="The command to send, 'str' representation of a 'dict' e.g. "
23+
"\"{ \\\"target\\\": \\\"BTCUSD\\\", \\\"$type\\\":\\\"MyCommand\\\" }\"")
24+
def command(project: str, data: str) -> None:
25+
"""
26+
Send a command to a running cloud live trading project.
27+
"""
28+
data = eval(data)
29+
30+
logger = container.logger
31+
api_client = container.api_client
32+
33+
cloud_project_manager = container.cloud_project_manager
34+
cloud_project = cloud_project_manager.get_cloud_project(project, False)
35+
logger.info(f"cloud.live.command(): sending command.")
36+
response = api_client.live.command_create(cloud_project.projectId, data)
37+
if response.success:
38+
logger.info(f"cloud.live.command(): command executed successfully.")
39+
else:
40+
raise Exception("cloud.live.command(): Failed: to execute the command successfully.")
41+

lean/commands/live/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from lean.commands.live.live import live
1515
from lean.commands.live.deploy import deploy
1616
from lean.commands.live.stop import stop
17+
from lean.commands.live.command import command
1718
from lean.commands.live.liquidate import liquidate
1819
from lean.commands.live.submit_order import submit_order
1920
from lean.commands.live.cancel_order import cancel_order
@@ -22,8 +23,9 @@
2223

2324
live.add_command(deploy)
2425
live.add_command(stop)
26+
live.add_command(command)
2527
live.add_command(liquidate)
2628
live.add_command(submit_order)
2729
live.add_command(cancel_order)
2830
live.add_command(add_security)
29-
live.add_command(update_order)
31+
live.add_command(update_order)

lean/commands/live/command.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
15+
from pathlib import Path
16+
from click import command, argument, option
17+
from lean.click import LeanCommand, PathParameter
18+
from lean.commands.live.live import get_result, send_command
19+
20+
21+
@command(cls=LeanCommand, requires_lean_config=True, requires_docker=True)
22+
@argument("project", type=PathParameter(exists=True, file_okay=True, dir_okay=True))
23+
@option("--data", type=str, help="The command to send, 'str' representation of a 'dict' e.g. "
24+
"\"{ \\\"target\\\": \\\"BTCUSD\\\", \\\"$type\\\":\\\"MyCommand\\\" }\"")
25+
def command(project: Path,
26+
data: str) -> None:
27+
"""
28+
Send a command to a local running live trading project.
29+
"""
30+
data = eval(data)
31+
if "id" not in data:
32+
from uuid import uuid4
33+
data["id"] = uuid4().hex
34+
35+
docker_container_name = send_command(project, data)
36+
get_result(data["id"], docker_container_name)
37+

lean/components/api/live_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,15 @@ def liquidate_and_stop(self, project_id: int) -> QCRestResponse:
123123
"projectId": project_id
124124
})
125125
return QCRestResponse(**data)
126+
127+
def command_create(self, project_id: int, command: dict) -> QCRestResponse:
128+
"""Sends a command to a live trading deployment
129+
130+
:param project_id: the id of the project
131+
:param command: the command to send
132+
"""
133+
data = self._api.post("live/commands/create", {
134+
"projectId": project_id,
135+
"command": command
136+
})
137+
return QCRestResponse(**data)

0 commit comments

Comments
 (0)