Warm tip: This article is reproduced from serverfault.com, please click

mql4-变量不断重置为初始声明值

(mql4 - Variables keep resetting to initial declared values)

发布于 2021-03-04 19:52:29

想知道是否有人能够指出我在这里做错了什么。

仅作为背景,我对 MQL4 非常陌生(或根本没有编码)

我编写了以下代码来充当虚拟追踪止损。

我想根据以下循环锁定 HH(最高点)和 LL(最低点)。但是,当我打印时,看起来 HH 和 LL 都在每个刻度上重置为买入价和卖出价,而不是“锁定”。

我有三个问题:

  1. 我究竟做错了什么?!
  2. HH 和 LL 是否会因为变量的声明位置而不断重置为零?
  3. 每个订单的 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));
                   }
                }
            }
        }
    }
}

Questioner
Intelis
Viewed
0
tomgny 2021-03-18 21:10:38

HHLL声明OnTick()函数外部的全局变量,以避免在每次滴答时重新初始化它们。