MT5编程--简单的开仓实例 [MT4]
作者:
MT4 来源:
cxh99.com 发布时间:2012年05月21日 点击数:
【
收藏到本网的会员中心】
- 一开始同学们都会被MQL5的开仓平仓搞晕,这里通过编制一个简单的开仓EA 让大家获得概念上的认识。
基础概念:
MQL5里开仓函数还是OrderSend,不过参数变了,它 的参数变成了两个需要提前赋值的“结构”
复制代码-
- bool OrderSend(
- MqlTradeRequest& request // query structure
- MqlTradeResult& result // structure of the answer
- );
参数一:是MqlTradeRequest类型,用于存储开仓平仓以及修改订单所需的各种信息。
复制代码-
- struct MqlTradeResult
- {
- struct MqlTradeRequest
- {
- ENUM_TRADE_REQUEST_ACTIONS action; // Trade operation type
- ulong magic; // Expert ID (magic mumber)
- ulong order; // Order ticket
- string symbol; // Trade symbol
- double volume; // Requested volume for a deal in lots
- double price; // Price
- double stoplimit; // StopLimit price of the order
- double sl; // Stop Loss price of the order
- double tp; // Take Profit price of the order
- ulong deviation; // Maximal possible deviation from the requested price
- ENUM_ORDER_TYPE type; // Order type
- ENUM_ORDER_TYPE_FILLING type_filling; // Order execution type
- ENUM_ORDER_TYPE_TIME type_time; // Order execution time
- datetime expiration; // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
- string comment; // Order comment
- };
参数二:是MqlTradeResult类型,用于存储开仓指令执行完的返回信息。
复制代码-
- struct MqlTradeResult
- {
- uint retcode; // Operation return code
- ulong deal; // Deal ticket, if it has performed
- ulong order; // Order ticket, if it has placed
- double volume; // Deal volume, confirmed by broker
- double price; // Deal price, confirmed by broker
- double bid; // Current Bid price
- double ask; // Current Ask price
- string comment; // Broker comment to operation (by default it filled by the operation description)
- };
因此要开仓前,先要定义好着两个结构变量,然后对MqlTradeRequest变量进行必要的赋值,例如:开仓价 买卖 开仓数量 止损止盈等等。
然后,使用OrderSend语句发送出这个指令。至于是否执行成功,则需要查询执行后的MqlTradeResult结构的返回值。
本例的源代码如下:
复制代码- //+------------------------------------------------------------------+
- //| 1.mq5 |
- //| Copyright 2009, 520FX. |
- //| bbs.520fx.com |
- //+------------------------------------------------------------------+
- #property copyright "2009, 520FX."
- #property link "bbs.520fx.com"
- #property version "1.00"
- /*
- 本程序为简单示意如何在MQL5下开仓
- */
- MqlTradeRequest MyTrade;
- MqlTradeResult MyResult;
- MqlTick MyTick;
- void OnTick()
- {
- SymbolInfoTick(Symbol(),MyTick);
- MyTrade.action=TRADE_ACTION_DEAL;
- MyTrade.magic=123456;
- MyTrade.symbol=Symbol();
- MyTrade.volume=0.1;
- MyTrade.price=MyTick.ask;
- MyTrade.deviation=100;
- MyTrade.type=ORDER_TYPE_BUY;
- MyTrade.type_filling=ORDER_FILLING_AON;
- MyTrade.comment="Test";
- MyTrade.sl=0.0;
- MyTrade.tp=0.0;
- PositionSelect(Symbol(),0);
- double MyVolume=PositionGetDouble(POSITION_VOLUME);
- if(MyVolume<=0.3)
- OrderSend(MyTrade,MyResult);
- Print(MyResult.retcode," ",MyResult.ask," ",SymbolInfoInteger(Symbol(),SYMBOL_DIGITS)," ",MyVolume);
- }
细心的同学可能会注意到本例里还演示了其他几个常用的问题:
1、如何获得当前商品的买卖价格
2、如何获得当前持仓单的具体信息
注意:
程序里这句话
MyTrade.symbol=Symbol();
一定不能使用下面的方式
MyTrade.symbol=NULL;
栽自www.520fx.com