ลองแบบนี้นะครับ ยังไม่ได้เทส
while(OrdersTotal()>0)
{
bool result;
int ticket=-1;
double profit=-9999999;
for(int i=0;i<OrdersTotal();i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderProfit()>profit)
{
ticket=OrderTicket();
profit=OrderProfit();
}
}
}
}
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET))
{
RefreshRates();
if(OrderType()==OP_BUY)
result=OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE);
if(OrderType()==OP_SELL)
result=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrNONE);
}
}
}