特定时间范围内的矩形不会在较低时间范围内扩展

时间:2019-07-24 16:30:19

标签: mql4 algorithmic-trading metatrader4 mt4

我编写了以下代码,为两个输入的时间范围绘制了一个矩形,用于看跌吞没模式。我将默认设置为每天4小时。当我在日线图上时,我期望一只蜡烛的每日矩形应该出现,而当我在4小时图上时,日矩形区域应延伸6根蜡烛,而4小时矩形仅显示一根蜡烛,等等,因为我移到了较短的时间范围。

通常的想法是矩形应延伸以覆盖加总其时间的蜡烛。但这并没有发生,总是只出现一根蜡烛。我该如何解决?这是我的代码如下:

int numBars = 1;

extern ENUM_TIMEFRAMES higherRegionPeriod = PERIOD_D1;
extern ENUM_TIMEFRAMES lowerRegionPeriod = PERIOD_H4;

extern color higherRegionColorSupply = clrRed;
extern color lowerRegionColorSupply = clrChocolate;

bool isBearishEngulfing(int current, ENUM_TIMEFRAMES cDuration)   {


   if((iClose(_Symbol,cDuration,current) < iOpen(_Symbol,cDuration,current)) && 
      (iClose(_Symbol,cDuration,current + 1) > iOpen(_Symbol,cDuration,current + 1))  && 
      (iOpen(_Symbol,cDuration,current) > iClose(_Symbol,cDuration,current + 1)) && 
      (iClose(_Symbol,cDuration,current) < iOpen(_Symbol,cDuration,current + 1)))
         return true;
      return false;  
    }


void showRectangles() {

   for (int i=300;i>=1;i--)   {

if(isBearishEngulfing(i, lowerRegionPeriod)) {

        drawBearRectangle(i + 1,iHigh(_Symbol,lowerRegionPeriod,i + 1),iOpen(_Symbol,lowerRegionPeriod,i + 1), lowerRegionPeriod, lowerRegionColorSupply);
    }

    if(isBearishEngulfing(i, higherRegionPeriod)) {

        drawBearRectangle(i + 1,iHigh(_Symbol,higherRegionPeriod,i + 1),iOpen(_Symbol,higherRegionPeriod,i + 1), higherRegionPeriod, higherRegionColorSupply);
    }
   }
}

bool drawBearRectangle(int candleInt,const double top,const double bottom, ENUM_TIMEFRAMES cDuration, color rectColor)
{


    const datetime starts=iTime(_Symbol,cDuration,candleInt); 
    const datetime ends=starts+PeriodSeconds()*NumBars;
    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, rectColor);


   ObjectSetInteger(0,name,OBJPROP_STYLE, STYLE_DASHDOT);
   ObjectSetInteger(0,name,OBJPROP_WIDTH,1);

   ObjectSetInteger(0,name,OBJPROP_FILL, false);

    return true;
}

void OnDeinit(const int reason){ObjectsDeleteAll(0,prefix);}
void OnTick()
{
    if(!isNewBar(higherRegionPeriod))
        return;     //not necessary but waste of time to check every second

   if(!isNewBar(lowerRegionPeriod))
        return;     //not necessary but waste of time to check every second

showRectangles();
}

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

0 个答案:

没有答案