Files
aiomql/docs/bot_builder.md
T

163 lines
6.1 KiB
Markdown
Raw Normal View History

2024-02-12 21:09:28 +01:00
# Bot
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
## Table of Contents
- [Bot](#bb.Bot)
- [\_\_init\_\_](#bb.__init__)
- [initialize](#bb.initialize)
- [execute](#bb.execute)
- [start](#bb.start)
- [add_coroutine](#bb.add_coroutine)
- [add_function](#bb.add_function)
- [add_strategy](#bb.add_strategy)
- [add_strategies](#bb.add_strategies)
- [add_strategy_all](#bb.add_strategy_all)
- [init_symbols](#bb.init_symbols)
- [init_symbol](#bb.init_symbol]())
- [run_bots](#bb.run_bots)
<a id='bb.Bot'></a>
### Bot
2023-10-11 09:49:06 +01:00
```python
2024-01-01 05:25:41 +01:00
class Bot
2023-10-11 09:49:06 +01:00
```
The bot class. Create a bot instance to run your strategies.
2024-02-12 21:09:28 +01:00
#### Attributes:
| Name | Type | Description | Default |
|------------|----------------------|------------------------------------------|----------|
| `account` | `Account` | Account Object. | None |
| `executor` | `ThreadPoolExecutor` | The default thread executor. | None |
| `symbols` | `set[Symbols]` | A set of symbols for the trading session | set() |
| `config` | `Config` | A Config instance | Config() |
<a id='bb.__init__'></a>
### \_\_init\_\_
```python
def __init__()
```
Initializes the Bot class.
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.initialize'></a>
2023-10-11 09:49:06 +01:00
### initialize
```python
async def initialize()
```
Prepares the bot by signing in to the trading account and initializing the symbols for the trading session.
2023-10-16 15:36:31 +01:00
#### Raises:
2024-02-12 21:09:28 +01:00
| Exception | Description |
|--------------|-------------------------------|
| `SystemExit` | If sign in was not successful |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.execute'></a>
2023-10-16 15:36:31 +01:00
### execute
2023-10-11 09:49:06 +01:00
```python
def execute()
```
2024-02-12 21:09:28 +01:00
Execute the bot. Use this method to run the bot.
<a id='bb.start'></a>
2023-10-16 15:36:31 +01:00
### start
2023-10-11 09:49:06 +01:00
```python
async def start()
```
2024-02-12 21:09:28 +01:00
Initialize the bot and execute it. Similar to calling **execute** method but is asynchronous.
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.add_coroutine'></a>
2023-10-16 15:36:31 +01:00
### add_coroutine
```python
def add_coroutine(coro: Coroutine, **kwargs)
```
2024-02-12 21:09:28 +01:00
Add a coroutine to the executor.
2024-01-01 05:25:41 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description |
|----------|-------------|--------------------------------------------|
| `coro` | `Coroutine` | A coroutine to run in the executor |
| `kwargs` | `Any` | Keyword arguments to pass to the coroutine |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.add_function'></a>
2023-10-16 15:36:31 +01:00
### add_function
```python
2024-01-01 05:25:41 +01:00
def add_function(func: Callable, **kwargs)
2023-10-16 15:36:31 +01:00
```
2024-02-12 21:09:28 +01:00
Add a function to the executor.
2024-01-01 05:25:41 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description |
|----------|------------|-------------------------------------------|
| `func` | `Callable` | A function to run in the executor |
| `kwargs` | `Any` | Keyword arguments to pass to the function |
2023-10-16 15:36:31 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.add_strategy'></a>
2023-10-16 15:36:31 +01:00
### add_strategy
2023-10-11 09:49:06 +01:00
```python
def add_strategy(strategy: Strategy)
```
Add a strategy to the executor. An added strategy will only run if it's symbol was successfully initialized.
2024-01-01 05:25:41 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description |
|------------|------------|-----------------------------------|
| `strategy` | `Strategy` | A Strategy instance to run on bot |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.add_strategies'></a>
2023-10-16 15:36:31 +01:00
### add_strategies
2023-10-11 09:49:06 +01:00
```python
def add_strategies(strategies: Iterable[Strategy])
```
Add multiple strategies at the same time
2024-01-01 05:25:41 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description |
|--------------|----------------------|-----------------------------------|
| `strategies` | `Iterable[Strategy]` | An iterable of Strategy instances |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.add_strategy_all'></a>
2023-10-16 15:36:31 +01:00
### add_strategy_all
2023-10-11 09:49:06 +01:00
```python
def add_strategy_all(*, strategy: Type[Strategy], params: dict | None = None)
```
Use this to run a single strategy on all available instruments in the market using the default parameters
2024-02-12 21:09:28 +01:00
i.e. one set of parameters for all trading symbols
2024-01-01 05:25:41 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description |
|------------|------------------|---------------------------------------------|
| `strategy` | `Type[Strategy]` | A Strategy class |
| `params` | `dict` or `None` | A dictionary of parameters for the strategy |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id='bb.init_symbols'></a>
2023-10-16 15:36:31 +01:00
### init_symbols
2023-10-11 09:49:06 +01:00
```python
async def init_symbols()
```
Initialize the symbols for the current trading session. This method is called internally by the bot.
2024-02-12 21:09:28 +01:00
<a id='bb.init_symbol'></a>
2023-10-16 15:36:31 +01:00
### init_symbol
2023-10-11 09:49:06 +01:00
```python
async def init_symbol(symbol: Symbol) -> Symbol
```
Initialize a symbol before the beginning of a trading session.
2024-02-12 21:09:28 +01:00
Removes it from the list of symbols if it was not successfully initialized or not available for the account.
2024-01-01 05:25:41 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description |
|----------|----------|-------------------|
| `symbol` | `Symbol` | A Symbol instance |
2023-10-16 15:36:31 +01:00
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|----------|-------------------|
| `Symbol` | A Symbol instance |
<a id='bb.run_bots'></a>
```python
@classmethod
2024-05-05 00:08:57 +01:00
def run_bots(cls, funcs: dict[Callable: dict] = None, num_workers: int = None):
2024-02-12 21:09:28 +01:00
```
2024-05-05 00:08:57 +01:00
Run multiple functions (scripts, bots) at the same time in parallel with different accounts.
Running multiple functions is useful when you want to run different strategies on different accounts.
The callable can for example be a bot instance that defines its own Config instance within the function scope.
The dictionary should contain the callable as the key and the dictionary of keyword arguments to pass to the callable as
the value. Use the path attribute of the config instance to specify the terminal path of each account.
The num_workers parameter specifies the number of workers to use. If not specified, the number of workers will be the
number of bots.
2024-02-12 21:09:28 +01:00
#### Parameters
| Name | Type | Description |
|---------------|------------------------|---------------------------------------------------------------------------------|
2024-05-05 00:08:57 +01:00
| `funcs` | `dict[Callable: dict]` | A dictionary of callables and their keyword arguments to run as bots |
2024-02-12 21:09:28 +01:00
| `num_workers` | `int` | The number of workers to use. If not specified, the number of bots will be used |