//+------------------------------------------------------------------+
//|                                            RLF_Fxx_Engulfing.mq4 |
//|                                                      RedLineFred |
//|                                                                  |
//+------------------------------------------------------------------+
/*
Assume that the last 3 candles are x,y,z. 
* Current candle bar is Z.
* Candle prior to that is Y and the one prior to that is X.

First the candle Y has to be checked to see if it has completely 
engulfed the previous candle X's body from open to close to confirm if
its a bullish engulfing or bearish engulfing.

** NOTE : THE WICKS OF THE CANDLE X ARE NOT TO BE CONSIDERED 
WHILE CHECKING TO SEE IF CANDLE Y IS ENGULFING CANDLE X. 
ONLY THE BODY OF THE CANDLE EXCLUDING THE WICKS**

If and only if the body of candle Y has fully engulfed the 
body of the the previous candle X, following information should be displayed.

I need info of the candle bar Y to indicate how many pips it is above 
or below the candle X's high or low. 
Suppose candle X close is 1.30139 and candle Y closed at 1.30199 then 
info should be displayed that the candle Y is 6.0 pips above candle X 
and it should indicate in Green color.

Similarly Suppose candle X close is 1.31553 and candle Y closed at 1.31257 
then info should be displayed that the candle Y is 29.6 pips below candle 
X and it should indicate in Red color.

No other info or popup alert or sound alert is needed. No other indication. 
The info should be displayed in any corner of the chart.


Version 1:
Thanks. This indicator is exactly what I need. There is one small modification 
that I seek. I am not sure if you would be able to implement the same. 
The modification that I seek is : I am naming the candle before X as M to illustrate my point.
If and when a bullish engulf is encountered, I would like this candle Y to 
check and confirm if it engulfs the candle just before X also. 
So when a Y > X is encountered, I would like to know if candle Y engulfs M also. 
(Same conditions as before. Only the body of the candle M not the wicks.)
Similarly, If and when a bearish engulf is encountered, I would like this candle Y 
to check and confirm if it engulfs the candle just before X also. So when a Y < X 
is encountered, I would like to know if candle Y engulfs M also. 
(Same conditions as before. Only the body of the candle M not the wicks.)
A simple text to that effect in the window as is available now. So the text 
should be something like this maybe in two lines if the same cannot be accommodated 
in one single line in the window.

Bullish Engulf Y > X. Double bar engulf Y > M. OR
Bearish Engulf Y < X. Double bar engulf Y < M.

The rest remains the same as before. The candles would be the last 3 x,y,z and 
one new one that is *M*. Not to go further back than the last 4 candles. 

*/


#property copyright "RedLineFred"
#property link      ""
#property indicator_chart_window




int				    LabelPosX				= 20;
int				    LabelPosY				= 20;
extern int 			LabelCorner				= 0;
string			    LabelText				;
extern string		LabelFont				= "Arial";
extern int			LabelFontSize			= 12;
color		        LabelFontColor			;
double   		    multi       			= 10000; 															
datetime 			CurrentTime			    ;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- 
 	if(Point == 0.01 || Point == 0.001)  													
	{
		multi = 100; // must be a JPY pair 													
	}
 
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
    ObjectDelete("Comment");
//----
    return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
