int PositionTicketMaxProfit(string f_symbol="", // instrument finansowy
                            int    f_cmd=-1,    // typ pozycji
                            int    f_magic=-1)  // identyfikator
  {
   int    f_ticket=-1;
   double f_min=0;
//---
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         int f_type=OrderType();
         if(f_type==OP_BUYLIMIT || f_type==OP_SELLLIMIT ||
            f_type==OP_BUYSTOP  || f_type==OP_SELLSTOP)
            continue;
         //---
         if((f_symbol==OrderSymbol()     || f_symbol=="") && 
            (f_cmd==OrderType()          || f_cmd==-1)    && 
            (f_magic==OrderMagicNumber() || f_magic==-1))
           {
            double f_profit=OrderProfit()+OrderCommission()+OrderSwap();
            if(f_profit>f_min)
              {
               f_min=f_profit;
               f_ticket=OrderTicket();
              }
           }
        }
   return f_ticket;
  }