//+------------------------------------------------------------------+
//|                                                 corr_INDICES.mq4 |
//|                                                           cu6yu4 |
//|                                                 cu6yu4@gmail.com |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- parameters
extern bool index1inverse = false;
extern string index1       = "NIKKEI";
extern bool index2inverse = false;
extern string index2       = "DJIA30";
extern double drawingscale     = 1;

//---- buffers
double ExtMapBuffer1[];

//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   IndicatorBuffers(1);
   SetIndexStyle(0,DRAW_LINE, 0, 0);
   SetIndexBuffer(0, ExtMapBuffer1);

//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int i=Bars-ExtCountedBars-1;
   
   while(i>=0)
     {
     
      //---- no holes
      
      string d=TimeToStr(Time[i],TIME_DATE);
      datetime date= StrToTime(d);
      
      int t1=iBarShift(index1,0,Time[i]);
      int t2=iBarShift(index2,0,Time[i]);
      int t01=iBarShift(index1,0,date);
      int t02=iBarShift(index2,0,date);
      int t0=iBarShift(0,0,date);
      
      //---- 

      //---- dif

      double close1=iClose(index1,0,t01);

      if(Close[0]<20)
         {
         close1=close1/10000;
         }
      else if(Close[0]<200)
         {
         close1=close1/100;
         }
      else if(Close[0]<2000)
         {
         close1=close1/10;
         }
      
         //---- inverse function
       
         if(!index1inverse)
            {
            double numclose1=0;
            }
         else
            {
            numclose1=2*close1;
            }
            
         close1=close1-numclose1;
         
         //----
            
      double close2=iClose(index2,0,t02);

      if(Close[0]<20)
         {
         close2=close2/10000;
         }
      else if(Close[0]<200)
         {
         close2=close2/100;
         }
      else if(Close[0]<2000)
         {
         close2=close2/10;
         }
      
         //---- inverse function    
                        
         if(!index2inverse)
            {
            double numclose2=0;
            }
         else
            {
            numclose2=2*close2;
            }
                       
            close2=close2-numclose2;
            
          //----   
         
      double dif=Close[t0]-((close1+close2)*drawingscale);
      
      //---- 

      //---- total

      double buf1=iClose(index1,0,t1);

      if(Close[0]<20)
         {
         buf1=buf1/10000;
         }
      else if(Close[0]<200)
         {
         buf1=buf1/100;
         }
      else if(Close[0]<2000)
         {
         buf1=buf1/10;
         }
            
         //---- inverse function
       
         if(!index1inverse)
            {
            double numbuf1=0;
            }
         else
            {
            numbuf1=2*buf1;
            }
            
         buf1=buf1-numbuf1;
            
         //----
            
      double buf2=iClose(index2,0,t2);

      if(Close[0]<20)
         {
         buf2=buf2/10000;
         }
      else if(Close[0]<200)
         {
         buf2=buf2/100;
         }
      else if(Close[0]<2000)
         {
         buf2=buf2/10;
         }
      
         //---- inverse function    
                        
         if(!index2inverse)
            {
            double numbuf2=0;
            }
         else
            {
            numbuf2=2*buf2;
            }
                       
            buf2=buf2-numbuf2;
            
          //----   
         
      ExtMapBuffer1[i]=((buf1+buf2)*drawingscale)+dif;
      
      //---- 
           
      i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+