How to prevent EA to open order/s on multiple pairs?

 

Hi all

I want to run my EA on all pairs but to open on one pair only. So for an example, EA is currently running on EUR/USD,GBP/USD,XAU/USD etc. The EA decides to open a trade/s on EUR/USD, my EA will open trade/s on other pairs. Please help on how I can prevent this from happening.

#property strict

extern int MagicNumber=10;

int DelayTime=1;
static datetime NextTradeBuy=0;
static datetime NextTradeSell=0;
 
 int CountSymbol(string Pair)
 {
  int NoSymbol=0;
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Pair)
   NoSymbol++;
  }
  return(NoSymbol);
 }

int CountPenOrders()
 {
  int Pending=0;
  for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
  {
   if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP)
   Pending++;
  }
   
  return(Pending);
 } 
 
double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3){PipPoint=Point*100;}
  else if(Digit==4||Digit==5){PipPoint=Point*10;}
  return(PipPoint);
 } 
 
bool CheckOpenOrders()
 {
  for(int Loop3=OrdersTotal()-1;Loop3>=0;Loop3--)
  {
   if(OrderSelect(Loop3,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol())return(true);
  }
  return(false);
 }  

void Buy()
 {
  double Lowest=iLowest(Symbol(),0,MODE_LOW,10,1);
  double Lowest2=iLowest(Symbol(),0,MODE_LOW,10,2);
  
  if(CheckOpenOrders())
  if(TimeCurrent()>=NextTradeBuy)
  if(CountSymbol(Symbol())==0)
  if(Close[1]>Lowest&&Close[1]<Lowest2)
  {
   int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
  }
  
 }
 
void Sell()
 {
  double Highest=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,10,1));
  double Highest2=iHigh(Symbol(),0,iHighest(Symbol(),0,MODE_HIGH,10,2));

  if(CheckOpenOrders())
  if(TimeCurrent()>=NextTradeSell)
  if(CountSymbol(Symbol())==0)
  if(Close[1]<Highest&&Close[1]>Highest2)
  {
   int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
  }
  
 }  
 
double SpreadInfo()
 {
  double Info=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==2||Digit==3){Info=Point*100;Info=Point*10000;}
  else if(Digit==4||Digit==5){Info=Point*10;Info=Point*1000000;}
  return(Info);
 }  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
 {
  double Spread=MarketInfo(Symbol(),MODE_SPREAD);
  double MaxLot=MarketInfo(Symbol(),MODE_MAXLOT);
  int Leverage=AccountLeverage();
 
  Comment("Spread is "+DoubleToStr(Spread/SpreadInfo(),1)+
  "\nMax Lot For Broker is "+DoubleToStr(MaxLot/1,1)+
  "\nLeverage For Broker is "+DoubleToStr(Leverage/1,1)+
  "\nMagicNumber is "+IntegerToString(MagicNumber/1,1));
  
  double AccBalance=AccountBalance();
  
  int Figures=0;

  if(AccBalance>10)
   {
    Figures=2;
   }
  
  switch(Figures)
  {
   case 2:
   if(CountPenOrders()<1){Buy();}

   if(CountPenOrders()<1){Sell();}

   break;
  }
   
 }
 
Jack Buda:

Hi all

I want to run my EA on all pairs but to open on one pair only. So for an example, EA is currently running on EUR/USD,GBP/USD,XAU/USD etc. The EA decides to open a trade/s on EUR/USD, my EA will open trade/s on other pairs. Please help on how I can prevent this from happening.

You could create a global variable which holds the preferred currency pair - when you open your 1st trade, set the global variable

After that if the EA wants to open a trade, make sure it checks the global variable and only does so if _Symbol matches

 
  1. Jack Buda: I want to run my EA on all pairs but to open on one pair only.
    Stop counting how many orders on the current symbol
      if(CheckOpenOrders())
      if(TimeCurrent()>=NextTradeBuy)
      if(CountSymbol(Symbol())==0)
    And just look at how many total orders are open
    //if(CheckOpenOrders())
      if(TimeCurrent()>=NextTradeBuy)
    //if(CountSymbol(Symbol())==0)
      if(OrdersTotal()==0)
  2. R4tna C #: You could create a global variable which holds the preferred currency pair - when you open your 1st trade, set the global variable
    Totally unnecessary.
 
William Roeder #:
  1. Stop counting how many orders on the current symbol
    And just look at how many total orders are open
  2. Totally unnecessary.
Thank you William, it works now