问题
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:
- declare all the necessary timeframes
ENUM_TIMEFRAMES tfs[];
- fill it in
OnInit()
then loop over values:for(int i=ArraySize(tfs)-1;i>=0;i--){showPinbarRectOnDispTime(tfs[i]);}
; - edit the
showPinbarRectOnDispTime(ENUM_TIMEFRAMES tf)
function:double barOpen = iOpen(_Symbol,tf,i + 1);
and so on; - 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