2

I want to create a function that can get random profit open position.
For example :

  • a profit of the Last 2 Open Position of overall Total Position

  • a profit of the First 3 Open Position of overall Total Position

Below function seems it only gets the First Open Position Profit :

double BuyProfit()
{
   Price = 0;
   datetime EarliestOrder = TimeCurrent();

   for(int i = 0; i < OrdersTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS))
      {
         if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
            if(EarliestOrder > OrderOpenTime())
            {
               EarliestOrder = OrderOpenTime();
               Price = OrderProfit()+OrderSwap()+OrderCommission();
            }
         }
      }
   }
   return Price;
}
user3666197
  • 1
  • 6
  • 50
  • 92
Roller
  • 35
  • 7

1 Answers1

1

Q : "How to get a profit of Nth open position MQL4/MT4?
I want to create a function that can get random profit open position."

You may take this rapid-prototype to complete your actual logic code:

double nthNetPROFIT( const int Nth ) {
       
       if ( Nth > OrdersTotal() ) return( EMPTY );
       
       int    softCount    = 0;
       double nthNetProfit = EMPTY;,

       for( int ii = 0; ii < OrdersTotal(); ii++ ) {
            if (        OrderSelect( ii, SELECT_BY_POS ) ) {
                  if (  OrderType()        == OP_BUY          // DEPENDS ON ACTUAL LOGIC
                     && OrderSymbol()      == Symbol()        // DEPENDS ON ACTUAL LOGIC
                     && OrderMagicNumber() == MagicNumber     // DEPENDS ON ACTUAL LOGIC
                        ) {
                        ...                                   // DEPENDS ON ACTUAL LOGIC
                        if ( ++softCount == Nth ){
                        nthNetProfit = OrderProfit()
                                     + OrderSwap()
                                     + OrderCommission();
                        break;
                  }
            }
       }
       return( NormalizeDouble( nthNetProfit, 2 ) );
}
user3666197
  • 1
  • 6
  • 50
  • 92