//+------------------------------------------------------------------+
//|                                            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.
*/


#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[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]) 
        {
            LabelText = "Close Y > Close 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]) 
        {
            LabelText = "Close Y < Close 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]) // bearish engulfing
        {
            LabelText = "Close Y < Close 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]) // bullish engulfing
        {
            LabelText = "Close Y > Close 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);
}


