expert advisor work in demo account but freeze in real account after a while

 

Hi every one

I wrote an arbitrage expert advisor which is trade two pairs in same time. expert is working good in demo account but when I change account to real, it s working for some time  and also open positions but after a while it s freezing. restarting the MT4 don't solve the problem. again when i change account from real to demo it works.

 

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file. We can't see your broken code.


Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 

/I don't expect you debug my code. I think its platform problem or broker. is this true: multipair expert adviser is crashes more than single pair؟ if it is true why it works in demo but crash after a while in real account

 
mmarry sadr #: I don't expect you debug my code. I think its platform problem or broker. is this true: multipair expert adviser is crashes more than single pair؟ if it is true why it works in demo but crash after a while in real account

99.9% of the time, it is the code that is the problem and not the platform nor the broker.

But since you did not provide your code, you will have to debug it and figure out the problem on your own.

You also did not mention verifying your log files, so you should do that too.

 

this is my code:

//+------------------------------------------------------------------+
//|                                       Zscore Diff 2 pairs EA.mq4 |
//|                                         Ronaldo Araújo de Farias |
//|                                         ronaldo.a.f@gmail.com    |
//+------------------------------------------------------------------+
#property copyright "mm"
#property link      "http://www.forexfactory.com"
#property version   "2.10"
//--- main parameters
//--- input parameters
extern int       Period_=500;
extern string    Pair2="NZDUSD";
//timeframe 
extern int tf=15;
//pair number eurusd=1,usdchf=2,usdcad=3,gbpusd=4,nzdusd=5,audusd=6
extern int Pairnumber=5;
//volume
 double    lotss=0.56;
 //levels !note the zero level is not acouunted 
 extern string   HighLevelValues="4.5;5.5;6.5";
 extern string    LowLevelValues="-4.5;-5.5;-6.5";
 int       numberofleveles=8;
 extern double    CloseHighAtLevel=0;
 double    CloseLowAtLevel=- CloseHighAtLevel;
 int       Slippage=5;  
 int  MAGICNUMBER;

double High_Level_Value[];
bool High_Level_Actived[];

double Low_Level_Value[];
bool Low_Level_Actived[];

  static datetime timeprev;
int i, nHL, nLL, Instance,x;

string arr[];
string arr1[];
string arrpair[];
string arrtf[]; 
string Current_Level="-";


//--idiff------------------------------------------------------------------------------------------
double iDiff(){
   double s,desv_1,desv_2,MA_1,MA_2,c2;
   int j;
   MA_1 = iMA(Symbol(),0,Period_,0,MODE_SMA,PRICE_CLOSE,0);
   MA_2 = iMA(Pair2,0,Period_,0,MODE_SMA,PRICE_CLOSE,0);  
  
   //---Calculates the sample standard deviation Pair 1 -------
   s=0;
   for(j=0;j<Period_;j++){
      s+=MathPow(MA_1-Close[j],2);      
   }
   desv_1 = MathSqrt(s/(Period_-1));
   if(desv_1 == 0) desv_1 = 0.00001; // divide zero protection
   s=0;
   for(j=0;j<Period_;j++){
      //Close Pair 2
      c2=iClose(Pair2,0,j);
      
      s+=MathPow(MA_2-c2,2);      
   }
   desv_2 = MathSqrt(s/(Period_-1)); 
   if(desv_2 == 0) desv_2 = 0.00001; // divide zero protection
   //----------------
   c2=iClose(Pair2,0,0);
   double Zscore1=(Close[0]-MA_1)/desv_1;     //Pair1
   double Zscore2=(c2-MA_2)/desv_2;           //Pair2  
   return (Zscore1-Zscore2);  
}

//-----long and short-----------------------------------------
int _Long(string pair, color cor, string comment,string level){
   return ( OrderSend(pair,OP_BUY,lotss,MarketInfo(pair,MODE_ASK),Slippage,0,0,comment,MAGICNUMBER,0)   ); 
}

int _Short(string pair, color cor, string comment,string level){
   return ( OrderSend(pair,OP_SELL,lotss,MarketInfo(pair,MODE_BID),Slippage,0,0,comment,MAGICNUMBER,0)   ); 
}

