You cannot group a retrieve that easily because it does not let you do a distinct retrieve in that way. However what you can do is create a new entity, if you do not already have that, called instrument type. Instead of each trade having a instrument code you could link it to an instrument type that has the code.
Then when you want to generate your positions for all codes you retrieve all instrument types, which should return a distinct list of your codes and from there you retrieve and agregate the trades connected to each code.
Thanks, Pieter. I solved this along the lines you mention by using the instrument type I already had. However, instead of retrieving all instrument types, I created a list of unique instruments from all the trades. I did this because there are many instruments that have not been traded.