you definitely need to read the mql5 docs, and a few scripts/experts codes, which hundreds of them can be found easily.
int all = PositionsTotal();
if(all>0)
{
for(int i=0; i<all; i++)
{
if(PositionGetSymbol(i)!=_Symbol) continue; // optional filter
if(PositionGetInteger(POSITION_MAGIC) != MyMagic) continue; // optional
if(PositionGetString(POSITION_COMMENT) != "BLA BLA") continue;
if(PositionGetDouble(POSITION_PROFIT)>1000000) // don't settle for less that $ 1 Million :)
{
// close position here :
ulong TickeT = (ulong)PositionGetInteger(POSITION_TICKET);
// use standard libraries , or whatever, and close position with the TickeT
}
}
}