When I want to close only buy or only sell positions in my EAs, I just filter on the position type inside the loop. Usually, I’ll grab each open position, check if it’s a buy or sell using PositionGetInteger(POSITION_TYPE), and then call the close function only when it matches what I want. That way, I don’t touch the other trades, and it’s quick because the function just skips over the wrong side. You can throw in this check right before you call PositionClose on each ticket.
If you're automating this sort of thing or want even more customization, it helps to use something modern like a prop trading API. Makes bulk management, filtering and order handling way smoother without messing with a ton of MQL directly.