This commit is contained in:
Ichinga Samuel
2024-11-10 12:25:50 +01:00
parent d9bd84bc2e
commit f56d0c85b3
39 changed files with 1538 additions and 1179 deletions
+45 -53
View File
@@ -3,11 +3,9 @@
## 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)
- [add_function](#executor.add_function)
- [add_coroutine](#executor.add_coroutine)
- [run_function](#executor.run_function)
- [execute](#executor.execute)
<a id='executor.Executor'></a>
@@ -17,12 +15,12 @@ class Executor
```
Executor class for running multiple strategies on multiple symbols concurrently.
#### 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 | {} |
| Name | Type | Description | Default |
|--------------------|----------------------|------------------------------------------------|---------|
| `executor` | `ThreadPoolExecutor` | The default thread executor. | None |
| `strategy_runners` | `list[Strategy]` | 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\_\_
@@ -31,72 +29,66 @@ def __init__(self):
```
Initialize the executor class.
<a id="executor.add_workers"></a>
### add\_workers
<a id="executor.add_coroutine"></a>
### add_coroutine
```python
def add_workers(strategies: Sequence[type(Strategy)])
def add_coroutine(self,*,coroutine: Callable | Coroutine,kwargs: dict = None,on_separate_thread=False):
```
Add multiple strategies at once
#### Arguments:
| Name | Type | Description |
|--------------|----------------------------|---------------------------|
| `strategies` | `Sequence[type(Strategy)]` | A sequence of strategies. |
Submit a coroutine to the executor. The coroutines are run in parallel using *asyncio.gather* except when the
on_spate_thread flag is set to True. In that case, the coroutine is run in a separate thread.
<a id="executor.remove_workers"></a>
### remove\_workers
#### Arguments:
| Name | Type | Description |
|----------------------|------------|-------------------------------------------------|
| `coroutine` | `Callable` | The coroutine |
| `kwargs` | `Dict` | The keyword arguments to pass to the coroutine. |
| `on_separate_thread` | `bool` | If True run the coroutine on a separate thread |
<a id="executor.add_function"></a>
### add_function
```python
def remove_workers(*symbols: Sequence[Symbol])
def add_function(self, *, function: Callable, kwargs: dict = None)
```
Removes any worker running on a symbol not successfully initialized.
#### Arguments:
| Name | Type | Description |
|-----------|--------------------|------------------------|
| `symbols` | `Sequence[Symbol]` | A sequence of symbols. |
Submit a function to the executor. Each functions runs on a separate thread.
<a id="executor.add_worker"></a>
### add\_worker
```python
def add_worker(strategy: type(Strategy))
```
Add a strategy instance to the list of workers
#### Arguments:
| Name | Type | Description |
|------------|------------------|----------------------|
| `strategy` | `type(Strategy)` | A strategy instance. |
| Name | Type | Description |
|------------|------------|--------------------------------------------|
| `function` | `Callable` | The function to run in the executor |
| `kwargs` | `Dict` | Keyword arguments to pass to the function. |
<a id="executor.run"></a>
### run
<a id="executor.run_function"></a>
### run_function
```python
@staticmethod
def run(func: Callable|Coroutine, kwargs: dict)
def run_function(function: Callable, kwargs: dict)
```
Wrap the input coroutine function with 'asyncio.run' so that it can be executed in a threadpool executor.
#### Arguments:
| Name | Type | Description |
|----------|-----------|--------------------------------------------|
| `func` | `Callable | Coroutine` |A coroutine function.|
| `kwargs` | `Dict` | Keyword arguments to pass to the function. |
| Name | Type | Description |
|------------|------------|--------------------------------------------|
| `function` | `Callable` | Run a function in the executor |
| `kwargs` | `Dict` | Keyword arguments to pass to the function. |
<a id="executor.trade"></a>
### trade
<a id="executor.exit"></a>
### exit
```python
def trade(strategy: Strategy)
async def exit()
```
Wrap coroutine trade method of each strategy with 'asyncio.run'.
#### Arguments:
| Name | Type | Description |
|------------|------------|----------------------|
| `strategy` | `Strategy` | A strategy instance. |
Shutdowns the executor. Due to the nature of threadpool executors, shutdown is not usually an immediate process.
This exit function is added as a coroutine function to the bot or backtester during initialization.
<a id="executor.execute"></a>
### execute
```python
async def execute(workers: int = 5)
def execute(workers: int = 5)
```
Run the strategies with a threadpool executor.
#### Arguments:
| Name | Type | Description |
|-----------|-------|-----------------------------------------------------------|
| `workers` | `int` | Number of workers to use in executor pool. Defaults to 5. |
#### Notes:
No matter the number specified, the executor will always use a minimum of 5 workers.
No matter the number specified, the number of workers will always be greater than equal to the minimum number of
workers required to run all functions, coroutines and strategies added to the executor.