CurrencyIndexC

Author: Copyright � 2010, S.Boykov (Mislaid)
Price Data Components
Series array that contains close prices for each bar
Miscellaneous
Implements a curve of type %1Uses files from the file systemIt writes information to file
0 Views
0 Downloads
0 Favorites
CurrencyIndexC
//+------------------------------------------------------------------+
//|                                               CurrencyIndexC.mq4 |
//|                       èíäèêàòîð íàïðàâëåíèÿ äâèæåíèÿ äëÿ 8 âàëþò |
//|                      íà îñíîâå ïðÿìîãî âû÷èñëåíèÿ èíäåêñîâ âàëþò |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, S.Boykov (Mislaid)"
#property link      "E-mail: fort-ino@yandex.ru"

#property indicator_separate_window
#property  indicator_buffers 8

extern   int   ChartBars=300;  // êîëè÷åñòâî òî÷åê íà ãðàôèêå

//---- indicator buffers
double     b0[],b1[],b2[],b3[],b4[],b5[],b6[],b7[];

datetime LastTrial;
bool  NoLabels;
string IndicatorName="CurrencyIndexC";

// ðàáî÷èå ìàññèâû
double   Y0[8],Y1[8],S0[8],D[8];
double   Ra[8,8];

// ÷èñëî âàëþòíûõ ïàð
int SymbolNumber;
// ñïèñîê âàëþòíûõ ïàð, äëèíà çàïèñè äëÿ ïàðû 7 ñèìâîëîâ
string SymbolList="EURUSD GBPUSD AUDUSD EURGBP USDJPY EURJPY GBPJPY AUDJPY CHFJPY CADJPY EURAUD GBPAUD USDCHF EURCHF GBPCHF AUDCHF CADCHF USDCAD EURCAD GBPCAD AUDCAD NZDUSD EURNZD GBPNZD NZDJPY AUDNZD NZDCHF NZDCAD ";

// âàëþòíàÿ êîðçèíà äëÿ ðàñ÷åòà èíäåêñà äîëëàðà
int    BasketCount=6;
string BasketOfCurrencies="EURUSD USDJPY GBPUSD USDCAD USDSEK USDCHF ";

double BasketWeights[6]={-0.576,0.136,-0.119,0.0091,0.0042,0.0036};
double c0=50.14348112;

// ÷èñëî âàëþò
int CurrencyNumber;
// ñïèñîê âàëþò, äëèíà çàïèñè 4 ñèìâîëà
// ïåðâàÿ âàëþòà äîëæíà áûòü USD
string CurrencyList="USD EUR GBP JPY AUD CHF CAD NZD ";
color CurrencyColor[8]={ Coral,Gold,LimeGreen,Blue,DeepPink,Violet,Goldenrod,DarkViolet};
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(8);
   IndicatorDigits(2);
//---- 8 indicator buffers mapping
   SetIndexBuffer(0,b0);
   SetIndexBuffer(1,b1);
   SetIndexBuffer(2,b2);
   SetIndexBuffer(3,b3);
   SetIndexBuffer(4,b4);
   SetIndexBuffer(5,b5);
   SetIndexBuffer(6,b6);
   SetIndexBuffer(7,b7);

//---- drawing settings
   int i;
   string   CurrencyName;
   CurrencyNumber=(StringLen(CurrencyList)+1)/4; // +1, åñëè âûòðóò ïîñëåäíèé ïðîáåë
   if(CurrencyNumber>8)
     {
      CurrencyNumber=8;
      CurrencyList=StringSubstr(CurrencyList,0,4*CurrencyNumber);
     }
   for(i=0; i<CurrencyNumber; i++)
     {
      SetIndexStyle(i,DRAW_LINE,STYLE_SOLID,2,CurrencyColor[i]);
      CurrencyName=StringSubstr(CurrencyList,4*i,3);
      SetIndexLabel(i,CurrencyName);
     }

//---- name for DataWindow and indicator subwindow label
   IndicatorShortName(IndicatorName);

//----
   LastTrial= 0;
   NoLabels = true;
//---- initialization done

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(NoLabels) CreateLabels(); // ïðîäîëæåíèå èíèöèàëèçàöèè
   int NotCountedBars=IndicatorCounted();
//---- last counted will be recounted
   int limit=MathMin(ChartBars,NotCountedBars)-1;
   if(limit<0) return(0);  // all Bars already counted

   if(TimeLocal()-LastTrial<4) return(0);
   LastTrial=TimeLocal();

   int i;

   for(i=limit; i>=0; i--)
     {
      IterationMatrixCount(i);  // âû÷èñëÿåì èòåðàöèîííóþ ìàòðèöó äëÿ ðàñ÷åòà âåñîâ âàëþò
      IterationSolve(i); // èòåðàöèîííîå âû÷èñëåíèå èíäåêñîâ
      if(i==ChartBars-1)
         ArrayCopy(S0,Y0,0,0,CurrencyNumber);  // çàïîìèíàåì ñòàðòîâûå èíäåêñû

      b0[i] =  Y0[0] - S0[0];
      b1[i] =  Y0[1] - S0[1];
      b2[i] =  Y0[2] - S0[2];
      b3[i] =  Y0[3] - S0[3];
      b4[i] =  Y0[4] - S0[4];
      b5[i] =  Y0[5] - S0[5];
      b6[i] =  Y0[6] - S0[6];
      b7[i] =  Y0[7] - S0[7];

      //      if (i == 1) WriteToFile();
     }

   return(0);
