//+------------------------------------------------------------------+
//|                                                      Percent.mq4 |
//|  Copyright © 2009, Arif Endro Nugroho <arif_endro@vectra.web.id> |
//|                                             http://vectra.web.id |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Arif E. Nugroho <arif_endro@vectra.web.id>"
#property link      "http://vectra.web.id"

#property indicator_chart_window
//---- input parameters
extern int       corner=2;
extern int       xdis=5;
extern int       ydis=20;
extern string    Font="Lucida Console";
extern int       FontSize=8;
extern color     FontPlus=Blue;
extern color     FontMinus=Red;
extern int       timezone=7;
extern bool      back=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  
   ObjectDelete("DailyStat");
   ObjectCreate("DailyStat", OBJ_LABEL, 0, 0, 0);
   ObjectSet("DailyStat", OBJPROP_BACK, back);
   ObjectSet("DailyStat", OBJPROP_CORNER, corner);
   ObjectSet("DailyStat", OBJPROP_XDISTANCE, xdis);
   ObjectSet("DailyStat", OBJPROP_YDISTANCE, ydis);

   ObjectDelete("WeeklyStat");
   ObjectCreate("WeeklyStat", OBJ_LABEL, 0, 0, 0);
   ObjectSet("WeeklyStat", OBJPROP_BACK, back);
   ObjectSet("WeeklyStat", OBJPROP_CORNER, corner);
   ObjectSet("WeeklyStat", OBJPROP_XDISTANCE, xdis);
   ObjectSet("WeeklyStat", OBJPROP_YDISTANCE, ydis + 10);

   ObjectDelete("MonthlyStat");
   ObjectCreate("MonthlyStat", OBJ_LABEL, 0, 0, 0);
   ObjectSet("MonthlyStat", OBJPROP_BACK, back);
   ObjectSet("MonthlyStat", OBJPROP_CORNER, corner);
   ObjectSet("MonthlyStat", OBJPROP_XDISTANCE, xdis);
   ObjectSet("MonthlyStat", OBJPROP_YDISTANCE, ydis + 20);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("DailyStat");
   ObjectDelete("WeeklyStat");
   ObjectDelete("MonthlyStat");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();

   int    DayOpShift  = iBarShift(NULL, PERIOD_D1, iTime(NULL, PERIOD_D1, 0)) + 0;
   int    DayClShift  = iBarShift(NULL, PERIOD_D1, iTime(NULL, PERIOD_D1, 0)) + 0;
   double DayOpPrice  = iOpen(NULL, PERIOD_D1, DayOpShift); // this should be the close of the first hour (I doesn't like this idea) :(
   double DayClPrice  = iClose(NULL, PERIOD_D1, DayClShift);

//   int    WeekOpShift = iBarShift(NULL, PERIOD_H4, iTime(NULL, PERIOD_W1, 0)) - 1;
   int    WeekClShift = iBarShift(NULL, PERIOD_W1, iTime(NULL, PERIOD_W1, 0)) + 0;
   double WeekOpPrice = iOpen(NULL, PERIOD_W1, WeekClShift); // this should be the close of the first at 4H bar in the first day (this from thomson reuters)
   double WeekClPrice = iClose(NULL, PERIOD_W1, WeekClShift);

   int    MonthClShift= iBarShift(NULL, PERIOD_MN1, iTime(NULL, PERIOD_MN1, 0)) + 0;
   double MonthOpPrice= iOpen(NULL, PERIOD_MN1, MonthClShift);
   double MonthClPrice= iClose(NULL, PERIOD_MN1, MonthClShift);

   color  FontColorDayPercentGain = Silver;
   color  FontColorWeekPercentGain = Silver;
   color  FontColorMonthPercentGain = Silver;
   

   string DailyInfo  = "Daily....: " + DoubleToStr(DayOpPrice, Digits) + "/" + DoubleToStr(DayClPrice, Digits) + " [" + DoubleToStr(MathPow(10, Digits)*MathAbs(DayClPrice-DayOpPrice), 0) + "|" + DoubleToStr(100*(MathAbs(DayClPrice-DayOpPrice)/DayOpPrice),2) + "%]";
   string WeeklyInfo = "Weekly...: " + DoubleToStr(WeekOpPrice, Digits) + "/" + DoubleToStr(WeekClPrice, Digits) + " [" + DoubleToStr(MathPow(10, Digits)*MathAbs(WeekClPrice-WeekOpPrice), 0) + "|" + DoubleToStr(100*(MathAbs(WeekClPrice-WeekOpPrice)/WeekOpPrice),2) + "%]";
   string MonthlyInfo= "Monthly..: " + DoubleToStr(MonthOpPrice, Digits) + "/" + DoubleToStr(MonthClPrice, Digits) + " [" + DoubleToStr(MathPow(10, Digits)*MathAbs(MonthClPrice-MonthOpPrice), 0) + "|" + DoubleToStr(100*(MathAbs(MonthClPrice-MonthOpPrice)/MonthOpPrice),2) + "%]";
   
   if ((DayClPrice - DayOpPrice) < 0 )
     FontColorDayPercentGain = FontMinus;
    else
     FontColorDayPercentGain = FontPlus;
    
   if ((WeekClPrice - WeekOpPrice) < 0)
     FontColorWeekPercentGain = FontMinus;
    else
     FontColorWeekPercentGain = FontPlus;
    
   if ((MonthClPrice - MonthOpPrice) < 0)
     FontColorMonthPercentGain = FontMinus;
    else
     FontColorMonthPercentGain = FontPlus;

//----
   ObjectSetText("DailyStat", DailyInfo, FontSize, Font, FontColorDayPercentGain);
   ObjectSetText("WeeklyStat", WeeklyInfo, FontSize, Font, FontColorWeekPercentGain);
   ObjectSetText("MonthlyStat", MonthlyInfo, FontSize, Font, FontColorMonthPercentGain);
//----
   return(0);
  }
//+------------------------------------------------------------------+  