There are few things wrong with the above code.
- The returned data type should be
double
, not int
because the
prices are of double
type.
- You should not iterate to the
OrdersTotal()
. The max index
possible is OrdersTotal()-1
since the orders are numbered in a 0
based way.
- 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.
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(OrderOpenPrice() > highestOpenPrice)highestOpenPrice = OrderOpenPrice();
}
return(highestOpenPrice);
}