检查和发送交易订单

如有必要,你可以直接从 Python 脚本进行交易。order_checkorder_send 这两个函数允许你预先检查,然后执行交易操作。

对于这两个函数,唯一的参数是请求结构 TradeRequest(它可以在 Python 中初始化为字典,参见例子)。结构字段与 MqlTradeRequest 完全相同

OrderCheckResult order_check(request)

order_check 函数检查交易请求字段的正确性以及完成所需交易操作的资金充足性。

函数的结果作为 OrderCheckResult 结构体返回。它重复了 MqlTradeCheckResult 的结构体,但是还包含具有原始请求副本的 request 字段。

order_check 函数类似于 OrderCheck

示例 (MQL5/Scripts/MQL5Book/python/ordercheck.py):

import MetaTrader5 as mt5
   
# let's establish a connection to the MetaTrader 5 terminal
...   
# get account currency for information
account_currency=mt5.account_info().currency
print("Account currency:", account_currency)
   
# get the necessary properties of the deal symbol
symbol = "USDJPY"
symbol_info = mt5.symbol_info(symbol)
if symbol_info is None:
   print(symbol, "not found, can not call order_check()")
   mt5.shutdown()
   quit()
   
point = mt5.symbol_info(symbol).point
# if the symbol is not available in the Market Watch, add it
if not symbol_info.visible:
   print(symbol, "is not visible, trying to switch on")
   if not mt5.symbol_select(symbol, True):
      print("symbol_select({}) failed, exit", symbol)
      mt5.shutdown()
      quit()
   
# prepare the query structure as a dictionary
request = \
{
   "action": mt5.TRADE_ACTION_DEAL,
   "symbol": symbol,
   "volume"1.0,
   "type": mt5.ORDER_TYPE_BUY,
   "price": mt5.symbol_info_tick(symbol).ask,
   "sl": mt5.symbol_info_tick(symbol).ask - 100 * point,
   "tp": mt5.symbol_info_tick(symbol).ask + 100 * point,
   "deviation"10,
   "magic"234000,
   "comment""python script",
   "type_time": mt5.ORDER_TIME_GTC,
   "type_filling": mt5.ORDER_FILLING_RETURN,
}
   
# run the test and display the result as is
result = mt5.order_check(request)
print(result)                       # [?this is not in the help log?]
   
# convert the result to a dictionary and output element by element
result_dict = result._asdict()
for field in result_dict.keys():
   print("   {}={}".format(field, result_dict[field]))
   # if this is the structure of a trade request, then output it element by element as well
   if field == "request":
      traderequest_dict = result_dict[field]._asdict()
      for tradereq_filed in traderequest_dict:
         print("       traderequest: {}={}".format(tradereq_filed,
         traderequest_dict[tradereq_filed]))
   
# terminate the connection to the terminal
mt5.shutdown()

结果:

账户货币: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)

order_send 函数从终端向交易服务器发送一个请求以进行交易操作。

函数的结果作为 OrderSendResult 结构体返回。它重复了 MqlTradeResult 的结构体,但是还包含具有原始请求副本的 request 字段。

该函数类似于 OrderSend

示例 (MQL5/Scripts/MQL5Book/python/ordersend.py):

import time 
import MetaTrader5 as mt5 
   
# let's establish a connection to the MetaTrader 5 terminal
...   
# assign the properties of the working symbol
symbol = "USDJPY"
symbol_info = mt5.symbol_info(symbol)
if symbol_info is None:
   print(symbol, "not found, can not trade")
   mt5.shutdown()
   quit()
   
# if the symbol is not available in the Market Watch, add it
if not symbol_info.visible:
   print(symbol, "is not visible, trying to switch on")
   if not mt5.symbol_select(symbol, True):
      print("symbol_select({}) failed, exit", symbol)
      mt5.shutdown()
      quit()
   
# let's prepare the request structure for the purchase
lot = 0.1
point = mt5.symbol_info(symbol).point
price = mt5.symbol_info_tick(symbol).ask
deviation = 20
request = \

   "action": mt5.TRADE_ACTION_DEAL, 
   "symbol": symbol, 
   "volume": lot, 
   "type": mt5.ORDER_TYPE_BUY, 
   "price": price, 
   "sl": price - 100 * point, 
   "tp": price + 100 * point, 
   "deviation": deviation, 
   "magic"234000
   "comment""python script open"
   "type_time": mt5.ORDER_TIME_GTC, 
   "type_filling": mt5.ORDER_FILLING_RETURN, 
}
   
# send a trade request to open a position
result = mt5.order_send(request)
# check the execution result
print("1. order_send(): by {} {} lots at {}".format(symbol, lot, price));
if result.retcode != mt5.TRADE_RETCODE_DONE:
   print("2. order_send failed, retcode={}".format(result.retcode))
   # request the result as a dictionary and display it element by element
   result_dict = result._asdict()
   for field in result_dict.keys():
      print("   {}={}".format(field, result_dict[field]))
      # if this is the structure of a trade request, then output it element by element as well
      if field == "request":
         traderequest_dict = result_dict[field]._asdict()
         for tradereq_filed in traderequest_dict: 
            print("       traderequest: {}={}".format(tradereq_filed,
            traderequest_dict[tradereq_filed]))
   print("shutdown() and quit")
   mt5.shutdown()
   quit()
   
print("2. order_send done, ", result)
print("   opened position with POSITION_TICKET={}".format(result.order))
print("   sleep 2 seconds before closing position #{}".format(result.order))
time.sleep(2)
# create a request to close 
position_id = result.order
price = mt5.symbol_info_tick(symbol).bid
request = \
{
   "action": mt5.TRADE_ACTION_DEAL, 
   "symbol": symbol, 
   "volume": lot, 
   "type": mt5.ORDER_TYPE_SELL, 
   "position": position_id, 
   "price": price, 
   "deviation": deviation, 
   "magic"234000
   "comment""python script close"
   "type_time": mt5.ORDER_TIME_GTC, 
   "type_filling": mt5.ORDER_FILLING_RETURN, 

# send a trade request to close the position
result = mt5.order_send(request)
# check the execution result
print("3. close position #{}: sell {} {} lots at {}".format(position_id,
symbol, lot, price));
if result.retcode != mt5.TRADE_RETCODE_DONE:
   print("4. order_send failed, retcode={}".format(result.retcode))
   print("   result", result)
else
   print("4. position #{} closed, {}".format(position_id, result))
   # request the result as a dictionary and display it element by element
   result_dict = result._asdict()
   for field in result_dict.keys():
      print("   {}={}".format(field, result_dict[field])) 
      # if this is the structure of a trade request, then output it element by element as well
      if field == "request"
         traderequest_dict = result_dict[field]._asdict()
         for tradereq_filed in traderequest_dict:
            print("       traderequest: {}={}".format(tradereq_filed,
            traderequest_dict[tradereq_filed]))
   
# terminate the connection to the terminal
mt5.shutdown()

结果:

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