How to count how many currency pair(s) running with active order(s) - page 3

 
SanjayBalraj:
  1. What is the purpose for this code? It's very confusing because it looks like the MQL convention used for enum return type.
  2. Why use a separated funtion instead of a nested for loop?
  1. Self documenting code. You asked for a count, an enum is not a count.
  2. Self documenting code. Write general functions when possible. Write once, use many. What do you think symbols.SearchLinear(), symbols.Add(), and symbols.Total() are?
#define INDEX uint // 0 based
#define COUNT uint // 1 based

template<typename T>
bool     resize_vector(T&     vector[],         ///<[in,out]The vector.
                       COUNT  nRows)            /**<[in]Row count.*/{
   return ArrayResize(vector, nRows, extra) >= nRows;
}
/// Adds the given _value_ to the end of the vector.
template<typename T>
bool     push_back(      T&   vector[],         ///<[in,out] The vector.
                   const T&   value,            /**<[in] New elements value.*/{
   INDEX    iEmpty   = ArraySize(vector);
   bool     result   = resize_vector(vector, iEmpty + 1);
   if(result)  vector[iEmpty] = value;
   return result;
}
/** Linear search an _array_ for a specific _value_. @returns INDEX
 * of the first element found with _value_ or _end_ if not found.          */
template<typename Ti1, typename Ti2>
INDEX      find(const Ti1& inp[], INDEX iBeg, INDEX iEnd,
                       const Ti2& value){
   while(iBeg != iEnd && !(inp[iBeg] == value) )   ++iBeg;
   return iBeg;
}
COUNT count_open_symbols(void){
   string symbols[];    COUNT n=0;
   for(int iPos=OrdersTotal()-1; iPos >= 0; --iPos) if(
      OrderSelect(iPos, SELECT_BY_POS)
   {
      string symbol = OrderSymbol();
      if(find(symbols, 0, n, symbol) == n){
         push_back(symbols, OrderSymbol() ); ++n;
   }  }
   return n;
}
 
whroeder1:
  1. Self documenting code. You asked for a count, an enum is not a count.
  2. Self documenting code. Write general functions when possible. Write once, use many. What do you think symbols.SearchLinear(), symbols.Add(), and symbols.Total() are?

Thank you for taking the time with your explanations. I'm still confused why you are adding extra layers of complexity by redefining the data-types. The variable name itself should be the self-documenting component, no?

int count;

versus

COUNT count;

why?

It seems both redundant and confusing... programmer has to remember that "COUNT" is really an "int". I'm still trying to understand what is benefit? It only looks like extra layers of complexity to me and no benefits. I'm sorry for my bad English. I hope that made sense. 

 
int totalpairs()
  {
   int total=0;
   string msymbol="";
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()!=msymbol)
           {
            total++;
            msymbol=OrderSymbol();
           }
     }
   Print("total:",total);
   return(total);
  }

 
kal004551:

int totalpairs()
  {
   int total=0;
   string msymbol="";
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()!=msymbol)
           {
            total++;
            msymbol=OrderSymbol();
           }
     }
   Print("total:",total);
   return(total);
  }

That won't work.

If the loop finds GBPUSD,EURUSD,GBPUSD it will count 3 instead of 2.

 
int totalpairs()
  {
   int total=0;
   string msymbols="";
   for(int i=0; i<OrdersTotal(); i++)
     {
      if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
         if( 0>StringFind(mysymbols,OrderSymbol()) )
           {
            total ++;
            msymbols += OrderSymbol()+";";
           }
     }
   Print("total:",total);
   return(total);
  }

In this way it can works.

 
Fabio Cavalloni:

In this way it can works.

Thank you very much. its works.
 
Fabio Cavalloni #:

In this way it can works.

int totalpairs()
  {
   int total=0;
   string msymbols="";
   for(int i=0; i<OrdersTotal(); i++)
     {
      if( OrderSelect(i,SELECT_BY_POS,MODE_TRADES) )
         if( 0>StringFind(msymbols,OrderSymbol()) )
           {
            total ++;
            msymbols += OrderSymbol()+";";
           }
     }
   Print("total:",total);
   return(total);
  }

Just fixed a little mistake .

Thanks

 
The best way I do it is by starting with an empty array of symbols and each time I find the symbol in the array of symbols tab, I add the new one after the loop, and the answer is in the size of the array.
 
int countPairsWithOrders()
{
    int count = 0;
    string[] symbols;
    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            string symbol = OrderSymbol();
            if (OrderType() == OP_BUY || OrderType() == OP_SELL)
            {
                bool exists = false;
                for (int j = 0; j < ArraySize(symbols); j++)
                {
                    if (symbols[j] == symbol)
                    {
                        exists = true;
                        break;
                    }
                }
                if (!exists)
                {
                    count++;
                    ArrayResize(symbols, ArraySize(symbols) + 1);
                    symbols[ArraySize(symbols) - 1] = symbol;
                }
            }
        }
    }
    return count;
}