// done   
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
double  LastKnownPrice(string SymbolName,datetime cTime)
  {
   int   shift;

   shift=iBarShift(SymbolName,0,cTime,false);
   if(iTime(SymbolName,0,shift)>cTime) shift++;
   return(iClose(SymbolName,0,shift));
  }
//+------------------------------------------------------------------+
void IterationMatrixCount(int shift)
  {
   string FirstCurr,SecondCurr,SymbolName;
   int SymbolIndex,FirstCurrIndex,SecondCurrIndex;
   datetime cTime;
   double r;

   cTime=Time[shift];

   for(SymbolIndex=0; SymbolIndex<SymbolNumber; SymbolIndex++)
     {
      SymbolName = StringSubstr( SymbolList, 7 * SymbolIndex, 6);
      FirstCurr  = StringSubstr( SymbolName, 0, 3);
      SecondCurr = StringSubstr( SymbolName, 3, 3);
      FirstCurrIndex  = StringFind( CurrencyList,  FirstCurr, 0) / 4;
      SecondCurrIndex = StringFind( CurrencyList, SecondCurr, 0) / 4;
      r=LastKnownPrice(SymbolName,cTime);

      if(r!=0)
        {
         Ra[  FirstCurrIndex, SecondCurrIndex] =  r;
         Ra[ SecondCurrIndex,  FirstCurrIndex] = 1.0 / r;
        }
     }
  }
//+------------------------------------------------------------------+
void  IterationSolve(int shift)
  {
   int i,j;
   double   r,r0,s;
   double rUsdIndex;
   rUsdIndex=UsdIndex(shift);
   s=100.0;
   if(rUsdIndex!=0)
     {
      while(s>0.000001)
        {
         s=0.0;
         for(i=0; i<CurrencyNumber; i++)
           {
            r=0.0;
            for(j = 0; j < CurrencyNumber; j++)
               r += Ra[ i, j] * Y1[j];
            r=r/D[i];
            if(i==0) r0=r/rUsdIndex;
            if(r0!=0)
              {
               r=r/r0;
              }
            Y0[i]=r;
            s+=MathAbs(r-Y1[i]);
           }
         ArrayCopy(Y1,Y0,0,0,CurrencyNumber);
        }
      // converse to log scale
      for(i=1; i<CurrencyNumber; i++)
         Y0[i]=MathLog(Y1[i])*100.0;
     }
  }
//+------------------------------------------------------------------+
double UsdIndex(int shift)
  {
   double rIndex=0.0;
   int i;
   string SymbolName;
   datetime cTime;
   cTime=Time[shift];

   for(i=0; i<BasketCount; i++)
     {
      SymbolName=StringSubstr(BasketOfCurrencies,7*i,6);
      rIndex=rIndex+BasketWeights[i]*MathLog(LastKnownPrice(SymbolName,cTime));
     }

   rIndex=c0*MathExp(rIndex);
   return(rIndex);
  }
//+------------------------------------------------------------------+
void WriteToFile()
  {
   string FileName,TimeString;
   FileName="8currencyIndexClear";

   int i,FileHandle,ChrtBars;
   ChrtBars=ChartBars;
   FileHandle=FileOpen(FileName,FILE_CSV|FILE_WRITE,";");
   FileWrite(FileHandle,ChrtBars);
   for(i=0; i<ChrtBars; i++)
     {
      TimeString=TimeToStr(Time[i],TIME_DATE|TIME_MINUTES);
      FileWrite(FileHandle,TimeString,b0[i],b1[i],b2[i],b3[i],b4[i],b5[i],b6[i]);
     }

   FileClose(FileHandle);
  }
