MQL Connector Library

What is the MQL Connector Library:

The MT2Trading library allows you to send trading signals directly from MQL code (either from a technical indicator or an expert advisor).

Requirements:

In order to use MT2Trading library make sure the library (mt2trading_library for MT4/5) is copied to your MetaTrader terminal’s library folder (MQL4/Libraries or MQL5/Libraries).
Declare the functions in your indicator or EA at the top of your code with #import.

Defines Section


enum broker {
	All = 0,
	IQOption = 1,
	Binary = 2,
	Spectre = 3,
	Alpari = 4,
	InstaBinary = 5,
	OptionField = 6,
	CLMForex = 7,
	DukasCopy = 8,
	GCOption = 9,
	StrategyTester = 10,
	CapitalCore = 11,
	PocketOption = 12,
	Bitness = 13
};
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
};

Library Import Section

MQL4

#import "mt2trading_library.ex4"
	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

MQL5

#import "mt2trading_library.ex5" 
	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

Input Parameters Section

extern broker Broker = All;
input string SignalName = "My Signal";             // Signal Name (optional)
input double TradeAmount = 1;                      // Trade Amount
input int ExpiryMinutes = 5;                       // Expiry Time [minutes]
input martingale MartingaleType = NoMartingale;    // Martingale
input int MartingaleSteps = 2;                     // Martingale Steps
input double MartingaleCoef = 2.0;                 // Martingale Coefficient

datetime signalTime;
string signalID;

OnInit() Section contains important initializations.

  1. signalTime is a timestamp variable in order to prevent sending more than one signal per candle.
  2. signalID is the identification parameter that is used for signing the signal and can be used
    later to receive the trade result with the traderesult(signalID) library function. Also the signalID is used to
    keep the track of martingale
    signal sequence. While OnNextExpiry signals can have a unique id, the OnNextSignal signals should have unique
    id, in order the bot can recognize the signals as one martingale sequence.
  3. asset is the currency pair of the chart. It should consist of 6 characters (e.g. EURUSD).
int OnInit()
{
	//Initialize the time flag
	signalTime = 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() – Section

Example for placing Up/Down trades on Up_Arrow_Condition and Down_Arrow_Condition:

MQL4

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);
}

MQL5

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);
}

Parameters of MT2Trading function:

  1. Asset: Currency pair (6 characters long, e.g. EURUSD).
  2. Direction: “CALL” / “PUT”. A call trade means buy (direction up), a put trade means sell (direction down).
  3. Trade Amount: Amount in your broker account currency.
  4. Expiration time: Expiration time in minutes.
  5. Martingale Type: Define the martingale type that should be applied (ignored, if martingale is not used).
  6. Martingale Steps: Number of martingale steps.
  7. Martingale Coefficient: The coefficient that should be applied for calculation trade amounts for every
    martingale step (ignored, if martingale is not used).
  8. Broker: Define the broker (optional), or all brokers.
  9. Signal Name: Just a name of the signal that will be displayed in the trades table. (optional)
  10. Signal ID: Every trade has own ID that can be used in order to retrieve the trade result later. Also the ID is
    used to build a martingale sequence. All trades of one martingale sequence has same ID.