mirror of
https://github.com/Ichinga-Samuel/aiomql.git
synced 2026-08-20 15:28:08 +00:00
v4
This commit is contained in:
+127
-45
@@ -1,28 +1,38 @@
|
||||
from aiomql import TradePositionfrom aiomql.lib.sessions import Duration
|
||||
|
||||
# Session and Sessions
|
||||
Sessions allow you to run a strategy at specific times of the day.
|
||||
|
||||
## Table of Contents
|
||||
- [Session](#session)
|
||||
- [\_\_init\_\_](#session.__init__)
|
||||
- [\__init\__](#session.__init__)
|
||||
- [begin](#session.begin)
|
||||
- [close](#session.close)
|
||||
- [action](#session.action)
|
||||
- [Sessions](#sessions)
|
||||
- [\_\_init\_\_](#sessions.__init__)
|
||||
- [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__)
|
||||
- [find](#sessions.find)
|
||||
- [find_next](#sessions.find_next)
|
||||
- [check](#sessions.check)
|
||||
- [delta](#delta)
|
||||
- [until](#until)
|
||||
- [delta](#sessions_mod.delta)
|
||||
- [backtest_sleep](#sessions_mod.backtest_sleep)
|
||||
|
||||
|
||||
<a id="session"></a>
|
||||
<a id="session.session"></a>
|
||||
## Session
|
||||
```python
|
||||
class Session
|
||||
```
|
||||
A session is a time period between two `datetime.time` objects specified in utc.
|
||||
#### Attributes
|
||||
|
||||
#### Attributes:
|
||||
| Name | Type | Description | Default |
|
||||
|----------------|-------------------------------------------------------------------|------------------------------------------------------------------------|---------|
|
||||
| `start` | `datetime.time` | The start time of the session. | None |
|
||||
@@ -39,7 +49,7 @@ opened during the session or not or even by a strategy using the session. This i
|
||||
positions opened by the strategy. This will be handled in a future release.
|
||||
|
||||
<a id="session.__init__"></a>
|
||||
### \_\_init\_\_
|
||||
### \__init\__
|
||||
```python
|
||||
def __init__(*,
|
||||
start: int | time,
|
||||
@@ -51,8 +61,8 @@ def __init__(*,
|
||||
custom_start: Callable = None,
|
||||
custom_end: Callable = None)
|
||||
```
|
||||
Create a session.
|
||||
#### Arguments
|
||||
Create a session
|
||||
#### Parameters:
|
||||
| Name | Type | Description | Default |
|
||||
|----------------|-------------------------------------------------------------------|---------------------------------------------------------------------|---------|
|
||||
| `start` | `int` \| `datetime.time` | The start time of the session in UTC. | None |
|
||||
@@ -63,6 +73,7 @@ Create a session.
|
||||
| `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 |
|
||||
|
||||
|
||||
<a id="session.begin"></a>
|
||||
### begin
|
||||
```python
|
||||
@@ -70,6 +81,7 @@ async def begin()
|
||||
```
|
||||
Call the action specified in on_start or custom_start.
|
||||
|
||||
|
||||
<a id="session.close"></a>
|
||||
### close
|
||||
```python
|
||||
@@ -77,72 +89,143 @@ async def close()
|
||||
```
|
||||
Call the action specified in on_end or custom_end.
|
||||
|
||||
|
||||
<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>
|
||||
### action
|
||||
```python
|
||||
async def action(action): pass
|
||||
async def action(*, action: Literal["close_all", "close_win", "close_loss", "custom_start", "custom_end"]): pass
|
||||
```
|
||||
Used by begin and close to call the action specified.
|
||||
|
||||
#### Arguments
|
||||
| Name | Type | Description | Default |
|
||||
|----------|---------------------------------------------------------------------------------|---------------------|---------|
|
||||
| `action` | `Literal['close_all', 'close_win', 'close_loss', 'custom_start', 'custom_end']` | The action to take. | None |
|
||||
#### Parameters:
|
||||
| Name | Type | Description |
|
||||
|----------|---------------------------------------------------------------------------------|---------------------|
|
||||
| `action` | `Literal['close_all', 'close_win', 'close_loss', 'custom_start', 'custom_end']` | The action to take. |
|
||||
|
||||
|
||||
<a id="sessions"></a>
|
||||
<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>
|
||||
## 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.
|
||||
|
||||
### Attributes:
|
||||
| Name | Type | Description | Default |
|
||||
|-------------------|-----------------|----------------------------|---------|
|
||||
| `sessions` | `list[Session]` | A list of Session objects. | [] |
|
||||
| `current_session` | `Session` | The current session. | None |
|
||||
|
||||
|
||||
<a id="sessions.__init__"></a>
|
||||
#### \_\_init\_\_
|
||||
#### \__init\__
|
||||
```python
|
||||
def __init__(*sessions)
|
||||
def __init__(*sessions: Iterable[Session])
|
||||
```
|
||||
Create a Sessions object.
|
||||
#### Arguments
|
||||
| Name | Type | Description | Default |
|
||||
|------------|------------------|-----------------------------|---------|
|
||||
| `sessions` | `tuple[Session]` | A tuple of Session objects. | None |
|
||||
#### Parameters:
|
||||
| Name | Type | Description |
|
||||
|------------|---------------------|--------------------------------|
|
||||
| `sessions` | `Iterable[Session]` | A iterable of Session objects. |
|
||||
|
||||
|
||||
<a id="sessions.find"></a>
|
||||
### find
|
||||
```python
|
||||
def find(obj: time) -> Session | None
|
||||
def find(*, moment: time = None) -> Session | None
|
||||
```
|
||||
Find a session that contains a datetime.time object.
|
||||
#### Arguments
|
||||
| Name | Type | Description | Default |
|
||||
|-------|-----------------|-------------------------|---------|
|
||||
| `obj` | `datetime.time` | A datetime.time object. | None |
|
||||
|
||||
#### Returns
|
||||
#### Parameters:
|
||||
| Name | Type | Description | Default |
|
||||
|----------|--------|-------------------------|---------|
|
||||
| `moment` | `time` | A datetime.time object. | None |
|
||||
|
||||
#### Returns:
|
||||
| Type | Description |
|
||||
|-----------|----------------------------------------|
|
||||
| `Session` | A Session object or None if not found. |
|
||||
|
||||
|
||||
<a id="sessions.find_next"></a>
|
||||
### find\_next
|
||||
### find_next
|
||||
```python
|
||||
def find_next(obj: time) -> Session
|
||||
def find_next(*, moment: time = None) -> Session
|
||||
```
|
||||
Find the next session that contains a datetime.time object.
|
||||
#### Arguments
|
||||
| Name | Type | Description | Default |
|
||||
|-------|-----------------|-------------------------|---------|
|
||||
| `obj` | `datetime.time` | A datetime.time object. | |
|
||||
#### Returns
|
||||
#### Parameters:
|
||||
| Name | Type | Description | Default |
|
||||
|----------|--------|-------------------------|---------|
|
||||
| `moment` | `time` | A datetime.time object. | |
|
||||
|
||||
#### Returns:
|
||||
| Type | Description |
|
||||
|-----------|-------------------|
|
||||
| `Session` | A Session object. |
|
||||
|
||||
|
||||
<a id="sessions.check"></a>
|
||||
### check
|
||||
```python
|
||||
@@ -150,13 +233,15 @@ async def check(): pass
|
||||
```
|
||||
Check if the current session has started and if not, wait until it starts.
|
||||
|
||||
<a id="delta"></a>
|
||||
|
||||
<a id="sessions_mod.delta"></a>
|
||||
### delta
|
||||
```python
|
||||
def delta(obj: time) -> timedelta: pass
|
||||
```
|
||||
Get the timedelta of a datetime.time object.
|
||||
#### Arguments:
|
||||
|
||||
#### Parameters:
|
||||
| Name | Type | Description | Default |
|
||||
|-------|-----------------|-------------------------|---------|
|
||||
| `obj` | `datetime.time` | A datetime.time object. | None |
|
||||
@@ -165,13 +250,10 @@ Get the timedelta of a datetime.time object.
|
||||
|-------------|---------------------|
|
||||
| `timedelta` | A timedelta object. |
|
||||
|
||||
<a id="until"></a>
|
||||
### until
|
||||
|
||||
<a id="sessions_mod.backtest_sleep"></a>
|
||||
### backtest_sleep
|
||||
```python
|
||||
def until()
|
||||
async def backtest_sleep(secs)
|
||||
```
|
||||
Get the seconds until the session starts from the current time.
|
||||
#### Returns:
|
||||
| Type | Description |
|
||||
|-------|---------------------------------------|
|
||||
| `int` | The seconds until the session starts. |
|
||||
Sleep method for backtesting.
|
||||
|
||||
Reference in New Issue
Block a user