Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 17

 
Can you tell me where the error is? The values of the array elements Koef[] are calculated, each element is assigned a value. Why isn't Buffer1[] assigned?

#property copyright "Copyright 2016, T"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_level1 0.8
#property indicator_level2 -0.8
#property indicator_levelcolor Black
#property indicator_color1 Blue
#property indicator_minimum -1
#property indicator_width1 2
#property indicator_style1 0
#property indicator_maximum 1

double Buffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//--- indicator buffers mapping
  SetIndexBuffer(0, Buffer1);
  SetIndexStyle(0, DRAW_LINE);
  return(0);
//---
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   int n=25;                                                            // количество элементов в массиве(для периода H1 n=25, тк i<n)
   int m=24;                                                            // количество массивов(для периода H1 m=24, тк p=0, p<m)
   int w=18;                                                            // количество заков после запятой
  
   double Price_CloseX[][24];                                           // Массив цен закрытия 1 пары  
   double Price_CloseY[][24];                                           // Массив цен закрытия 2 пары
   double dx[][24];                                                     // Отклонение от среднего значения для пары 1 dx
   double dy[][24];                                                     // Отклонение от среднего значения для пары 2 dy
   double dx2[][24];                                                    // Квадрат отклонения ср.значения dx2
   double dy2[][24];                                                    // Квадрат отклонения ср.значения dy2
   double dxdy[][24];                                                   // Произведение dx и dy
  
   double sum_x[][24];
   double sum_y[][24];
  
   double Mx[][24];                                                         // Массив среднего значения цен закрытия пары 1 Mx
   double My[][24];                                                         // Массив среднего значения цен закрытия пары 2 My
  
   double Edx2[][24];                                                       // Сумма квадратов отклонений Edx2
   double Edy2[][24];                                                       // Сумма квадратов отклонений Edy2
   double Edxdy[][24];                                                      // Сумма произведений отклонений Edxdy
  
   double Koef[];
  
   ArrayResize(Price_CloseX, n);
   ArrayResize(Price_CloseY, n);
   ArrayResize(dx, n);
   ArrayResize(dy, n);  
   ArrayResize(dx2, n);
   ArrayResize(dy2, n);
   ArrayResize(dxdy, n);
   ArrayResize(sum_x, n);
   ArrayResize(sum_y, n);
   ArrayResize(Mx, n);
   ArrayResize(My, n);
   ArrayResize(Edx2, n);
   ArrayResize(Edy2, n);
   ArrayResize(Edxdy, n);
   ArrayResize(Koef, n);
  
   string sym_x="EURUSD";
   string sym_y="GBPUSD";
  
   for(int i=1; i<n; i++)
      {
      for(int p=0; p<m; p++)
         {
         Price_CloseX[i][p]=iClose(sym_x, PERIOD_H1, i+p);
         Price_CloseY[i][p]=iClose(sym_y, PERIOD_H1, i+p);
        
         }
      }
      
    for(int i=1; i<n; i++)
      {    
      for(int p=0; p<m; p++)
         {  
         sum_x[i][p]=sum_x[i][p-1]+Price_CloseX[i][p];                                        
         sum_y[i][p]=sum_y[i][p-1]+Price_CloseY[i][p];
        
         }        
      }
  
   for(int i=1; i<n; i++)
      {    
      for(int p=0; p<m; p++)
         {      
         Mx[i][p]=sum_x[p+1][m-1]/(n-1);  
         My[i][p]=sum_y[p+1][m-1]/(n-1);
                
         }
       }
  
   for(int i=1; i<n; i++)
      {
      for(int p=0; p<m; p++)
         {
         dx[i][p]=Price_CloseX[i][p]-Mx[i][p];
         dy[i][p]=Price_CloseY[i][p]-My[i][p];
        
         }
      }
    
   for(int i=1; i<n; i++)                                                                  
      {
      for(int p=0; p<m; p++)
         {
         dx2[i][p]=(dx[i][p]*dx[i][p])*1000000;
         dy2[i][p]=(dy[i][p]*dy[i][p])*1000000;
        
         }
      }
    
   for(int i=1; i<n; i++)                                                                  
      {
      for(int p=0; p<m; p++)
         {
         dxdy[i][p]=(dx[i][p]*dy[i][p])*1000000;
        
         }
      }  
                         
   for(int i=1; i<n; i++)                                                                  
      {
      for(int p=0; p<m; p++)
         {
         Edx2[i][p]=(Edx2[i-1][p]+dx2[i][p]);                                        
         Edy2[i][p]=(Edy2[i-1][p]+dy2[i][p]);
         Edxdy[i][p]=(Edxdy[i-1][p]+dxdy[i][p]);
         }
      }
  

      for(int p=0; p<m; p++)
         {
         Koef[p]=Edxdy[n-1][p]/sqrt(Edx2[n-1][p]*Edy2[n-1][p]);
         Buffer1[p]=Koef[p];
         Alert("Коэффициент корреляции Пирсона между ", sym_x, " и ", sym_y, " равен ", DoubleToString(Koef[p], w));
         Alert("Значение буфера ", p, " равно ", Buffer1[p]);
         }
   return(0);
   }
 
