//+------------------------------------------------------------------+
//|                                   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 Top_PipRaise = 3;
extern int DisplayDistance = 3;
extern int DisplayOffset = 0;
extern bool UseBarCount = true;
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 int Front_Label_Forward_Offset = 7000;
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 TopDiffHighest;
double BottomDiffBuffer[];
double BotDiffLowest;
string shortname;
datetime lastAlert;

double FirstPairCurrentGainLoss;
double SecondPairCurrentGainLoss;


double DiffGainLoss;
int digits1,digits2;

int BotDiffObjectIdx;
int ObjectIdx;
string lbl_obj_name,lbl_obj_name2, lbl_obj_name3;
int TopCycle;
int BotCycle;

int init()
{
   CheckTimeFrame();
   if(Digits==5 || Digits == 3)
   {
      DisplayDistance = DisplayDistance * 10;
      Top_PipRaise = Top_PipRaise * 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()
{  
   double temp_result;
   double FirstPairCurrentGainLoss_temp;
   double SecondPairCurrentGainLoss_temp;
   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);
   }
   CheckTimeFrame();
   for(int i = limit; i >= 0; i--)
   //for(int i = 0; i < limit; 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;
      }
      
      temp_result = 0;
      FirstPairCurrentGainLoss_temp = 0;
      SecondPairCurrentGainLoss_temp = 0;
      FirstPairCurrentGainLoss_temp = MathAbs(FirstPairCurrentGainLoss);
      SecondPairCurrentGainLoss_temp = MathAbs(SecondPairCurrentGainLoss);
      if(FirstPairCurrentGainLoss_temp > SecondPairCurrentGainLoss_temp)
      {
         temp_result = FirstPairCurrentGainLoss_temp - SecondPairCurrentGainLoss_temp;
         if (FirstPairCurrentGainLoss < 0.0)
         {
            DiffGainLoss = temp_result * (-1);           
         }
         else
         {
            DiffGainLoss = temp_result;         
         }
      }
      else if(FirstPairCurrentGainLoss_temp < SecondPairCurrentGainLoss_temp)
      {
         temp_result = SecondPairCurrentGainLoss_temp - FirstPairCurrentGainLoss_temp;
         if (SecondPairCurrentGainLoss < 0.0)
         {
            DiffGainLoss = temp_result * (-1);
         }
         else
         {
            DiffGainLoss = temp_result;
         }
      }
      
      
      //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] > High[i+1])
      {
         if(High[i]>LWMovingAverage[i] && High[i+1]>LWMovingAverage[i+1])
         {
            BotDiffLowest = 0;
            BotCycle = 0;
            if(TopCycle == 0)
            {
               TopDiffBuffer[i+DisplayOffset] = High[i+DisplayOffset] + (DisplayDistance * Point);
               DrawDiff(Time[i], TopDiffBuffer[i+DisplayOffset]+Top_PipRaise*Point, DiffGainLoss, "Top");
               TopDiffHighest = TopDiffBuffer[i+DisplayOffset];   
               TopCycle++;
            }
            else if(TopCycle > 0)
            {
               TopDiffBuffer[i+DisplayOffset] = High[i+DisplayOffset] + (DisplayDistance * Point);
               if(TopDiffBuffer[i+DisplayOffset] > TopDiffHighest)
               {  
                  MoveDiff(Time[i], TopDiffBuffer[i+DisplayOffset]+Top_PipRaise*Point, DiffGainLoss, ObjectIdx, "Top");
                  TopDiffHighest = TopDiffBuffer[i+DisplayOffset];   
               }
            }
               
         }
      }
      else if (Low[i] < Low[i+1])
      {
         //If lower lows
         if(Low[i]<LWMovingAverage[i] && Low[i+1]<LWMovingAverage[i+1])         
         {
            TopDiffHighest = 0;
            TopCycle = 0;
            if(BotCycle == 0)
            {
               BottomDiffBuffer[i+DisplayOffset] = Low[i+DisplayOffset] - (DisplayDistance * Point);
               DrawDiff(Time[i],BottomDiffBuffer[i+DisplayOffset], DiffGainLoss, "Bottom");
               BotDiffLowest = BottomDiffBuffer[i+DisplayOffset]; 
               BotCycle++;
            }
            else if(BotCycle >0)
            {
               BottomDiffBuffer[i+DisplayOffset] = Low[i+DisplayOffset] - (DisplayDistance * Point);
               if(BottomDiffBuffer[i+DisplayOffset] < BotDiffLowest)
               {
                  MoveDiff(Time[i], BottomDiffBuffer[i+DisplayOffset], DiffGainLoss, ObjectIdx, "Bottom");
                  BotDiffLowest = BottomDiffBuffer[i+DisplayOffset]; 
               }
            }
         }
      }
   }
   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, string direction)
{
   ObjectIdx++;

   if(direction == "Top")
   {
      lbl_obj_name = shortname+"LabelTop"+DoubleToStr(ObjectIdx,0);
   }
   else if(direction == "Bottom")
   {
      lbl_obj_name = shortname+"LabelBot"+DoubleToStr(ObjectIdx,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));

}
//+------------------------------------------------------------------+
void MoveDiff(datetime x1, double y1,double diff_Price, int temp_objid, string direction)
{
   if(direction == "Top")
   {
      lbl_obj_name = shortname+"LabelTop"+DoubleToStr(temp_objid,0);
      ObjectDelete(lbl_obj_name);
      ObjectIdx++;
      lbl_obj_name3 = shortname+"LabelTop"+DoubleToStr(ObjectIdx,0);
      ObjectCreate(lbl_obj_name3, OBJ_TEXT, 0, x1, y1);
      ObjectSetText(lbl_obj_name3, CheckIfPositive(diff_Price)+" %", Label_font_size, Label_font_type, CheckColor(diff_Price));      
   }
   else if(direction == "Bottom")
   {
      lbl_obj_name = shortname+"LabelBot"+DoubleToStr(temp_objid,0);
      ObjectDelete(lbl_obj_name);
      ObjectIdx++;      
      lbl_obj_name3 = shortname+"LabelBot"+DoubleToStr(ObjectIdx,0);      
      ObjectCreate(lbl_obj_name3, OBJ_TEXT, 0, x1, y1);
      ObjectSetText(lbl_obj_name3, CheckIfPositive(diff_Price)+" %", Label_font_size, Label_font_type, CheckColor(diff_Price));      
   }   
}
//+------------------------------------------------------------------+
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+Front_Label_Forward_Offset, 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+Front_Label_Forward_Offset, y1+(10*Point));
      ObjectSetText(lbl_obj_name2, CheckIfPositive(diff_Price)+" %", Front_Label_font_size, Label_font_type, Front_Label_font_color);            
   }   
}
//+------------------------------------------------------------------+
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);
}
void CheckTimeFrame()
{
   if(Period() == PERIOD_M1)
   {

   }
   else if(Period() == PERIOD_M5)
   {

   }
   else if(Period() == PERIOD_M15)
   {

   }
   else if(Period() == PERIOD_M30)
   {
  
   }
   else if(Period() == PERIOD_H1)
   {

   }
   else if(Period() == PERIOD_H4)
   {
      if(Start_Hour == 22 || Start_Hour == 23 || Start_Hour==0 || Start_Hour == 1)
      {
         Start_Hour = 0;
      }
      else if(Start_Hour == 2 || Start_Hour == 3 || Start_Hour == 4 || Start_Hour == 5)
      {
         Start_Hour = 4;
      }
      else if(Start_Hour == 6 || Start_Hour == 7 || Start_Hour == 8 || Start_Hour == 9)
      {
         Start_Hour = 8;
      }
      else if(Start_Hour == 10 || Start_Hour == 11 || Start_Hour == 12 || Start_Hour == 13)
      {
         Start_Hour = 12;
      }
      else if(Start_Hour == 14 || Start_Hour == 15 || Start_Hour == 16 || Start_Hour == 17)
      {
         Start_Hour = 16;
      }
      else if(Start_Hour == 18 || Start_Hour == 19 || Start_Hour == 20 || Start_Hour == 21)
      {
         Start_Hour = 20;
      }            
      Start_Min = 0;
   }
   else if(Period() == PERIOD_D1)
   {
      Start_Hour = 0;
      Start_Min = 0;
   }   
   else if(Period() == PERIOD_W1)
   {
      Start_Hour = 0;
      Start_Min = 0;
   }
   else if(Period() == PERIOD_MN1)
   {
      Start_Hour = 0;
      Start_Min = 0;
   }
}
//TLimS


