1

My code below draws a rectangle based on certain conditions. I want to add another condition for drawing the rectangle. The rectangle is drawn from the open to the high of a bullish candle with index (i+1) as shown in the code. How can I tell if :

  1. price has already reentered the region
  2. price has passed the lower value (bullish candle open) of the region

so that I don’t even draw the rectangle the first place.


int NumOfDisplayBars = 2000;
string prefix="RectRegion";

input bool onlyFreshZones = true;

bool isCandlePattern(int candleIndex, ENUM_TIMEFRAMES cPeriod)  {
   
   
   if ( (iOpen(  _Symbol, cPeriod, candleIndex) > iClose(  _Symbol, cPeriod, candleIndex)) &&
        (iOpen(  _Symbol, cPeriod, candleIndex + 1) < iClose(  _Symbol, cPeriod, candleIndex + 1)) &&
        (iOpen(  _Symbol, cPeriod, candleIndex + 2) > iClose(  _Symbol, cPeriod, candleIndex + 2))
   ){
         
         double upperWickC1 = iHigh(  _Symbol, cPeriod, candleIndex ) - iOpen( _Symbol, cPeriod, candleIndex );
         double bodyC1 = iOpen(  _Symbol, cPeriod, candleIndex ) - iClose( _Symbol, cPeriod, candleIndex );
         double lowerWickC1 = iClose(  _Symbol, cPeriod, candleIndex ) - iLow( _Symbol, cPeriod, candleIndex );
   
         double upperWickC2 = iHigh(  _Symbol, cPeriod, candleIndex + 2) - iOpen( _Symbol, cPeriod, candleIndex + 2);
         double bodyC2 = iOpen(  _Symbol, cPeriod, candleIndex + 2) - iClose( _Symbol, cPeriod, candleIndex + 2);
         double lowerWickC2 = iClose(  _Symbol, cPeriod, candleIndex + 2) - iLow( _Symbol, cPeriod, candleIndex + 2);
   
   
         if ( (iClose(  _Symbol, cPeriod, candleIndex) < iOpen(  _Symbol, cPeriod, candleIndex + 1)) && 
         (iOpen(  _Symbol, cPeriod, candleIndex + 2) > iClose(  _Symbol, cPeriod, candleIndex + 1)) &&
         (bodyC1 > upperWickC1 + lowerWickC1) && (bodyC2 > upperWickC2 + lowerWickC2)
         /*&&
         (iOpen(  _Symbol, cPeriod, candleIndex) > iClose(  _Symbol, cPeriod, candleIndex + 1)) && 
         (iClose(  _Symbol, cPeriod, candleIndex + 2) < iOpen(  _Symbol, cPeriod, candleIndex + 1))*/
         
         ) {
              
             if (onlyFreshZones)  {
            // // finding highest high on timeframe
            double c_candle[];
            ArrayResize(c_candle,candleIndex);

            for(int i=candleIndex-1;i>0;i--)
            {
                c_candle[i]=iHigh(Symbol(),PERIOD_CURRENT,i);
               
            }
            int cC=ArrayMaximum(c_candle,0,WHOLE_ARRAY);
            
            
            if (iOpen(  _Symbol, cPeriod, candleIndex + 1) > iHigh(  _Symbol, cPeriod, cC + 1)) {
               // checking to make sure highest high hasn't passed bull candle's open 
               return true;  
            
            }
            
            
            
            else
            return false;
            }
            
            
            else
            return true;
            
            
            
         }
         
      else
      return false;
   }
   
   
   else
      return false;
      
     
}

void showRectRegion()   {
   
    for (int i=NumOfDisplayBars;i>=1;i--)   {
         
     if (isCandlePattern(i, 0))   {
          drawRectangle(i + 2,iHigh(_Symbol,0,i + 1),iOpen(_Symbol,0,i + 1), 0, clrOrange);
     }
   }
}

bool drawRectangle(int candleInt,const double top,const double bottom, ENUM_TIMEFRAMES cDuration, color rectColor)
{ 
     bool checkBarCount = true;
     int useCurrDuration = PeriodSeconds(cDuration)/PeriodSeconds();   
         
    const datetime starts = iTime(_Symbol,_Period,candleInt);
    
    const datetime ends = starts + useCurrDuration*PeriodSeconds();
    const string name=prefix+"_"+"_"+TimeToString(starts)+TimeToString(ends);
    if(!ObjectCreate(0,name,OBJ_RECTANGLE,0,starts ,top, ends, bottom))
    {
        return false;
    }
  
    
   ObjectSetInteger(0,name,OBJPROP_COLOR, rectColor);
    
   ObjectSetInteger(0,name,OBJPROP_STYLE, STYLE_DASHDOT);

   ObjectSetInteger(0,name,OBJPROP_WIDTH,1);

   ObjectSetInteger(0,name,OBJPROP_FILL, true);
     
    return true;
}

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


void OnTick()
{
   
   showRectRegion();
}

NSSwift
  • 395
  • 1
  • 12
  • get lowest candle in the range i-1 to 0 (if 0 is the last known candle), find its low and compare to target zones – Daniel Kniaz Jul 06 '20 at 15:27
  • @DanielKniaz I am actually looking for bullish candle between two bearish candles based on some extra conditions that will ultimately be a sell trade. Hence I realised I need to rather get the highest high in the range of candles and compare it to my candle in question. I've updated the code with my solution. My code is having issues. It doesn't work consistently at times showing zones that have already past the region and at times it gets it right. Not sure whats causing this behaviour. – NSSwift Jul 07 '20 at 21:28

0 Answers0