//+------------------------------------------------------------------+
//|                                   Previous Day Close Diff v1.mq4 |
//|                                        Copyright © 2011, tigpips |
//|                                                tigpips@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, tigpips"
#property link      "tigpips@gmail.com"

#property indicator_buffers 3
#property indicator_chart_window
#property indicator_color1 Magenta       
#property indicator_color2 Aqua
#property indicator_color3 Red         
#property indicator_width2 3  
#property indicator_width3 3  


extern string FirstPair = "EURUSD";
extern string SecondPair = "AUDUSD";
extern int Start_Hour = 22;
extern int Start_Min = 0;
extern int DisplayDistance = 10;
extern int DisplayOffset = 0;
extern bool UseBarCount = false;
extern int BarCount = 10000;
extern int MA_Period = 20;
extern int MA_ma_shift = 0;
extern int MA_ma_method = 3;
extern int MA_ma_Applied_Price = 0;
extern int MA_shift = 0;
extern color TextColor = Yellow;
extern int font_size = 12;
extern color ProfitColor = Green;
extern color LossColor = Red;
extern color NoProfitNoLossColor = White;
extern int myXoffset = 0;
extern int myYoffset = 0;
extern string Label_font_type = "Arial Bold";
extern int Label_font_size = 10;
extern int Front_Label_font_size = 16;
extern color Front_Label_font_color = Aqua;

double cal_percent_firstpair;
double cal_percent_secondpair;
double firstpair_prev_day_close;
double secondpair_prev_day_close;

double LWMovingAverage[];
double TopDiffBuffer[];    
double BottomDiffBuffer[];
string shortname;
datetime lastAlert;

double FirstPairCurrentGainLoss;
double SecondPairCurrentGainLoss;
double DiffGainLoss;
int digits1,digits2;
int ObjectIdx;
string lbl_obj_name,lbl_obj_name2;
int init()
{
   if(Digits==5 || Digits == 3)
   {
      DisplayDistance = DisplayDistance * 10;
   }
   digits1=MarketInfo(FirstPair,MODE_DIGITS);
   digits2=MarketInfo(SecondPair,MODE_DIGITS);
   
   SetCalculatePercent();
   shortname = "PDCD";
   IndicatorShortName(shortname);

   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(0, LWMovingAverage);
   SetIndexDrawBegin(0, MA_Period);
   SetIndexLabel(0, "MA");
   
   SetIndexStyle(1, DRAW_NONE);
   SetIndexBuffer(1, TopDiffBuffer);
   
   SetIndexStyle(2, DRAW_NONE);
   SetIndexBuffer(2, BottomDiffBuffer);
   

   return(0);
}

int deinit()
{
   ObjectsDeleteAll();
   return(0);
}

