Open and write to 2 files at the same time, in 1 indicator

 

I want my indicator to open and write to 2 files  but the indicator opens and writes only to the first file.


I use the names and handles like this to write and open the file.


First I define variables in the global scope:

int handle1=0;

int handle2=10;

string name1=ABC;

string name2= XYZ;


then I define my function :

void myfunction(int & handle, string name)

{

handle =FileOpen( name ,FILE_CSV|FILE_READ|FILE_WRITE,',');

 stuff here

}

but I think FileOpen returns the number +1 to both Handle1 and Handle2, and then MT4 gets confused.


Then I call the function in OnCalculate

myfunction (handle1, name1);

myfunction (handle2, name2);



the result is that only the file1 with name1 is created and filled with data.

File2 with name2 is never created.


If I do not pass the handle by reference, ie NOT doing this 

int & handle,

but only this

int handle,

then the terminal says it can't open or create the first file...

 

Try as follows.

int handle1;
int handle2;
string name1 = "ABC.csv";
string name2 = "XYZ.csv";
int myfunction(string name)
{
   int handle = FileOpen( name, FILE_CSV | FILE_READ | FILE_WRITE, ',');
   return(handle);
}
handle1 = myfunction(name1);
handle2 = myfunction(name2);
 
Yes it's the smart way of doing it