whats the wrong with this code?? please help

 
hello
im trying to code an expert with multi orders for 1 pair
the idea is opening x amount of orders
and when one or more order closed at the same time, the EA will collect its ticket numbers and deal with each order separately.

i tried arrays and files, even both together
the problem
EA some times duplicate the orders, i dont know why
and some times it over the limit of number of orders

i wish someone help me with this because i really  feel disappoint after one month of searching and trying many codes without any progress

here is the code and thanks in advance

//+------------------------------------------------------------------+
//|                                                           14.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double SStartPoints = 2;

int No_of_Positions = 20;
int History_Order_Ticket[1];
int History_Order_Ticket1[1];
int History_Order_Ticket2[1];
int z_Array[];
int Ticket = 0;
int Temp_Array[];

double Starting_Lot = 0.01;
double StartPoints;
double Pips;
double TickSize; 
double Rnd;
double RRnd;
double Random;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){
   EventSetMillisecondTimer(500);
   TickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if(TickSize == 0.00001 || 0.001){
      Pips = TickSize * 10;
   }else{
      Pips = TickSize;
   }  
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
   EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer(){
   // add random number from 0.1 to 0.9
   Rnd = MathRand() / 3640;
   RRnd = MathRound(Rnd);
   Random = RRnd / 10;
   StartPoints = SStartPoints + Random;
   //-----------------------------------------------
   if(OrdersTotal() == No_of_Positions){
      //free all arrays and collect history orders
      ArrayFree(History_Order_Ticket);
      ArrayFree(History_Order_Ticket1);
      ArrayFree(z_Array);      
      ArrayFree(History_Order_Ticket2);
      if(ArrayResize(History_Order_Ticket, OrdersHistoryTotal()) == OrdersHistoryTotal()){
         for(int a = OrdersHistoryTotal() - 1; a >= 0; a--){
            if(OrderSelect(a, SELECT_BY_POS, MODE_HISTORY)){
               History_Order_Ticket[a] = OrderTicket();
            }else{
               History_Order_Ticket[a] = EMPTY;
            }
         }
      }
      ArrayCopy(History_Order_Ticket1, History_Order_Ticket, 0, 0, WHOLE_ARRAY);
   }else if(OrdersTotal() < No_of_Positions){
      if(ArraySize(History_Order_Ticket1) == 1){
         //if first time of running expert
         if((iMA(NULL,PERIOD_H1,9,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,0))){
            Ticket = OrderSend(Symbol(), OP_BUY, Starting_Lot, Ask, 3, Ask-(StartPoints*Pips), Ask+(StartPoints*Pips),
                               "New B-0.01", 0, 0, clrNONE);
         }else if((iMA(NULL,PERIOD_H1,9,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,0))){
            Ticket = OrderSend(Symbol(), OP_SELL, Starting_Lot, Bid, 3, Bid+(StartPoints*Pips), Bid-(StartPoints*Pips),
                               "New S-0.01", 0, 0, clrNONE);
         }
      }else{
         //if one or more order close
         //collect history orders again
         if(ArrayResize(History_Order_Ticket2, OrdersHistoryTotal()) == OrdersHistoryTotal()){
            for(int b = OrdersHistoryTotal() - 1; b >= 0; b--){
               if(OrderSelect(b, SELECT_BY_POS, MODE_HISTORY)){
                  History_Order_Ticket2[b] = OrderTicket();
               }else{
                  History_Order_Ticket2[b] = EMPTY;
               }
            }
         }
         //z_array = his2 - his1
         //z_array = {closed order1 ticket, closed order2 ticket, closed order3 ticket, etc...}
         int x_Size=ArraySize(History_Order_Ticket1),y_Size=ArraySize(History_Order_Ticket2);
         bool found;
         for(int i=0;i<y_Size;i++){
            found=false;
            for(int j=0;j<x_Size && !found;j++) found=History_Order_Ticket2[i]==History_Order_Ticket1[j];
            if(!found){
               int z_Size=ArraySize(z_Array);
               ArrayResize(z_Array,z_Size+1);
               z_Array[z_Size]=History_Order_Ticket2[i];
            }
         }
      }
      //for each ticket open new order
      for(int i=0; i<ArraySize(z_Array); i++){
         int ss = z_Array[i];
         Ticket = OrderSend(Symbol(), OP_SELL, Starting_Lot, Bid, 3, Bid+(StartPoints*Pips), Bid-(StartPoints*Pips),
                                              "MLOL B-" + DoubleToString(0.01, 2) + " " + IntegerToString(ss),
                                              0, 0, clrNONE);
      }
      ArrayFree(z_Array);
      ArrayFree(History_Order_Ticket2);
   }
   PrintFormat("Copied array History_Order_Ticket1 size=%d",ArraySize(History_Order_Ticket1));
   
   PrintFormat("Copied array History_Order_Ticket2 size=%d",ArraySize(History_Order_Ticket2));
   
   PrintFormat("Copied array z_Array size=%d",ArraySize(z_Array));
   for(int i=0; i<ArraySize(z_Array); i++){
      PrintFormat("index=%d, value=%d",i,z_Array[i]);
   }
}
//+------------------------------------------------------------------+
 
Wessam Nabil:
hello
im trying to code an expert with multi orders for 1 pair
the idea is opening x amount of orders
and when one or more order closed at the same time, the EA will collect its ticket numbers and deal with each order separately.

i tried arrays and files, even both together
the problem
EA some times duplicate the orders, i dont know why
and some times it over the limit of number of orders

i wish someone help me with this because i really  feel disappoint after one month of searching and trying many codes without any progress

here is the code and thanks in advance

You want to get the difference between history1 and history2 and keep it in z_Array, so that for all recently closed orders you can start a new one. But when the timer is called the next time, you might run into the same code, and you obtain the same difference set again. You should update history1 immediately after building the difference in z_Array, like with
ArrayCopy(History_Order_Ticket1, History_Order_Ticket2, 0, 0, WHOLE_ARRAY);

And it looks like you don't need History_Order_Ticket, that is History_Order_Ticket1 and 2 should be sufficient to do the task.

 

lippmaje

thanks for your help:)

yes you are right thats exactly my problem

your solution is working well but in just a few orders 20 may be

but if it become mare orders its start to repeat some tickets number

what if No_of_position is 1000 thats mean 1000 order at the same time 

if 300 closed i need to respond to the 300 one by one however it take time more than expert execution time