int start()
{
   int limit;
   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return (-1);
   if (counted_bars > 0) counted_bars--;
   if(UseBarCount == true)
   {
      limit = MathMin(BarCount, Bars - 1 - counted_bars);
   }
   else
   {
      limit=MathMin(Bars-counted_bars,Bars-1);
   }
   
   for(int i = limit; i >= 0; i--)
   {
      //Get values
      if(Start_Hour == TimeHour(iTime(FirstPair,0,i)) && Start_Min == TimeMinute(iTime(FirstPair,0,i)))
      {
         firstpair_prev_day_close = iClose(FirstPair, 0, i);
      }
      if(firstpair_prev_day_close > 0.0)
      {
         FirstPairCurrentGainLoss = (iClose(FirstPair,0,i) - firstpair_prev_day_close) * cal_percent_firstpair;       
      }
      
      if(Start_Hour == TimeHour(iTime(SecondPair,0,i)) && Start_Min == TimeMinute(iTime(SecondPair,0,i)))
      {
         secondpair_prev_day_close = iClose(SecondPair,0,i);
      } 
      if(secondpair_prev_day_close > 0)
      {
         SecondPairCurrentGainLoss = (iClose(SecondPair,0,i) - secondpair_prev_day_close) * cal_percent_secondpair;
      }
      DiffGainLoss = FirstPairCurrentGainLoss - SecondPairCurrentGainLoss;
      //Check MA
      LWMovingAverage[i] = iMA(FirstPair,0,MA_Period,MA_ma_shift,MA_ma_method,MA_ma_Applied_Price,i);            
      //If higher highs

      if(High[i]>LWMovingAverage[i] && High[i+1]>LWMovingAverage[i+1] && High[i]>High[i+1] && Open[i]>Close[i] && Open[i+1]<Close[i+1])
      {
         TopDiffBuffer[i+DisplayOffset] = High[i+DisplayOffset] + (DisplayDistance * Point);
         DrawDiff(Time[i], TopDiffBuffer[i+DisplayOffset], DiffGainLoss);
      }

      //If lower lows
      if(Low[i]<LWMovingAverage[i] && Low[i+1]<LWMovingAverage[i+1] && Low[i]<Low[i+1] && Open[i]<Close[i] && Open[i+1]>Close[i+1])         
      {
         BottomDiffBuffer[i+DisplayOffset] = Low[i+DisplayOffset] - (DisplayDistance * Point);
         DrawDiff(Time[i], BottomDiffBuffer[i+DisplayOffset], DiffGainLoss);
      }
      
   }
   DrawStaticDiff(Time[0], Bid, DiffGainLoss);
   DrawObjects1();  
   return(0);
}
//+------------------------------------------------------------------+
void DrawObjects1()
{
   ObjectCreate(shortname + "1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText(shortname + "1",FirstPair,font_size,"Arial Bold", TextColor);
   ObjectSet(shortname + "1", OBJPROP_CORNER, 1);
   ObjectSet(shortname + "1", OBJPROP_XDISTANCE, 90+myXoffset);
   ObjectSet(shortname + "1", OBJPROP_YDISTANCE, 25+myYoffset);
   
   ObjectCreate(shortname + "2", OBJ_LABEL, 0, 0, 0);
   ObjectSetText(shortname + "2",CheckIfPositive(FirstPairCurrentGainLoss)+" %",font_size,"Arial Bold", CheckColor(FirstPairCurrentGainLoss));
   ObjectSet(shortname + "2", OBJPROP_CORNER, 1);
   ObjectSet(shortname + "2", OBJPROP_XDISTANCE, 10+myXoffset);
   ObjectSet(shortname + "2", OBJPROP_YDISTANCE, 25+myYoffset);
   
   ObjectCreate(shortname + "3", OBJ_LABEL, 0, 0, 0);
   ObjectSetText(shortname + "3",SecondPair,font_size,"Arial Bold", TextColor);
   ObjectSet(shortname + "3", OBJPROP_CORNER, 1);
   ObjectSet(shortname + "3", OBJPROP_XDISTANCE, 90+myXoffset);
   ObjectSet(shortname + "3", OBJPROP_YDISTANCE, 50+myYoffset);   
   
   ObjectCreate(shortname + "4", OBJ_LABEL, 0, 0, 0);
   ObjectSetText(shortname + "4",CheckIfPositive(SecondPairCurrentGainLoss)+" %",font_size,"Arial Bold", CheckColor(SecondPairCurrentGainLoss));
   ObjectSet(shortname + "4", OBJPROP_CORNER, 1);
   ObjectSet(shortname + "4", OBJPROP_XDISTANCE, 10+myXoffset);
   ObjectSet(shortname + "4", OBJPROP_YDISTANCE, 50+myYoffset);   

   ObjectCreate(shortname + "5", OBJ_LABEL, 0, 0, 0);
   ObjectSetText(shortname + "5","Diff",font_size,"Arial Bold", TextColor);
   ObjectSet(shortname + "5", OBJPROP_CORNER, 1);
   ObjectSet(shortname + "5", OBJPROP_XDISTANCE, 90+myXoffset);
   ObjectSet(shortname + "5", OBJPROP_YDISTANCE, 75+myYoffset);   
   
   ObjectCreate(shortname + "6", OBJ_LABEL, 0, 0, 0);
   ObjectSetText(shortname + "6",CheckIfPositive(DiffGainLoss)+" %",font_size,"Arial Bold", CheckColor(DiffGainLoss));
   ObjectSet(shortname + "6", OBJPROP_CORNER, 1);
   ObjectSet(shortname + "6", OBJPROP_XDISTANCE, 10+myXoffset);
   ObjectSet(shortname + "6", OBJPROP_YDISTANCE, 75+myYoffset);   
   

}
//+------------------------------------------------------------------+         
color CheckColor(double value1)
{
   color result;
   if(value1 >0.0)
   {
      result = ProfitColor;
   }
   else if(value1 <0.0)
   {
      result = LossColor;
   }
   else if(value1==0.0)
   {
      result = NoProfitNoLossColor;
   }
   return(result);
}
//+------------------------------------------------------------------+
void DrawDiff(datetime x1, double y1, double diff_Price)
{
   ObjectIdx++;
   lbl_obj_name = shortname+"Label" + DoubleToStr(ObjectIdx,0);   
      if(ObjectFind(lbl_obj_name) != 0)
      {
      ObjectCreate(lbl_obj_name, OBJ_TEXT, 0, x1, y1);
      ObjectSetText(lbl_obj_name, CheckIfPositive(diff_Price)+" %", Label_font_size, Label_font_type, CheckColor(diff_Price));
      }
      else
      {
      ObjectMove(lbl_obj_name, 0,x1, y1);
      }
}
//+------------------------------------------------------------------+
void DrawStaticDiff(datetime x1, double y1, double diff_Price)
{
   lbl_obj_name2 = shortname+"CurrentDiff";
   if(ObjectFind(lbl_obj_name2) != 0)
   {
      ObjectCreate(lbl_obj_name2, OBJ_TEXT, 0, x1+2000, y1+(10*Point));
      ObjectSetText(lbl_obj_name2, CheckIfPositive(diff_Price)+" %", Front_Label_font_size, Label_font_type, Front_Label_font_color);      
   }
   else
   {
      ObjectMove(lbl_obj_name2, 0,x1+2000, y1+(10*Point));
   }   
}
//+------------------------------------------------------------------+
void SetCalculatePercent()
{
   if(digits1==5) cal_percent_firstpair=1000;
   if(digits1==4) cal_percent_firstpair=100;
   if(digits1==3) cal_percent_firstpair=10;
   if(digits1==2) cal_percent_firstpair=1;
   if(digits1==1) cal_percent_firstpair=0.1;
   
   if(digits2==5) cal_percent_secondpair=1000;
   if(digits2==4) cal_percent_secondpair=100;
   if(digits2==3) cal_percent_secondpair=10;
   if(digits2==2) cal_percent_secondpair=1;
   if(digits2==1) cal_percent_secondpair=0.1;
}
//+------------------------------------------------------------------+
string CheckIfPositive(double num)
{
   string result;
   if(num >0.0)
   {
      result = "+"+DoubleToStr(num,2);  
   }
   else if(num <0.0)
   {
      result = DoubleToStr(num,2);
   }
   return(result);
}
//TLimS


