0

I have written the following code that draws a rectangle for bearish engulfing patterns for two inputed timeframes. I set the defaults to daily and 4 hours. When I am on the daily chart I expect that only the daily rectangles should appear for one candle and when I am on the 4 hour chart, the daily rectangle region should extend for 6 candles whiles the 4-hour rectangle shows for only one candle, and so on as I move to lower time frames.

The general idea is the rectangle should extend to cover the candles that sum it's period. But that is not happening, only one candle appears always. How can I solve this? Here's my code below:

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;

TenOutOfTen
  • 467
  • 6
  • 14
  • Are there any error messages in the expert tab when drawing the rectangle, because the name of the 00:00th H4 bar and Daily bar could be the same? – TheLastStark Jul 24 '19 at 16:49
  • @TheLastStark I was getting a `error=4200`. I Googled it and found out it was because of a duplicate object as you mentioned. I appended an `int` variable to the object name in `drawBearRectangle` which I increase by one anytime `drawBearRectangle` is called. Not sure whether that's the best way to go about it , but it resolved `error=4200`. Now the rectangles expand occasionally on some pairs but don't on others, they also flicker frequently and expand beyond their region. I'm not sure what's causing all this strange behaviour. – TenOutOfTen Jul 25 '19 at 10:10
  • @TheLastStark Also an H4 rectangle should not show on a D1 chart but it appears, likewise an H1 rectangle should not show on an H4 chart and so on, but this is happening. – TenOutOfTen Jul 25 '19 at 10:26
  • use this as the name `const string name=prefix+"_"+(candleInt>0?"DEMAND":"SUPPLY")+"_"+TimeToString(starts)+TimeToString(ends);` in your method you could be drawing on top of a already drawn one in another iteration – TheLastStark Jul 25 '19 at 11:24
  • @TheLastStark I updated the name with the code you provided but only the rectangle flickering stopped. All the other issues are still there – TenOutOfTen Jul 26 '19 at 13:21
  • @TheLastStark I think maybe calling `isNewBar()` twice in the `OnTick()` and calling `drawBearRectangle()` twice in `showRectangles()` might be causing the issues. – TenOutOfTen Jul 26 '19 at 14:34
  • Yeah, no use in calling that twice, because it's 2 timeframes and only 1 static variable 2 hold the state – TheLastStark Jul 26 '19 at 14:35
  • @TheLastStark how to combine `isNewBar()` to check for two timeframes combine `drawBearRectangle()` to draw rectangles on two timeframes doesn't seem straight forward :) – TenOutOfTen Jul 26 '19 at 14:45
  • use 2 static variables, separate 1 for each time frame – TheLastStark Jul 26 '19 at 15:10

0 Answers0