mql4algorithmic-tradingmetatrader4mt4

How can I solve the duplicates of pending order issues?


My code below places sell pending orders when certain candle patterns are met on the H_1 chart. But duplicate pending orders are created when I change the chart timeframe and return to H_1. Also old orders that should have hit stop loss or take profit seem to still be open. I need to have multiple pending orders, but the duplicates and orders that should have closed are not wanted. How can I solve this?

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

Solution

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

    Well, this is rather a feature of the MQL4/5 code-execution ecosystem.


    Solution:

    Configure a preventive checkmark, setup in MT4-Terminal in Tools > Options > Expert Advisor-tab so as to become:

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