//+------------------------------------------------------------------+
//|                                   Currency Strength - Giraia.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

#include <hanover --- function header (np).mqh>

extern string   CurrencyPairs                = "GU,UF,EU,UJ,UC,NU,AU,AN,AC,AF,AJ,CJ,FJ,EG,EA,EF,EJ,EN,EC,GF,GA,GC,GJ,GN,NJ,NC,CF,NF";
extern string   CurrencySuffix               = "";
extern string   TimeFrame                    = "D1";
extern string   FontNameAndSize              = "Lucida Console,16";
extern string   WindowCornerHposVposVspacing = "0,TL,10,20,18";  // Window, Corner, Start Hpos, Start Vpos, Vert Spacing
extern string   OutputFormat                 = "R3.1";
extern string   Colors                       = "99,Red,7.0,Orange,5.0,Yellow,2.0,DodgerBlue,-1";  // e.g. 100 <= Red > 80 etc
extern bool     ShowNoOfPairs                = false;
extern bool     SortDescending               = true;
extern string   RefreshPeriod                = "T";
extern string   Visibility                   = "M1,M5,M15,M30,H1,H4,D1,W1,MN";

double    spr, pnt, tickval, ccy_strength[8];
int       dig, tf, ccy_count[8], nCP, vis, Window, Corner, HorizPos, VertPos, VertSpacing, RefreshEveryXMins;
string    IndiName, ccy, CP[99], clrs[20], FNS[2], arr[5];
string    ccy_name[8] = {"USD","EUR","GBP","CHF","CAD","AUD","JPY","NZD"};
datetime  prev_time;

//+------------------------------------------------------------------+
int init()   {
//+------------------------------------------------------------------+
  IndiName = "CurrencyStrength-";
  IndicatorShortName(IndiName);

  ccy     = Symbol();
  tf      = Period();
  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;
  }

  RefreshEveryXMins = StrToTF(RefreshPeriod);
  vis               = GetVisibility(Visibility);

  if (CurrencyPairs == "")  CurrencyPairs = Symbol();
  nCP = StrToStringArray(StringUpper(CurrencyPairs),CP);
  for (int i=0; i<nCP; i++)
    CP[i] = ExpandCcy(CP[i]) + CurrencySuffix;

  StrToStringArray(Colors,clrs);
  StrToStringArray(FontNameAndSize,FNS);
  StrToStringArray(WindowCornerHposVposVspacing,arr);
  Window      = StrToInteger(arr[0]);
  Corner      = 2*(StringFind(StringUpper(arr[1]),"B")>=0) + (StringFind(StringUpper(arr[1]),"R")>=0);
  HorizPos    = StrToInteger(arr[2]);
  VertPos     = StrToInteger(arr[3]);
  VertSpacing = StrToInteger(arr[4]);
  if (Corner > 1)
    SortDescending = 1-SortDescending;

  del_obj();
  plot_obj();    
  prev_time = -9999;
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  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);
}
//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  if (RefreshEveryXMins == 0) {
    del_obj();
    plot_obj();    
  }
  else {
    if(prev_time != iTime(Symbol(),RefreshEveryXMins,0))  {
      del_obj();
      plot_obj();
      prev_time = iTime(Symbol(),RefreshEveryXMins,0);
  } }      
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()   {
//+------------------------------------------------------------------+

  ArrayInitialize(ccy_strength,0.0);
  ArrayInitialize(ccy_count,0);

  for (int i=0; i<nCP; i++)   {
    int timef           = StrToTF(TimeFrame);
    double curr_bid     = MarketInfo(CP[i],MODE_BID);
    if (curr_bid == 0)   continue;
    double candle_high  = iHigh(CP[i],timef,0);
    double candle_low   = iLow(CP[i],timef,0);
    double bid_ratio    = DivZero(curr_bid - candle_low, candle_high - candle_low);

    double ind_strength = 0;
    if (bid_ratio >= 0.97)   ind_strength = 9;    else
    if (bid_ratio >= 0.90)   ind_strength = 8;    else
    if (bid_ratio >= 0.75)   ind_strength = 7;    else
    if (bid_ratio >= 0.60)   ind_strength = 6;    else
    if (bid_ratio >= 0.50)   ind_strength = 5;    else
    if (bid_ratio >= 0.40)   ind_strength = 4;    else
    if (bid_ratio >= 0.25)   ind_strength = 3;    else
    if (bid_ratio >= 0.10)   ind_strength = 2;    else
    if (bid_ratio >= 0.03)   ind_strength = 1;

    string temp = StringSubstr(CP[i],0,3);
    for (int j=0; j<8; j++)   {
      if (ccy_name[j] == temp)  {
        ccy_strength[j] += ind_strength;
        ccy_count[j]    += 1;
        break;
    } }    

    temp = StringSubstr(CP[i],3,3);
    for (j=0; j<8; j++)   {
      if (ccy_name[j] == temp)  {
        ccy_strength[j] += 9 - ind_strength;
        ccy_count[j]    += 1;
        break;
  } } }    

  // This routine loads the strength values and currency symbols into an array, and sorts the array......
  string array_to_sort[8];
  for (j=0; j<8; j++)  {
    array_to_sort[j] = "";
    if (ccy_count[j] < 1)   continue;
    double out_value = DivZero(ccy_strength[j],ccy_count[j]);                // calculate the strength value = total strength / number of pairs that were summed
    string tstr1     = NumberToStr(out_value,OutputFormat) + ccy_name[j];    // build a string (tstr1) with the formatted number value, followed by the currency name
    int    length    = StringLen(NumberToStr(out_value,OutputFormat));       // length of the formatted number value
    if (ShowNoOfPairs)  
      tstr1 = tstr1 + NumberToStr(-ccy_count[j],"(3");                       // append the currency count to the string
    array_to_sort[j] = tstr1;                                                // load the string into an array to be sorted
  }
  ShellsortStringArray(array_to_sort,0,SortDescending);                      // sort the array alphabetically. Because the formatted number is at the beginning of the
                                                                             //   string, it has priority, but everything in the string gets sorted
  // Display the end result.....
  int xp = HorizPos;
  int yp = VertPos;
  for (j=0; j<8; j++)  {
    if (ccy_count[j] < 1)   continue;
    out_value = StrToNumber(StringSubstr(array_to_sort[j],0,length));                          // extract the value from the string
    tstr1 = StringSubstr(array_to_sort[j],length,3) + NumberToStr(out_value,OutputFormat);     // build a new string to be output, from the sorted array, with the currency sybol first
    if (ShowNoOfPairs)  
      tstr1 = tstr1 + StringSubstr(array_to_sort[j],length+3);                                 // extract and append the currency count to the string
      
    string objname = IndiName+NumberToStr(j,"'-'Z6");
    color colr = DimGray;
    for (int k=0; k<10; k++)  {
      if (out_value <= StrToNumber(clrs[k*2]) && out_value > StrToNumber(clrs[k*2+2]))  {
        colr = StrToColor(clrs[k*2+1]);
        break;  
    } }
    PlotLabel (objname, true, Window, Corner, xp, yp, tstr1, colr, StrToInteger(FNS[1]), FNS[0],  0.0, false, vis);      // Plot text label
    yp += VertSpacing;
  }        
  return(0);
}

//+------------------------------------------------------------------+
#include <hanover --- extensible functions (np).mqh>

