MQL4如何检查利润中的最后未平仓头寸

MQL4如何检查利润中的最后未平仓头寸,mql4,metatrader4,forex,Mql4,Metatrader4,Forex,我想创建一个专家顾问,它以\金字塔\滚雪球的方式扩展到趋势中 另一个获胜的位置是在第一个已经处于盈亏平衡的位置之后打开的 我被一个功能卡住了,该功能检查之前的多头/空头空头头寸是否已经盈利 似乎我当前的函数总是返回1 extern double ProfitForOpenAnother = 30; double IsLastLongProfitable(string sy="", int op=OP_BUY) { int LastLongProfitable

我想创建一个专家顾问,它以\金字塔\滚雪球的方式扩展到趋势中

另一个获胜的位置是在第一个已经处于盈亏平衡的位置之后打开的

我被一个功能卡住了,该功能检查之前的多头/空头空头头寸是否已经盈利

似乎我当前的函数总是返回1

      extern double ProfitForOpenAnother = 30;

      double IsLastLongProfitable(string sy="", int op=OP_BUY) {
      int LastLongProfitable = 0;
      datetime o;
      double   l=-1;
      int      i, k=OrdersTotal();

      if (sy=="0") sy=Symbol();
      for (i=0; i<k; i++) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
          if (OrderSymbol()==sy || sy=="") {
            if (OrderType()==OP_BUY) {
              if (op<0 || OrderType()==op) {
                if (OrderMagicNumber()==Magic) {
                  if (o<OrderOpenTime()) {
                    o=OrderOpenTime();
                    l=OrderProfit();
                    if(l>ProfitForOpenAnother)
                    {
                      LastLongProfitable=1;
                    }
                  }
                }
              }
            }
          }
        }
      }
      return(LastLongProfitable);
    } ``` 


double profit_buy=0,profit_sell=0;
for(int i=OrdersTotal()-1; i>=0; i--)
  {
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
     {
      datetime time_order=OrderOpenTime();
      double profit_order=OrderProfit()-OrderCommission()+OrderSwap();
      if(OrderType()==OP_BUY && time_buy<time_order)
        {
         time_buy=time_order;
         profit_buy=profit_order;
        }
      if(OrderType()==OP_SELL && time_sell<time_order)
        {
         time_sell=time_order;
         profit_sell=profit_order;
        }
     }
  }```