Enum as a function parameter - can I check for NULL?

 

Hi, I'm  trying to figure out a way to use ENUM_POSITION_TYPE in function argument in such a way that:

- if function argument is empty then I count all open positions for the symbol

PositionCount();

- if function argument is POSITION_TYPE_BUY then I count only BUY positions for the symbol

PositionCount(POSITION_TYPE_BUY);

- if function argument is POSITION_TYPE_SELL then I count only SELL positions for the symbol

PositionCount(POSITION_TYPE_SELL);

Unfortunately so far no success. I noticed that default NULL value is interpreted as 0 which is POSITION_TYPE_BUY in this case... Any ideas I could make it work?

int PositionCount(ENUM_POSITION_TYPE type = NULL)
  {
   int result = 0;

   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      PositionInfo.SelectByIndex(i);

      if(PositionInfo.Symbol() == _Symbol)
         if(type == NULL)
            result++;
         else
            if(PositionInfo.Type() == type)
               result++;
     }

   return(result);
  }

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I guess I answered my own question... I can just define the function with and without argument. As you can tell I'm a bit rusty with programming...

//+----------------------------------------------------------------------+
//| FOR NO TYPE COUNTS ALL POSITIONS FOR THE SYMBOL                      |
//| WHEN THE TYPE IS GIVEN THEN COUNTS THIS TYPE ONLY (BUY/SELL)         |
//| COUNTS POSITIONS OPENED BY HUMAN AND EA                              |
//+----------------------------------------------------------------------+
int PositionCount(ENUM_POSITION_TYPE type)
  {
   int result = 0;

   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      PositionInfo.SelectByIndex(i);

      if(PositionInfo.Symbol() == _Symbol && PositionInfo.PositionType() == type)
         result++;
     }

   return(result);
  }


int PositionCount()
  {
   int result = 0;

   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      PositionInfo.SelectByIndex(i);

      if(PositionInfo.Symbol() == _Symbol)
         result++;
     }

   return(result);
  }

Or convert enum to int and assign -1 as default... 

int PositionCount(int type = -1)
  {
   int result = 0;

   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      PositionInfo.SelectByIndex(i);

      if(PositionInfo.Symbol() == _Symbol)
         if(type == -1)
            result++;
         else
            if(PositionInfo.PositionType() == type)
               result++;
     }

   return(result);
  }
 
int PositionCount(ENUM_POSITION_TYPE type = NULL)
You can't use NULL, use WRONG_VALUE. Or properly modify the enumerations.
 
William Roeder #:
You can't use NULL, use WRONG_VALUE. Or properly modify the enumerations.
Thanks mate, the WRONG_VALUE is exactly what I needed!

What did you mean by properly modify enumerations? 
 
salvatoreone #: What did you mean by properly modify enumerations? 

enum E{eA, eB, eNULL};