MT5编程,有两种方式,一种是函数式编程,一种是面向对象编程。
面向对象编程,会让我们编写代码变得非常简单。
面向对象编程,主要是要熟悉4个类库。
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\OrderInfo.mqh>
其中
PositionInfo 是持仓单信息
OrderInfo 是挂单信息
SymbolInfo 是品种信息
Trade 是交易类库
使用之前,需要定义各种对象的实例
CPositionInfo m_position; // trade position object
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
COrderInfo m_order; // pending orders object
交易之前,在初始化函数里面,要指定一些相应的属性
//---
m_trade.SetExpertMagicNumber(m_magic); //指定魔术号
//---
if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))
m_trade.SetTypeFilling(ORDER_FILLING_FOK);
else
if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))
m_trade.SetTypeFilling(ORDER_FILLING_IOC);
else
m_trade.SetTypeFilling(ORDER_FILLING_RETURN);
//---
m_trade.SetAsyncMode(true); //指定异步模式,有了这条,可瞬时下单上万单
m_trade.SetDeviationInPoints(m_slippage);
下面就不一一介绍了,我给出一个挂单EA的全部源码,可直接下载下去好好学习。
//+------------------------------------------------------------------+
//| EA Stop Order(barabashkakvn's edition).mq5 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Хлыстов Владимир"
#property link "cmillion@narod.ru"
#property version "2.00"
#property description " Advisor opens a grid of stop orders"
#property description " When the specified profit level is reached,"
#property description " it closes all positions and deletes all pending orders"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\OrderInfo.mqh>
CPositionInfo m_position; // trade position object
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
COrderInfo m_order; // pending orders object
//--- input parameters
input double InpProfitClose = 5; // Profit purpose (in money)
input bool InpBuyStop = true; // Use Buy stop
input bool InpSellStop = true; // Use Sell stop
input int InpMaxOrders = 10; // Maximum pending orders
input ushort InpStopLoss = 0; // StopLoss ("0" -> off)
input ushort InpTakeProfit = 0; // TakeProfit ("0" -> off)
input ushort InpDistance = 100; // Distance from current price
input double InpLots = 0.1; // Lots
input double InpLotRatio = 1.5; // Lot Ratio
input ulong m_magic=72994760; // magic number
//---
ulong m_slippage=10; // slippage
double ExtStopLoss=0.0;
double ExtTakeProfit=0.0;
//---
//---
bool m_bln_close_all=false; // "true" -> you must close all positions
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(InpLotRatio<=0.0)
{
string text=__FUNCTION__+", ERROR: "+"The parameter \"Lot Ratio\" can not be less than or equal to zero";


868

被折叠的 条评论
为什么被折叠?



