MQL 连接器库

在本章中,我们将学习如何使用MT2Trading库直接从MQL代码(指标或专家顾问)发送信号。

要求:

为了使用 MT2Trading 库,请确保将库(MT2trading_mt4/5)复制到 MetaTrader 终端的库文件夹(MQL4/库或 MQL5/库)。

在代码顶部声明指标或 EA 中的函数,#import

定义节
enum broker {
     All = 0,
     IQOption = 1,
     Binary = 2,
     Spectre = 3,
     Alpari = 4
};
enum martingale {
     NoMartingale = 0,
     OnNextExpiry = 1,
     OnNextSignal = 2,
     Anti_OnNextExpiry = 3,
     Anti_OnNextSignal = 4,
     OnNextSignal_Global= 5,
     Anti_OnNextSignal_Global = 6
};

enum result {
     TIE = 0,
     WIN = 1,
     LOSS = 2
};
库导入部分
#import "mt2trading_library.ex4" // Please use only library version 12.4 or higher !!!

bool mt2trading (string symbol, string direction, double amount, int expiryMinutes);
bool mt2trading (string symbol, string direction, double amount, int expiryMinutes, string signalname);
bool mt2trading (string symbol, string direction, double amount, int expiryMinutes,
                 martingale martingaleType, int martingaleSteps, double martingaleCoef, 
                 broker myBroker, string signalName, string signalid);
int traderesult (string signalid);

#import
#import "mt2trading_library.ex5" // Please use only library version 12.4 or higher !!!

bool mt2trading (string symbol, string direction, double amount, int expiryMinutes);
bool mt2trading (string symbol, string direction, double amount, int expiryMinutes, string signalname);
bool mt2trading (string symbol, string direction, double amount, int expiryMinutes,
                 martingale martingaleType, int martingaleSteps, double martingaleCoef, 
                 broker myBroker, string signalName, string signalid);
int traderesult (string signalid);

#import
输入参数部分
extern broker Broker = All;
extern string SignalName = "My Signal";             // Signal Name (optional)
extern double TradeAmount = 1;                      // Trade Amount
extern int ExpiryMinutes = 5;                       // Expiry Time [minutes]
extern martingale MartingaleType = NoMartingale;    // Martingale
extern int MartingaleSteps = 2;                     // Martingale Steps
extern double MartingaleCoef = 2.0;                 // Martingale Coefficient
OnInit() 节包含重要的初始化。
  1. sendOnce是一个时间戳变量,以防止一个蜡烛发送多个信号。
  2. signalID是用于对信号进行签名的标识参数,以后可以使用交易结果(signalID)库函数接收交易结果。 此外,信号ID用于跟踪马丁格尔信号序列。 OnNextExpiry信号可以有一个唯一的id,OnNextSignal信号应该有唯一的id,为了使bot能够识别这些信号为一个马丁格尔序列。
  3. 资产是图表的货币对。 它应由 6 个字符(例如 EURUSD)组成。
int OnInit()
{
     // Initialize the time flag
     sendOnce = TimeCurrent();
     // Generate a unique signal id for signals management (based on timestamp, chart id and some random number)
     MathSrand(GetTickCount());
     if (MartingaleType == OnNextSignal || MartingaleType == Anti_OnNextSignal)
          signalID = IntegerToString(GetTickCount()) + IntegerToString(MathRand()); // indicator-wide id
     else if (MartingaleType = OnNextSignal_Global || MartingaleType == Anti_OnNextSignal_Global)
          signalID = IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN))
                     + IntegerToString(TerminalInfoInteger(TERMINAL_BUILD))
                     + AccountInfoString(ACCOUNT_NAME);   // terminal-wide id
     if (StringLen(Symbol()) >= 6)
          asset = StringSubstr(Symbol(),0,6);
     else
          asset = Symbol();
}
OnCalculate() – 部分

在向上+箭头+条件+向下+箭头+条件上放置向上/向下交易的示例:

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{

// Reinitialization of signalID if no martingale is used or martingale type is OnNextExpiry

if (MartingaleType == NoMartingale || MartingaleType == OnNextExpiry || MartingaleType == Anti_OnNextExpiry)
     signalID = IntegerToString(GetTickCount()) + IntegerToString(MathRand()); // candle-wide unique id

//...

bool Up_Arrow_Condition = ...
bool Down_Arrow_Condition = ...

if (UP_Arrow_Condition && signaltime != time[0])
{
     mt2trading (asset, "CALL", TradeAmount, ExpiryMinutes, MartingaleType, MartingaleSteps,
                 MartingaleCoef, Broker, SignalName, signalID);
     signaltime = time[0];
}

if (DOWN_Arrow_Condition && signaltime != time[0])
{
     mt2trading (asset, "PUT", TradeAmount, ExpiryMinutes, MartingaleType, MartingaleSteps,
                 MartingaleCoef, Broker, SignalName, signalID);
     signaltime = time[0];
}

//...
return(rates_total);
}
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{

// Reinitialization of signalID if no martingale is used or martingale type is OnNextExpiry

if (MartingaleType == NoMartingale || MartingaleType == OnNextExpiry || MartingaleType == Anti_OnNextExpiry)
     signalID = IntegerToString(GetTickCount()) + IntegerToString(MathRand()); // candle-wide unique id

//...

ArraySetAsSeries(time,true); // ---> only in MQL5

bool Up_Arrow_Condition = ...
bool Down_Arrow_Condition = ...

if (UP_Arrow_Condition && signaltime != time[0])
{
     mt2trading (asset, "CALL", TradeAmount, ExpiryMinutes, MartingaleType, MartingaleSteps,
                 MartingaleCoef, Broker, SignalName, signalID);
     signaltime = time[0];
}

if (DOWN_Arrow_Condition && signaltime != time[0])
{
     mt2trading (asset, "PUT", TradeAmount, ExpiryMinutes, MartingaleType, MartingaleSteps,
                 MartingaleCoef, Broker, SignalName, signalID);
     signaltime = time[0];
}

//...
return(rates_total);
}
MT2交易函数的参数:
  1. 资产:货币对(6 个字符长,例如 EURUSD)。
  2. 方向:“买入”/”卖出”。 看涨交易意味着买入(方向向上),看跌交易意味着卖出(方向下跌)。
  3. 交易金额:以您的经纪商账户货币表示的金额。
  4. 过期时间:过期时间(以分钟为单位)
  5. 马丁格尔类型:定义应应用的马丁格尔类型(如果不使用马丁格尔,则忽略)。
  6. 马丁格尔步骤:马丁格尔步数。
  7. 马丁格尔系数:应应用于计算每个马丁格尔步进的交易金额的系数(如果不使用马丁格尔,则忽略)。
  8. 经纪人:定义代理(可选)或所有代理。
  9. 信号名称:只是将在交易表中显示的信号的名称。 (可选)
  10. 信号 ID:每个交易都有自己的 ID,可用于以后检索交易结果。 此外,ID 还用于构建马丁格尔序列。 一个马丁格尔序列的所有交易具有相同的 ID。