EA opens trades randomly while tester does well

 

Hi community,

recently started algo trading and creating my own EA.

When I try to test my EA on tester, it works pretty well but when I use it on real pair, something go wild and it opens a trade at random time (the signal is Valid but I made max trades allowed per symbol which doesn't work).



Here's my code on mq4 expert:

void OnTick()
  {
   Comment (Symbol(), ":" , trade_counter());
   double atr     =  iATR(NULL,0,13,1);
   
   double tp      =  0;
   string comment =  "macd";
   int    mn      =  555; 
   int    trade_limit = 1;
   
   double ma      = iMA(NULL,0,5,1,MODE_SMMA,PRICE_WEIGHTED,0);
   
   if(bars_count != Bars)
     {
        if (  trade_counter() < trade_limit) 
        {
         if(macd_signal() == "macd_buy")
         {
         double entry   =  Ask;
         double sl      =  Low[iLowest(NULL,0,MODE_LOW,10,0)] - atr*0;
         buy_order(0.005,entry,5,sl,0,comment,0); 
         }
           
         else if(macd_signal() == "macd_sell")
         {
         double entry   =  Bid;
         double sl      =  High[iHighest(NULL,0,MODE_HIGH,10,0)] - atr*0;
         sell_order(0.005,entry,5,sl,0,comment,0); 
         }
         
        }
     bars_count = Bars;
     }
      order_ticket(); // orders searchs for open trades in order to provide any detail required from the tarde info.          
      if(OrderType() == OP_BUY)
       {
         if(macd_signal() == "macd_sell" )
         {
         order_close(1,Bid);
         }
       } 
      if(OrderType() == OP_SELL)
       {
         if(macd_signal() == "macd_buy" )
         {
         order_close(1,Ask);
         } 
       }
  }
int         trade_counter ()  //Trade counter function
   {
      int counter   = 0;
      
      int order_total = OrdersTotal();
      
      for(int i=order_total;i>=0;i--)
      {
         if(OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))
         {
            if(OrderSymbol() == Symbol())
            {
               counter ++; 
            }
         }
      }
      return (counter);
   }

Would love to hear your thoughts and if you find any issue with my code.


Thanks alot :)

 
      for(int i=order_total;i>=0;i--)
      {
         if(OrderSelect(i,SELECT_BY_TICKET,MODE_TRADES))
         {
            if(OrderSymbol() == Symbol())
            {
               counter ++; 
            }
         }
      }

Select by position, not ticket. You are looping index not tickets.

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:

Select by position, not ticket. You are looping index not tickets.

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

Thanks alot!