Trade
Module for trading operations with MetaTrader 5.
Provides a Trade class for managing trading operations.
Trade #
Trade(expert_name, version, symbol, magic_number, lot, stop_loss, emergency_stop_loss, take_profit, emergency_take_profit, start_time='9:15', finishing_time='17:30', ending_time='17:50', fee=0.0)
Represents a trading strategy for a financial instrument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expert_name
|
str
|
The name of the expert advisor. |
required |
version
|
str
|
The version of the expert advisor. |
required |
symbol
|
str
|
The financial instrument symbol. |
required |
magic_number
|
int
|
The magic number for identifying trades. |
required |
lot
|
float
|
The number of lots to trade. |
required |
stop_loss
|
float
|
The stop loss level. |
required |
emergency_stop_loss
|
float
|
Emergency stop loss as a protection. |
required |
take_profit
|
float
|
The take profit level. |
required |
emergency_take_profit
|
float
|
Emergency take profit for gain. |
required |
start_time
|
str
|
The time when the expert advisor can start trading. |
'9:15'
|
finishing_time
|
str
|
The time until new positions can be opened. |
'17:30'
|
ending_time
|
str
|
The time when any remaining position will be closed. |
'17:50'
|
fee
|
float
|
The average fee per trading. |
0.0
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
initialize #
Initialize the MetaTrader 5 instance.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
select_symbol #
prepare_symbol #
Prepare the trading symbol for opening positions.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
summary #
Print a summary of the expert advisor parameters.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
statistics #
Print statistics of the expert advisor.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
open_buy_position #
Open a Buy position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
comment
|
str
|
A comment for the trade. |
''
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
open_sell_position #
Open a Sell position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
comment
|
str
|
A comment for the trade. |
''
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
request_result #
Process the result of a trading request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
price
|
float
|
The price of the trade. |
required |
result
|
Mt5.TradeResult
|
The result of the trading request. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
open_position #
Open a position based on buy and sell conditions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
should_buy
|
bool
|
True if a Buy position should be opened, False otherwise. |
required |
should_sell
|
bool
|
True if a Sell position should be opened, False otherwise. |
required |
comment
|
str
|
A comment for the trade. |
''
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
close_position #
Close an open position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
comment
|
str
|
A comment for the trade. |
''
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
stop_and_gain #
Check for stop loss and take profit conditions and close positions accordingly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
comment
|
str
|
A comment for the trade. |
''
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in mqpy\trade.py
days_end #
Check if it is the end of trading for the day.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if it is the end of trading for the day, False otherwise. |
Source code in mqpy\trade.py
trading_time #
Check if it is within the allowed trading time.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if it is within the allowed trading time, False otherwise. |