How to count the total number of magic numbers created?

 

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)".

 
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
 
WHRoeder:
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] Check your return codes What are Function return values ? How do I use them ? - MQL4 forum




Thanks for reply.

I think your approach will have a duplicate check for each magic number. So it may increases the overall computational costs. Is there any other ways to count the magic numbers created?

 
user0123:


Is there any other ways to count the magic numbers created?

count them as they are created.

you know everytime an order is accepted it must have a magic number.

int magicnumbers=0;

if(OrderSend(,,,,,,,,,,,,,) >0 //order was accepted.

magicnumbers ++;

WHR's code is a better way of doing it for live trading, counting them afterwards in an OrderSelect() loop is more robust. For testing purposes counting them as they are created will be fine.

 

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
     }
  
Reason: