Loop for most profitable order

 

Hi guys,

I try to code a function, that always return the orderticket for the order, which is currently the most profitable.

Attached you can find my code, but there seems to be a problem, because if the most profitable order is colsed, the function will still return the same orderticket as most profitable order.

Does anyone have a hint or function for me to help? thx in advance


int HIGHEST_PROFIT_TICKET()
  {
   double highest_profit_value = 0;
   int highest_profit_ticket = 0;

   for(int i = OrdersTotal()-1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
           {
            if(OrderProfit() + OrderSwap() + OrderCommission() > (0 && highest_profit_value))
              {
               highest_profit_value = OrderProfit() + OrderSwap() + OrderCommission();
               highest_profit_ticket = OrderTicket();
              }
           }
        }
     }
   return(highest_profit_ticket);
  };
 
Markus Wilhelm:

Hi guys,

I try to code a function, that always return the orderticket for the order, which is currently the most profitable.

Attached you can find my code, but there seems to be a problem, because if the most profitable order is colsed, the function will still return the same orderticket as most profitable order.

Does anyone have a hint or function for me to help? thx in advance


int HIGHEST_PROFIT_TICKET()
  {
   double highest_profit_value = 0;
   int highest_profit_ticket = 0;

   for(int i = OrdersTotal()-1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
           {
            if((OrderProfit() + OrderSwap() + OrderCommission())>=highest_profit_value)
              {
               highest_profit_value = OrderProfit() + OrderSwap() + OrderCommission();
               highest_profit_ticket = OrderTicket();
              }
           }
        }
     }
   return(highest_profit_ticket);
  };
 
Mehmet Bastem:

doesn't work... he's not able to find the order with the best positive profit.

As soon as the first "most profitable" order is closed, he will not proceed how he should, finding the new "most profitable" order

 
Markus Wilhelm:

doesn't work... he's not able to find the order with the best positive profit.

As soon as the first "most profitable" order is closed, he will not proceed how he should, finding the new "most profitable" order

I've tried BTCUSD, XAUUSD and GBPJPY it works. You want to find the most profitable of all open orders, or only one parity. It is not open. If this is not the solution for you, you can sort with Array. Screenshots are attached.


example
//+------------------------------------------------------------------+
//|                                             HighProfitTicket.mq4 |
//|                                 Copyright 2019, Haskaya Software |
//|                                   https://www.haskayayazilim.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Haskaya Software"
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict
input int MagicNumber=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   HIGHEST_PROFIT_TICKET();
   ExpertRemove();
   
  }
//+------------------------------------------------------------------+

int HIGHEST_PROFIT_TICKET()
  {
   double highest_profit_value = 0;
   int highest_profit_ticket = 0;

   for(int i = OrdersTotal()-1; i >= 0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
        if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType()<2)
         
         
           {
            if((OrderProfit() + OrderSwap() + OrderCommission())>highest_profit_value)
              {
               highest_profit_value = OrderProfit() + OrderSwap() + OrderCommission();
               highest_profit_ticket = OrderTicket();
           
              }
           }
        }
     }
   Print("High Profit Order Ticket is : ",Symbol()+" / ", string(highest_profit_ticket)); 
  
   Alert("High Profit Order Ticket is : ",Symbol()+" / ", string(highest_profit_ticket)); 
   return(highest_profit_ticket);
  }