//+------------------------------------------------------------------+
//|                                   FXPT_CandleRangeDifference.mq4 |
//|                                         Developed by fxprotrader |
//|                                     http://www.fxpro-trader.com" |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, fxprotrader"
#property link      "http://www.fxpro-trader.com"
//-------- HISTORY----------------
// v0.1 Initial release(04262012)
//--------------------------------
//coded for himnoor
//http://www.forexfactory.com/showthread.php?p=5607959#post5607959

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_minimum -80
#property indicator_maximum 80
#property indicator_level1 0

#property indicator_levelcolor LimeGreen
#property indicator_levelwidth 2
#property indicator_style1 STYLE_SOLID
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_width1 4
#property indicator_width2 4

#property indicator_color3 Red
#property indicator_color4 Green
#property indicator_width3 4
#property indicator_width4 4

extern string Pair1 = "EURUSD";
extern string Pair2 = "GBPUSD";
extern int mHist = 40, mDiffVal = 5;
extern color mTextColour = White;
int mTime;
//---- Buffers
double TopBufferPos[];
double TopBufferNeg[];
double BotBufferPos[];
double BotBufferNeg[];
//global
double Poin;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

if (Point == 0.00001) Poin = 0.0001; 
else if (Point == 0.001) Poin = 0.01; 
else Poin = Point; 
   IndicatorShortName("");
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexLabel(0,Pair1);
   SetIndexBuffer(0,TopBufferPos);
   SetIndexBuffer(1,TopBufferNeg);
   SetIndexBuffer(2,BotBufferPos);
   SetIndexBuffer(3,BotBufferNeg);
 
//    SetLevelValue(1,250);
   
//    SetIndexLabel(3,Pair2);
//    SetIndexDrawBegin(0,TopBuffer);
//    IndicatorDigits(Digits+2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
DeleteObjects(); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteObjects(){
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--){
      name=ObjectName(cnt);
      if (StringFind(name,"FXPTlbl",0)>-1) ObjectDelete(name);
      WindowRedraw();
   }
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start(){

   double P1H1,P1L1,P1O1,P1C1;
   double P2H1,P2L1,P2O1,P2C1;
   double NumValT1,NumValT2,NumValB1,NumValB2,Difference;  
	string sObjName;
DeleteObjects();
// sObjName = "FXPTlbl_Pair1";
//  ObjectCreate(sObjName, OBJ_TEXT,1, Time[0],40);
//  ObjectSetText(sObjName,Pair1, 18, "Corbal", DimGray);
// 
// sObjName = "FXPTlbl_Pair2";
//  ObjectCreate(sObjName, OBJ_TEXT,1, Time[0],-40);
//  ObjectSetText(sObjName,Pair2, 18, "Corbal", Green);
// 
sObjName = "FXPTlbl_Tpos"+Time[0];
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[5],60);
 ObjectSetText(sObjName,Pair1, 6, "Corbal", mTextColour);
sObjName = "FXPTlbl_Bpos"+Time[0];
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[5],-60);
 ObjectSetText(sObjName,Pair2, 6, "Corbal", mTextColour);

for(int i=1; i < mHist; i++){
  TopBufferPos[i]=EMPTY_VALUE;
  TopBufferNeg[i]=EMPTY_VALUE;
  BotBufferPos[i]=EMPTY_VALUE;
  BotBufferNeg[i]=EMPTY_VALUE;
 
//Pair1  
P1H1= iHigh(Pair1,NULL,i);
P1L1= iLow(Pair1,NULL,i);
P1O1= iOpen(Pair1,NULL,i);
P1C1= iClose(Pair1,NULL,i);
// NumValT1= MathAbs(P1H1-P1L1)/MarketInfo(Pair1,MODE_POINT);
NumValT1= MathAbs(P1O1-P1C1)/Poin;

if(P1O1>P1C1){          
TopBufferPos[i]=NumValT1;
TopBufferNeg[i]=EMPTY_VALUE;

sObjName = "FXPTlbl_Tpos"+Time[i];
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],80);
 ObjectSetText(sObjName,DoubleToStr(NumValT1,0), 8, "Corbal", Red);
}
else{
TopBufferNeg[i]=NumValT1;
TopBufferPos[i]=EMPTY_VALUE;

sObjName = "FXPTlbl_Tneg"+i;
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],80);
 ObjectSetText(sObjName,DoubleToStr(NumValT1,0), 8, "Corbal", Green);
}

//Pair2
P2H1= iHigh(Pair2,NULL,i);
P2L1= iLow(Pair2,NULL,i);
P2O1= iOpen(Pair2,NULL,i);
P2C1= iClose(Pair2,NULL,i);
// NumValB1= MathAbs(P2H1-P2L1)/MarketInfo(Pair2,MODE_POINT);
NumValB1= MathAbs(P2O1-P2C1)/Poin;

if(P2O1>P2C1){   
BotBufferPos[i]= -NumValB1;
BotBufferNeg[i]=EMPTY_VALUE;
sObjName = "FXPTlbl_Bpos"+Time[i];
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],-80);
 ObjectSetText(sObjName,DoubleToStr(NumValB1,0), 8, "Corbal", Red);
}
else{
BotBufferNeg[i]= -NumValB1;
BotBufferPos[i]=EMPTY_VALUE;
sObjName = "FXPTlbl_Bneg"+i;
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],-80);
 ObjectSetText(sObjName,DoubleToStr(NumValB1,0), 8, "Corbal", Green);
}
if(P2O1>P2C1 && P1O1>P1C1){   
Difference=MathAbs(NumValT1-NumValB1);
sObjName = "FXPTlbl_Diff"+i;
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],-90);
 ObjectSetText(sObjName,DoubleToStr(Difference,0), 8, "Corbal", Yellow);
}
else if(P2O1<P2C1 && P1O1>P1C1){   
Difference=MathAbs(NumValT1+NumValB1);
sObjName = "FXPTlbl_Diff"+i;
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],-90);
 ObjectSetText(sObjName,DoubleToStr(Difference,0), 8, "Corbal", Yellow);
}
else if(P2O1<P2C1 && P1O1<P1C1){   
Difference=MathAbs(NumValT1-NumValB1);
sObjName = "FXPTlbl_Diff"+i;
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],-90);
 ObjectSetText(sObjName,DoubleToStr(Difference,0), 8, "Corbal", Yellow);
}
else if(P2O1>P2C1 && P1O1<P1C1){   
Difference=MathAbs(NumValT1+NumValB1);
sObjName = "FXPTlbl_Diff"+i;
 ObjectCreate(sObjName, OBJ_TEXT,1, Time[i],-90);
 ObjectSetText(sObjName,DoubleToStr(Difference,0), 8, "Corbal", Yellow);
}
else{
Difference=0;
}
if(Difference > mDiffVal && Time[0] > mTime && i == 1)
  {
   string mString = "\n" + Pair1 + "\n" + Pair2 + "\n" + DoubleToStr(Difference, 1);
   Alert(TimeHour(TimeCurrent()), ":", TimeMinute(TimeCurrent()), "  Candle difference exceeded", mString);
   SendMail("Candle Difference exceeded", mString);
   mTime = Time[0];
  }
  
}//for
   
//----
   return(0);
  }
//+------------------------------------------------------------------+