int Buy_And_Sell(string level, double zscore_diff=0){    //Buy first pair and Sell second pair

  
   _Long(Symbol(),Green, "L;" +level +";" +"+" +Pairnumber+"@" +tf+";" + zscore_diff,level );  
      
    _Short(Pair2,Green, "L;" +level +";" +"+" +Pairnumber+"@" +tf+";" + zscore_diff,level);  
  
   
   Current_Level="L"+level;
   
   return(0);
}

int Sell_And_Buy(string level, double zscore_diff=0){   //Sell first pair and Buy second pair
   
   
   _Short(Symbol(),Red, "H;" +level +";" +"+" +Pairnumber+"@" +tf+";" + zscore_diff,level);   
    
    
  _Long(Pair2,Green ,"H;" +level +";" +"+" +Pairnumber+"@" +tf+";" + zscore_diff,level);
   
   Current_Level="H"+level;
   
   return(0);
}

//------checkorder-----------------------------------------------------

void CheckOrders(){
     Current_Level="-";
     
     int max_level=-1;
     // iterate the orders
     for (i=OrdersTotal()-1;i>=0;i--) {
         // select the order
         x=OrderSelect(i,SELECT_BY_POS);
         
         //Read OrderComent and parse fields
         //H or L; Level; Zscore_diff
         
         
         if   (OrderMagicNumber()==MAGICNUMBER )   {
            StringSplit(OrderComment(), StringGetCharacter(";",0) ,  arr );
            if (StrToInteger(arr[1])>max_level) max_level=arr[1];
         }  
        
     }
        
     if (max_level>-1){
         for(i=0;i<=max_level;i++){
            if (arr[0]=="H" ) High_Level_Actived[i]=true;
            if (arr[0]=="L" ) Low_Level_Actived[i]=true;
         }
         Current_Level=arr[0]+max_level;
        
     }

}
//-------------close order----------------------
int Close_All_Orders(){
           
    // while there are open orders...
    while (OrdersTotal() > 0) {
        // iterate the orders
        for (i=OrdersTotal()-1;i>=0;i--) {
            // select the order
            x=OrderSelect(i,SELECT_BY_POS);
            if (OrderMagicNumber()==MAGICNUMBER ) {
               //Read OrderComent and parse fields
               //H or L; Level; Zscore_diff
               StringSplit(OrderComment(), StringGetCharacter("+",0) ,  arrpair );     
                StringSplit(arrpair[1], StringGetCharacter("@",0) ,  arrpair );     
               StringSplit(OrderComment(), StringGetCharacter("@",0) ,  arrtf );       
              StringSplit(arrtf[1], StringGetCharacter(";",0) ,  arrtf );       
       if (arrpair[0]==IntegerToString(Pairnumber)&&(arrtf[0]==IntegerToString(tf) )){
         switch(OrderType()) {
                      case OP_BUY:  x=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol() ,MODE_BID),Slippage); break;
                      case OP_SELL: x=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol() ,MODE_ASK),Slippage); break;
                      default:      x=OrderDelete(OrderTicket());
                  }
                  }                   
            }
        }
        
    }
       
      for(i=0;i< ArraySize(High_Level_Value );i++){
         Low_Level_Actived[i]=false;
         High_Level_Actived[i]=false;
      }
   
  
   return(0);
}


int MagicNumber(){
        string pairs=Symbol()+Pair2;

        int sum=0;
        for (i=0;i<StringLen(pairs);i++){
                sum+=MathPow(13,i)*StringGetChar(pairs, i);
        }

        return( MathMod(sum,32767) );
}


