mirror of
https://github.com/Ichinga-Samuel/aiomql.git
synced 2026-07-28 04:37:43 +00:00
4.8 KiB
4.8 KiB
Utils
Utils is a collection of utility functions that are used throughout the codebase. It is a collection of functions.
Table of Contents
- backtest_sleep
- round_off
- dict_to_string
- round_down
- round_up
- async_cache
- backoff_decorator
- error_handler
- error_handler_sync
round_off
def round_off(value: float, step: float, round_down: bool = True) -> float:
Rounds off a value to the nearest step. If round_down is True, it will round down, otherwise it will round up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| value | float | The value to round off. | |
| step | float | The step to round off to. | |
| round_down | bool | Whether to round down. If False, round up. | True |
Returns:
| Type | Description |
|---|---|
| float | The rounded off value. |
dict_to_string
def dict_to_string(data: dict, multi=True) -> str:
Converts a dictionary to a string. If multi is True, it will return a multi-line string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| data | dict | The dictionary to convert to a string. | |
| multi | bool | Whether to return a multi-line string. | True |
Returns:
| Type | Description |
|---|---|
| str | The dictionary as a string. |
round_down
def round_down(value: float, base: int) -> int:
Rounds down a value to the nearest base.
Parameters:
| Name | Type | Description |
|---|---|---|
| value | float | The value to round down. |
| base | int | The base to round down to. |
round_up
def round_up(value: float, base: int) -> int:
Rounds up a value to the nearest base.
Parameters:
| Name | Type | Description |
|---|---|---|
| value | float | The value to round up. |
| base | int | The base to round up to. |
async_cache
def async_cache(func: Callable) -> Callable:
A decorator to cache the result of an async function.
backoff_decorator
def backoff_decorator(func=None, *, max_retries: int = 2, retries: int = 0, error="") -> Callable:
A decorator to retry a function with exponential backoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| func | The function to decorate. | ||
| max_retries | int | The maximum number of retries. | 2 |
| retries | int | The current number of retries. | 0 |
| error | str | The error message to display on exception. |
error_handler
async def error_handler(func=None, *, msg="", exe=Exception, response=None, log_error_msg=True) -> Callable:
A decorator to handle exceptions in an async function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| func | The function to decorate. | ||
| msg | str | The error message to display on exception. | |
| exe | The exception to catch. | ||
| response | The response to return on exception. | ||
| log_error_msg | bool | Whether to log the error message. | True |
error_handler_sync
def error_handler_sync(func=None, *, msg="", exe=Exception, response=None, log_error_msg=True) -> Callable:
A decorator to handle exceptions in a sync function.
backtest_sleep
def backtest_sleep(seconds: float) -> None:
Sleeps for a given number of seconds in backtest mode.
Parameters:
| Name | Type | Description |
|---|---|---|
| seconds | float | The number of seconds to sleep. |