As we say in the Netherlands,
There are different ways to get to Rome.
Meaning problems can be solved in several ways.
This is why I came up with this to identify engulfing candles:
Code: Select all
//+------------------------------------------------------------------+
//| WhatsThisCandle.mq4 |
//| Copyright 2015, Deltabron |
//| http://www.deltabron.nl |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Deltabron, Paul Geirnaerdt"
#property link "http://www.deltabron.nl"
#property version "1.00"
#property strict
#property indicator_chart_window
enum ENUM_CANDLE_TYPES
{
BULL,
BEAR,
DOJI
};
string candleType[] = { "BULLISH", "BEARISH", "DOJI" };
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
if ( id == CHARTEVENT_CLICK )
{
int x = (int)lparam;
int y = (int)dparam;
datetime dt = 0;
double price = 0;
int window = 0;
if ( ChartXYToTimePrice( 0, x, y, window, dt, price ) )
{
int shift = iBarShift( NULL, 0, dt );
if ( High[shift] > price && price > Low[shift] )
{
// Candle hit!
ENUM_CANDLE_TYPES candle;
Print (candleType[candle], " candle is ", IsEngulfing( shift, candle, 2 ) ? "engulfing" : "NOT engulfing");
}
}
}
}
ENUM_CANDLE_TYPES GetCandleType( int shift )
{
ENUM_CANDLE_TYPES result = DOJI;
if ( Close[shift] > Open[shift] ) result = BULL;
else if ( Close[shift] < Open[shift] ) result = BEAR;
return ( result );
}
bool IsEngulfing( int shift, ENUM_CANDLE_TYPES &candle, int count = 1 )
{
bool result = true;
candle = GetCandleType( shift );
for ( int i = shift + 1; i <= shift + count; i++ )
{
switch ( candle )
{
case BULL:
if ( GetCandleType( i ) == BULL ) result = false;
if ( Open[i] < Open[shift] ) result = false;
else if ( Close[i] > Close[shift] ) result = false;
break;
case BEAR:
if ( GetCandleType( i ) == BEAR ) result = false;
if ( Open[i] > Open[shift] ) result = false;
else if ( Close[i] < Close[shift] ) result = false;
break;
case DOJI:
default:
result = false;
break;
}
}
return ( result );
}
This indicator identifies candles that are clicked on, check the 'Experts' tab. File added to this post.
Ask away if the code is not clear enough.
Enjoy, it is my birthday today.
Paul
You do not have the required permissions to view the files attached to this post.