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.
Thanks mate, the WRONG_VALUE is exactly what I needed!
What did you mean by properly modify enumerations?
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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
- 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?