order_send
order_send
从程序端向交易服务器发送请求来执行交易操作。该函数类似于OrderSend。
order_send(
request // 请求结构
);参数
- request
[in] 描述所需交易操作的MqlTradeRequest类型结构。所需的未命名参数。下面描述了填写请求和枚举内容的示例。
返回值
执行结果以MqlTradeResult结构返回。回答中的request字段包含传递到order_send()的交易请求的结构。可使用last_error()获取错误信息。
MqlTradeRequest交易请求结构
| 字段 | 描述 |
|---|---|
| action | 交易请求类型。该值可以是TRADE_REQUEST_ACTIONS其中一个枚举值 |
| 幻数 | EA ID。可以安排交易订单的分析处理。每个EA交易都可以在发送交易请求时设置一个专有ID |
| 订单 | 订单单号。更改挂单所需 |
| symbol | 要下单的交易品种名称。更改订单和平仓时不需要 |
| volume | 请求的交易量(以手数表示)交易时的真实交易量取决于订单执行类型。 |
| price | 执行订单的价格。对于具有TRADE_ACTION_DEAL类型的“市价执行”(SYMBOL_TRADE_EXECUTION_MARKET)类型的交易品种的市价单,不设置价格 |
| 止损限价 | 当价格达到’price’值时将设置Limit挂单(此条件是强制性的)。在此之前,挂单不会传递到交易系统中 |
| sl | 当价格向不利的方向移动时,激活止损单 |
| tp | 当价格向有利的方向移动时,激活止盈单 |
| 偏差 | 请求价格的最大可接受偏差,在points中指定 |
| 类型 | 订单类型。该值可以是ORDER_TYPE其中一个枚举值 |
| type_filling | 订单成交类型。该值可以是ORDER_TYPE_FILLING其中一个值 |
| type_time | 订单到期类型。该值可以是ORDER_TYPE_TIME其中一个值 |
| 到期 | 挂单到期时间(用于TIME_SPECIFIED类型订单) |
| 评论 | 订单注释 |
| 持仓 | 持仓单号。当更改和关闭持仓时为清楚识别而填写。通常,它与持仓的订单单号相同。 |
| position_by | 反向持仓单号。它用于通过反向持仓平仓时(反向打开一个同名的交易品种)。 |
注意
交易请求在交易服务器上通过多个验证阶段。首先,检查所有必要request字段的有效性。如果没有错误,服务器接受订单以进一步处理。有关执行交易操作的详细信息,请参阅OrderSend函数的描述。
例如:
import time
import MetaTrader5 as mt5
# 显示有关MetaTrader 5程序包的数据
print("MetaTrader5 package author: ", mt5.__author__)
print("MetaTrader5 package version: ", mt5.__version__)
# 建立与MetaTrader 5程序端的连接
if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
# 准备买入请求结构
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()
# 如果市场报价中没有此交易品种,请添加
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()
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,
}
# 发送交易请求
result = mt5.order_send(request)
# 检查执行结果
print("1. order_send(): by {} {} lots at {} with deviation={} points".format(symbol,lot,price,deviation));
if result.retcode != mt5.TRADE_RETCODE_DONE:
print("2. order_send failed, retcode={}".format(result.retcode))
# 请求词典结果并逐个元素显示
result_dict=result._asdict()
for field in result_dict.keys():
print(" {}={}".format(field,result_dict[field]))
# if this is a trading request structure, display 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)
# 创建一个关闭请求
position_id=result.order
price=mt5.symbol_info_tick(symbol).bid
deviation=20
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,
}
# 发送交易请求
result=mt5.order_send(request)
# 检查执行结果
print("3. close position #{}: sell {} {} lots at {} with deviation={} points".format(position_id,symbol,lot,price,deviation));
if result.retcode != mt5.TRADE_RETCODE_DONE:
print("4. order_send failed, retcode={}".format(result.retcode))
print(" result",result)
其他:
print("4. position #{} closed, {}".format(position_id,result))
# 请求词典结果并逐个元素显示
result_dict=result._asdict()
for field in result_dict.keys():
print(" {}={}".format(field,result_dict[field]))
# if this is a trading request structure, display 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]))
# 断开与MetaTrader 5程序端的连接
mt5.shutdown()
结果
MetaTrader5程序包作者:MetaQuotes Software Corp.
MetaTrader5程序包版本:5.0.29
1. order_send(): by USDJPY 0.1 lots at 108.023 with deviation=20 points
2. order_send done, OrderSendResult(retcode=10009, deal=535084512, order=557416535, volume=0.1, price=108.023, ...
opened position with POSITION_TICKET=557416535
sleep 2 seconds before closing position #557416535
3. close position #557416535: sell USDJPY 0.1 lots at 108.018 with deviation=20 points
4. position #557416535 closed, OrderSendResult(retcode=10009, deal=535084631, order=557416654, volume=0.1, price=...
retcode=10009
deal=535084631
order=557416654
volume=0.1
price=108.015
bid=108.015
ask=108.02
comment=Request executed
request_id=55
retcode_external=0
request=TradeRequest(action=1, magic=234000, order=0, symbol='USDJPY', volume=0.1, price=108.018, stoplimit=0.0, ...
traderequest: action=1
traderequest: magic=234000
traderequest: order=0
traderequest: symbol=USDJPY
traderequest: volume=0.1
traderequest: price=108.018
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=557416535
traderequest: position_by=0