Files
aiomql/docs/lib/trader.md
T

183 lines
7.7 KiB
Markdown
Raw Normal View History

2024-02-12 21:09:28 +01:00
# Trader
2023-10-11 09:49:06 +01:00
Trader class module. Handles the creation of an order and the placing of trades
2024-02-12 21:09:28 +01:00
## Table of Contents
- [Trader](#trader)
2024-11-10 12:25:50 +01:00
- [\_\_init\_\_](#trader.__init__)
- [set_trade_stop_levels_points](#trader.set_trade_stop_levels_points)
- [set_trade_stop_levels_pips](#trader.set_trade_stop_levels_pips)
- [create_order_with_points](#trade.create_order_with_points)
- [create_order_with_sl](#trade.create_order_with_sl)
- [create_order_with_stops](#trade.create_order_with_stops)
- [create_order_no_stops](#trade.create_order_no_stops)
- [send_order](#trader.send_order)
- [check_order](#trader.check_order)
- [record_trade](#trader.record_trade)
- [place_trade](#trader.place_trade)
<a name="trader.trader"></a>
2024-02-12 21:09:28 +01:00
### Trader
2023-10-11 09:49:06 +01:00
```python
class Trader()
```
Base class for creating a Trader object. Handles the creation of an order and the placing of trades
2024-11-10 12:25:50 +01:00
#### Attributes:
| Name | Type | Description | Default |
|--------------|----------|-------------------------------------------------------|---------|
| `ram` | `RAM` | Risk Assessment Management System. | None |
| `config` | `Config` | Config instance. | None |
| `order` | `Order` | Order instance. | None |
| `symbol` | `Symbol` | The Financial Instrument | None |
| `parameters` | `dict` | A dictionary of parameters associated with the trade. | None |
<a name="trader.__init__"></a>
2023-10-16 15:36:31 +01:00
### \_\_init\_\_
2023-10-11 09:49:06 +01:00
```python
def __init__(*, symbol: Symbol, ram: RAM = None)
```
2024-11-10 12:25:50 +01:00
#### Parameters:
2024-02-12 21:09:28 +01:00
| Name | Type | Description | Default |
|----------|----------|-----------------------------------------|---------|
2024-11-10 12:25:50 +01:00
| `symbol` | `Symbol` | The Financial instrument | |
2024-02-12 21:09:28 +01:00
| `ram` | `RAM` | Risk Assessment and Management instance | None |
2024-01-01 05:25:41 +01:00
2024-11-10 12:25:50 +01:00
<a name="trader.set_trade_stop_levels_pips"></a>
### set_trade_stop_levels_pips
2023-10-11 09:49:06 +01:00
```python
2024-11-10 12:25:50 +01:00
async def set_trade_stop_levels_pips(*, pips: float, risk_to_reward: float = None):
2023-10-11 09:49:06 +01:00
```
2024-11-10 12:25:50 +01:00
Sets the stop loss and take profit for the order. This method uses pips as defined for forex instruments.
#### Parameters:
| Name | Type | Description | Default |
|------------------|---------|-------------------------------|---------|
| `pips` | `float` | Target pips | |
| `risk_to_reward` | `float` | Optional risk to reward ratio | None |
<a name="trader.set_trade_stop_levels_points"></a>
### set_trade_stop_levels_points
2023-10-11 09:49:06 +01:00
```python
2024-11-10 12:25:50 +01:00
async def set_trade_stop_levels_points(*, points: float, risk_to_reward: float = None):
2023-10-11 09:49:06 +01:00
```
2024-11-10 12:25:50 +01:00
Sets the stop loss and take profit for the order. This method uses points as defined for forex instruments.
2023-10-11 09:49:06 +01:00
2024-11-10 12:25:50 +01:00
#### Parameters:
| Name | Type | Description | Default |
|------------------|---------|-------------------------------|---------|
| `points` | `float` | Target points | |
| `risk_to_reward` | `float` | Optional risk to reward ratio | None |
<a name="trader.create_order_no_stops"></a>
### create_order_no_stops
2024-01-01 05:25:41 +01:00
```python
2024-11-10 12:25:50 +01:00
async def create_order_no_stops(*, order_type: OrderType, volume: float = None)
2024-01-01 05:25:41 +01:00
```
2024-11-10 12:25:50 +01:00
Create an order without setting stop loss and take profit. Using minimum lot size.
#### Parameters:
| Name | Type | Description | Default |
|--------------|-------------|---------------------|---------|
| `order_type` | `OrderType` | The order type | |
| `volume` | `float` | The volume to trade | None |
<a name="trader.create_order_with_stops"></a>
### create_order_with_stops
2024-01-01 05:25:41 +01:00
```python
2024-11-10 12:25:50 +01:00
async def create_order_with_stops(*, order_type: OrderType, sl: float, tp: float, amount_to_risk: float = None)
2024-01-01 05:25:41 +01:00
```
2024-11-10 12:25:50 +01:00
Create an order with stop loss and take profit levels. Use the amount to risk per trade to
calculate the volume.
#### Parameters:
| Name | Type | Description | Default |
|------------------|-------------|--------------------|---------|
| `order_type` | `OrderType` | The order type | |
| `sl` | `float` | The stop loss` | |
| `tp` | `float` | The take profit` | |
| `amount_to_risk` | `float` | The amount to risk | None |
2024-01-01 05:25:41 +01:00
2024-11-10 12:25:50 +01:00
<a name="trader.create_order_with_sl"></a>
### create_order_with_sl
```python
async def create_order_with_sl(*, order_type: OrderType, sl: float, amount_to_risk: float = None, risk_to_reward: float = None)
```
Create an order with a given stop_loss level. Use the amount to risk per trade to calculate the volume.
#### Parameters:
| Name | Type | Description | Default |
|------------------|-------------|------------------------------------------|---------|
| `order_type` | `OrderType` | The order type | |
| `sl` | `float` | The stop loss` | |
| `risk_to_reward` | `float` | Risk to reward ratio. Optional parameter | None |
| `amount_to_risk` | `float` | The amount to risk | None |
<a name="trader.create_order_with_points"></a>
### create_order_with_points
```python
async def create_order_with_points(*, order_type: OrderType, points: float, amount_to_risk: float = None, risk_to_reward: float = None)
```
Create an order with specific points to risk. Use the amount to risk per trade to calculate the volume.
#### Parameters:
| Name | Type | Description | Default |
|------------------|-------------|------------------------------------------|---------|
| `order_type` | `OrderType` | The order type | |
| `points` | `float` | Points to risk | |
| `risk_to_reward` | `float` | Risk to reward ratio. Optional parameter | None |
| `amount_to_risk` | `float` | The amount to risk | None |
<a name="trader.send_order"></a>
### send_order
```python
async def send_order() -> OrderSendResult
```
Sends the order to the broker for execution.
#### Returns:
| Type | Description |
|-------------------|----------------------------|
| `OrderSendResult` | The OrderSendResult object |
<a name="trader.check_order"></a>
2024-01-01 05:25:41 +01:00
### check_order
```python
2024-11-10 12:25:50 +01:00
async def check_order() -> OrderCheckResult
2024-01-01 05:25:41 +01:00
```
Checks the status of the order before placing the trade.
2024-11-10 12:25:50 +01:00
#### Returns:
| Type | Description |
|--------------------|-----------------------------|
| `OrderCheckResult` | The OrderCheckResult object |
<a name="trader.record_trade"></a>
2024-01-01 05:25:41 +01:00
### record_trade
```python
2024-11-10 12:25:50 +01:00
async def record_trade(*, result: OrderSendResult, parameters: dict = None, name: str = '')
2024-01-01 05:25:41 +01:00
```
2024-05-05 00:08:57 +01:00
Records the trade and the order details if `Config.record_trades` is true. Trades are recorded as either json or csv.
2024-11-10 12:25:50 +01:00
#### Parameters:
2024-05-05 00:08:57 +01:00
| Name | Type | Description | Default |
|--------------|-------------------|--------------------------------------------------------------|---------|
2024-11-10 12:25:50 +01:00
| `result` | `OrderSendResult` | The result of the placed order | |
2024-05-05 00:08:57 +01:00
| `parameters` | `dict` | parameters to saved instead of the ones in `self.parameters` | None |
| `name` | `str` | Name for the csv or json file | '' |
2024-01-01 05:25:41 +01:00
2024-11-10 12:25:50 +01:00
<a name="trader.place_trade"></a>
### place_trade
2023-10-11 09:49:06 +01:00
```python
2024-02-12 21:09:28 +01:00
@abstractmethod
2024-11-10 12:25:50 +01:00
async def place_trade(self, *args, **kwargs)
2023-10-11 09:49:06 +01:00
```
2024-02-12 21:09:28 +01:00
Places a trade. All traders must implement this method.