//+------------------------------------------------------------------+
//|                                                  Indexes_v9L.mq4 |
//|                                         Copyright � 2009, Xupypr |
//|                              http://www.mql4.com/ru/users/Xupypr |
//|                                             ������ �� 27.05.2009 |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2009, Xupypr"
#property link      "http://www.mql4.com/ru/users/Xupypr"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_level1 0
#property indicator_color1 SteelBlue
#property indicator_color2 SteelBlue
#property indicator_color3 OrangeRed
#property indicator_color5 Silver
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
//---- input parameters
extern string Start_Time="2008.08.04 00:00"; // ����� ������ (� ��� ��� ������� ����� 0%)
extern int    Choice_Start_Point=1;          // ����� ����� ������: 1-�� �������� ����, 2-�� ���� �������� ��, 3-�� ��������� �������
extern bool   Use_Alert=false;               // ������������ ����������
extern double Step_Size=0.1;                 // ������� �����, ��� ��������� ������� �� ������ �������� ��������� ������
extern int    Pos=0;                         // ��������� ����������� �� ��������� �����
//---- indicator buffers
double Index[],Max[],Min[],Balance[],Prev[];
double StepAlert[8];
string Currency[8]={"USD","EUR","GBP","JPY","CHF","AUD","CAD","NZD"};
string Symbols[7]={"AUDUSD","EURUSD","GBPUSD","NZDUSD","USDCAD","USDCHF","USDJPY"};

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
 SetIndexBuffer(0,Max);
 SetIndexStyle(0,DRAW_HISTOGRAM);
 SetIndexLabel(0,"Maximum");
 SetIndexEmptyValue(0,0.0);
 SetIndexBuffer(1,Min);
 SetIndexStyle(1,DRAW_HISTOGRAM);
 SetIndexLabel(1,"Minimum");
 SetIndexEmptyValue(1,0.0);
 SetIndexBuffer(2,Index);
 SetIndexStyle(2,DRAW_HISTOGRAM);
 SetIndexLabel(2,"Index");
 SetIndexBuffer(3,Balance);
 SetIndexStyle(3,DRAW_SECTION);
 SetIndexLabel(3,"Balance");
 SetIndexEmptyValue(3,0.0);
 SetIndexBuffer(4,Prev);
 SetIndexStyle(4,DRAW_ARROW);
 SetIndexArrow(4,119);
 SetIndexLabel(4,"Previous");
 IndicatorDigits(2);
 return(0);
}
//+------------------------------------------------------------------+
//|  Custom indicator deinitialization function                      |
//+------------------------------------------------------------------+
int deinit()
{
 for (int i=0;i<8;i++) ObjectDelete(Currency[i]+Pos);
 ObjectDelete("Name"+Pos);
 return(0);
}
//+------------------------------------------------------------------+
//| Indexes foreign exchange                                         |
//+------------------------------------------------------------------+
int start()
{
 int    i,window,shiftbar[7];
 bool   wait=false;
 double change,indexUSD,sprice[7];
 static datetime timebar;
  
 indexUSD=1.0;
 for (i=0;i<7;i++)
 {
  shiftbar[i]=iBarShift(Symbols[i],0,Time[0],true);
  if (shiftbar[i]==-1) wait=true;
  sprice[i]=StartPrice(i);
  if (sprice[i]==-1) wait=true;
  change=iClose(Symbols[i],0,shiftbar[i])/sprice[i];
  if (change!=0)
  {
   if (StringSubstr(Symbols[i],0,3)==Currency[0]) indexUSD*=change;
   if (StringSubstr(Symbols[i],3,3)==Currency[0]) indexUSD/=change;
  } 
 }
 if (wait) return(0);
 if (timebar!=Time[0])
 {
  timebar=Time[0];
  switch (Choice_Start_Point)
  {
   case 1:
   {
    ArrayInitialize(Index,EMPTY_VALUE);
    ArrayInitialize(Max,0.0);
    ArrayInitialize(Min,0.0);
    ArrayInitialize(Balance,0.0);
    ArrayInitialize(StepAlert,0.0);
    ArrayInitialize(Prev,EMPTY_VALUE);
   } break;
   case 2:
   {
    if (Time[0]==iTime(NULL,UpPeriod(),0))
    {
     ArrayInitialize(Index,EMPTY_VALUE);
     ArrayInitialize(Max,0.0);
     ArrayInitialize(Min,0.0);
     ArrayInitialize(Balance,0.0);
     ArrayInitialize(StepAlert,0.0);
     ArrayInitialize(Prev,EMPTY_VALUE);
    }
   }
   default:
   {
    for (i=0;i<8;i++)
    {
     Index[i+10*Pos]=Index[i+1+10*Pos];
     Max[i+10*Pos]=Max[i+1+10*Pos];
     Min[i+10*Pos]=Min[i+1+10*Pos];
     Prev[i+10*Pos]=Prev[i+1+10*Pos];
    }
    Index[8+10*Pos]=EMPTY_VALUE;
    Max[8+10*Pos]=0.0;
    Min[8+10*Pos]=0.0;
    Balance[9+10*Pos]=0.0;
    Balance[10+10*Pos]=0.0;
    Prev[8+10*Pos]=EMPTY_VALUE;
   }
  } 
 }
 indexUSD=100*MathPow(indexUSD,0.125);
 Index[7+10*Pos]=indexUSD-100.0;
 Index[6+10*Pos]=indexUSD*(iClose(Symbols[1],0,shiftbar[1])/sprice[1])-100.0;
 Index[5+10*Pos]=indexUSD*(iClose(Symbols[2],0,shiftbar[2])/sprice[2])-100.0;
 Index[4+10*Pos]=indexUSD/(iClose(Symbols[6],0,shiftbar[6])/sprice[6])-100.0;
 Index[3+10*Pos]=indexUSD/(iClose(Symbols[5],0,shiftbar[5])/sprice[5])-100.0;
 Index[2+10*Pos]=indexUSD*(iClose(Symbols[0],0,shiftbar[0])/sprice[0])-100.0;
 Index[1+10*Pos]=indexUSD/(iClose(Symbols[4],0,shiftbar[4])/sprice[4])-100.0;
 Index[0+10*Pos]=indexUSD*(iClose(Symbols[3],0,shiftbar[3])/sprice[3])-100.0;
 for (i=0;i<8;i++)
 {
  if (Index[i+10*Pos]>StepAlert[i]+Step_Size || Index[i+10*Pos]<StepAlert[i]-Step_Size)
  {
   Prev[i+10*Pos]=StepAlert[i];
   if (Index[i+10*Pos]>0) StepAlert[i]=MathFloor(Index[i+10*Pos]/Step_Size)*Step_Size;
   else StepAlert[i]=MathCeil(Index[i+10*Pos]/Step_Size)*Step_Size;
   if (Use_Alert) Alert("������ ",Currency[7-i]," ������ ������� ",StepAlert[i]);
  }
  if (Index[i+10*Pos]>Max[i+10*Pos]) Max[i+10*Pos]=Index[i+10*Pos];
  if (Index[i+10*Pos]<Min[i+10*Pos]) Min[i+10*Pos]=Index[i+10*Pos];
  Balance[8+10*Pos]=MathMax(Max[ArrayMaximum(Max)],MathAbs(Min[ArrayMinimum(Min)]));
  Balance[9+10*Pos]=Balance[8+10*Pos]*(-1);
  if (ObjectFind(Currency[i]+Pos)==-1)
  {
   window=WindowFind(WindowExpertName());
   ObjectCreate(Currency[i]+Pos,OBJ_TEXT,window,Time[7-i+10*Pos],0);
   ObjectSetText(Currency[i]+Pos,Currency[i]+"   "+DoubleToStr(Index[7-i+10*Pos],2),8,"Arial",Gray);
   ObjectSet(Currency[i]+Pos,OBJPROP_ANGLE,90.0);
  }
  else
  {
   ObjectSet(Currency[i]+Pos,OBJPROP_TIME1,Time[7-i+10*Pos]);
   ObjectSet(Currency[i]+Pos,OBJPROP_PRICE1,0);
   ObjectSetText(Currency[i]+Pos,Currency[i]+"   "+DoubleToStr(Index[7-i+10*Pos],2),8,"Arial",Gray);
  }
 }
 if (ObjectFind("Name"+Pos)==-1)
 {
  window=WindowFind(WindowExpertName());
  ObjectCreate("Name"+Pos,OBJ_TEXT,window,Time[8+10*Pos],0);
  switch (Choice_Start_Point)
  {
   case 1: ObjectSetText("Name"+Pos,"�� �������� ��: "+PeriodToStr(Period()),8,"Arial",Gray); break;
   case 2: ObjectSetText("Name"+Pos,"�� �������� ��: "+PeriodToStr(UpPeriod()),8,"Arial",Gray); break;
   default: ObjectSetText("Name"+Pos,"�� �������: "+Start_Time,8,"Arial",Gray);
  }
  ObjectSet("Name"+Pos,OBJPROP_ANGLE,90.0);
 }
 else
 {
  ObjectSet("Name"+Pos,OBJPROP_TIME1,Time[8+10*Pos]);
  ObjectSet("Name"+Pos,OBJPROP_PRICE1,0);
  switch (Choice_Start_Point)
  {
   case 1: ObjectSetText("Name"+Pos,"�� �������� ��: "+PeriodToStr(Period()),8,"Arial",Gray); break;
   case 2: ObjectSetText("Name"+Pos,"�� �������� ��: "+PeriodToStr(UpPeriod()),8,"Arial",Gray); break;
   default: ObjectSetText("Name"+Pos,"�� �������: "+Start_Time,8,"Arial",Gray);
  }
 }
 return(0);
}

