English

## How to count the total number of magic numbers created?

 6 user0123 2014.03.02 15:22   In my EA, I always need to have multiple orders sent at the same time when some conditions fulfilled. Then, I use the time at transaction as the magic number for that set of orders. For example, At 2013/02/26 15:00:00, I make orders for EURUSD, USDJPY, and USDCAD. And I assign the same magic number "20130226150000" for them. At 2013/02/26 15:05:17, I make orders for USDCHF, AUDUSD, AUDJPY and EURJPY. And I assign the same magic number "20130226150517" for them. ... the program keep going for an infinite loop, and many magic numbers are created Then, I want to scan through all the magic numbers, and see if a particular SET of orders has a positive profit. If so, closes all the orders within that set. How can I code something like this? ```for (each magicnumber) {    double dblProfit=0;    for(int CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {       if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;       else       if(OrderMagicNumber()==magicnumber)  dblProfit=dblProfit+OrderProfit();    }    if  (dblProfit>0) {       for(int CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {          if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;          else          if(OrderMagicNumber()==magicnumber)  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3);       }    } }``` However, the problem is that I don't know the total number of magic numbers created, and the way to count them in the first line "for (each magicnumber)".
 13116 WHRoeder 2014.03.02 16:47 #   You could create a list of unique number, but even that isn't necessary - Just loop through all orders, and process all MN's per order. (Note orders range [OrdersTotal() - 1 .. 0]```for(int iALL=OrdersTotal() - 1; iALL >= 0; iALL--) if(    OrderSelect(iALL, SELECT_BY_POS) ){    int magicnumber = OrderMagicNumber(); // Remember before inner select loop.    double dblProfit=0;    for(int CO_cnt=OrdersTotal() - 1; CO_cnt>=0;CO_cnt--) {       if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;       else       if(OrderMagicNumber()==magicnumber)  dblProfit=dblProfit+OrderProfit();    }    if  (dblProfit>0) {       for(int CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {          if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;          else          if(OrderMagicNumber()==magicnumber) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);       }    } }  // iALL``` Check your return codes What are Function return values ? How do I use them ? - MQL4 forum
 Moderator3501 GumRai 2014.03.03 04:27 #   I'm not an expert with using arrays, so I thought that I would use this as an exercise The array magnum should hold the different magic numbers that exist in the open orders ```     int magnum[];   int arrsize ;   ArrayResize(magnum,1 );      for(int index=OrdersTotal()-1;index>=0;index--)      {      if(!OrderSelect(index,SELECT_BY_POS,MODE_TRADES) ) continue;      int mn=OrderMagicNumber();      arrsize = ArraySize(magnum);      ArraySort(magnum);      if(magnum[ArrayBsearch(magnum,mn)] != mn)         {         ArrayResize(magnum,arrsize+1 );         magnum[arrsize] = mn;         }           }   arrsize = ArraySize(magnum);   for(int index = 1;index < arrsize; index++)      {      // code for calculations using magic number      }   ```