//+------------------------------------------------------------------+
//|Â ïðîöåäóðó âûíåñåíû îïåðàöèè, êîòîðûå íå âñåãäà ðàáîòàþò â init()|
//+------------------------------------------------------------------+
void CreateLabels()
  {
   int ChartWindow=WindowFind(IndicatorName);  // WindowFind()
   if(ChartWindow<0) return;

   int i;
   string   CurrencyName;
   for(i=0; i<CurrencyNumber; i++)
     {
      // ñîçäàåì ñïèñîê âàëþò
      CurrencyName=StringSubstr(CurrencyList,4*i,3);
      ObjectCreate(CurrencyName,OBJ_LABEL,ChartWindow,0,0);
      ObjectSetText(CurrencyName,CurrencyName,12,"Times New Roman",CurrencyColor[i]);
      ObjectSet(CurrencyName,OBJPROP_CORNER,1);
      ObjectSet(CurrencyName,OBJPROP_XDISTANCE,4);
      ObjectSet(CurrencyName,OBJPROP_YDISTANCE,i*16);
     }
   NoLabels=false;

// ïîñòðîåíèå, ïîääåðæèâàåìîãî ÄÖ, ñïèñêà âàëþòíûõ ïàð
// òàêæå ïðîâåðêà êîððåêòíîñòè ñïèñêîâ âàëþò è ïàð
   int err;
   double r;
   string FirstCurr,SecondCurr,SymbolName;
   int SymbolIndex,FirstCurrIndex,SecondCurrIndex;

   string ActualSymbolList="";
   SymbolNumber=(StringLen(SymbolList)+1)/7; // +1, åñëè âûòðóò ïîñëåäíèé ïðîáåë
   for(i=0; i<SymbolNumber; i++)
     {
      SymbolName = StringSubstr( SymbolList, 7 * i, 6);
      FirstCurr  = StringSubstr( SymbolName, 0, 3);
      FirstCurrIndex=StringFind(CurrencyList,FirstCurr,0)/4;
      SecondCurr=StringSubstr(SymbolName,3,3);
      SecondCurrIndex=StringFind(CurrencyList,SecondCurr,0)/4;
      if(FirstCurrIndex<0 || SecondCurrIndex<0)
        {
         Print("Îäíà èç âàëþò ïàðû ",SymbolName," íå âõîäèò â ñïèñîê âàëþò");
         Print("Ïðîâåðüòå ñïèñîê ïàð SymbolList. Äëèíà çàïèñè äëÿ ïàðû 7 ñèìâîëîâ. Ïîñëåäíèé ñèìâîë çàïèñè - ðàçäåëèòåëü");
         continue;
        }
      if(FirstCurr!=StringSubstr(CurrencyList,4*FirstCurrIndex,3) || 
         SecondCurr!=StringSubstr(CurrencyList,4*SecondCurrIndex,3))
        {
         Print("Îäíà èç âàëþò ïàðû ",SymbolName," íåêîððåêòíî èùåòñÿ â ñïèñêå âàëþò");
         Print("Ïðîâåðüòå ñïèñîê âàëþò CurrencyList. Äëèíà çàïèñè äëÿ âàëþòû 4 ñèìâîëà. Ïîñëåäíèé ñèìâîë çàïèñè - ðàçäåëèòåëü");
         continue;
        }
      r=MarketInfo(SymbolName,MODE_BID); // MarketInfo()
      err=GetLastError();
      if(err==4106)
        {
         Print(i," îøèáêà 4106 - íåèçâåñòíûé ñèìâîë ",SymbolName);
         Print("Äîáàâüòå ñèìâîë ",SymbolName," â îáçîð ðûíêà");
        }
      else
         ActualSymbolList=ActualSymbolList+SymbolName+" ";
     }
   SymbolList=ActualSymbolList;
   SymbolNumber=StringLen(SymbolList)/7;
// âûñòàâëÿåì íà äèàãîíàëü ìàòðèöû åäèíèöó
   ArrayInitialize(Ra,0.0);
   for(i=0; i<CurrencyNumber; i++)
      Ra[i,i]=1.0;
   ArrayInitialize(Y1,1.0);    // ñòàðòîâûå èíäåêñû âàëþò

                               // âû÷èñëåíèå ÷èñëà íåíóëåâûõ çíà÷åíèé â ñòðîêàõ ìàòðèöû êðîññîâ
   ArrayInitialize(D,1.0); // åäèíèöà íà äèàãîíàëè ìàòðèöû Ra

   for(SymbolIndex=0; SymbolIndex<SymbolNumber; SymbolIndex++)
     {
      SymbolName = StringSubstr( SymbolList, 7 * SymbolIndex, 6);
      FirstCurr  = StringSubstr( SymbolName, 0, 3);
      SecondCurr = StringSubstr( SymbolName, 3, 3);
      FirstCurrIndex  = StringFind( CurrencyList,  FirstCurr, 0) / 4;
      SecondCurrIndex = StringFind( CurrencyList, SecondCurr, 0) / 4;
      if(FirstCurrIndex<0 || SecondCurrIndex<0) continue;
      D[FirstCurrIndex]  += 1.0;
      D[SecondCurrIndex] += 1.0;
     }

// ïðîâåðêà äîñòóïíîñòè âàëþò èç êîðçèíû
   int j;
   string ActualBasket="";
   BasketCount=(StringLen(BasketOfCurrencies)+1)/7; // +1, åñëè âûòðóò ïîñëåäíèé ïðîáåë
   for(i=0; i<BasketCount; i++)
     {
      SymbolName=StringSubstr(BasketOfCurrencies,7*i,6);
      r=MarketInfo(SymbolName,MODE_BID);
      err=GetLastError();
      if(err==4106)
        {
         Print(i," îøèáêà 4106 - íåèçâåñòíûé ñèìâîë ",SymbolName);
         Print("Äîáàâüòå ñèìâîë ",SymbolName," â îáçîð ðûíêà");
        }
      else
        {
         ActualBasket=ActualBasket+SymbolName+" ";
         BasketWeights[j]=BasketWeights[i];
         j++;
        }
     }
   BasketOfCurrencies=ActualBasket;
   BasketCount=StringLen(BasketOfCurrencies)/7;
  }
//+------------------------------------------------------------------+

Comments