array in function exchange dubt

 
"Hi guys, I have two functions. One is named '_ReturnHOCLVofXcandel', and the other is named '_PearsonCorrelation'. The 'PearsonCorrelation' function calculates the Pearson correlation coefficient. The first function ('_ReturnHOCLVofXcandel') is supposed to return an array with the opening prices of the first instrument. When I call it a second time, I scrape the array of opening prices for a second instrument. After this, I want to insert the scraped data into the 'PearsonCorrelation' function to calculate the correlation. This is my code."

double _PearsonCorrelation(double& data1[], double& data2[])
{
    int n = ArraySize(data1);
    
    // Calcola la media dei dati
    double mean1 = _ArrayAverage(data1);
    double mean2 = _ArrayAverage(data2);
    
    // Calcola la somma dei prodotti dei dati meno le medie
    double sumProduct = 0;
    for (int i = 0; i < n; i++)
    {
        sumProduct += (data1[i] - mean1) * (data2[i] - mean2);
    }
    
    // Calcola la somma dei quadrati delle differenze dei dati meno le medie
    double sumSq1 = 0;
    double sumSq2 = 0;
    for (int Vi = 0; Vi < n; Vi++)
    {
        sumSq1 += MathPow(data1[Vi] - mean1, 2);
        sumSq2 += MathPow(data2[Vi] - mean2, 2);
    }
    
    // Calcola la correlazione di Pearson
    double correlation = sumProduct / MathSqrt(sumSq1 * sumSq2);
    
    return correlation;
}

the program to return open value of some candel  is this

