Help ! EA opens an endless amount of Orders? What to do? MT4

 

What can I do? The EA opens an endless Amount of Orders ( and shows "error 1"& "error 130") (important -> MT4)


int total= OrdersTotal();
   LongOrder=0; ShortOrder=0;  //If i delete this line it still opens endless orders
   int freeze_level = (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL);
   for(int Ordercount=0; Ordercount<=total; Ordercount++ ){
      if( !OrderSelect(Ordercount,SELECT_BY_POS,MODE_TRADES) )
         continue;

      if( OrderSymbol()!=Symbol() )
         continue;
      
      if( OrderMagicNumber()!=EA1_MagicNummer )
         continue;
         
         if (total ==0){break;}
         
         
 if( OrderType()== OP_BUY ){
         
         
         //SL for LongOrder
   if (OrderCloseTime()==0 && OrderStopLoss()==0 )//Order still open? No SL?
      { StopLossLong = NormalizeDouble(OrderOpenPrice()/(1+(SL_prozent/100)),Digits);
      if (StopLossLong<freeze_level){StopLossLong=freeze_level;}
        SL_Setzen = OrderModify(OrderTicket(),OrderOpenPrice(),StopLossLong,OrderTakeProfit(),0,Yellow);}
         
         //TP for LongOrder
   if (OrderCloseTime()==0 && OrderTakeProfit()==0 ) //Order Still open ? No TP?
      {
      TakeProfitLong=NormalizeDouble(OrderOpenPrice()*(1+(TP_prozent/100)),Digits);
      if (TakeProfitLong<freeze_level){TakeProfitLong=freeze_level;}
      TP_Setzen=OrderModify(OrderTicket(), OrderOpenPrice(),OrderStopLoss(),TakeProfitLong,0,Orange);
      }
            
        if (OrderCloseTime()>0){LongOrder--;} 
         
            }
            
         
else if( OrderType()== OP_SELL ){
         ShortOrder++;
         
         //SL for ShortOrder
         if (OrderCloseTime()==0 && OrderStopLoss()==0) //Order still open? & No SL?
      { StopLossShort=NormalizeDouble(OrderOpenPrice()*(1+(SL_prozent/100)),Digits);
      if (StopLossShort<freeze_level){StopLossShort=freeze_level;}
      SL_Setzen=OrderModify(OrderTicket(),OrderOpenPrice(),StopLossShort,OrderTakeProfit(),0,Yellow);}
      
      //TP for ShortOrder 
   if (OrderCloseTime()==0 && OrderTakeProfit()==0) //Order still open? & No TP?
      {
     TakeProfitShort=NormalizeDouble(OrderOpenPrice()/(1+(TP_prozent/100)),Digits);
     if (TakeProfitShort<freeze_level){TakeProfitShort=freeze_level;}
     TP_Setzen=OrderModify(OrderTicket(), OrderOpenPrice(),OrderStopLoss(),TakeProfitShort,0,Orange);
      }
   
if (OrderCloseTime()>0){ShortOrder--;}
      }
 

}
//Orders
   //Long
    //if (DayOfWeek()==0 && Hour()<23 && DayOfWeek()==5 && Hour()<23){return(0);}
if (DayOfWeek()==Beginnday && Hour()<BeginndayHour && DayOfWeek()==Endday && Hour()<EnddayHour){return;}
//else if (DayOfWeek()==5 && Hour()>23){return(0);}//lohnt sich nur bei backtesting ab 6 Monaten
else if (Hour()>HandelsbeginnUrhzeit && Hour()<HandelsendeUhrzeit) // 
{
   if(LongOrder==0 && MAsmallhandle>MAbighandle && Bid>=MAsmallhandle)
   {
   LongOrder = OrderSend(Symbol(),OP_BUY,Lot,Bid,10,0,0,"einfach Long",EA1_MagicNummer,0,Blue);
   LongOrder++;
   total++;
   }
   
   
   //Short
 else if(ShortOrder==0 && MAkurzhandle<MAlanghandle && Ask<=MAkurzhandle){
   ShortOrder = OrderSend(Symbol(),OP_SELL,Lot,Ask,10,0,0,"einfach Short",EA1_MagicNummer,0,Red);
   ShortOrder++;
   total++;

  Also interesting: Only 20 Buy Orders ...
 
 for(int i=OrdersTotal()-1 ; i>=0 ; i-- )
 
for(int Ordercount=0; Ordercount<=total; Ordercount++ )

to

for(int Ordercount=0; Ordercount<total; Ordercount++ )

although that doesn't affect the result

 if (OrderCloseTime()==0........ //Not necessary as you are checking open orders

if (OrderCloseTime()>0){ShortOrder--;} Again, you are checking open orders - No point to this check.

Count the long/short orders in your loop. Do not open another is there is already 1 open.