MQL4 - 如何获得最低和较高的订单开盘价

  • 本文关键字:开盘价 何获得 MQL4 mql4
  • 更新时间 :
  • 英文 :


请给我建议如何从现有的未平仓交易头寸中获得最低和最高价格,如下代码所示。

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

谢谢

圣诞颂歌

上面的代码几乎没有问题。

  1. 返回的数据类型应为double,而不是int因为 价格double类型。
  2. 您不应迭代到OrdersTotal()。最大索引 可能OrdersTotal()-1,因为订单编号为 0 基于方式。
  3. 我有点不清楚最高和最低 值应从同一函数返回。如果是这样,你可以 通过使用reference parameters来实现这一点

但是,以下是我编写的代码,用于获得已开仓头寸的最高开盘价。

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

最新更新