Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

algorithmic trading - How to search for a candlestick pattern on multiple timeframes

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();

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...