Skip navigation.
Home
Metatrader community - Forex Trading with Metatrader

Hosting multi Expert Advisors in MetaTrader

Hi folks,

Today I'm going to answer frequently asked question which it deserve to be answered!
A lot of people asking:
How could I host more than expert advisor in the same instance of MetaTrader? What should I change in the code to prevent the conflict between the multi expert advisors hosted in MetaTrader?

You'll find the answer here!

How to run more than one EA in MetaTrader?

 

You can load more than one EA for the same currency and the same timeframe by:

1- Opening the charts you want to load your EA on (Figure 1 and 2).

 

 

 

2- Setting the timeframe (Figure 3).

 

3- Attaching the expert advisor to each chart by activating the chart you want to attach the EA on (Activating means clicking the chart window) then from the Navigator window choose the EA and right click to the get the context menu (Figure 4) then choose "attach to a chart" command.

 

I know by asking the above question you know very much the previous steps but they are for the novices, don't be in a hurry! the coming section needs an advanced user of MetaTrader and who can handle MQL4 programs.

 

Why do the expert advisors conflict which each others?

 

Just imagine you have an expert advisor that buying EURUSD when - for instance - the price crosses the EMA13 upward on 1 hour timeframe. There's no problem yet!

You have another EURUSD 15 minutes chart that hosts another expert advisor. The second expert advisor will buying EURUSD when the price crosses - say - EMA24.

Where are the conflicts?

1- The most of the expert advisor checks how many order had been opened and decide to enter the market only of the total of the orders are 0, this is the frequently used code:

total = OrdersTotal();
if(total < 1)
{
//Do trade
}

 

When you host more than expert advisor a code like above will lead to a lot of problems when on of the expert advisors open a trade; the other expert advisor will not work because total opened trades are not 0.

2- If the expert advisors you are hosting use different Trailing Stop values you will get a lot of trouble if you used the normal trailing stop code like that:

// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}

 

3- There is no problem for the normal  Take Profit and Stop Loss levels because you sent them with OrderSend function when you open the trade; but if the expert advisor uses Total Profit/Loss calculation (for example the expert advisor closes all the opened trade when the total of profits of the orders open by the expert advisor is 100 Pips). In this case there will be another conflict.

These were some of the conflicts of hosting more than expert advisors in the same instance of MetaTrader.

 

Form a complaint to a solution:

 

The conflict between the expert advisors occur when the code of them is not "exclusive code".

Exclusive code means you have to write (or modify) you expert advisor to the way they can live with the other expert advisors without problems by distinguishing the orders the expert advisor opens so he can work with them and with them only, at the same time the expert advisor hasn't to use general codes like OrdersTotal() without looping through all the opened orders to examine who opened them.

The best way to distinguish the orders the expert advisor opens is using the MagicNumber.

Review: The Magic Number is one of the parameters (optional parameter) of the OrderSend Function.
OrderSend() is the function you use in MQL4 to open a new order (instant or pending order) and the magic parameter (the ninth parameter ) is an integer value you set as a magic number of the opened order!
The Magic Number is a unique number you assign to your order(s) as a reference enables you to distinguish between the orders that opened by your expert advisor and the orders that opened by another expert advisors or manually.

Let's solve our first code problem, How could an exclusive version of OrdersTotal to get the only count of orders opened by our expert advisor.

 

OrdersTotal exclusive:

 

Let's create a function that returns the count of orders opened by our expert advisor:

int ExOrdersTotal(int MagicNumber)
{
int total = OrdersTotal();
int extotal = 0;
for(int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderMagicNumber()==MagicNumber)
extotal++;
}
return (extotal);
}

The ExOrderTotal is equivalent to OrderTotal function with only one difference; it takes the MagicNumber constant you use in your expert advisor and returns only the count of orders opened by our expert advisor.
The function loop through all the opened orders and select them one by one, then it compares the magic number of the selected order with the MagicNumber constant of your expert advisor and increase the count of orders (extotal variable) in the case the two are identical.

 

How to write an exclusive trailing stop and close order code:

 

Lets create two functions that trail and close only our opened orders:

Trailing stop function:

void TrailOrder(int type)
{
if(TrailingStop>0)
{
if(OrderMagicNumber() == MagicNumber)
{
if(type==OP_BUY)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
}
}
}
if(type==OP_SELL)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
}
}
}
}
}
}

The TrailOrder function use the same technique of ExOrdersTotal function by comparing the MagicNumber constant of the expert advisor against the magic number of the selected order and Trail only the orders that has identical magic numbers.
 

Close orders function:

bool CloseOrder(int type)
{
if(OrderMagicNumber() == MagicNumber)
{
if(type==OP_BUY)
return (OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet));
if(type==OP_SELL)
return (OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet));
}
}

The CloseOrder function closes only the orders opened by the expert advisor (which has an identical magic numbers).

Note: to use the TrailOrder and CloseOrder functions you have to use the normal loop used in the most of expert advisors to select the order you want to trail or close upon the conditions of trailing or closing, like this code:

      

//some code ......
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) //<-- Long position is opened
{
if(CloseBuyCondition) //<-- Close the order and exit!
{
CloseOrder(OrderType());
}
TrailOrder(OrderType()); //<-- Trailling the order
}
if(OrderType()==OP_SELL) //<-- Go to short position
{
if(CloseSellCondition) //<-- Close the order and exit!
{
CloseOrder(OP_SELL);
}
TrailOrder(OrderType()); //<-- Trailling the order
}
}
}
//some code ......

 

How to generate unique MagicNumber:

 

It's very rare to find two expert advisors use the same magic number, the magic is an integer data type which vary from -2147483648 to 2147483647. At the same time you can check the MagicNumber of the expert advisors you want to host in MetaTrader and change them in the code.

But if you want to generate a unique MagicNumber you can use this script.

Run it and copy from the alert window the generated MagicNumber (Figure 5).

 

 

Hope you find it a helpful article and any comments are welcomed!

Coders Guru