//===================================================================================================================



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   string Hs[], Ls[];
   
   MAGICNUMBER=MagicNumber();
   
   
   nHL=StringSplit(HighLevelValues, StringGetCharacter(";",0) ,  Hs );
   nLL=StringSplit(LowLevelValues, StringGetCharacter(";",0) ,  Ls );
   
  
   
   ArrayResize(High_Level_Value, nHL);
   ArrayResize(Low_Level_Value, nLL);
   
   ArrayResize(High_Level_Actived, nHL);
   ArrayResize(Low_Level_Actived, nLL);
   
   
   for (i=0;i<nHL;i++){
        High_Level_Value[i]=StrToDouble(Hs[i]);
        High_Level_Actived[i]=false;
   }
   
   for (i=0;i<nLL;i++){
        Low_Level_Value[i]=StrToDouble(Ls[i]);
        Low_Level_Actived[i]=false;
   }
   
   CheckOrders();
   

   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete(0,"ObjName");
   

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void OnTick(){


   //insurance miscalculation
    if ( Bars( Symbol(),0 )<Period_ || Bars( Pair2 ,0 )<Period_ ) return(0);
   
   
  
   //Display("Zscore Diff "+ Symbol() + "-" + Pair2 + ": " + NormalizeDouble(Diff,3)  +" | "+ Current_Level); 
   
static datetime timeprev;
if(timeprev==Time[0]) {
return(0); //only execute on new bar
} else if (timeprev==0) {
timeprev=Time[0]; // do nothing if freshly added to chart
return(0);
} else {
  timeprev=Time[0]; 
   double Diff = iDiff();
  
   //double coint =icoint();
    CheckOrders();  
  for(i=0;i<nHL-1;i++){
      if(High_Level_Actived[i]==false){
   
          if(Diff>High_Level_Value[i] &&Diff<High_Level_Value[i+1]){
          
      
            High_Level_Actived[i]=true;
            Sell_And_Buy(i,Diff);
           
         }
      }
    if(Low_Level_Actived[i]==false){
   
           if(Diff<Low_Level_Value[i]&&Diff>Low_Level_Value[i+1] ){
          
            Low_Level_Actived[i]=true;
            Buy_And_Sell(i,Diff);
          
         }
      }    
     if(Diff<=CloseHighAtLevel &&  High_Level_Actived[i]==true){
  Close_All_Orders();

 }
 
 if(Diff>=CloseLowAtLevel  &&  Low_Level_Actived[i]==true){
  Close_All_Orders();
 
 }   
   }
      Comment(
    //  "---------------------------------------------------------" + "\n",
      "Pairs : " + Symbol() + " - " +  Pair2 + "\n",
      "Zsocre: " + Diff +"\n"
    //  "Level : " + Current_Level +"\n",
    //  "Profit: " + Profit_By_Level(-1) + " pips" + "\n",
    //  "---------------------------------------------------------"+ "\n
   
      
      );
    }
   return(0);
  }
//+------------------------------------------------------------------+
 
Most probably an infinite loop in your Close_All_Orders() function. Check your log and debug your code.
 

Your "Your "Close_All_Orders()" will go into an infinite loop and "freeze" if there are any orders that do not qualify by magic number or if it is unable to close those orders ...

int Close_All_Orders(){
    // while there are open orders...
    while (OrdersTotal() > 0) ...

You should not rely on OdersTotal being zero because it will clash with manual trading or other EAs.

Also you are mixing old MQL4 event handlers with the newer MQL4+ handlers. Instead of init() and deinit(), use OnInit() and OnDeinit().

Also use "#property strict" to pick up on more errors and warnings and fix all of of then, including the warnings.

 

Thanks Alain.

Alain Verleyen #:
Most probably an infinite loop in your Close_All_Orders() function. Check your log and debug your code.
 

Thanks Fernando. I will apply your advises and I hope it will be resolved.

Fernando Carreiro #:

Your "Your "Close_All_Orders()" will go into an infinite loop and "freeze" if there are any orders that do not qualify by magic number or if it is unable to close those orders ...

You should not rely on OdersTotal being zero because it will clash with manual trading or other EAs.

Also you are mixing old MQL4 event handlers with the newer MQL4+ handlers. Instead of init() and deinit(), use OnInit() and OnDeinit().

Also use "#property strict" to pick up on more errors and warnings and fix all of of then, including the warnings.

 
mmarry sadr #: Thanks Fernando. I will apply your advises and I hope it will be resolved.

You are welcome!

 
what is your suggestion to remove this infinite loop? 
Reason: