Fix backoff_decorator logic to reset number of retries correctly
6.2 KiB
Bot
Table of Contents
- Bot
- __init__
- initialize
- execute
- start
- add_coroutine
- add_function
- add_strategy
- add_strategies
- add_strategy_all
- process_pool
Bot
class Bot
"""The bot class. Create a bot instance to run strategies.
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
account |
Account |
Account Object. | None |
executor |
Executor |
The executor. | None |
strategies |
List[Strategies] |
A list of strategies to initialize and run | list() |
mt5 |
MetaTrader |
A MetaTrader Instance |
MetaTrader() |
config |
Config |
A Config instance | Config() |
_init_
def __init__()
Initializes the Bot class.
initialize
async def initialize(self)
Prepares the bot by signing in to the trading account and initializing the symbols for each strategy. Only strategies with successfully initialized symbols will be added to the executor. Starts the global task queue.
Note: initialize_sync is a synchronous version of this method.
Raises:
| Exception | Description |
|---|---|
SystemExit |
If sign in was not successful |
execute
def execute()
Executes the bot. Use this method to run the bot in a synchronous manner. This method is blocking and will not return until the bot is done running.
start
async def start()
Initialize the bot and execute it. Similar to calling execute method but is asynchronous.
add_coroutine
def add_coroutine(self, coroutine: Coroutine, on_separate_thread=False, **kwargs)
Add a coroutine to the executor. By default, all coroutines added to the executor run on this same thread,
using asyncio.gather, but if on_separate_thread is true then the coroutine is given it's own thread.
Parameters:
| Name | Type | Description |
|---|---|---|
coroutine |
Coroutine |
A coroutine to run in the executor |
on_separate_thread |
bool |
Run coroutine on a separate thread in the executor |
kwargs |
Any |
Keyword arguments to pass to the coroutine |
add_function
def add_function(self, function: Callable, **kwargs)
Add a function to the executor.
Parameters:
| Name | Type | Description |
|---|---|---|
function |
Callable |
A function to run in the executor |
kwargs |
Any |
Keyword arguments to pass to the function |
add_strategy
def add_strategy(self, strategy: Strategy)
Add a strategy to the list of strategies.
Parameters:
| Name | Type | Description |
|---|---|---|
strategy |
Strategy |
A Strategy instance to run on bot |
add_strategies
def add_strategies(strategies: Iterable[Strategy])
Add multiple strategies at the same time
Parameters:
| Name | Type | Description |
|---|---|---|
strategies |
Iterable[Strategy] |
An iterable of Strategy instances |
add_strategy_all
def add_strategy_all(*, strategy: Type[Strategy], params: dict | None = None, symbols: list[Symbol] = None, **kwargs)
Use this to run a single strategy on multiple symbols with the same parameters and keyword arguments.
Parameters:
| Name | Type | Description |
|---|---|---|
strategy |
Type[Strategy] |
A Strategy class |
params |
dict or None |
A dictionary of parameters for the strategy |
symbols |
list[Symbol] |
A list of symbols to run the strategy on |
**kwargs |
Any |
Keyword arguments |
@classmethod
def process_pool(cls, processes: dict[Callable: dict] = None, num_workers: int = None):
Run multiple processes (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.
Parameters
| Name | Type | Description |
|---|---|---|
processes |
dict[Callable: dict] |
A dictionary of callables and their keyword arguments to run as processes |
num_workers |
int |
The number of workers to use. If not specified, the number of bots will be used |