想知道是否有人能够指出我在这里做错了什么。
仅作为背景,我对 MQL4 非常陌生(或根本没有编码)
我编写了以下代码来充当虚拟追踪止损。
我想根据以下循环锁定 HH(最高点)和 LL(最低点)。但是,当我打印时,看起来 HH 和 LL 都在每个刻度上重置为买入价和卖出价,而不是“锁定”。
我有三个问题:
(~现在只是占位符 - 可以忽略,我会更正)
非常感谢!
double HH = 0;
double LL = 0;
int orderstotal = OrdersTotal();
int orders = 0;
int ordticket[90][2];
for (int i = 0; i < orderstotal; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol() || OrderMagicNumber() != ~OrderId~)
{
continue;
}
ordticket[orders][0] = OrderOpenTime();
ordticket[orders][1] = OrderTicket();
orders++;
}
if (orders > 1)
{
ArrayResize(ordticket,orders);
ArraySort(ordticket);
}
for (i = 0; i < orders; i++)
{
if (OrderSelect(ordticket[i][1], SELECT_BY_TICKET) == true)
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == ~OrderId~)
{
if (Ask > HH)
{
HH = Ask;
Print("Print(HH)=",HH);
}
if (Bid < LL)
{
LL = Bid;
Print("Print(LL)=",LL);
}
if ((OrderType() == OP_BUY && HH - OrderOpenPrice() > ~TrailingStartGap~*PipValue*Point) ||
(OrderType() == OP_SELL && OrderOpenPrice() - LL > ~TrailingStartGap~*PipValue*Point))
{
if ((OrderType() == OP_BUY && HH - Ask > ~TrailingStop~*PipValue*Point) ||
(OrderType() == OP_SELL && Bid - LL > ~TrailingStop~*PipValue*Point))
{
bool ret = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), ~Slippage~, ~Color~);
if (ret == true)
{
int error = GetLastError();
if (ret == false && error > 0)
Print("OrderClose() error - ", ErrorDescription(error));
}
}
}
}
}
}
将HH和LL声明为OnTick()
函数外部的全局变量,以避免在每次滴答时重新初始化它们。