//+------------------------------------------------------------------+
//|                                               KOS#14_monitor.mq4 |
//|                                           Copyright 2012, Dm_35. |
//|                                                     dm34@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Dm_35"
#property link      "dm34@mail.ru"

#property indicator_chart_window

extern string txt1 = "MA_ATR";
extern int LookBack = 14;
extern int ma_metod = MODE_SMMA;
extern int ma_price = PRICE_MEDIAN;

extern string txt2 = "Symbol";
extern string Symb = "#kos#14";
extern string SS = "";

extern string txt3 = "TF";
extern int TF1 = 15;
extern int TF2 = 0;
extern int TF3 = 0;

extern string txt4 = "Size";
extern int FontSize = 7;


string Alarms;
extern bool EnableAlarms = FALSE;
extern bool EnableEmail = FALSE;
int AlarmsDelay = 180;
string AlertsStartTime = "00:00";
string AlertsEndTime = "23:59";


int JPYUpperLevel = 100;
int JPYLowerLevel = -100;
int USDUpperLevel = 100;
int USDLowerLevel = -100;
int EURUpperLevel = 100;
int EURLowerLevel = -100;
int GBPUpperLevel = 100;
int GBPLowerLevel = -100;
int KOSUpperLevel = 100;
int KOSLowerLevel = -100;

int gi_316 = 0;

double gd_328;
double val_KOS;

double val_EUR;
double val_GBP;
double val_JPY;

double val_USD;
double gd_392;

double gd_480;



int gi_3272 = 0;
int gi_3276 = 0;
int gi_3280 = 0;
double gd_3284;

int gi_3324;
bool gi_3328;


int init() {
   gi_3324 = 0;
   initGraph();
   return (0);
}

int deinit() {
   ObjectsDeleteAll(0, OBJ_LABEL);
   return (0);
}

int start() {
   int li_0 = IndicatorCounted();
   gi_3328 = FALSE;
   CalculateStrength();
   objectBlank();
   CreateTitle();

     paint("KOS", val_KOS);
     paint("GBP", val_GBP);
     paint("EUR", val_EUR); 
     paint("JPY", val_JPY);     
     paint("USD", val_USD);
   

   WindowRedraw();
   if (gi_3328) gi_3324 = TimeCurrent();
   return (0);
}

void CreateTitle() {
   ObjectCreate("Overalltitle", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Overalltitle", "#KOS#14 monitor " + PeriodToStr(TF1) + "   " + PeriodToStr(TF2) + "   " + PeriodToStr(TF3) + "   (" + LookBack + ")", FontSize +
      2, "Tahoma", Gold);
   ObjectSet("Overalltitle", OBJPROP_CORNER, gi_3272);
   ObjectSet("Overalltitle", OBJPROP_XDISTANCE, gd_3284 - 13 * FontSize);
   ObjectSet("Overalltitle", OBJPROP_YDISTANCE, gi_3280 + 2.25 * FontSize);
}

void CalculateStrength() {
   CalculateEUR();
   CalculateJPY();
   CalculateUSD();
   CalculateGBP();
   CalculateKOS();
}

