Files
aiomql/docs/lib/sessions.md
T

260 lines
9.5 KiB
Markdown
Raw Normal View History

2024-11-10 12:25:50 +01:00
from aiomql import TradePositionfrom aiomql.lib.sessions import Duration
2024-02-12 21:09:28 +01:00
# Session and Sessions
Sessions allow you to run a strategy at specific times of the day.
## Table of Contents
- [Session](#session)
2024-11-10 12:25:50 +01:00
- [\__init\__](#session.__init__)
2024-02-12 21:09:28 +01:00
- [begin](#session.begin)
- [close](#session.close)
- [action](#session.action)
2024-11-10 12:25:50 +01:00
- [in_session](#session.in_session)
- [duration](#session.duration)
- [close_positions](#session.close_positions)
- [close_all](#session.close_all)
- [close_win](#session.close_win)
- [close_loss](#session.close_loss)
- [close_until](#session.until)
- [Sessions](#sessions.sessions)
- [\__init\__](#sessions.__init__)
2024-02-12 21:09:28 +01:00
- [find](#sessions.find)
- [find_next](#sessions.find_next)
- [check](#sessions.check)
2024-11-10 12:25:50 +01:00
- [delta](#sessions_mod.delta)
- [backtest_sleep](#sessions_mod.backtest_sleep)
2024-02-12 21:09:28 +01:00
2024-11-10 12:25:50 +01:00
<a id="session.session"></a>
2024-02-12 21:09:28 +01:00
## Session
2023-10-16 15:36:31 +01:00
```python
2024-02-12 21:09:28 +01:00
class Session
2023-10-16 15:36:31 +01:00
```
2024-02-12 21:09:28 +01:00
A session is a time period between two `datetime.time` objects specified in utc.
2024-11-10 12:25:50 +01:00
#### Attributes:
2024-02-12 21:09:28 +01:00
| Name | Type | Description | Default |
|----------------|-------------------------------------------------------------------|------------------------------------------------------------------------|---------|
| `start` | `datetime.time` | The start time of the session. | None |
| `end` | `datetime.time` | The end time of the session. | None |
| `on_start` | `Literal['close_all', 'close_win', 'close_loss', 'custom_start']` | The action to take when the session starts. Default is None. | None |
| `on_end` | `Literal['close_all', 'close_win', 'close_loss', 'custom_end']` | The action to take when the session ends. Default is None. | None |
| `custom_start` | `Callable` | A custom function to call when the session starts. Default is None. | None |
| `custom_end` | `Callable` | A custom function to call when the session ends. Default is None. | None |
| `name` | `str` | The name of the session. Default is a combination of start and finish. | |
#### Notes:
The `[close_all, close_win, close_loss]` will affect or open positions in the account irrespective of whether they were
opened during the session or not or even by a strategy using the session. This is because the session is not aware of the
positions opened by the strategy. This will be handled in a future release.
<a id="session.__init__"></a>
2024-11-10 12:25:50 +01:00
### \__init\__
2023-10-16 15:36:31 +01:00
```python
def __init__(*,
start: int | time,
end: int | time,
on_start: Literal['close_all', 'close_win', 'close_loss',
'custom_start'] = None,
on_end: Literal['close_all', 'close_win', 'close_loss',
'custom_end'] = None,
custom_start: Callable = None,
custom_end: Callable = None)
```
2024-11-10 12:25:50 +01:00
Create a session
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description | Default |
|----------------|-------------------------------------------------------------------|---------------------------------------------------------------------|---------|
| `start` | `int` \| `datetime.time` | The start time of the session in UTC. | None |
| `end` | `int` \| `datetime.time` | The end time of the session in UTC. | None |
| `on_start` | `Literal['close_all', 'close_win', 'close_loss', 'custom_start']` | The action to take when the session starts. Default is None. | None |
| `on_end` | `Literal['close_all', 'close_win', 'close_loss', 'custom_end']` | The action to take when the session ends. Default is None. | None |
| `custom_start` | `Callable` | A custom function to call when the session starts. Default is None. | None |
| `custom_end` | `Callable` | A custom function to call when the session ends. Default is None. | None |
| `name` | `str` | The name of the session. Default is None. | None |
2024-11-10 12:25:50 +01:00
2024-02-12 21:09:28 +01:00
<a id="session.begin"></a>
2023-10-16 15:36:31 +01:00
### begin
```python
async def begin()
```
Call the action specified in on_start or custom_start.
2024-11-10 12:25:50 +01:00
2024-02-12 21:09:28 +01:00
<a id="session.close"></a>
2023-10-16 15:36:31 +01:00
### close
```python
async def close()
```
Call the action specified in on_end or custom_end.
2024-02-12 21:09:28 +01:00
2024-11-10 12:25:50 +01:00
<a id="session.in_session"></a>
### in_session
```python
def in_session() -> bool
```
Check if the current time is within the current session.
<a id="session.duration"></a>
### duration
```python
def duration() -> Duration
```
Get the duration of the session in hours, minutes, and seconds.
<a id="session.close_positions"></a>
### close_positions
```python
async def close_positions(*, positions: tuple[TradePosition, ...])
```
Close positions in the sessions. This is used by the `close_all` action.
#### Parameters:
| Name | Type | Description |
|-------------|-----------------------------|---------------------------------------|
| `positions` | `tuple[TradePosition, ...]` | A tuple of TradePosition objects. |
<a id="session.close_all"></a>
### close_all
```python
async def close_all()
```
Close all open positions
<a id="session.close_win"></a>
### close_win
```python
async def close_win()
```
Close only winning positions
<a id="session.close_loss"></a>
### close_loss
```python
async def close_loss()
```
Close only losing positions
<a id="session.action"></a>
2023-10-16 15:36:31 +01:00
### action
```python
2024-11-10 12:25:50 +01:00
async def action(*, action: Literal["close_all", "close_win", "close_loss", "custom_start", "custom_end"]): pass
2023-10-16 15:36:31 +01:00
```
Used by begin and close to call the action specified.
2024-11-10 12:25:50 +01:00
#### Parameters:
| Name | Type | Description |
|----------|---------------------------------------------------------------------------------|---------------------|
| `action` | `Literal['close_all', 'close_win', 'close_loss', 'custom_start', 'custom_end']` | The action to take. |
2023-10-16 15:36:31 +01:00
2024-11-10 12:25:50 +01:00
<a id="session.until"></a>
### until
```python
def until() -> int
```
Get the seconds until the session starts from the current time.
<a id="sessions.sessions"></a>
2023-10-16 15:36:31 +01:00
## Sessions
```python
class Sessions()
```
Sessions allow you to run code at specific times of the day. It is a collection of Session objects.
Sessions are sorted by start time. The sessions object is an asynchronous context manager.
2024-11-10 12:25:50 +01:00
2023-10-16 15:36:31 +01:00
### Attributes:
2024-02-12 21:09:28 +01:00
| Name | Type | Description | Default |
|-------------------|-----------------|----------------------------|---------|
| `sessions` | `list[Session]` | A list of Session objects. | [] |
| `current_session` | `Session` | The current session. | None |
2023-10-16 15:36:31 +01:00
2024-11-10 12:25:50 +01:00
2024-02-12 21:09:28 +01:00
<a id="sessions.__init__"></a>
2024-11-10 12:25:50 +01:00
#### \__init\__
2023-10-16 15:36:31 +01:00
```python
2024-11-10 12:25:50 +01:00
def __init__(*sessions: Iterable[Session])
2023-10-16 15:36:31 +01:00
```
Create a Sessions object.
2024-11-10 12:25:50 +01:00
#### Parameters:
| Name | Type | Description |
|------------|---------------------|--------------------------------|
| `sessions` | `Iterable[Session]` | A iterable of Session objects. |
2023-10-16 15:36:31 +01:00
2024-02-12 21:09:28 +01:00
<a id="sessions.find"></a>
2023-10-16 15:36:31 +01:00
### find
```python
2024-11-10 12:25:50 +01:00
def find(*, moment: time = None) -> Session | None
2023-10-16 15:36:31 +01:00
```
Find a session that contains a datetime.time object.
2024-11-10 12:25:50 +01:00
#### Parameters:
| Name | Type | Description | Default |
|----------|--------|-------------------------|---------|
| `moment` | `time` | A datetime.time object. | None |
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|-----------|----------------------------------------|
| `Session` | A Session object or None if not found. |
2023-10-16 15:36:31 +01:00
2024-11-10 12:25:50 +01:00
2024-02-12 21:09:28 +01:00
<a id="sessions.find_next"></a>
2024-11-10 12:25:50 +01:00
### find_next
2023-10-16 15:36:31 +01:00
```python
2024-11-10 12:25:50 +01:00
def find_next(*, moment: time = None) -> Session
2023-10-16 15:36:31 +01:00
```
Find the next session that contains a datetime.time object.
2024-11-10 12:25:50 +01:00
#### Parameters:
| Name | Type | Description | Default |
|----------|--------|-------------------------|---------|
| `moment` | `time` | A datetime.time object. | |
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|-----------|-------------------|
| `Session` | A Session object. |
2024-11-10 12:25:50 +01:00
2024-02-12 21:09:28 +01:00
<a id="sessions.check"></a>
2023-10-16 15:36:31 +01:00
### check
```python
2024-02-12 21:09:28 +01:00
async def check(): pass
2023-10-16 15:36:31 +01:00
```
Check if the current session has started and if not, wait until it starts.
2024-02-12 21:09:28 +01:00
2024-11-10 12:25:50 +01:00
<a id="sessions_mod.delta"></a>
2024-02-12 21:09:28 +01:00
### delta
```python
def delta(obj: time) -> timedelta: pass
```
Get the timedelta of a datetime.time object.
2024-11-10 12:25:50 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description | Default |
|-------|-----------------|-------------------------|---------|
| `obj` | `datetime.time` | A datetime.time object. | None |
#### Returns
| Type | Description |
|-------------|---------------------|
| `timedelta` | A timedelta object. |
2024-11-10 12:25:50 +01:00
<a id="sessions_mod.backtest_sleep"></a>
### backtest_sleep
2024-02-12 21:09:28 +01:00
```python
2024-11-10 12:25:50 +01:00
async def backtest_sleep(secs)
2024-02-12 21:09:28 +01:00
```
2024-11-10 12:25:50 +01:00
Sleep method for backtesting.