Author: Integer
Price Data Components
0 Views
0 Downloads
0 Favorites
iCor2
#property copyright "Integer"
#property link      "https://www.mql5.com/ru/users/integer"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum -1
#property indicator_maximum 1
#property indicator_buffers 7
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters

input int      period=14;
input string   symbol2="GBPUSD";

//--- indicator buffers
double         Label1Buffer[];

double         bxa[],bya[],bxy[],bx2[],by2[],y[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {


   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   
   SetIndexBuffer(1,bxa,INDICATOR_CALCULATIONS);   
   SetIndexBuffer(2,bya,INDICATOR_CALCULATIONS);   
   SetIndexBuffer(3,bxy,INDICATOR_CALCULATIONS);   
   SetIndexBuffer(4,bx2,INDICATOR_CALCULATIONS);   
   SetIndexBuffer(5,by2,INDICATOR_CALCULATIONS);  
   SetIndexBuffer(6,y,INDICATOR_CALCULATIONS);        
      
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
  
   int b2=Bars(symbol2,Period());

   if(b2<=0){
      return(0);
   }
   int start;
   if(prev_calculated==0){   
      b2=MathMin(b2,TerminalInfoInteger(TERMINAL_MAXBARS));
      datetime tm[1];
      if(CopyTime(symbol2,Period(),b2-1,1,tm)==-1){
         return(0);
      }
      start=0;
      for(int i=0;i<rates_total;i++){
         
         if(time[i]>=tm[0]){
            start=i;
            break;
         }
      }
      if(start==-1){
         return(0);
      }
      
      ArrayInitialize(Label1Buffer,EMPTY_VALUE);
   }
   else{
      start=prev_calculated-1;
   }
   
   double b[1];
   for(int i=start;i<rates_total;i++){
      if(CopyClose(symbol2,Period(),time[i],1,b)==-1){
         return(0);
      }
      y[i]=b[0];
   }



   if(prev_calculated==0){
      start=start+(period-1);
      bxa[start]=0;
      bxa[start]=0;
      bxy[start]=0;
      bx2[start]=0;
      by2[start]=0;      
      for(int i=start-period+1;i<=start;i++){
         bxa[start]+=close[i];
         bya[start]+=y[i];
         bxy[start]+=close[i]*y[i];
         bx2[start]+=close[i]*close[i];
         by2[start]+=y[i]*y[i];            
      }
      bxa[start]/=period;      
      bya[start]/=period;
      start++;
   }

   


   for(int i=start;i<rates_total;i++){
      
      bxa[i]=bxa[i-1]+(close[i]-close[i-period])/period;
      bya[i]=bya[i-1]+(y[i]-y[i-period])/period;
      bxy[i]=bxy[i-1]-close[i-period]*y[i-period]+close[i]*y[i];      
      bx2[i]=bx2[i-1]-close[i-period]*close[i-period]+close[i]*close[i];            
      by2[i]=by2[i-1]-y[i-period]*y[i-period]+y[i]*y[i];

      double ya2=bya[i]*bya[i];
      double xa2=bxa[i]*bxa[i];      
      double xya=bxa[i]*bya[i];
          
      double sy2=by2[i]-ya2*period;            
      double sx2=bx2[i]-xa2*period;
      
      double cor=(bxy[i]-xya*period)/MathSqrt(sx2*sy2);  

      Label1Buffer[i]=cor;
      
   }


   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments