How to search for a candlestick pattern on multiple timeframes

懵懂的女人 提交于 2020-06-21 05:26:06

问题


I have an expert advisor that draws rectangle on a specifically defined bearish pinbar followed by a bullish candle. Please see the code below. It basically shows the rectangle on the timeframe displayed on the chart.

How can I search for this candlestick pattern on timeframes within an H1 candlestick down to M2 in such a way that I can filter the pattern that has the longest bearish pinbar from all timeframes?

string prefix="PBar";
int magicnumber = 12345;

bool drawBearPinbarRectangle(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;
}

bool isBearPinBarType(int candleInt, ENUM_TIMEFRAMES cDuration, double maxLowerWickSize, double maxBodySize)  {

   if (iOpen(  _Symbol, cDuration, candleInt ) > iClose( _Symbol, cDuration, candleInt )) {

   double upperWick = iHigh(  _Symbol, cDuration, candleInt ) - iOpen( _Symbol, cDuration, candleInt );
   double body = iOpen(  _Symbol, cDuration, candleInt ) - iClose( _Symbol, cDuration, candleInt );
   double lowerWick = iClose(  _Symbol, cDuration, candleInt ) - iLow( _Symbol, cDuration, candleInt );

   double totalCandle = upperWick + body + lowerWick;

   if (((lowerWick > 0.0) && (lowerWick <= totalCandle*maxLowerWickSize)) && ((body > 0.0) && (body <= totalCandle*maxBodySize)))
      return true;

   return false; 

   }

   else
      return false;
}


bool isBullPinBarType(int candleInt, ENUM_TIMEFRAMES cDuration, double maxLowerWickSize, double maxBodySize)  {

   if ((iHigh(  _Symbol, cDuration, candleInt ) - iClose( _Symbol, cDuration, candleInt )) > 0) {

   double upperWick = iHigh(  _Symbol, cDuration, candleInt ) - iOpen( _Symbol, cDuration, candleInt );
   double body = iOpen(  _Symbol, cDuration, candleInt ) - iClose( _Symbol, cDuration, candleInt );
   double lowerWick = iClose(  _Symbol, cDuration, candleInt ) - iLow( _Symbol, cDuration, candleInt );

   double totalCandle = upperWick + body + lowerWick;

   if (((lowerWick > 0.0) && (lowerWick <= totalCandle*maxLowerWickSize)) && ((body > 0.0) && (body <= totalCandle*maxBodySize)))
      return true;

   return false; 

   }

   else
      return false;
}

void showPinbarRectOnDispTime() {


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

      double barOpen = iOpen(_Symbol,0,i + 1);
       double barHigh = iHigh(_Symbol,0,i + 1);


     if (isBearPinBarType(i + 2, 0, 0.15, 0.3)
      && 
    (iOpen(_Symbol,0,i + 1) < iClose(_Symbol,0,i + 1))) {

      drawBearPinbarRectangle(i +2,iHigh(_Symbol,0,i + 2),iLow(_Symbol,0,i + 2), 0, clrCyan);


      }
   }

}

bool isBearPinBarWithOpenAndClose(int numCandle, ENUM_TIMEFRAMES cDuration, double maxLowerWickSize, double maxBodySize,
double candleOpen, double candleHigh)  {



   if ((NormalizeDouble((iOpen(  _Symbol, cDuration, numCandle)), 2) == NormalizeDouble(candleOpen, 2)) && 
   (NormalizeDouble((iHigh(  _Symbol, cDuration, numCandle)), 2) == NormalizeDouble(candleHigh, 2)) && 
   ((iHigh(  _Symbol, cDuration, numCandle ) - iClose( _Symbol, cDuration, numCandle )) > 0)) {

   double upperWick = iHigh(  _Symbol, cDuration, numCandle ) - iOpen( _Symbol, cDuration, numCandle );
   double body = iOpen(  _Symbol, cDuration, numCandle ) - iClose( _Symbol, cDuration, numCandle );
   double lowerWick = iClose(  _Symbol, cDuration, numCandle ) - iLow( _Symbol, cDuration, numCandle );

   double totalCandle = upperWick + body + lowerWick;

   if (((lowerWick > 0.0) && (lowerWick <= totalCandle*maxLowerWickSize)) && ((body > 0.0) && (body <= totalCandle*maxBodySize)))
      return true;

   return false; 

   }

   else
      return false;


}

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


void OnTick()
{



     showPinbarRectOnDispTime();

}


回答1:


  1. declare all the necessary timeframes ENUM_TIMEFRAMES tfs[];
  2. fill it in OnInit() then loop over values: for(int i=ArraySize(tfs)-1;i>=0;i--){showPinbarRectOnDispTime(tfs[i]);};
  3. edit the showPinbarRectOnDispTime(ENUM_TIMEFRAMES tf) function: double barOpen = iOpen(_Symbol,tf,i + 1); and so on;
  4. think whether you need just to draw the pin bar rectangles or somehow consume that data without drawing.


来源:https://stackoverflow.com/questions/62479581/how-to-search-for-a-candlestick-pattern-on-multiple-timeframes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!