double StartPrice(int n)
{
 double sp,bar;
 switch (Choice_Start_Point)
 {
  case 1:  sp=iOpen(Symbols[n],0,iBarShift(Symbols[n],0,Time[0])); break;
  case 2:
  {
    bar=iBarShift(Symbols[n],UpPeriod(),Time[0],true);
    if (bar==-1) return(-1);
    else sp=iOpen(Symbols[n],UpPeriod(),bar);
  } break;
  default: sp=iOpen(Symbols[n],0,iBarShift(Symbols[n],0,StrToTime(Start_Time)));
 }
 return(sp);
}

int UpPeriod()
{
 int p;
 switch (Period())
 {
  case PERIOD_M1:  p=PERIOD_M5;  break;
  case PERIOD_M5:  p=PERIOD_M15; break;
  case PERIOD_M15: p=PERIOD_M30; break;
  case PERIOD_M30: p=PERIOD_H1;  break;
  case PERIOD_H1:  p=PERIOD_H4;  break;
  case PERIOD_H4:  p=PERIOD_D1;  break;
  case PERIOD_D1:  p=PERIOD_W1;  break;
  case PERIOD_W1:  p=PERIOD_MN1; break;
  case PERIOD_MN1: p=PERIOD_MN1;
 }
 return(p);
}

string PeriodToStr(int period)
{
 string p;
 switch (period)
 {
  case PERIOD_M1:  p="M1";  break;
  case PERIOD_M5:  p="M5";  break;
  case PERIOD_M15: p="M15"; break;
  case PERIOD_M30: p="M30"; break;
  case PERIOD_H1:  p="H1";  break;
  case PERIOD_H4:  p="H4";  break;
  case PERIOD_D1:  p="D1";  break;
  case PERIOD_W1:  p="W1";  break;
  case PERIOD_MN1: p="MN1";
 }
 return(p);
}
//+------------------------------------------------------------------+