void CalculateUSD() {
   int li_0 = 0;
   double ld_4 = 0;
   double ld_12 = 0;
   double ld_20 = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   double ld_44 = 0;
   double ld_52 = 0;
   CalculateStrengthPairs("AUDUSD"+SS);
   if (gd_480 != 0.0) {
      ld_4 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("USDCHF"+SS);
   if (gd_480 != 0.0) {
      ld_12 = gd_480;
      li_0++;
   }
   CalculateStrengthPairs("EURUSD"+SS);
   if (gd_480 != 0.0) {
      ld_28 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("GBPUSD"+SS);
   if (gd_480 != 0.0) {
      ld_36 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("USDJPY"+SS);
   if (gd_480 != 0.0) {
      ld_44 = gd_480;
      li_0++;
   }
   CalculateStrengthPairs("NZDUSD"+SS);
   if (gd_480 != 0.0) {
      ld_52 = -1.0 * gd_480;
      li_0++;
   }
   if (li_0 > 0) {
      val_USD = (ld_4 + ld_12 + ld_20 + ld_28 + ld_36 + ld_44 + ld_52) / li_0;
      return;
   }
   val_USD = 0;
}


void CalculateJPY() {
   int li_0 = 0;
   double ld_4 = 0;
   double ld_12 = 0;
   double ld_20 = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   double ld_44 = 0;
   double ld_52 = 0;
   CalculateStrengthPairs("AUDJPY"+SS);
   if (gd_480 != 0.0) {
      ld_4 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("CHFJPY"+SS);
   if (gd_480 != 0.0) {
      ld_12 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("EURJPY"+SS);
   if (gd_480 != 0.0) {
      ld_28 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("GBPJPY"+SS);
   if (gd_480 != 0.0) {
      ld_36 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("NZDJPY"+SS);
   if (gd_480 != 0.0) {
      ld_44 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("USDJPY"+SS);
   if (gd_480 != 0.0) {
      ld_52 = -1.0 * gd_480;
      li_0++;
   }
   if (li_0 > 0) {
      val_JPY = (ld_4 + ld_12 + ld_20 + ld_28 + ld_36 + ld_44 + ld_52) / li_0;
      return;
   }
   val_JPY = 0;
}

void CalculateGBP() {
   int li_0 = 0;
   double ld_4 = 0;
   double ld_12 = 0;
   double ld_20 = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   double ld_44 = 0;
   double ld_52 = 0;
   CalculateStrengthPairs("GBPJPY"+SS);
   if (gd_480 != 0.0) {
      ld_4 = gd_480;
      li_0++;
   }
   CalculateStrengthPairs("GBPUSD"+SS);
   if (gd_480 != 0.0) {
      ld_20 = gd_480;
      li_0++;
   }
   CalculateStrengthPairs("EURGBP"+SS);
   if (gd_480 != 0.0) {
      ld_36 = -1.0 * gd_480;
      li_0++;
   }
   CalculateStrengthPairs("GBPCHF"+SS);
   if (gd_480 != 0.0) {
      ld_52 = gd_480;
      li_0++;
   }
   if (li_0 > 0) {
      val_GBP = (ld_4 + ld_12 + ld_20 + ld_28 + ld_36 + ld_44 + ld_52) / li_0;
      return;
   }
   val_GBP = 0;
}

void CalculateKOS() {
   int li_0 = 0;
   gd_392 = 0;
   CalculateStrengthPairs(Symb);
   if (gd_480 != 0.0) {
      val_KOS = gd_480;
      li_0++;
   }
}
//------------------------
void CalculateStrengthPairs(string as_0) {
   double ld_8;
   double ld_16;
   double ld_24;
   RefreshRates();
  // int li_32 = MarketInfo(as_0, MODE_DIGITS);
 int dig; double _close;
  if(as_0==Symb)
  {
  dig =3;
  _close = NormalizeDouble(iClose(as_0,TF1,0), dig);
  }
  else
  {
  dig = MarketInfo(as_0, MODE_DIGITS);
  _close = NormalizeDouble(MarketInfo(as_0, MODE_BID), dig);
  } 
   double _ma_tf1 = NormalizeDouble(iMA(as_0, TF1, LookBack, gi_316, ma_metod, ma_price, 0), dig);
   double _ma_tf2 = NormalizeDouble(iMA(as_0, TF2, LookBack, gi_316, ma_metod, ma_price, 0), dig);
   double _ma_tf3 = NormalizeDouble(iMA(as_0, TF3, LookBack, gi_316, ma_metod, ma_price, 0), dig);
  
   double _atr_tf1 = NormalizeDouble(iATR(as_0, TF1, LookBack, 0), dig);
   double _atr_tf2 = NormalizeDouble(iATR(as_0, TF2, LookBack, 0), dig);
   double _atr_tf3 = NormalizeDouble(iATR(as_0, TF3, LookBack, 0), dig);
  
  
   if (TF1 == 0) _atr_tf1 = 0;
   if (TF2 == 0) _atr_tf2 = 0;
   if (TF3 == 0) _atr_tf3 = 0;
   int ii = 0;
   
   if (_atr_tf1 > 0.0) 
   {
      ii++;
      ld_8 = 100.0 * NormalizeDouble(MathAbs(_close - _ma_tf1) / _atr_tf1, 2);
      if (_close < _ma_tf1) 
          ld_8 = -1.0 * ld_8;
   
   }
   
   if (_atr_tf2 > 0.0) 
   {
      ii++;
      ld_16 = 100.0 * (MathAbs(_close - _ma_tf2) / _atr_tf2);
      if (_close < _ma_tf2) 
          ld_16 = -1.0 * ld_16;
   }
   if (_atr_tf3 > 0.0) 
   {
      ii++;
      ld_24 = 100.0 * (MathAbs(_close - _ma_tf3) / _atr_tf3);
      if (_close < _ma_tf3) 
          ld_24 = -1.0 * ld_24;
   }
   if (ii > 0) 
   {
      gd_480 = NormalizeDouble((ld_8 + ld_16 + ld_24) / ii, 0);
      return;
   
   }
   gd_480 = 0;
}

void CalculateEUR() {
   int li_0 = 0;
   double ld_4 = 0;
   double ld_12 = 0;
   double ld_20 = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   double ld_44 = 0;
   double ld_52 = 0;
   CalculateStrengthPairs("EURJPY"+SS);
   if (gd_480 != 0.0) {
      ld_4 = gd_480;
      li_0++;
   }
   CalculateStrengthPairs("EURUSD"+SS);
   if (gd_480 != 0.0) {
      ld_20 = gd_480;
      li_0++;
   }
   CalculateStrengthPairs("EURGBP"+SS);
   if (gd_480 != 0.0) {
      ld_36 = gd_480;
      li_0++;
   }
   CalculateStrengthPairs("EURCHF"+SS);
   if (gd_480 != 0.0) {
      ld_52 = gd_480;
      li_0++;
   }
   if (li_0 > 0) {
      val_EUR = (ld_4 + ld_12 + ld_20 + ld_28 + ld_36 + ld_44 + ld_52) / li_0;
      return;
   }
   val_EUR = 0;
}


void objectCreate(string as_0, int ai_8, int ai_12, string as_16 = "_", int ai_24 = 60, string as_28 = "Arial", color ai_36 = -1) {
   if (ai_24 == 60) ai_24 = 3 * FontSize;
   ObjectCreate(as_0, OBJ_LABEL, 0, 0, 0);
   ObjectSet(as_0, OBJPROP_CORNER, 0);
   ObjectSet(as_0, OBJPROP_COLOR, ai_36);
   ObjectSet(as_0, OBJPROP_XDISTANCE, ai_8);
   ObjectSet(as_0, OBJPROP_YDISTANCE, ai_12);
   ObjectSetText(as_0, as_16, ai_24, as_28, ai_36);
}


void initGraph() {
   ObjectsDeleteAll(0, OBJ_LABEL);
   int li_0 = gi_3280 + 4.25 * FontSize + 67.0 * (FontSize / 4.0);
   int li_4 = gi_3280 + 4.25 * FontSize;
   int li_8 = 50 * FontSize / 10;
   int li_12 = li_4;
   for (int li_16 = 67; li_16 >= 1; li_16--) {
      objectCreate("gbp_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   objectCreate("gbptxt", li_8, li_12 + 9.5 * FontSize, "GBP", FontSize + 2, "Arial Narrow", White);
   objectCreate("gbpp", li_8, li_12 + 6.75 * FontSize, DoubleToStr(9, 1), FontSize + 2, "Arial Narrow", White);
   li_12 += 10 * FontSize;
   for (li_16 = 200; li_16 <= 266; li_16++) {
      objectCreate("gbp_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   li_8 = 115 * FontSize / 10;
   li_12 = li_4;
   for (li_16 = 67; li_16 >= 1; li_16--) {
      objectCreate("eur_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
    objectCreate("eurtxt", li_8, li_12 + 9.5 * FontSize, "EUR", FontSize + 2, "Arial Narrow", White);
    objectCreate("eurp", li_8, li_12 + 6.75 * FontSize, DoubleToStr(9, 1), FontSize + 2, "Arial Narrow", White);
   li_12 += 10 * FontSize;
   for (li_16 = 200; li_16 <= 266; li_16++) {
      objectCreate("eur_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   li_8 = 180 * FontSize / 10;
   li_12 = li_4;
   for (li_16 = 67; li_16 >= 1; li_16--) {
      objectCreate("jpy_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   objectCreate("jpytxt", li_8, li_12 + 9.5 * FontSize, "JPY", FontSize + 2, "Arial Narrow", White);
   objectCreate("jpyp", li_8, li_12 + 6.75 * FontSize, DoubleToStr(9, 1), FontSize + 2, "Arial Narrow", White);
   li_12 += 10 * FontSize;
   for (li_16 = 200; li_16 <= 266; li_16++) {
      objectCreate("jpy_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   li_8 = 245 * FontSize / 10;
   li_12 = li_4;
   for (li_16 = 67; li_16 >= 1; li_16--) {
      objectCreate("usd_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   objectCreate("usdtxt", li_8, li_12 + 9.5 * FontSize, "USD", FontSize + 2, "Arial Narrow", White);
   objectCreate("usdp", li_8, li_12 + 6.75 * FontSize, DoubleToStr(9, 1), FontSize + 2, "Arial Narrow", White);
   li_12 += 10 * FontSize;
   for (li_16 = 200; li_16 <= 266; li_16++) {
      objectCreate("usd_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   li_8 = 310 * FontSize / 10;
   li_12 = li_4;
   for (li_16 = 67; li_16 >= 1; li_16--) {
      objectCreate("kos_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   objectCreate("kostxt", li_8, li_12 + 9.5 * FontSize, "KOS", FontSize + 2, "Arial Narrow", White);
   objectCreate("kosp", li_8, li_12 + 6.75 * FontSize, DoubleToStr(9, 1), FontSize + 2, "Arial Narrow", White);
   li_12 += 10 * FontSize;
   for (li_16 = 200; li_16 <= 266; li_16++) {
      objectCreate("kos_" + li_16, li_8, li_12);
      li_12 = li_12 + FontSize / 4.0;
   }
   gd_3284 = (li_8 + 40 + FontSize) / 2;
   
}

void objectBlank() {
   for (int li_0 = 1; li_0 <= 67; li_0++) {
      ObjectSetBlank("usd_" + li_0);
      ObjectSetBlank("eur_" + li_0);
      ObjectSetBlank("kos_" + li_0);
      ObjectSetBlank("jpy_" + li_0);
      ObjectSetBlank("gbp_" + li_0);
   }
   for (li_0 = 200; li_0 <= 266; li_0++) {
      ObjectSetBlank("usd_" + li_0);
      ObjectSetBlank("eur_" + li_0);
      ObjectSetBlank("kos_" + li_0);
      ObjectSetBlank("jpy_" + li_0);
      ObjectSetBlank("gbp_" + li_0);
   
   }

   ObjectSet("usdtxt", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("usdp", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("eurtxt", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("eurp", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("kostxt", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("kosp", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("jpytxt", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("jpyp", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("gbptxt", OBJPROP_COLOR, CLR_NONE);
   ObjectSet("gbpp", OBJPROP_COLOR, CLR_NONE); 
   
}

void ObjectSetBlank(string as_0) {
   ObjectSet(as_0, OBJPROP_COLOR, CLR_NONE);
}


void paint(string as_0, double ad_8) {
   color li_16;
   string ls_20 = "";

   if (as_0 == "JPY") ls_20 = "jpy";
   if (as_0 == "USD") ls_20 = "usd";
   if (as_0 == "EUR") ls_20 = "eur";   
   if (as_0 == "GBP") ls_20 = "gbp";   
   if (as_0 == "KOS") ls_20 = "kos";
      
   bool li_28 = FALSE;
   int li_32 = 0;
   if (ad_8 > 0.0) {
      if (ad_8 <= 25.0) li_16 = White;
      else {
         if (ad_8 <= 50.0) li_16 = Gray;
         else {
            if (ad_8 <= 75.0) li_16 = LightSkyBlue;
            else {
               if (ad_8 <= 100.0) li_16 = RoyalBlue;
               else li_16 = Lime;
            }
         }
      }
      li_28 = MathMod(ad_8, 3);
      li_32 = (ad_8 - li_28) / 3.0 + 1.0;
      if (li_32 > 67) li_32 = 67;
      for (int li_36 = 1; li_36 <= li_32; li_36++) ObjectSet(ls_20 + "_" + li_36, OBJPROP_COLOR, li_16);
   } else {
      if (ad_8 < 0.0) {
         if (ad_8 >= -25.0) li_16 = Yellow;
         else {
            if (ad_8 >= -50.0) li_16 = Orange;
            else {
               if (ad_8 >= -75.0) li_16 = Sienna;
               else {
                  if (ad_8 >= -100.0) li_16 = MediumVioletRed;
                  else li_16 = Red;
               }
            }
         }
         li_28 = MathMod(MathAbs(ad_8), 3);
         li_32 = (MathAbs(ad_8) - li_28) / 3.0 + 1.0;
         if (li_32 > 66) li_32 = 66;
         for (li_36 = 200; li_36 <= li_32 + 199; li_36++) ObjectSet(ls_20 + "_" + li_36, OBJPROP_COLOR, li_16);
      }
   }
   if ( (as_0 == "EUR" && ad_8 <= EURLowerLevel) ||
        (as_0 == "KOS" && ad_8 <= KOSLowerLevel) || 
        (as_0 == "JPY" && ad_8 <= JPYLowerLevel) || 
        (as_0 == "GBP" && ad_8 <= GBPLowerLevel) || 
        (as_0 == "USD" && ad_8 <= USDLowerLevel)) 
        {
        ObjectSet(ls_20 + "txt", OBJPROP_COLOR, Red);
        ObjectSetText(ls_20 + "p", DoubleToStr(ad_8, 0), FontSize + 2, "Arial Narrow", Red);
         if (IsValidTT(AlertsStartTime, AlertsEndTime)) {
           if (TimeCurrent() > gi_3324 + AlarmsDelay) {
            if (EnableAlarms || EnableEmail) {
               if (EnableAlarms) {
                  Alert("Alert " + as_0 + " < Lower Level! (" + DoubleToStr(ad_8, 0) + ")");
                  
               }
               if (EnableEmail) SendMail("Dashboard Alert", "Alert " + as_0 + " < (" + DoubleToStr(ad_8, 0) + ")");
               gi_3328 = TRUE;
            }
         }
      }
   } else {
      if ((as_0 == "EUR" && ad_8 >= EURUpperLevel) ||
          (as_0 == "KOS" && ad_8 >= KOSUpperLevel) || 
          (as_0 == "JPY" && ad_8 >= JPYUpperLevel) || 
          (as_0 == "GBP" && ad_8 >= GBPUpperLevel) || 
          (as_0 == "USD" && ad_8 >= USDUpperLevel)) 
          {
           ObjectSet(ls_20 + "txt", OBJPROP_COLOR, Lime);
           ObjectSetText(ls_20 + "p", DoubleToStr(ad_8, 0) , FontSize + 2, "Arial Narrow", Lime);
            if (IsValidTT(AlertsStartTime, AlertsEndTime)) {
              if (TimeCurrent() > gi_3324 + AlarmsDelay) {
               if (EnableAlarms || EnableEmail) {
                  if (EnableAlarms) {
                     Alert("Alert " + as_0 + " > Upper Level! (" + DoubleToStr(ad_8, 0) + ")");
                     
                  }
                  if (EnableEmail) SendMail("Dashboard Alert", "Alert " + as_0 + " > Upper Level! (" + DoubleToStr(ad_8, 0) + ")");
                  gi_3328 = TRUE;
               }
            }
         }
      } else {
         ObjectSet(ls_20 + "txt", OBJPROP_COLOR, White);
         ObjectSetText(ls_20 + "p", DoubleToStr(ad_8, 0), FontSize + 2, "Arial Narrow", White);
      }
   }
}



string PeriodToStr(int ai_0) {
   if (ai_0 == 1) return ("M1");
   if (ai_0 == 5) return ("M5");
   if (ai_0 == 15) return ("M15");
   if (ai_0 == 30) return ("M30");
   if (ai_0 == 60) return ("H1");
   if (ai_0 == 240) return ("H4");
   if (ai_0 == 1440) return ("D1");
   if (ai_0 == 10080) return ("W1");
   if (ai_0 == 43200) return ("MN1");
   return ("");
}

bool IsValidTT(string as_0, string as_8) {
   int li_16 = StrToTime(Year() + "." + Month() + "." + Day() + " " + as_0);
   int li_20 = StrToTime(Year() + "." + Month() + "." + Day() + " " + as_8);
   if (li_20 <= li_16) {
      if (TimeCurrent() < li_16) {
         if (TimeCurrent() > li_20) li_20 += 86400;
         else li_16 -= 86400;
      } else li_20 += 86400;
   }
   if (TimeCurrent() >= li_16 && TimeCurrent() < li_20) return (TRUE);
   return (FALSE);
}