Using EA on multiple pairs

 

I've made a simple EA which works just fine when using it on a single pair and I've been trying to figure out how to make it work with several pairs at the same time. I just can't get it right :)
How do I make it keep track of which pair it is attached to and do multiple pairs simultaneously?

Thanks,
Rayner

My EA start function is as follows;

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//Set tracking variables
int ticket,cnt,total;
double stoploss;
double margin=AccountFreeMargin();
double macd_value=iMACD(Symbol(),PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
lots=(margin / 10000);

if (OrdersTotal()==1)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if (OrderType()==OP_BUY) in_long=true;
if (OrderType()==OP_SELL) in_short=true;
}
else
{
in_long=false;
in_short=false;
}

//Open first order when EA starts
if (in_long==false)
{
if(in_short==false)
{
int go_long = long_entry(reversal_short(valid_bars_limit));
int go_short = short_entry(reversal_long(valid_bars_limit));
}
}
if (go_long==1)
{
if (macd_value > 0)
{
stoploss=reversal_long(valid_bars_limit);
entry_point_long=Bid;
if (OrdersTotal()==0)
{
OrderSend(Symbol(),OP_BUY,lots, Ask, 3,stoploss, 0,"X-Bar", 12345, 0, Green);
in_long=true;
}
}
}

if (go_short==1)
{
if (macd_value < 0)
{
stoploss=reversal_short(valid_bars_limit)+(Ask-Bid);
entry_point_short=Bid;
if (OrdersTotal()==0)
{
OrderSend(Symbol(),OP_SELL,lots, Bid, 3,stoploss, 0,"X-Bar", 12345, 0, Red);
in_short=true;
}
}
}
//Trail Long order and reverse
if (in_long==true)
{
Comment("Long, Reversal=",reversal_long(valid_bars_limit)+(Ask-Bid));
}
else if (in_short==true)
{
Comment("Short, Reversal=",reversal_short(valid_bars_limit));
}
else
{
Comment("None, Open Long=",reversal_short(valid_bars_limit),", Open Short=",reversal_long(valid_bars_limit));
}


if (in_long==true)
{
total=OrdersTotal();
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
reversal_point_long=OrderStopLoss();
if (reversal_long(valid_bars_limit)>reversal_point_long)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
reversal_point_long=reversal_long(valid_bars_limit);
OrderModify(OrderTicket(),OrderOpenPrice(), reversal_point_long, 0, 0,Green);
}
}
}

//Trail Short order and reverse

if (in_short==true)
{
total=OrdersTotal();
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
reversal_point_short=OrderStopLoss();
if (reversal_short(valid_bars_limit)<reversal_point_short)
{
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
reversal_point_short=reversal_short(valid_bars_limit);
OrderModify(OrderTicket(),OrderOpenPrice(), reversal_point_short, 0,0,Red);
}
}
}



//----
return(0);
}
//+------------------------------------------------------------------+

 
use this code to calculate total of orders to only current symbol
int ordTotal = 0;
for(i = 0;i < OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol() == Symbol())
ordTotal++;
}

use "ordTotal" instead or OrdersTotal() all over your EA. except if you are scanning all orders, like for trailing stop, then use OrdersTotal() and combine it with "OrderSymbol() == Symbol()", Also, for "if" statements, add "Symbol() == OrderSymbol()", for example in your code use "if (in_short==true && Symbol() == OrderSymbol())" instead of just "if (in_short==true)"
you don't need sometimes but add it to make sure you are dealing with the right pair
Reason: