open 1 order when EA works at multiple pairs

 

Hello


i would like to add condition to my EA to allow open 1 order only when EA works at multiple pairs .

i have found that script but it doesn't work 

could you please help ?

int totalpairs()
  {
   int total=0;
   string msymbols="";
   for(int i=0; i<OrdersTotal(); i++)
     {
      if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
         if( 0>StringFind(mysymbols,OrderSymbol()) )
           {
            total ++;
            msymbols += OrderSymbol()+";";
           }
     }
   Print("total:",total);
   return(total);
  }
 
Oussama Mansour:

Hello


i would like to add condition to my EA to allow open 1 order only when EA works at multiple pairs .

i have found that script but it doesn't work 

could you please help ?

do you have a magic number on your EA?

control it with a magic number not a symbol ( without symbol )

 
Ahmet Metin Yilmaz:

do you have a magic number on your EA?

control it with a magic number not a symbol ( without symbol )

yes , i did .

how can i do it ?

 
Ahmet Metin Yilmaz:

do you have a magic number on your EA?

control it with a magic number not a symbol ( without symbol )

here is full code 

//+------------------------------------------------------------------+
//|                                                             .mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

#property description " MT4 Expert Advisor "
#property version   "1.00"
#property strict


input string A="========== EA settings ==========";      //========== EA settings ==========
extern bool                 useSL=true;                 
extern int                  StopLoss=300;                // Stop Loss Points
extern bool                 useTP=true;               
extern int                  TakeProfite =300;          // Take profit Points
extern bool                 opp=false;               // close in reverse signal
extern double               LotSize=0.1;           // lot size                              
extern int                  MagicNumber=88952;

input string B2="========== Time & Day filter ==========";      //========== Time & Day filter ========== 
extern bool                 TradingHours=false;
extern int                  Start_Hour=00;
extern int                  Finish_Hour=23;


input bool   Sunday=true,    
             Monday=true, 
             Tuesday=true, 
             Wednesday=true, 
             Thursday=true, 
             Friday=true; 
             
extern string B="========== MT4 Alerts ==========";//========== MT4 Alerts ==========
extern bool                 Email=false;           //Email Notification ?
extern bool                 PopUp=true;           //MT4 Alert ?
extern bool                 Mobile=false;        //Push Notification ?


//----------------

double close;
double minsltp;
double TP,SL; 
double point;
int digits,Q;
double M;
int ThisBarTrade=0;
bool NewBar;
double spreadd;
double spread;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---


    if(Digits==5 || Digits==3)Q=10;  
   else Q=1;
   
   
   if(Digits<4)
     {
      point=0.01;
      digits=2;
     }
   else
     {
      point=0.0001;
      digits=4;
      }
      
      minsltp=(MarketInfo(Symbol(),MODE_SPREAD)+MarketInfo(Symbol(),MODE_STOPLEVEL)+1)/Q;
        
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  
       { 
 if((!Sunday&&DayOfWeek()==0)||(!Monday&&DayOfWeek()==1)||(!Tuesday&&DayOfWeek()==2)|| 
 (!Wednesday&&DayOfWeek()==3)||(!Thursday&&DayOfWeek()==4)||(!Friday&&DayOfWeek()==5))return;  
 //----- 
}
   
    

                    if (iBars(Symbol(),0) != ThisBarTrade ) {
NewBar=true;
ThisBarTrade = iBars(Symbol(),0);  
NewBar=true;  
}

  
   double up=iCustom(NULL,0,"HolySignal",1,1); 
   double down=iCustom(NULL,0,"HolySignal",0,1); 
    double up2=iCustom(NULL,0,"HolySignal",1,3); 
   double down2=iCustom(NULL,0,"HolySignal",0,3);

    if( totalpairs()&&down!=EMPTY_VALUE  && up2==EMPTY_VALUE&& NewBar && orderscnt (OP_SELL)==0&& (AllowTradesByTime()||!TradingHours )){
        if(TakeProfite!=0){TP=Bid-TakeProfite*Point;}else TP=0;
        if(StopLoss!=0){SL=Bid+StopLoss*Point;}else SL=0; 
        double sell= OrderSend(NULL,OP_SELL,LotSize,Bid,5*Q,SL,TP,NULL,MagicNumber,0,clrRed);
        if(Email)SendMail(Symbol(),"New Sell Order @ "+(string)Ask);
        if(PopUp)Alert(Symbol()+" New Sell Order @ "+(string)Ask);
        if(Mobile)SendNotification(Symbol()+" New Sell Order @ "+(string)Ask);
        NewBar=false;
        }
        
        
        
    if(totalpairs() &&up!=EMPTY_VALUE && down2==EMPTY_VALUE&& NewBar && orderscnt (OP_BUY)==0&& (AllowTradesByTime()||!TradingHours ) ){
        if(TakeProfite!=0){TP=Ask+TakeProfite*Point;}else TP=0;
        if(StopLoss!=0){SL=Ask-StopLoss*Point;}else SL=0;
        double buy=OrderSend(NULL,OP_BUY,LotSize,Ask,5*Q,SL,TP,NULL,MagicNumber,0,clrBlue);
        if(Email)SendMail(Symbol(),"New Buy Order @ "+(string)Ask);
        if(PopUp)Alert(Symbol()+" New Buy Order @ "+(string)Ask);
        if(Mobile)SendNotification(Symbol()+" New Buy Order @ "+(string)Ask);
        NewBar=false;
        }
  
   if ( opp &&up!=EMPTY_VALUE && down2==EMPTY_VALUE){
   CloseOrders (OP_SELL);
   }
   
   
   if (opp&&down!=EMPTY_VALUE&& up2==EMPTY_VALUE){
   CloseOrders (OP_BUY);
   }
   
  }
