I think it’s not a unsafe procedure to group/categorize your positions based on comments, since you’re dealing with open positions, and comments of open positions are SUPPOSED to be the same strings you set when sending orders. the unsafe part is of course looking into comments of closed positions.
on your problem :
loop through open positions using a for loop ( loop limit set by PositionsTotal() ) , and using PositionInfoString(), read their comment, and if the comment is the one you’re looking for, (or more safely, if position comment string contains the string you’re looking for ) then check its profit using PositionInfoDouble() function. and close it if conditions are met