//+------------------------------------------------------------------+
//|                                                  KuklovodWPR.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <Kuklovod.mqh>

#property indicator_separate_window
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_buffers 3
#property indicator_color1 Aquamarine
#property indicator_color2 Red
#property indicator_color3 Aquamarine
#property indicator_width2 3

extern int enPeriod = 20;
extern string ecKitName = "USD";

string acPairNames[];
double afValBuf[];
int anTradeDirs[];

string cIndLabelName = "";

//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
  SetIndexStyle(0,DRAW_LINE);
  SetIndexBuffer(0,ExtMapBuffer1);
  SetIndexStyle(1,DRAW_LINE);
  SetIndexBuffer(1,ExtMapBuffer2);
  SetIndexStyle(2,DRAW_LINE);
  SetIndexBuffer(2,ExtMapBuffer3);
  SetLevelValue(0,0);
  SetLevelValue(1,10);
  SetLevelValue(2,30);
  SetLevelValue(3,70);
  SetLevelValue(4,-10);
  SetLevelValue(5,-30);
  SetLevelValue(6,-70);
//----
  int hEnumPairs=EnumPairs(ecKitName);
  int nCnt;
  int hProps;
  
  if (hEnumPairs==0)
  {
    Alert("EnumPairs() returns invalid handle");
    return(-1);
  }
  if (GetExchObjCmd(hEnumPairs)!=DO_ENUM_PAIRS)
  {
    Alert("EnumPairs() returns invalid command code \"",GetExchObjCmd(hEnumPairs),"\"");
    FreeExchObj(hEnumPairs);
    return(-1);
  }
  
  nCnt=GetExchObjCount(hEnumPairs);
  ArrayResize(acPairNames,nCnt);
  ArrayResize(afValBuf,nCnt);
  ArrayResize(anTradeDirs,nCnt);
  
  for (int i=0;i<nCnt;i++)
  {
    hProps=GetPropSet(hEnumPairs,i);
    acPairNames[i]=GetStrProp(hProps,ID_PAIR);
    anTradeDirs[i]=GetIntProp(hProps,ID_TRADE_DIR);
  }
  
  FreeExchObj(hEnumPairs);
  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
  if (cIndLabelName!="") 
    if (ObjectFind(cIndLabelName)>=0)
      ObjectDelete(cIndLabelName);
  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  if (ArraySize(afValBuf)==0)
    return(-1);

  int nCnt;
  int nCountedBars=IndicatorCounted();
  double fAvg;

  if (nCountedBars<0) 
    return(-1);

  if (nCountedBars>0)
    nCountedBars--;
  nCnt=Bars-nCountedBars;
  for (int i=0; i<nCnt; i++)
  {
    fAvg=0;
    for (int j=0;j<ArraySize(afValBuf);j++)
    {
      afValBuf[j]=(iWPR(acPairNames[j],0,enPeriod,i)+50)*2*anTradeDirs[j];
      fAvg+=afValBuf[j];
    }
    fAvg/=ArraySize(afValBuf);
    ArraySort(afValBuf);
    ExtMapBuffer1[i]=afValBuf[0];
    ExtMapBuffer3[i]=afValBuf[ArraySize(afValBuf)-1];
    ExtMapBuffer2[i]=fAvg;
  }
  
  if (cIndLabelName=="")
  {
    cIndLabelName="KuklovodWPR-"+ecKitName+"-"+WindowFind(WindowExpertName());
    ObjectCreate(cIndLabelName,OBJ_TEXT,WindowFind(WindowExpertName()),Time[0],0);
  }
  ObjectMove(cIndLabelName,0,Time[0],0);
  ObjectSetText(cIndLabelName,"                    "+ecKitName+": "+DoubleToStr(ExtMapBuffer2[0],2),15,"",Red);
  
  return(0);
}
//+------------------------------------------------------------------+