- Installing Python and the MetaTrader5 package
- Overview of functions of the MetaTrader5 package for Python
- Connecting a Python script to the terminal and account
- Error checking: last_error
- Getting information about a trading account
- Getting information about the terminal
- Getting information about financial instruments
- Subscribing to order book changes
- Reading quotes
- Reading tick history
- Calculating margin requirements and evaluating profits
- Checking and sending a trade order
- Getting the number and list of active orders
- Getting the number and list of open positions
- Reading the history of orders and deals
Checking and sending a trade order
If necessary, you can trade directly from a Python script. The pair of functions order_check and order_send allows you to pre-check and then execute a trading operation.
For both functions, the only parameter is the request structure TradeRequest (it can be initialized as a dictionary in Python, see an example). The structure fields are exactly the same as for MqlTradeRequest.
OrderCheckResult order_check(request)
The order_check function checks the correctness of trade request fields and the sufficiency of funds to complete the required trading operation.
The result of the function is returned as the OrderCheckResult structure. It repeats the structure of MqlTradeCheckResult but additionally contains the request field with a copy of the original request.
The order_check function is an analog of OrderCheck.
Example (MQL5/Scripts/MQL5Book/python/ordercheck.py):
import MetaTrader5 as mt5
|
Result:
Account currency: USD OrderCheckResult(retcode=0, balance=10000.17, equity=10000.17, profit=0.0, margin=1000.0,... retcode=0 balance=10000.17 equity=10000.17 profit=0.0 margin=1000.0 margin_free=9000.17 margin_level=1000.017 comment=Done request=TradeRequest(action=1, magic=234000, order=0, symbol='USDJPY', volume=1.0, price=144.128,... traderequest: action=1 traderequest: magic=234000 traderequest: order=0 traderequest: symbol=USDJPY traderequest: volume=1.0 traderequest: price=144.128 traderequest: stoplimit=0.0 traderequest: sl=144.028 traderequest: tp=144.228 traderequest: deviation=10 traderequest: type=0 traderequest: type_filling=2 traderequest: type_time=0 traderequest: expiration=0 traderequest: comment=python script traderequest: position=0 traderequest: position_by=0 |
OrderSendResult order_send(request)
The order_send function sends a request from the terminal to the trading server to make a trade operation.
The result of the function is returned as the OrderSendResult structure. It repeats the structure of MqlTradeResult but additionally contains the request field with a copy of the original request.
The function is an analog of OrderSend.
Example (MQL5/Scripts/MQL5Book/python/ordersend.py):
import time
|
Result:
1. order_send(): by USDJPY 0.1 lots at 144.132 2. order_send done, OrderSendResult(retcode=10009, deal=1445796125, order=1468026008, volume=0.1, price=144.132,... opened position with POSITION_TICKET=1468026008 sleep 2 seconds before closing position #1468026008 3. close position #1468026008: sell USDJPY 0.1 lots at 144.124 4. position #1468026008 closed, OrderSendResult(retcode=10009, deal=1445796155, order=1468026041, volume=0.1, price=144.124,... retcode=10009 deal=1445796155 order=1468026041 volume=0.1 price=144.124 bid=144.124 ask=144.132 comment=Request executed request_id=2 retcode_external=0 request=TradeRequest(action=1, magic=234000, order=0, symbol='USDJPY', volume=0.1, price=144.124, stoplimit=0.0,... traderequest: action=1 traderequest: magic=234000 traderequest: order=0 traderequest: symbol=USDJPY traderequest: volume=0.1 traderequest: price=144.124 traderequest: stoplimit=0.0 traderequest: sl=0.0 traderequest: tp=0.0 traderequest: deviation=20 traderequest: type=1 traderequest: type_filling=2 traderequest: type_time=0 traderequest: expiration=0 traderequest: comment=python script close traderequest: position=1468026008 traderequest: position_by=0 |