Multiple orders

 

Hi programmers, could you let me know if this code prevents opening more than one position simultaneously?

int OnInit()
{  
   EventSetMillisecondTimer(1);
   return(INIT_SUCCEEDED);
}

void OnTimer()
{
   if(!PositionSelect(Symbol()) && conditionToOpenPosition())
   {
      OpenPosition();
   }    
}
 
Francisco Biaso:

Hi programmers, could you let me know if this code prevents opening more than one position simultaneously?

It should work. Have you tried it in the strategy tester?

 
Fernando Morales:

It should work. Have you tried it in the strategy tester?

It is working fine on the tester but I have had some reports of more than one open order simultaneously.

Thank you for share this link!

 

Try this.

#define ALL_SYMBOLS "All Symbols"
#define ALL_MAGICS WRONG_VALUE

uint positionCount(string symbol=ALL_SYMBOLS, int magic=ALL_MAGICS)
{
   if (symbol == NULL)
      symbol = _Symbol;
   uint count = 0;
   for (int i=PositionsTotal()-1; i>=0; --i) {
      string position_symbol = PositionGetSymbol(i);
      if (position_symbol == "")
         continue;
      long position_magic = PositionGetInteger(POSITION_MAGIC);
      if ((symbol == ALL_SYMBOLS || position_symbol == symbol) 
         && (magic == ALL_MAGICS || magic == position_magic)
      ){
         count++;
      }
   }
   return count;
}