Correlation_v3.1_kroufr





#property copyright "Copyright © 2007, zIG"
#property link      ""

#property indicator_separate_window


#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_width1 1
#property indicator_color2 SkyBlue
#property indicator_width2 2
#property indicator_color3 Green
#property indicator_width3 1

#property indicator_level1 -1.3
#property indicator_level2 0
#property indicator_level3 1.3
#property indicator_levelwidth 1
#property indicator_levelstyle STYLE_DOT

double buff1[];
double buff2[];
double buff3[];

extern int BarsForComment=30;  //êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà êîððåëÿöèè, ðåçóëüòàòû êîòîðîé áóäóò âûâåäåíû â êîììåíòàðèè
extern int BarsForLongTF=30;  //êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà êîððåëÿöèè íà äëèòåëüíîì ïåðèîäå
extern int BarsForShortTF=7; //êîëè÷åñòâî áàðîâ äëÿ ðàñ÷åòà êîððåëÿöèè íà êîðîòêîì ïåðèîäå
int SortMode=2;       // ðåæèì ñîðòèðîâêè 1- ïî âîçðàñòàíèþ, 2 - ïî óáûâàíèþ

extern int HistoryBars=1000;
extern string Base="EURUSD";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
IndicatorBuffers(8); 
	
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,buff1);
   SetIndexEmptyValue(0,0); 
   SetIndexLabel(0,"Êîððåëÿöèÿ çà "+BarsForLongTF+" áàðîâ");
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,buff2);
   SetIndexEmptyValue(1,EMPTY_VALUE); 
   SetIndexLabel(1,"Êîððåëÿöèÿ çà "+BarsForShortTF+" áàðîâ");

   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(2,buff3);
   SetIndexEmptyValue(2,0); 
   SetIndexLabel(2,"Êîððåëÿöèÿ çà "+BarsForLongTF+" áàðîâ");
   IndicatorShortName("Correlation calculator v3.0 ("+Period()+","+Base+" vs "+Symbol()+")");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i;

   


// âûâîä ãðàôèêîâ êîððåëÿöèè íà äëèòåëüíîì è êîðîòêîì ïåðèîäàõ äëÿ áàçîâîé è òåêóùåé âàëþò
   for(i=HistoryBars;i>=0;i--)
   {
    double RL=Rcalc(Symbol(),BarsForLongTF,i);
    double RS=Rcalc(Symbol(),BarsForShortTF,i);
    if(buff1[i+1]+buff3[i+1]>RL)
      {
      buff1[i]=RL;
      buff3[i]=0;
      }
      else
      {
      buff3[i]=RL;
      buff1[i]=0;
      }
//    buff1[i]=RL;
    
    buff2[i]=RS;
   }

string txt=""
+"\n ----------------------------------------------------------------------------------"
+"\n Ðàñ÷åò êîýôôèöèåíòà êîððåëÿöèè ìåæäó âàëþòàìè"
+"\n  Copyright © 2007, zIG & akadex"
+"\n ----------------------------------------------------------------------------------";
Comment(txt);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

double Rcalc(string symb,int Bars_,int shift)
{
         int currday=iBarShift(NULL,PERIOD_D1,iTime(NULL,Period(),shift));
      double avgBase=iMA(Base,PERIOD_D1,Bars_,0,MODE_SMA,PRICE_CLOSE,currday);
      double avgSmb=iMA(symb,PERIOD_D1,Bars_,0,MODE_SMA,PRICE_CLOSE,currday);
      
      double E=0;
      double D=0;
      double F=0;
      
      for(int i=currday;i<=currday+Bars_;i++)
      {
       E+=((iClose(Base,PERIOD_D1,i))-avgBase)*((iClose(symb,PERIOD_D1,i))-avgSmb);
       D+=MathPow(((iClose(Base,PERIOD_D1,i))-avgBase),2);
       F+=MathPow(((iClose(symb,PERIOD_D1,i))-avgSmb),2);
      }
      D=MathSqrt(D);
      F=MathSqrt(F);
      double G=D*F;
      double R=E/G;
return(R);
}
//--------------------------------------------------------------------+
//+------------------------------------------------------------------+








Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar


Indicator Curves created:

Implements a curve of type DRAW_HISTOGRAM

Implements a curve of type DRAW_LINE

Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:

Other Features: