/*
  Modified by Dr Bean
*/

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 Aqua
#property indicator_width1 2
#property indicator_color2 Blue
#property indicator_width2 2
#property indicator_color3 Yellow
#property indicator_width3 2
#property indicator_color4 Green
#property indicator_width4 2
#property indicator_color5 MediumOrchid
#property indicator_width5 2
#property indicator_color6 Red
#property indicator_width6 2
#property indicator_color7 White
#property indicator_width7 2
#property indicator_color8 Black
#property indicator_width8 2
#property indicator_level1 0.0
#property indicator_level2 1.0
#property indicator_level3 -1.0

extern int SMA_variable = 1;
extern int SMA_base = 10;
extern int SMA_unit = 60;
extern bool show_eur = true;
extern bool show_gbp = true;
extern bool show_aud = true;
extern bool show_cad = true;
extern bool show_chf = true;
extern bool show_jpy = true;
extern bool show_usd = true;
extern bool show_labels = true;
extern string eurusd_id = "EURUSD";
extern string gpbusd_id = "GBPUSD";
extern string audusd_id = "AUDUSD";
extern string usdcad_id = "USDCAD";
extern string usdchf_id = "USDCHF";
extern string usdjpy_id = "USDJPY";

double eur_buf[];
double gbp_buf[];
double aud_buf[];
double cad_buf[];
double chf_buf[];
double jpy_buf[];
double usd_buf[];
string indicator_name;
bool   labels_shown=false;

int init() {
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID);
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID);
   SetIndexStyle(3, DRAW_LINE, STYLE_SOLID);
   SetIndexStyle(4, DRAW_LINE, STYLE_SOLID);
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID);
   SetIndexStyle(6, DRAW_LINE, STYLE_SOLID);
   SetIndexBuffer(0, eur_buf);
   SetIndexBuffer(1, gbp_buf);
   SetIndexBuffer(2, aud_buf);
   SetIndexBuffer(3, cad_buf);
   SetIndexBuffer(4, chf_buf);
   SetIndexBuffer(5, jpy_buf);
   SetIndexBuffer(6, usd_buf);
   SetIndexLabel(0, "EUR");
   SetIndexLabel(1, "GBP");
   SetIndexLabel(2, "AUD");
   SetIndexLabel(3, "CAD");
   SetIndexLabel(4, "CHF");
   SetIndexLabel(5, "JPY");
   SetIndexLabel(6, "USD");
   SMA_variable = SMA_variable * SMA_unit / Period();
   SMA_base = SMA_base * SMA_unit / Period();
   indicator_name="G7_USD(" + SMA_variable + "," + SMA_base + ")";
   IndicatorShortName(indicator_name);
   IndicatorDigits(4);
   labels_shown=false;
   return (0);
}

int deinit() {
  for(int i=ObjectsTotal()-1; i>-1; i--)
    if (StringFind(ObjectName(i),"G7_")>=0)  ObjectDelete(ObjectName(i));
  return(0);
}

int start() {
   double ma_avg[6];
   int counted_bars = IndicatorCounted();
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   
   if (labels_shown==false && show_labels==true) show_currency_labels();
   
   for (int i = 0; i < limit; i++) {
      if (SMA_variable == 0) {
         ma_avg[0] = iClose(eurusd_id, 0, i) / iMA(eurusd_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i);
         ma_avg[1] = iClose(gpbusd_id, 0, i) / iMA(gpbusd_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i);
         ma_avg[2] = iClose(audusd_id, 0, i) / iMA(audusd_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i);
         ma_avg[3] = iMA(usdcad_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i) / iClose(usdcad_id, 0, i);
         ma_avg[4] = iMA(usdchf_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i) / iClose(usdchf_id, 0, i);
         ma_avg[5] = iMA(usdjpy_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i) / iClose(usdjpy_id, 0, i);
      } else {
         if (SMA_variable > 0) {
            ma_avg[0] = iMA(eurusd_id, 0, SMA_variable, 0, MODE_SMA, PRICE_CLOSE, i) / iMA(eurusd_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i);
            ma_avg[1] = iMA(gpbusd_id, 0, SMA_variable, 0, MODE_SMA, PRICE_CLOSE, i) / iMA(gpbusd_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i);
            ma_avg[2] = iMA(audusd_id, 0, SMA_variable, 0, MODE_SMA, PRICE_CLOSE, i) / iMA(audusd_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i);
            ma_avg[3] = iMA(usdcad_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i) / iMA(usdcad_id, 0, SMA_variable, 0, MODE_SMA, PRICE_CLOSE, i);
            ma_avg[4] = iMA(usdchf_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i) / iMA(usdchf_id, 0, SMA_variable, 0, MODE_SMA, PRICE_CLOSE, i);
            ma_avg[5] = iMA(usdjpy_id, 0, SMA_base, 0, MODE_SMA, PRICE_CLOSE, i) / iMA(usdjpy_id, 0, SMA_variable, 0, MODE_SMA, PRICE_CLOSE, i);
         }
      }
      if (show_eur)
        eur_buf[i] = ma_avg[0] * (100 / ma_avg[1] + 100.0 + 100 / ma_avg[2] + 100 / ma_avg[3] + 100 / ma_avg[4] + 100 / ma_avg[5]) - 600.0;
      if (show_gbp)
         gbp_buf[i] = ma_avg[1] * (100 / ma_avg[0] + 100.0 + 100 / ma_avg[2] + 100 / ma_avg[3] + 100 / ma_avg[4] + 100 / ma_avg[5]) - 600.0;
      if (show_aud)
         aud_buf[i] = ma_avg[2] * (100 / ma_avg[0] + 100.0 + 100 / ma_avg[1] + 100 / ma_avg[3] + 100 / ma_avg[4] + 100 / ma_avg[5]) - 600.0;
      if (show_cad)
         cad_buf[i] = ma_avg[3] * (100 / ma_avg[0] + 100.0 + 100 / ma_avg[1] + 100 / ma_avg[2] + 100 / ma_avg[4] + 100 / ma_avg[5]) - 600.0;
      if (show_chf)
         chf_buf[i] = ma_avg[4] * (100 / ma_avg[0] + 100.0 + 100 / ma_avg[1] + 100 / ma_avg[2] + 100 / ma_avg[3] + 100 / ma_avg[5]) - 600.0;
      if (show_jpy)
         jpy_buf[i] = ma_avg[5] * (100 / ma_avg[0] + 100.0 + 100 / ma_avg[1] + 100 / ma_avg[2] + 100 / ma_avg[3] + 100 / ma_avg[4]) - 600.0;
      if (show_usd)
         usd_buf[i] = 100 / ma_avg[0] + 100 / ma_avg[1] + 100 / ma_avg[2] + 100 / ma_avg[3] + 100 / ma_avg[4] + 100 / ma_avg[5] - 600.0;
   }
   return (0);
}

void show_currency_labels(){
  int i;
  
  for(i=ObjectsTotal()-1; i>-1; i--)
    if (StringFind(ObjectName(i),"G7_")>=0)  ObjectDelete(ObjectName(i));
  
  i=0;
  
  if (show_eur) {
    currency_label("EUR",i,Aqua); 
    i++;
  }
  if (show_gbp) {
    currency_label("GBP",i,Blue);
    i++;
  }
  if (show_aud) {
    currency_label("AUD",i,Yellow);
    i++;
  } 
  if (show_cad) {
    currency_label("CAD",i,Green);
    i++;
  }
  if (show_chf) {
    currency_label("CHF",i,MediumOrchid);
    i++;
  }
  if (show_jpy) {
    currency_label("JPY",i,Red);
    i++;
  }
  if (show_usd) {
    currency_label("USD",i,White);
    i++;
  }
}
   
void currency_label(string currency, int txt_pos, color txt_color) {
  string obj_id;
  int win_idx=WindowFind(indicator_name);
  
  if (win_idx>0) 
    labels_shown=true;

  obj_id="G7_"+currency;
  if(ObjectFind(obj_id)<0) ObjectCreate(obj_id, OBJ_LABEL, win_idx, 0, 0);  
  ObjectSet(obj_id, OBJPROP_XDISTANCE, 4+txt_pos*35);
  ObjectSet(obj_id, OBJPROP_YDISTANCE, 15);
  ObjectSetText(obj_id, currency, 9, "Arial Black", txt_color);
}

