How to filter symbols with PositionGetSymbol()

 

Dear Experts,

I am trying to get the list of symbols that has open trades.

the below code does the job but it is getting duplicate symbol name when there are multiple open positions in the same symbol.


I would very much appreciate if you kindly guide me on How we can ask ea to get the list of symbols without duplication!!

        void OnStart() 
         { 
         string position_symbol = "";
         for(int i=PositionsTotal()-1; i>=0 && GetLastError()==0; i--)
         position_symbol += PositionGetSymbol(i)+"\n";
         
         Print(position_symbol+"\nLast error : "+(string)GetLastError());
         }
 
dbmlbm:

Dear Experts,

I am trying to get the list of symbols that has open trades.

the below code does the job but it is getting duplicate symbol name when there are multiple open positions in the same symbol.


I would very much appreciate if you kindly guide me on How we can ask ea to get the list of symbols without duplication!!

To use PositionGetSymbol, you must first select symbol. Also remove the condition: &&GetLastError==0 , it is not necessary here. Use "Continue"  to skip the symbol that you don't want.
 
dbmlbm:

Dear Experts,

I am trying to get the list of symbols that has open trades.

the below code does the job but it is getting duplicate symbol name when there are multiple open positions in the same symbol.


I would very much appreciate if you kindly guide me on How we can ask ea to get the list of symbols without duplication!!

No proper solution. You just need to create a list for symbols. Add every symbol which is not already there. 

 

Create the array of symbols and every time you get a new position in the loop – check if the symbol is already in the array (by anther loop). If it’s not yet in the array – add new symbol to the symbols array, otherwise - continue to next position, So you need to do this manually.

Reason: