//+------------------------------------------------------------------+
//|                                                         thjw.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
//#property indicator_minimum -10
//#property indicator_maximum 110
#property indicator_level1 70
#property indicator_level2 50
#property indicator_level3 30

#property indicator_levelcolor DarkSlateGray
#property indicator_buffers 8
#property indicator_width1  2
#property indicator_style1  STYLE_SOLID
#property indicator_width2  2
#property indicator_style2  STYLE_SOLID
#property indicator_width3  2
#property indicator_style3  STYLE_SOLID
#property indicator_width4  2
#property indicator_style4  STYLE_SOLID
#property indicator_width5  2
#property indicator_style5  STYLE_SOLID
#property indicator_width6  2
#property indicator_style6  STYLE_SOLID
#property indicator_width7  2
#property indicator_style7  STYLE_SOLID
#property indicator_width8  2
#property indicator_style8  STYLE_SOLID

#include <hanover --- function header b600 (np).mqh>

extern int    LookbackCandles      = 500;
extern int    RSIperiod            = 14;
extern bool   IncludeNZD           = true;
extern color  color_EUR            = HotPink;
extern color  color_USD            = White;
extern color  color_GBP            = Crimson;
extern color  color_AUD            = MediumSeaGreen;
extern color  color_CHF            = Gold;
extern color  color_JPY            = MediumOrchid;
extern color  color_CAD            = Sienna;
extern color  color_NZD            = DodgerBlue;

//---- buffers ------------------------------------------------------+
double   Buffer0[], Buffer1[], Buffer2[], Buffer3[], Buffer4[], Buffer5[], Buffer6[], Buffer7[];

string   ccy, sym, IndiName;
int      dig, tf, tmf, Wnum;
int      RefreshEveryXMins;
double   spr, pnt, tickval, bidp, askp;
datetime prev_time, lastick;


//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+

  IndiName = "thjw";
  IndicatorShortName(IndiName);

  if (RefreshEveryXMins > 240)                             RefreshEveryXMins = 240;
  if (RefreshEveryXMins > 60 && RefreshEveryXMins < 240)   RefreshEveryXMins = 60;
  if (RefreshEveryXMins > 30 && RefreshEveryXMins < 60)    RefreshEveryXMins = 30;
  if (RefreshEveryXMins > 15 && RefreshEveryXMins < 30)    RefreshEveryXMins = 15;
  if (RefreshEveryXMins > 5  && RefreshEveryXMins < 15)    RefreshEveryXMins = 5;
  if (RefreshEveryXMins > 1  && RefreshEveryXMins < 5)     RefreshEveryXMins = 1;

  sym     = Symbol();
  ccy     = Symbol();
  tmf     = Period();
  bidp    = MarketInfo(ccy,MODE_BID);
  askp    = MarketInfo(ccy,MODE_ASK);
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  }  

//  Comment("pnt=" + pnt + "    dig=" + dig + "    spr=" + spr);

  prev_time = -9999;

  //---- set a accuracy of values of the indicator -----------------+
  IndicatorDigits(dig);
  //---- set a style for line --------------------------------------+
  SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,color_EUR);
  SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,color_USD);
  SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,color_GBP);
  SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2,color_AUD);
  SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2,color_CHF);
  SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,2,color_JPY);
  SetIndexStyle(6,DRAW_LINE,STYLE_SOLID,2,color_CAD);
  SetIndexStyle(7,DRAW_LINE,STYLE_SOLID,2,color_NZD);

  //---- set a arrays for line -------------------------------------+
  SetIndexBuffer(0,Buffer0);
  SetIndexBuffer(1,Buffer1);
  SetIndexBuffer(2,Buffer2);
  SetIndexBuffer(3,Buffer3);
  SetIndexBuffer(4,Buffer4);
  SetIndexBuffer(5,Buffer5);
  SetIndexBuffer(6,Buffer6);
  SetIndexBuffer(7,Buffer7);
 
  del_obj();
  plot_obj();    
 
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
    if (RefreshEveryXMins == 0) {
      del_obj();
      plot_obj();    
    }
    else {
      if(prev_time != iTime(sym,RefreshEveryXMins,0))  {
        del_obj();
        plot_obj();
        prev_time = iTime(sym,RefreshEveryXMins,0);
    } }      
     
//  }  
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()   {
//+------------------------------------------------------------------+
  lastick = MarketInfo(ccy,MODE_TIME);
  bidp    = MarketInfo(ccy,MODE_BID);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  if (dig == 3 || dig == 5) {
    spr     /= 10;
    tickval *= 10;
  }  
  Wnum = WindowFind(IndiName);
  if (Wnum < 0)   Wnum=0;
 
  for (int i=LookbackCandles; i>=0; i--)  {
    double euraud = iRSI("EURAUD",Period(),RSIperiod,PRICE_CLOSE,i);
    double eurcad = iRSI("EURCAD",Period(),RSIperiod,PRICE_CLOSE,i);
    double eurchf = iRSI("EURCHF",Period(),RSIperiod,PRICE_CLOSE,i);
    double eurgbp = iRSI("EURGBP",Period(),RSIperiod,PRICE_CLOSE,i);
    double eurjpy = iRSI("EURJPY",Period(),RSIperiod,PRICE_CLOSE,i);
    double eurnzd = iRSI("EURNZD",Period(),RSIperiod,PRICE_CLOSE,i);
    double eurusd = iRSI("EURUSD",Period(),RSIperiod,PRICE_CLOSE,i);
   
    double gbpaud = iRSI("GBPAUD",Period(),RSIperiod,PRICE_CLOSE,i);
    double gbpcad = iRSI("GBPCAD",Period(),RSIperiod,PRICE_CLOSE,i);
    double gbpchf = iRSI("GBPCHF",Period(),RSIperiod,PRICE_CLOSE,i);
    double gbpjpy = iRSI("GBPJPY",Period(),RSIperiod,PRICE_CLOSE,i);
    double gbpnzd = iRSI("GBPNZD",Period(),RSIperiod,PRICE_CLOSE,i);
    double gbpusd = iRSI("GBPUSD",Period(),RSIperiod,PRICE_CLOSE,i);

    double audcad = iRSI("AUDCAD",Period(),RSIperiod,PRICE_CLOSE,i);
    double audchf = iRSI("AUDCHF",Period(),RSIperiod,PRICE_CLOSE,i);
    double audjpy = iRSI("AUDJPY",Period(),RSIperiod,PRICE_CLOSE,i);
    double audnzd = iRSI("AUDNZD",Period(),RSIperiod,PRICE_CLOSE,i);
    double audusd = iRSI("AUDUSD",Period(),RSIperiod,PRICE_CLOSE,i);

    double nzdcad = iRSI("NZDCAD",Period(),RSIperiod,PRICE_CLOSE,i);
    double nzdchf = iRSI("NZDCHF",Period(),RSIperiod,PRICE_CLOSE,i);
    double nzdjpy = iRSI("NZDJPY",Period(),RSIperiod,PRICE_CLOSE,i);
    double nzdusd = iRSI("NZDUSD",Period(),RSIperiod,PRICE_CLOSE,i);

    double usdcad = iRSI("USDCAD",Period(),RSIperiod,PRICE_CLOSE,i);
    double usdchf = iRSI("USDCHF",Period(),RSIperiod,PRICE_CLOSE,i);
    double usdjpy = iRSI("USDJPY",Period(),RSIperiod,PRICE_CLOSE,i);

    double cadchf = iRSI("CADCHF",Period(),RSIperiod,PRICE_CLOSE,i);
    double cadjpy = iRSI("CADJPY",Period(),RSIperiod,PRICE_CLOSE,i);

    double chfjpy = iRSI("CHFJPY",Period(),RSIperiod,PRICE_CLOSE,i);

    double eur = ( euraud + eurcad + eurchf + eurgbp + eurjpy + eurusd + eurnzd*IncludeNZD) / (6+IncludeNZD);
    double gbp = ( gbpaud + gbpcad + gbpchf + (100-eurgbp) + gbpjpy + gbpusd + gbpnzd*IncludeNZD) / (6+IncludeNZD);
    double aud = ( (100-gbpaud) + audcad + audchf + (100-euraud) + audjpy + audusd + audnzd*IncludeNZD) / (6+IncludeNZD);
    double usd = ( (100-gbpusd) + (100-audusd) + usdchf + (100-eurusd) + usdcad + usdjpy + (100-nzdusd)*IncludeNZD) / (6+IncludeNZD);
    double cad = ( (100-gbpcad) + (100-audcad) + (100-usdcad) + (100-eurcad) + cadchf + cadjpy + (100-nzdcad)*IncludeNZD) / (6+IncludeNZD);
    double chf = ( (100-gbpchf) + (100-audchf) + (100-usdchf) + (100-eurchf) + (100-cadchf) + cadjpy + (100-nzdchf)*IncludeNZD) / (6+IncludeNZD);
    double jpy = ( (100-gbpjpy) + (100-audjpy) + (100-usdjpy) + (100-eurjpy) + (100-cadjpy) + (100-chfjpy) + (100-nzdjpy)*IncludeNZD) / (6+IncludeNZD);
    double nzd = ( (100-eurnzd) + (100-audnzd) + (100-gbpnzd) + nzdusd + nzdcad + nzdchf + nzdjpy) / 7 * IncludeNZD;

    Buffer0[i] = eur;
    Buffer1[i] = usd;
    Buffer2[i] = gbp;
    Buffer3[i] = aud;
    Buffer4[i] = chf;
    Buffer5[i] = jpy;
    Buffer6[i] = cad;
    if (IncludeNZD)
      Buffer7[i] = nzd;
  }
  PlotLabel (IndiName+"EUR", false, WindowFind("thjw"), 0,  10, 15, "EUR", color_EUR, 10, "Arial Black", 0.0, false, 0);       // Plot text label
  PlotLabel (IndiName+"USD", false, WindowFind("thjw"), 0,  50, 15, "USD", color_USD, 10, "Arial Black", 0.0, false, 0);       // Plot text label
  PlotLabel (IndiName+"GBP", false, WindowFind("thjw"), 0,  90, 15, "GBP", color_GBP, 10, "Arial Black", 0.0, false, 0);       // Plot text label
  PlotLabel (IndiName+"AUD", false, WindowFind("thjw"), 0, 130, 15, "AUD", color_AUD, 10, "Arial Black", 0.0, false, 0);       // Plot text label
  PlotLabel (IndiName+"CHF", false, WindowFind("thjw"), 0, 170, 15, "CHF", color_CHF, 10, "Arial Black", 0.0, false, 0);       // Plot text label
  PlotLabel (IndiName+"JPY", false, WindowFind("thjw"), 0, 210, 15, "JPY", color_JPY, 10, "Arial Black", 0.0, false, 0);       // Plot text label
  PlotLabel (IndiName+"CAD", false, WindowFind("thjw"), 0, 250, 15, "CAD", color_CAD, 10, "Arial Black", 0.0, false, 0);       // Plot text label
  if (IncludeNZD)
    PlotLabel (IndiName+"NZD", false, WindowFind("thjw"), 0, 290, 15, "NZD", color_NZD, 10, "Arial Black", 0.0, false, 0);       // Plot text label

  return(0);
}

//+------------------------------------------------------------------+
#include <hanover --- extensible functions b600 (np).mqh>