Timur1988:
Can you tell me where the error is? The values of the array elements Koef[] are calculated, each element is assigned a value. Why isn't Buffer1[] assigned?

Try to removeAlert from the last loop
 
Renat Akhtyamov:
Try removing the Alert from the last cycle
didn't help(
 
Timur1988:
didn't help(

Good, they're useless there. You'd better use Print(...) instead of alerts, and look at the printout on "Experts" tab

You have the same value Edxdy[n-1][p] being assigned. If in previous loops you have a loop within a loop and "n" index changes, in the last one it doesn't. Last n-1=24.

Edxdy[n-1][p]

Try it boldly - will it assign? //indicator should draw a horizontal black line. However, it will not be visible on a black background. Change the colour of the background, or better the line.

Buffer1[p]=4;

If it will be assigned, you should think - what is written here: Edxdy[24][p]

Hehe, or maybe everything is assigned and drawn, but it's about drawing a black line on a black background?

Here's where the colour, type and thickness of the line are specified:

SetIndexStyle(0, DRAW_LINE, 0, 1, Green);
 
Renat Akhtyamov:

Good, they're useless there. You'd better use Print(...) instead of alerts, and look at the printout on "Experts" tab

You have the same value Edxdy[n-1][p] being assigned. If in previous loops you have a loop within a loop and "n" index changes, in the last one it doesn't. Last n-1=24.

Edxdy[n-1][p]

Try it boldly - will it assign? //indicator should draw a horizontal black line. However, it will not be visible on a black background. Change the colour of the background, or better the line.

Buffer1[p]=4;

If it will be assigned, you should think - what is written here: Edxdy[24][p]

Heh, maybe everything is assigned and drawn, but it's about drawing a black line on a black background?

Here you can see the colour, type and thickness of the line:

SetIndexStyle(0, DRAW_LINE, 0, 1, Green);
Edxdy[n-1][p] - here I have written the sum of product dx and dy. This value is needed to calculate Pearson's correlation coefficient Koef for an array consisting of n-1 elements of the p-th dimension. In this case, I decided to calculate the correlation for 24 bars, starting from the first bar. The result compared to similar calculation of Pearson's correlation coefficient, which calculates a simple script (was taken only one-dimensional array), as well as the site "online currency pair correlation calculator". All the values are the same. I believe that there is an error in declaration of array Buffer1[]. I created a script with similar code, plus array declaration via ArrayResize(Buffer1, n). In script code Buffer1[p]=Koef[p] - values are assigned correctly. There is no indicator in the code(((.
 
Timur1988:
Edxdy[n-1][p] - here I have written the sum of the product of dx and dy. This value is needed to calculate the Pearson correlation coefficient Koef for an array consisting of n-1 elements of the p-th dimension. In this case, I decided to calculate the correlation for 24 bars, starting from the first bar. The result compared to similar calculation of Pearson's correlation coefficient, which calculates a simple script (was taken only one-dimensional array), as well as the site "online currency pair correlation calculator". All the values are the same. I believe that there is an error in declaration of array Buffer1[]. I created a script with similar code, plus array declaration via ArrayResize(Buffer1, n). In script code Buffer1[p]=Koef[p] - values are assigned correctly. Not in the indicator code(((.
Have you read my post?
 
Renat Akhtyamov:
Did you read my post?
Yes, thank you!
SetIndexStyle(0,DRAW_LINE,0,1,Green); - put values, but still doesn't draw a line.
 
Timur1988:
Yes, thank you!
SetIndexStyle(0,DRAW_LINE,0,1,Green); - put values, but still doesn't draw line.
Try also to remove limiters#propertyindicator_minimum-1,#propertyindicator_maximum1, maybe buffer values are drawn behind them.
 
Vitalie Postolache:
Try also removing the#propertyindicator_minimum-1,#propertyindicator_maximum1 limiters, maybe the buffer values are drawn behind them.
Thanks! Removed #property strict - helped)
 
Timur1988:
Thank you! Removed #property strict - helped)
That's not very good. Did you probably get warnings at compile time? You need to get rid of the causes of the warnings in such cases, not #property strict