Too many open orders

 

I just finish de MQL4 book and i was traing to make a simple EA, it didn't go well. I tried to backtest it overnight, with little to no result; then i tried to test it with 5 currencies on a demo account and it opened up to 35 operations per currency. I have check the code several times and have no idea of what could be the problem, can some body give some ideas?

void OnTick()
 {
//---Declaracion de variables
 string Par=Symbol();
 int
  OA=0,
  TicketOA=0,
  TipoOA=-1;
 double
  EMA80=iMA(Par ,PERIOD_CURRENT, 80, 0, MODE_EMA, PRICE_CLOSE, 0),
  EMA10=iMA(Par ,PERIOD_CURRENT, 10, 0, MODE_EMA, PRICE_CLOSE, 0),
  EMA5=iMA(Par ,PERIOD_CURRENT, 5, 0, MODE_EMA, PRICE_CLOSE, 0),
  PrecioOA=0.00,
  LotOA=0.00;
 bool
  Compra=false,
  Venta=false,
  Cerrar_compra=false,
  Cerrar_venta=false,
  Cierre=false,
  Apertura=false;
 int Trend=Tendencia(EMA80, EMA10, EMA5);
 double
  DifMin=DiferenciaMin,
  DifMax=DiferenciaMax,
  DifA=Diferencia10_80(EMA10, EMA80),
  DifB=Diferencia80_10(EMA80, EMA10);
  
//---Loop buscador de ordenes abiertas
 for(int i=1; i<=OrdersTotal(); i++)
  {
  if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES))
   {
   if(OrderSymbol()!=Par)continue;
   if(OrderType()>=2)continue;
   OA++;
   TicketOA=OrderTicket();
   PrecioOA=OrderOpenPrice();
   LotOA=OrderLots();
   break;
   }
  }

//---Criterios de trading
 if(false)Compra=True;
 if(false)Venta=True;
 if(false)Cerrar_compra= True;
 if(false)Cerrar_venta= True;

//---Loop cerrador de ordenes abiertas
 while(true)
  {
  if(Cerrar_compra==true)
   {
   Print("Cerrando compra", TicketOA);
   RefreshRates();
   Cierre=OrderClose(TicketOA, LotOA, PrecioOA, 5, clrBlue);
   if(Cierre==true)Print("Compra", TicketOA, "cerrada");
   Cerrar_compra=false;
   OA=0;
   break;
   }
  if(Cerrar_venta==true)
   {
   Print("Cerrando venta", TicketOA);
   RefreshRates();
   Cierre=OrderClose(TicketOA, LotOA, PrecioOA, 5, clrRed);
   if(Cierre==true)Print("Venta", TicketOA, "cerrada");
   Cerrar_venta=false;
   OA=0;
   break;
   }
  break;
  }

//---Loop abridor de ordenes
 while(true)
  {
  if(OA==0&&Compra==true)
   {
   Print("Comprando", Par);
   RefreshRates();
   Apertura=OrderSend(Par, 0, Lotaje, Ask, 5, NULL, NULL, NULL, 0, 0, clrBlue);
   if(Apertura==true)Print(Par, "Comprado");
   Compra=false;
   break;
   }
  if(OA==0&&Venta==true)
   {
   Print("Vendiendo", Par);
   RefreshRates();
   Apertura=OrderSend(Par, 1, Lotaje, Bid, 5, NULL, NULL, NULL, 0, 0, clrRed);
   if(Apertura==true)Print(Par, "Vendido");
   Venta=false;
   break;
   }
  break;
  }
 return;
 }
 
Yes, there is likely a bug in your code. 
 
Jose Villarello: have no idea of what could be the problem, can some body give some ideas?
  1. Do you check for existing orders, before opening an order, each tick?

  2. Do you check your opening condition once per new bar, or every tick?

  3. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 (2017.12.12)

 
William Roeder:
  1. Do you check for existing orders, before opening an order, each tick?

  2. Do you check your opening condition once per new bar, or every tick?

  3. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 (2017.12.12)

1. yes, or at least i think it does on the section "Loop buscador de ordenes abiertas"

2. every tick

3. I saw the post and your answer, i have tried to adapt it to my code but i cant find the way.