MQL4 / MT4 第 N 个未平仓头寸如何获利?

How to get a profit of Nth open position MQL4 / MT4?

我想创建一个可以获取开仓随机利润的函数。
例如:

下面的函数似乎只获得第一个未平仓头寸的利润:

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;
}

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."

您可以用这个rapid-prototype来完成您的实际逻辑代码:

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 ) );
}