close all profitable deals in mql5

 

Hi

deals that are initiated on the desktop terminal. Can mql5 EA code close them? or they have to be opened by the EA?

I am thinking to loop over all deals and find which one is profitable and close it, here is a getting started effort...

but the "total" variable failed to show the total deals, prints 1 instead of 4. 4 is the filled orders "deals" I have in the account for this symbol.


Thanks

   uint total=0;
   ulong pos_id=0;
  
  if(PositionSelect("EURAUD")) 
    {
        pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER);
        HistorySelectByPosition(pos_id);
        total=HistoryDealsTotal();
        Print(total);                      // <<<<<<<<<<<<<<<< 1 instead of 4 expected? why
        for(int i = 0; i < total; i++){
          // get the deal, if in profit, close it
        }
    }