2024-02-12 21:09:28 +01:00
|
|
|
# Executor
|
2023-10-11 09:49:06 +01:00
|
|
|
|
2024-02-12 21:09:28 +01:00
|
|
|
## Table of Contents
|
|
|
|
|
- [Executor](#executor.Executor)
|
|
|
|
|
- [__init__](#executor.__init__)
|
|
|
|
|
- [add_workers](#executor.add_workers)
|
|
|
|
|
- [remove_workers](#executor.remove_workers)
|
|
|
|
|
- [add_worker](#executor.add_worker)
|
|
|
|
|
- [run](#executor.run)
|
|
|
|
|
- [trade](#executor.trade)
|
|
|
|
|
- [execute](#executor.execute)
|
|
|
|
|
|
|
|
|
|
<a id='executor.Executor'></a>
|
|
|
|
|
### Executor
|
2023-10-11 09:49:06 +01:00
|
|
|
```python
|
|
|
|
|
class Executor
|
|
|
|
|
```
|
|
|
|
|
Executor class for running multiple strategies on multiple symbols concurrently.
|
2024-02-12 21:09:28 +01:00
|
|
|
#### Attributes:
|
|
|
|
|
| Name | Type | Description | Default |
|
|
|
|
|
|--------------|----------------------|------------------------------------------------|---------|
|
|
|
|
|
| `executor` | `ThreadPoolExecutor` | The default thread executor. | None |
|
|
|
|
|
| `workers` | `list` | List of strategies. | [] |
|
|
|
|
|
| `coroutines` | `dict` | Dictionary of coroutines and keyword arguments | {} |
|
|
|
|
|
| `functions` | `dict` | Dictionary of functions and keyword arguments | {} |
|
|
|
|
|
|
|
|
|
|
<a id="executor.__init__"></a>
|
|
|
|
|
#### \_\_init\_\_
|
|
|
|
|
```python
|
|
|
|
|
def __init__(self):
|
|
|
|
|
```
|
|
|
|
|
Initialize the executor class.
|
2023-10-16 15:36:31 +01:00
|
|
|
|
2024-02-12 21:09:28 +01:00
|
|
|
<a id="executor.add_workers"></a>
|
2023-10-16 15:36:31 +01:00
|
|
|
### add\_workers
|
2023-10-11 09:49:06 +01:00
|
|
|
```python
|
|
|
|
|
def add_workers(strategies: Sequence[type(Strategy)])
|
|
|
|
|
```
|
|
|
|
|
Add multiple strategies at once
|
2023-10-16 15:36:31 +01:00
|
|
|
#### Arguments:
|
2024-02-12 21:09:28 +01:00
|
|
|
| Name | Type | Description |
|
|
|
|
|
|--------------|----------------------------|---------------------------|
|
|
|
|
|
| `strategies` | `Sequence[type(Strategy)]` | A sequence of strategies. |
|
2023-10-11 09:49:06 +01:00
|
|
|
|
2024-02-12 21:09:28 +01:00
|
|
|
<a id="executor.remove_workers"></a>
|
2023-10-16 15:36:31 +01:00
|
|
|
### remove\_workers
|
2023-10-11 09:49:06 +01:00
|
|
|
```python
|
|
|
|
|
def remove_workers(*symbols: Sequence[Symbol])
|
|
|
|
|
```
|
|
|
|
|
Removes any worker running on a symbol not successfully initialized.
|
2023-10-16 15:36:31 +01:00
|
|
|
#### Arguments:
|
2024-02-12 21:09:28 +01:00
|
|
|
| Name | Type | Description |
|
|
|
|
|
|-----------|--------------------|------------------------|
|
|
|
|
|
| `symbols` | `Sequence[Symbol]` | A sequence of symbols. |
|
2023-10-11 09:49:06 +01:00
|
|
|
|
2024-02-12 21:09:28 +01:00
|
|
|
<a id="executor.add_worker"></a>
|
2023-10-16 15:36:31 +01:00
|
|
|
### add\_worker
|
2023-10-11 09:49:06 +01:00
|
|
|
```python
|
|
|
|
|
def add_worker(strategy: type(Strategy))
|
|
|
|
|
```
|
|
|
|
|
Add a strategy instance to the list of workers
|
2023-10-16 15:36:31 +01:00
|
|
|
#### Arguments:
|
2024-02-12 21:09:28 +01:00
|
|
|
| Name | Type | Description |
|
|
|
|
|
|------------|------------------|----------------------|
|
|
|
|
|
| `strategy` | `type(Strategy)` | A strategy instance. |
|
2023-10-11 09:49:06 +01:00
|
|
|
|
2024-02-12 21:09:28 +01:00
|
|
|
<a id="executor.run"></a>
|
2023-10-16 15:36:31 +01:00
|
|
|
### run
|
2023-10-11 09:49:06 +01:00
|
|
|
```python
|
|
|
|
|
@staticmethod
|
|
|
|
|
def run(func: Callable|Coroutine, kwargs: dict)
|
|
|
|
|
```
|
|
|
|
|
Wrap the input coroutine function with 'asyncio.run' so that it can be executed in a threadpool executor.
|
2023-10-16 15:36:31 +01:00
|
|
|
#### Arguments:
|
2024-02-12 21:09:28 +01:00
|
|
|
| Name | Type | Description |
|
|
|
|
|
|----------|-----------|--------------------------------------------|
|
|
|
|
|
| `func` | `Callable | Coroutine` |A coroutine function.|
|
|
|
|
|
| `kwargs` | `Dict` | Keyword arguments to pass to the function. |
|
2023-10-11 09:49:06 +01:00
|
|
|
|
2024-02-12 21:09:28 +01:00
|
|
|
<a id="executor.trade"></a>
|
2023-10-16 15:36:31 +01:00
|
|
|
### trade
|
2023-10-11 09:49:06 +01:00
|
|
|
```python
|
|
|
|
|
def trade(strategy: Strategy)
|
|
|
|
|
```
|
2024-02-12 21:09:28 +01:00
|
|
|
Wrap coroutine trade method of each strategy with 'asyncio.run'.
|
2023-10-16 15:36:31 +01:00
|
|
|
#### Arguments:
|
2024-02-12 21:09:28 +01:00
|
|
|
| Name | Type | Description |
|
|
|
|
|
|------------|------------|----------------------|
|
|
|
|
|
| `strategy` | `Strategy` | A strategy instance. |
|
2023-10-11 09:49:06 +01:00
|
|
|
|
2024-02-12 21:09:28 +01:00
|
|
|
<a id="executor.execute"></a>
|
2023-10-16 15:36:31 +01:00
|
|
|
### execute
|
2023-10-11 09:49:06 +01:00
|
|
|
```python
|
2024-02-12 21:09:28 +01:00
|
|
|
async def execute(workers: int = 5)
|
2023-10-11 09:49:06 +01:00
|
|
|
```
|
|
|
|
|
Run the strategies with a threadpool executor.
|
2023-10-16 15:36:31 +01:00
|
|
|
#### Arguments:
|
2024-02-12 21:09:28 +01:00
|
|
|
| Name | Type | Description |
|
|
|
|
|
|-----------|-------|-----------------------------------------------------------|
|
|
|
|
|
| `workers` | `int` | Number of workers to use in executor pool. Defaults to 5. |
|
2023-10-16 15:36:31 +01:00
|
|
|
#### Notes:
|
|
|
|
|
No matter the number specified, the executor will always use a minimum of 5 workers.
|