abudabi
abudabi

Reputation: 89

MQL4 How to check if last open position in profit

I want to create "Expert advisor" which scaling in\pyramiding\snowballing into trend,

(Another winning position is openning after first one already in breakeven)

Im stuck with function which checks if previous LONG\SHORT open position is already profitable

Seems like my current function always return 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);
    } ``` 


Upvotes: 0

Views: 2587

Answers (1)

abudabi
abudabi

Reputation: 89

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

Upvotes: 1

Related Questions