//+------------------------------------------------------------------+


    


 int orderscnt(int tip)
     {
      int cnt=0;
      for(int i=0;i<OrdersTotal();i++)
        {
         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(OrderSymbol()==Symbol() && MagicNumber==OrderMagicNumber()&&OrderType()==tip)
           {
            cnt++;
           }
        }
      return(cnt);
      }
      

//-------------------------------------------------------------------+
    void CloseOrders(int tip) {
         int cnt=OrdersTotal();
         for(int i=cnt-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
            if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber&&OrderType()==tip)
                 {
                 
                     close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),50,clrGreen);   
           }
       }
   



}

//------------------------------------------
 //------------------------------------------



//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Misc function                                                    |
 //***************************//



     //==============
         bool AllowTradesByTime()
     {
      double Current_Time=TimeHour(TimeCurrent());

      if(Start_Hour==0) Start_Hour=24; if(Finish_Hour==0) Finish_Hour=24; if(Current_Time==0) Current_Time=24;

      if(Start_Hour<Finish_Hour)
         if( (Current_Time < Start_Hour) || (Current_Time >= Finish_Hour) ) return(false);

      if(Start_Hour>Finish_Hour)
         if( (Current_Time < Start_Hour) && (Current_Time >= Finish_Hour) ) return(false);

      return(true);
     }
     
    
     
     
//+-----------------------------
int totalpairs()
  {
   int total=0;
   string msymbols="";
   for(int i=0; i<OrdersTotal(); i++)
     {
      if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
         if( 0>StringFind(mysymbols,OrderSymbol()) )
           {
            total ++;
            msymbols += OrderSymbol()+";";
           }
     }
   Print("total:",total);
   return(total);
  }
 
Oussama Mansour:

here is full code 


int totalpairs()
  {
   int total=0;
   string msymbols="";
   for(int i=0; i<OrdersTotal(); i++)
     {
      if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
         if( 0>StringFind(mysymbols,OrderSymbol()) )
           {
            total ++;
            msymbols += OrderSymbol()+";";
           }
     }
   Print("total:",total);
   return(total);
  }


This is calling a sub-routine that doesn't exist:

PrintInfoToChart();



This will throw an error because of the different variable names:

string msymbols="";     if( 0>StringFind(mysymbols,OrderSymbol()) )



Go to here: https://www.mql5.com/en/forum/159398

Prevent EA from opening trades at the same time across 2 or more pairs?
Prevent EA from opening trades at the same time across 2 or more pairs?
  • 2016.06.20
  • www.mql5.com
I've got an EA that runs on many currency pairs/charts, and sometimes 2 or more charts will trigger trades at nearly the exact same time...
 
Max Brown:



This is calling a sub-routine that doesn't exist:



Different variable names:



Go to here: https://www.mql5.com/en/forum/159398

is the code correct ?

 

Oussama Mansour:

i would like to add condition to my EA to allow open 1 order only when EA works at multiple pairs .

i have found that script but it doesn't work 

could you please help ?

  1. So do that. Add a test.

  2. Do you understand what that script does? It doesn't check for 1 order. You could fix it as Max showed, it still won't do what you want

  3. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

 
Ahmet Metin Yilmaz:

do you have a magic number on your EA?

control it with a magic number not a symbol ( without symbol )

true

 
Oussama Mansour:

true

did you find the way :)

 
Ahmet Metin Yilmaz:

did you find the way :)

yes , i have corrected it  .

with magic number .

its a good idea 

 
Oussama Mansour:

yes , i have corrected it by myself .

with magic number .

its a good idea 

good for you

Reason: