Files
aiomql/docs/lib/positions.md
T

176 lines
5.7 KiB
Markdown
Raw Normal View History

2024-02-12 21:09:28 +01:00
# Positions
## Table of contents
2024-11-10 12:25:50 +01:00
- [Positions](#positions.positions)
- [\_\_init\_\_](#positions.__init__)
- [get_positions](#positions.get_positions)
- [get_position_by_ticket](#positions.get_position_by_ticket)
- [get_positions_by_symbol](#positions.get_positions_by_symbol)
- [close](#positions.close)
- [close_position_by_ticket](#positions.close_position_by_ticket)
- [close_position](#positions.close_position)
- [close_all](#positions.close_all)
2024-11-30 23:10:31 +01:00
- [get_total_positions](#positions.get_total_positions)
2024-11-10 12:25:50 +01:00
<a id="positions.positions"></a>
2024-02-12 21:09:28 +01:00
### Positions
2023-10-11 09:49:06 +01:00
```python
class Positions
```
Get and handle Open positions.
2024-02-12 21:09:28 +01:00
#### Attributes
2024-11-30 23:10:31 +01:00
| Name | Type | Description |
|-------------|-----------------------------|-------------------------------------------------------------------------------------|
| `positions` | `tuple[TradePosition, ...]` | Financial instrument name. |
| `mt5` | `MetaTrader` | MetaTrader instance. |
|`total_positions`| `int` | Total number of open positions. Can be set in `get_positions` or `get_total_positions`. |
2024-11-10 12:25:50 +01:00
<a id="positions.__init__"></a>
2024-02-12 21:09:28 +01:00
### \_\_init\_\_
2023-10-11 09:49:06 +01:00
```python
2024-11-10 12:25:50 +01:00
def __init__()
2023-10-11 09:49:06 +01:00
```
2024-11-10 12:25:50 +01:00
Initialize a position instance
2023-10-11 09:49:06 +01:00
2024-11-10 12:25:50 +01:00
<a id="positions.get_position"></a>
### get_positions
2023-10-11 09:49:06 +01:00
```python
2024-11-30 23:10:31 +01:00
async def get_positions(*, symbol: str = None, ticket: int = None, group: str = None) -> tuple[TradePosition, ...]:
2023-10-11 09:49:06 +01:00
```
2024-11-30 23:10:31 +01:00
Get open positions, with the ability to filter by symbol, ticket, or group.
2023-10-11 09:49:06 +01:00
2024-11-30 23:10:31 +01:00
#### Parameters:
| Name | Type | Description |
|----------|-------|-----------------|
| `symbol` | `str` | Symbol |
| `ticket` | `int` | Position ticket |
| `group` | `str` | Group name |
#### Returns:
2024-11-10 12:25:50 +01:00
| Type | Description |
|-----------------------------|--------------------------------|
| `tuple[TradePosition, ...]` | A list of open trade positions |
2023-10-11 09:49:06 +01:00
2024-11-10 12:25:50 +01:00
<a id="positions.get_position_by_ticket"></a>
### get_position_by_ticket
2024-05-05 00:08:57 +01:00
```python
2024-11-10 12:25:50 +01:00
async def get_position_by_ticket(self, *, ticket: int) -> TradePosition
2024-05-05 00:08:57 +01:00
```
2024-11-10 12:25:50 +01:00
Get a position by ticket id.
#### Parameters:
2024-05-05 00:08:57 +01:00
| Name | Type | Description |
|----------|-------|-----------------|
| `ticket` | `int` | Position ticket |
2024-11-10 12:25:50 +01:00
#### Returns:
2024-05-05 00:08:57 +01:00
| Type | Description |
|-----------------|----------------|
| `TradePosition` | Trade position |
2023-10-11 09:49:06 +01:00
2024-11-10 12:25:50 +01:00
<a id="positions.get_positions_by_symbol"></a>
### get_positions_by_symbol
2024-02-12 21:09:28 +01:00
```python
2024-11-10 12:25:50 +01:00
async def get_positions_by_symbol(self, *, symbol: str) -> tuple[TradePosition, ...]
2024-02-12 21:09:28 +01:00
```
2024-11-10 12:25:50 +01:00
Filter positions by symbols
#### Parameters:
| Name | Type | Description |
|----------|-------|-------------|
| `symbol` | `str` | Symbol |
#### Returns:
| Type | Description |
|-----------------------------|----------------|
| `tuple[TradePosition, ...]` | Trade position |
<a id="positions.close"></a>
### close
2024-02-12 21:09:28 +01:00
```python
2024-11-10 12:25:50 +01:00
async def close(self, *, ticket: int, symbol: str, price: float, volume: float, order_type: OrderType) -> OrderSendResult:
2024-02-12 21:09:28 +01:00
```
2024-11-10 12:25:50 +01:00
Close a position using its details.
2024-05-05 00:08:57 +01:00
2024-11-10 12:25:50 +01:00
#### Parameters:
| Name | Type | Description |
|--------------|-------------|----------------------------|
| `ticket` | `int` | Position ticket. |
| `symbol` | `str` | Financial instrument name. |
| `price` | `float` | Closing price. |
| `volume` | `float` | Volume to close. |
| `order_type` | `OrderType` | Order type. |
2024-02-12 21:09:28 +01:00
2024-11-10 12:25:50 +01:00
#### Returns:
| Type | Description |
|--------------------|----------------------------------------------------|
| `OrderSendResult ` | The result of the order sent to close the position |
<a id="positions.close_position"></a>
2024-05-05 00:08:57 +01:00
### close_position
```python
2024-11-10 12:25:50 +01:00
async def close_position(self, *, position: TradePosition) -> OrderSendResult:
2024-05-05 00:08:57 +01:00
```
Close a position by position object.
2024-11-10 12:25:50 +01:00
#### Parameters:
2024-05-05 00:08:57 +01:00
| Name | Type | Description |
|------------|-----------------|-----------------|
| `position` | `TradePosition` | Position object |
2024-11-10 12:25:50 +01:00
#### Returns:
| Type | Description |
|-------------------|----------------------------------------------------|
| `OrderSendResult` | The result of the order sent to close the position |
<a id='positions.close_position_by_ticket'></a>
### close_position_by_ticket
```python
async def close_position_by_ticket(self, *, position: TradePosition) -> OrderSendResult:
```
Close a position by position object.
#### Parameters:
| Name | Type | Description |
|------------|-----------------|-----------------|
| `position` | `TradePosition` | Position object |
#### Returns:
| Type | Description |
|-------------------|----------------------------------------------------|
| `OrderSendResult` | The result of the order sent to close the position |
<a id="positions.close_all"></a>
2024-02-12 21:09:28 +01:00
### close_all
2023-10-11 09:49:06 +01:00
```python
async def close_all() -> int
```
Close all open positions for the trading account.
2024-11-10 12:25:50 +01:00
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|-------|--------------------------------------|
| `int` | Return total number of closed trades |
2024-11-30 23:10:31 +01:00
<a id="positions.get_total_positions"></a>
### get_total_positions
```python
async def get_total_positions() -> int
```
Get the total number of open positions and set the `total_positions` attribute.
#### Returns:
| Type | Description |
|-------|--------------------------------------|
| `int` | Return total number of open trades |