void _ReturnHOCLVofXcandel(double& DataHOCLV[], int ManyCandle, string TypeHOCLV, string HowSymbolWant) {
   int Di = 0;
   int PosArr = 0;
   if (TypeHOCLV == "H") {
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[Di] = iHigh(HowSymbolWant, _Period, Di);
      }
   }
   else if (TypeHOCLV == "O") {
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[Di] = iOpen(HowSymbolWant, _Period, Di);
      }
   }
   else if (TypeHOCLV == "C") {
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[Di] = iClose(HowSymbolWant, _Period, Di);
      }
   }
   else if (TypeHOCLV == "L") {
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[Di] = iLow(HowSymbolWant, _Period, Di);
      }
   }
   else if (TypeHOCLV == "V") {
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[Di] = iVolume(HowSymbolWant, _Period, Di);
      }
   }
   else if (TypeHOCLV == "HOLCV") {
      ArrayResize(DataHOCLV, ArraySize(DataHOCLV) * 5);
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[PosArr++] = iHigh(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iOpen(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iClose(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iLow(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iVolume(HowSymbolWant, _Period, Di);
      }
   }
   else if (TypeHOCLV == "HOLC") {
      ArrayResize(DataHOCLV, ArraySize(DataHOCLV) * 4);
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[PosArr++] = iHigh(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iOpen(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iClose(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iLow(HowSymbolWant, _Period, Di);
      }
   }
}

in main program i try to call with this  code

  double myDataPrimario[];
   double myDataCorrelato[];
   myDataPrimario = _ReturnHOCLVofXcandel(myDataPrimario, 10, "O",Symbol());
   myDataCorrelato = _ReturnHOCLVofXcandel(myDataCorrelato, 10, "O",Cross_Correlato);

but  return me thIs  error

'myDataPrimario' - invalid array access    DEMADE2.mq4    372    4
'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    372    21
'myDataCorrelato' - invalid array access    DEMADE2.mq4    373    4
'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    373    22

anyone  have some idea why ?

Documentation on MQL5: Language Basics / Variables
Documentation on MQL5: Language Basics / Variables
  • www.mql5.com
Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
There are some much typos in your post, it makes it hard to read. Please be more careful and eventually edit and fix them.
 
faustf:

this is my code

Could you please describe the logic you followed using post-increment here:

faustf:
else if (TypeHOCLV == "HOLCV") {
      ArrayResize(DataHOCLV, ArraySize(DataHOCLV) * 5);
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[PosArr++] = iHigh(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iOpen(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iClose(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iLow(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iVolume(HowSymbolWant, _Period, Di);
      }
   }
   else if (TypeHOCLV == "HOLC") {
      ArrayResize(DataHOCLV, ArraySize(DataHOCLV) * 4);
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[PosArr++] = iHigh(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iOpen(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iClose(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iLow(HowSymbolWant, _Period, Di);

What is it there for? What does it do?

 
The block you are referring to has been modified by increasing its capacity five times to accommodate the data of Open, High, Low, Close, and Volume. This allows the first five values to be associated with one candle, and so on. Therefore, the index you see is used to select the various cells of the array.
Vladislav Boyko #:

Could you please describe the logic you followed using post-increment here:

What is it there for? What does it do?

 
faustf #:
The block you are referring to has been modified by increasing its capacity five times to accommodate the data of Open, High, Low, Close, and Volume. This allows the first five values to be associated with one candle, and so on. Therefore, the index you see is used to select the various cells of the array.

My attempt to see which element you are requesting access to:

#property strict
#define INT_NOT_USED 0
#define STRING_NOT_USED ""

void OnStart()
  {
   double someArray[];
   ArrayResize(someArray, 10);
   _ReturnHOCLVofXcandel(someArray, INT_NOT_USED, "HOLC", STRING_NOT_USED);
  }

void _ReturnHOCLVofXcandel(double& DataHOCLV[], int ManyCandle, string TypeHOCLV, string HowSymbolWant) {
   int Di = 0;
   int PosArr = 0;
   if (TypeHOCLV == "HOLC") {
      ArrayResize(DataHOCLV, ArraySize(DataHOCLV) * 4);
      for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         PosArr++;
         PosArr++;
         PosArr++;
         PosArr++;
      }
      Alert(StringFormat("Array size %i, attempt to access element at index %i", ArraySize(DataHOCLV), PosArr - 1));
   }
}

 
but how is possible if  i do
 ArrayResize(DataHOCLV, ArraySize(DataHOCLV) * 4);

suppose  if i sett arraymany candel in top of  function , set 10 he must  give me 40


with  this   for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[PosArr++] = iHigh(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iOpen(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iClose(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iLow(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iVolume(HowSymbolWant, _Period, Di);
      }


for  cicle for 10 time aaaa ok i must  use  ManyCandle  in for because  if i use arraysize  after risize  rigth    but retrun me always  many  error

'myDataPrimario' - invalid array access    DEMADE2.mq4    372    4
'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    372    21
'myDataCorrelato' - invalid array access    DEMADE2.mq4    373    4
'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    373    22


 
faustf:

'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    372    21

Reason is here:

double myDataPrimario[];
double myDataCorrelato[];
myDataPrimario = _ReturnHOCLVofXcandel(myDataPrimario, 10, "O",Symbol());
myDataCorrelato = _ReturnHOCLVofXcandel(myDataCorrelato, 10, "O",Cross_Correlato);

This is hard to comment on. Such code only says that you need to learn the basics of programming

 
faustf #:
but how is possible if  i do

suppose  if i sett arraymany candel in top of  function , set 10 he must  give me 40


with  this   for ( Di = 0; Di < ArraySize(DataHOCLV); Di++) {
         DataHOCLV[PosArr++] = iHigh(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iOpen(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iClose(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iLow(HowSymbolWant, _Period, Di);
         DataHOCLV[PosArr++] = iVolume(HowSymbolWant, _Period, Di);
      }

The number of iterations will be equal to the size of the array. But in each iteration you add 1 to the PosArr 5 times.


It looks like you're spending a lot of time trying to get the code to work, but you don't understand what you're doing. Better spend time learning programming. Or at least try to implement something easier.

A person who writes such functions will not do this:

double myDataPrimario[];
double myDataCorrelato[];
myDataPrimario = _ReturnHOCLVofXcandel(myDataPrimario, 10, "O",Symbol());
myDataCorrelato = _ReturnHOCLVofXcandel(myDataCorrelato, 10, "O",Cross_Correlato);
But if you did write the functions, then why are you passing an array by reference, and then using an assignment operator to get the result of the function?
 

i know  i am not good...., thanks you for reminding me, but i come here because i want to learn.not to show how I am cool when I write code :D, said this i follow the  example here  https://www.mql5.com/en/forum/117484&nbsp; (function with array as return value? ) for return a array at function , i think the syntax is correct , ofcourse i did do a mistake  in for i just fixed  ,but problem return the  same , if  someone can help me or explain me   much better , is  welcome , also after the modify at a script return

'myDataPrimario' - invalid array access    DEMADE2.mq4    372    4
'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    372    21
'myDataCorrelato' - invalid array access    DEMADE2.mq4    373    4
'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    373    22


thanks at all




function with array as return value? - Can I help me with a problem with an array?
function with array as return value? - Can I help me with a problem with an array?
  • 2009.05.23
  • www.mql5.com
Is it possible to give an array as a return value, like this. No but you can do this see could ea really live by order_history alone. If i use you example i get an error message at the point where the function is supposed to return the array
 
faustf #:

i know  i am not good...., thanks you for reminding me

I didn't say it. What I'm talking about is that you are trying to implement complex things, having a misunderstanding of some basic. Try to simplify your task first.

faustf #:

'myDataPrimario' - invalid array access    DEMADE2.mq4    372    4

'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    372    21
'myDataCorrelato' - invalid array access    DEMADE2.mq4    373    4
'_ReturnHOCLVofXcandel' - expression of 'void' type is illegal    DEMADE2.mq4    373    22

These errors are fixed like this:

double myDataPrimario[];
double myDataCorrelato[];
_ReturnHOCLVofXcandel(myDataPrimario, 10, "O",Symbol());
_ReturnHOCLVofXcandel(myDataCorrelato, 10, "O",Cross_Correlato);

But I think there should be "array out of range" for "HOLCV" and "HOLC" (this is not accurate, I didn't check)

 
thanks so much