EA works fine sometimes, other times not. Code problem or Broker Problem?

 

Hi there,


this simple Martingail Expert here compiles fine and catches all the trades in backtesting. In Live trading though, it catches maybe about 50% of the intended trades and misses out on the other half.
I traded it live on a single pair for about half a year and it maybe caught 80%. Recently I rewrote the code to work on multiple pairs and the problem seems to be worse.


I originally thought that this IF(Volume[0]==1) condition might be jumped over sometimes, but the EA also skips over the closing routine where no such condition is set.


Or could it be the broker? I did try it on three different brokers though and the problem persists with all of them...


Any suggestions to make this one run 100% would be highly appreciated.


Thanks


BCQ


//+------------------------------------------------------------------+
//|                                    RSIMartingailConservative.mq4 |
//|                                        Copyright © 2018, BCQuant |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, BCQuant"
extern double minstep=190;
extern double period_RSI=20;
extern double zoneBUY=30;
extern double zoneSELL=70;
double lot,n,openprice,profitgoal;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   //Calculate current profit and number of open orders on current pair
   double pairprofit=0;
   int total=0;
   for (int cnt=0; cnt<OrdersTotal();cnt++)
   {
    if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true)
     if (OrderSymbol()==Symbol()) 
      pairprofit+=OrderProfit()+OrderCommission()+OrderSwap();
      total++;
   }
   //Prepare lot size and profitgoal
   
   if(total<2) 
   {
    lot=0.01;
    profitgoal=0.53;
   }
   else
   if(total==2) 
   {
    lot=0.02;
    profitgoal=0.8;
   }
   else
   if(total==3) 
   {
    lot=0.04;
    profitgoal=1.2;
   }
   else
   if(total==4) 
   {
    lot=0.08;
    profitgoal=1.8;
   }
   else
   if(total>4) 
   {
    lot=0.16;
    profitgoal=2.7;
   }
   
   //CloseOpenOrders if Profit
   if(pairprofit>=profitgoal)
     {
      for(int i=0;i<OrdersTotal();i++)
      {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        if(OrderSymbol()==Symbol())
         if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Yellow))
          i--;
      }
     }
     
    //Only start checking open conditions if first tick
    if(Volume[0]==1)
    { 
    //Calculate RSI's
    double RSI0=iRSI(NULL,0,period_RSI,PRICE_CLOSE,1);
    double RSI1=iRSI(NULL,0,period_RSI,PRICE_CLOSE,2);
    
    //Open conditions
    if(total<1)
     {
      if(RSI1<zoneBUY&&RSI0>RSI1)
      {
       n=OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,NULL,0,0,Green);
      }
      if(RSI1>zoneSELL&&RSI0<RSI1)
      {
       n=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,NULL,0,0,Red);
      }
     }
    //Double up conditions
    if (OrderSelect(n,SELECT_BY_TICKET,MODE_TRADES)==true)
     if (OrderSymbol()==Symbol())
      openprice=OrderOpenPrice();
    if(total>0)
     {
      if(OrderType()==OP_BUY)
      {
       if(RSI1<zoneBUY&&RSI0>zoneBUY)
       {
        if((openprice-minstep*Point)>Ask)
        {
         n=OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,NULL,0,0,Green);
        }
       }
      }
      if(OrderType()==OP_SELL)
      {
       if(RSI1>zoneSELL&&RSI0<zoneSELL)
       {
       if ((openprice+minstep*Point)<Bid)
        {
         n=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,NULL,0,0,Red);
        }
       }
      }
     }
    else return(0);
    }   
 return(0);
}