如何解决挂单重复问题?

How can I solve the duplicates of pending order issues?

当 H_1 图表上满足某些蜡烛形态时,我下面的代码会发出卖出挂单。但是,当我将图表时间范围和 return 更改为 H_1 时,会创建重复的挂单。此外,本应触及止损或获利的旧订单似乎仍未平仓。 我需要有多个挂单,但不需要重复的订单和应该关闭的订单。我该如何解决这个问题?

       string prefix = "HG"; 
const  int    N_bars =  1;

       int    numBars = 1;
       int    numBarsArray[];
       int    tempVal = 0;
       int    NumOfDisplayBars = 300;

       int    count = 0;

extern double lotSize = 0.01;
       int    magicnumber = 1337;

void showRectangles()
{
   for (int i=NumOfDisplayBars;i>=1;i--)
   {
       if(isBearishEngulfing(i))
       {
           drawBearRectangle(i + 1,iHigh(_Symbol,0,i + 1),iOpen(_Symbol,0,i + 1));
       }
   }
}

bool isBearishEngulfing(int current)
{   
    if(  (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current    )) 
      && (iClose(_Symbol,0,current + 1) > iOpen( _Symbol,0,current + 1)) 
      && (iOpen( _Symbol,0,current    ) > iClose(_Symbol,0,current + 1)) 
      && (iClose(_Symbol,0,current    ) < iOpen( _Symbol,0,current + 1))
         )
          return true;
    return false;      
}

bool drawBearRectangle(int candleInt,const double top,const double bottom)
{  
    const datetime starts = iTime(_Symbol,0,candleInt); 
    const datetime   ends = starts+PeriodSeconds()*N_bars;
    const   string   name = prefix+"_"+(candleInt>0?"DEMAND":"SUPPLY")+"_"+TimeToString(starts);

    if(!ObjectCreate(0,name,OBJ_RECTANGLE,0,0,0,0,0))
    {
        printf("%i %s: failed to create %s. error=%d",__LINE__,__FILE__,name,_LastError);
        return false;
    }
    ObjectSetInteger(0,name,OBJPROP_TIME1, starts);
    ObjectSetInteger(0,name,OBJPROP_TIME2, ends);
    ObjectSetDouble( 0,name,OBJPROP_PRICE1,bottom);
    ObjectSetDouble( 0,name,OBJPROP_PRICE2,top);
    ObjectSetInteger(0,name,OBJPROP_COLOR, clrChocolate);
    ObjectSetInteger(0,name,OBJPROP_STYLE, STYLE_DASHDOT);
    ObjectSetInteger(0,name,OBJPROP_WIDTH, 1);
    ObjectSetInteger(0,name,OBJPROP_FILL,  false);

    if(_Period == 60){
       double entryPrice=bottom-3*_Point; 
       double stopLoss=top; 
       double slDist=fabs(entryPrice-stopLoss); 
       double dTakeProfit=entryPrice-2*slDist;
       int    ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, entryPrice,0,stopLoss,dTakeProfit,"SellOrder",magicnumber,0,Red);
    }

    return true;
}

void OnDeinit(const int reason){ObjectsDeleteAll(0,prefix);}

void OnTick()
{
    if(!isNewBar())
        return;     // not necessary but waste of time to check every second        
    showRectangles();
}

bool isNewBar()
{
   static datetime lastbar;
          datetime curbar = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(lastbar != curbar)
   {
      lastbar = curbar;
      return true;
   }
   return false;
}

Q : How can I solve the duplicates of pending order issues?
... when I change the chart timeframe and return to H_1.

嗯,这是 MQL4/5 代码执行生态系统的一个特点。


解决方案:

配置预防性复选标记,在 MT4 终端中设置 工具 > 选项 > Expert Advisor-tab 以便成为:

[x] Disable automated trading when the chart symbol or period has been changed