//----

    if (Time[0] != CurrentTime) 
   	{
      	CurrentTime = Time[0];
        ObjectDelete("Comment");
   	}
	else 
	{
		return(0);
	}


    if(Close[3] > Open[3]) int m=1; else m=-1;
    if(Close[2] > Open[2]) int x=1; else x=-1;
    if(Close[1] > Open[1]) int y=1; else y=-1;
    
    
    if(y==1 && x==1) 
    {
        if(Close[1] >= Close[2] && Open[1] <= Open[2]) 
        {
            if((Close[1] >= Close[3] && Open[1] <= Open[3] && m==1) || (Close[1] >= Open[3] && Open[1] <= Close[3] && m==-1))
            {
                LabelText = "BullEngulf Y>X (DoubleEngulf Y>M) by "+DoubleToStr((Close[1] - Close[2])*multi,1) +" pips";
            }
            else 
            {
                LabelText = "Bullish Engulf Y>X by "+DoubleToStr((Close[1] - Close[2])*multi,1) +" pips";
            }
            LabelFontColor = Lime;
            label("Comment", LabelPosX, LabelPosY, LabelCorner, LabelText, LabelFontSize, LabelFont, LabelFontColor); 
        }
    }
        
    if(y==-1 && x==-1) 
    {
        if(Close[1] <= Close[2] && Open[1] >= Open[2]) 
        {
            if((Close[1] <= Close[3] && Open[1] >= Open[3] && m==-1) || (Close[1] <= Open[3] && Open[1] >= Close[3] && m==1))
            {
                LabelText = "BearEngulf Y<X (DoubleEngulf Y<M) by "+DoubleToStr((Close[2] - Close[1])*multi,1) +" pips";
            }
            else 
            {
                LabelText = "Bearish Engulf Y<X by "+DoubleToStr((Close[2] - Close[1])*multi,1) +" pips";
            }
            LabelFontColor = Red;
            label("Comment", LabelPosX, LabelPosY, LabelCorner, LabelText, LabelFontSize, LabelFont, LabelFontColor); 
        }
    }
    
    if(y==-1 && x==1) 
    {
        if(Close[1] <= Open[2] && Open[1] >= Close[2]) 
        {
            if((Close[1] <= Close[3] && Open[1] >= Open[3] && m==-1) || (Close[1] <= Open[3] && Open[1] >= Close[3] && m==1))
            {
                LabelText = "BearEngulf Y<X (DoubleEngulf Y<M) by "+DoubleToStr((Close[2] - Close[1])*multi,1) +" pips";
            }
            else 
            {
                LabelText = "Bearish Engulf Y<X by "+DoubleToStr((Close[2] - Close[1])*multi,1) +" pips";
            }
            LabelFontColor = Red;
            label("Comment", LabelPosX, LabelPosY, LabelCorner, LabelText, LabelFontSize, LabelFont, LabelFontColor); 
        }
    }

    if(y==1 && x==-1) 
    {
        if(Close[1] >= Open[2] && Open[1] <= Close[2]) 
        {
            if((Close[1] >= Close[3] && Open[1] <= Open[3] && m==1) || (Close[1] >= Open[3] && Open[1] <= Close[3] && m==-1))
            {
                LabelText = "BullEngulf Y>X (DoubleEngulf Y>M) by "+DoubleToStr((Close[1] - Close[2])*multi,1) +" pips";
            }
            else 
            {
                LabelText = "Bullish Engulf Y>X by "+DoubleToStr((Close[1] - Close[2])*multi,1) +" pips";
            }
            LabelFontColor = Lime;
            label("Comment", LabelPosX, LabelPosY, LabelCorner, LabelText, LabelFontSize, LabelFont, LabelFontColor); 
        }
    }

   
//----
    return(0);
}



//+----------------------------------------------------------------------------+
//| Create a text label.
//+----------------------------------------------------------------------------+
string label(string name, int x, int y, int corner, string text, int argFontSize, string argfont, color argColour) 
{
	if (!IsOptimization()) 
    {
		if (name == "") 
        {
			name = "label_" + Time[0];
		}
		if (ObjectFind(name) == -1) 
        {
			ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
		}
		ObjectSet(name, OBJPROP_COLOR, argColour);
		ObjectSet(name, OBJPROP_CORNER, corner);
		ObjectSet(name, OBJPROP_XDISTANCE, x);
		ObjectSet(name, OBJPROP_YDISTANCE, y);
		ObjectSetText(name, text, argFontSize,argfont);
	}
	return(name);
}


