0
votes

Please give me advice how to get Lowest and Highest price from existing open-trade position like the code below.

int OPLowHigh(string symbol, int dir, int mgc)
{

double HLPrice;

for(int d = 0; d <= OrdersTotal(); d++)
  {
    if(OrderSelect(d,SELECT_BY_POS,MODE_TRADES) == true)
      {
        if(OrderSymbol() == symbol && OrderType() == dir && OrderMagicNumber() == mgc)
          {
            HLPrice = OrderOpenPrice ???????????

          }
      }
  }

return(HLPrice);

}

Thank you very much,

Noel

1

1 Answers

0
votes

There are few things wrong with the above code.

  1. The returned data type should be double, not int because the prices are of double type.
  2. You should not iterate to the OrdersTotal(). The max index possible is OrdersTotal()-1 since the orders are numbered in a 0 based way.
  3. I am a little bit unclear whether both the highest and lowest values should be returned from the same function. If so you can achieve that by using reference parameters

However, the following is the code I wrote for getting the highest open price of the opened positions.

//+------------------------------------------------------------------------+
//| Returns the highest order open price among the opened orders           |
//| Return value -1: there is no position(of symbol,dir,magicNum) available|
//+------------------------------------------------------------------------+
double GetHighestOpenPrice(const string symbol, const int dir, const int magicNum)
{
   int total = OrdersTotal();
   if(total == 0) return(-1);
   double highestOpenPrice = -1;
   for(int i = total - 1; i >= 0; i--)
   {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
      if(OrderCloseTime() != 0) continue;
      if(OrderMagicNumber() != magicNum) continue;
      if(OrderSymbol() != Symbol()) continue;
      if(OrderType() != dir) continue;

      // if the order open price is greater than the previously stored order open price then update it
      if(OrderOpenPrice() > highestOpenPrice)highestOpenPrice = OrderOpenPrice();
   }
   return(highestOpenPrice);
}