Orders Information to CSV File

 
Hello!


I would test my expert advisor .
I would write to csv file to following information:Orders open,Orders Close,Buy or Sell,and how many bars is the orders(when I in the 15M charts,and the SUM OrderTime is 45minute the bars number is 3)
Can anybody help me how i make this program?Or where find Sample programme.


Very Thanks,and sorry for my english.

I try this first :

int start()
{

int bars_count=Bars;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
if(handle>0)
{
FileWrite(handle, "#","OPEN","CLOSE","HIGH","LOW");
for(int i=0;i<bars_count;i++)
FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
FileFlush(handle);

}

return(0);
}



but i get this error message:File Open- To many Opened Files
 
am2008:
Hello!


I would test my expert advisor .
I would write to csv file to following information:Orders open,Orders Close,Buy or Sell,and how many bars is the orders(when I in the 15M charts,and the SUM OrderTime is 45minute the bars number is 3)
Can anybody help me how i make this program?Or where find Sample programme.


Very Thanks,and sorry for my english.

I try this first :

int start()
{

int bars_count=Bars;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
if(handle>0)
{
FileWrite(handle, "#","OPEN","CLOSE","HIGH","LOW");
for(int i=0;i<bars_count;i++)
FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
FileFlush(handle);

}

return(0);
}



but i get this error message:File Open- To many Opened Files


am2008:

The way I see it is you never close the file after you write the data to it, you need this line:

FileClose(handle);

At the end of the if statement, that way you will not open the file multiple times.

This should solve the error message.

Hope this helps.

Hazem

 
hmmlotfy:

Thanks for your help!
Now,i don not get error message but the expert not working.I can find mydat.csv.Or first I need create this file in files directory?

int start()
{

int bars_count=Bars;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
if(handle>0)
{
FileWrite(handle, "#","OPEN","CLOSE","HIGH","LOW");
for(int i=0;i<bars_count;i++)
FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
FileFlush(handle);

}
FileClose(handle);




Thanks
 

am2008:

If you check the documentation:

https://docs.mql4.com/files/FileOpen

You will find the automatically it when you use file open with a "FILE_WRITE" it will automatically create it in either:

terminal_directory\experts\files

or:

terminal_directory\tester\files

this depends on how you are using the expert, if you are using the tester to testing the expert then it will reside in the second folder, if testing it live then it will reside in the first folder.

One more thing I am not sure if that’s what you want, but from the way you coded this it seems that the EA will keep trying to write the information of each bar into the file and repeat that at every tick after its done which is redundant. because for the most part it will repeat the data over and over, if that’s not what you wanted then i suggest having the code in the init function instead, that way it will write the data once before the EA start, and there is other ways of doing this too. So let me know if you need more help.

Hope this helps

Hazem

 
Thanks!
But,In terminal_directory\experts\files, terminal_directory\tester\files not found the file:( What i make wrong?

I find this code(https://www.mql5.com/en/articles/1489)


#property indicator_chart_window
extern int length = 100;   // The amount of bars sent to be processed
double ExtMap[];           // Chart buffer
string nameData;
int init()
{
   nameData = Symbol()+".txt";         // name of the data file to be sent
   return(0);
}
int start()
{
   static int old_bars = 0;   // remember the amount of bars already known   
   if (old_bars != Bars)      // if a new bar is received 
   {
      write_data();                             // write the data file                              
   }      
   old_bars = Bars;              // remember how many bars are known
   return(0);
}
//+------------------------------------------------------------------+
void write_data()
{
  int handle;
  handle = FileOpen(nameData, FILE_CSV|FILE_WRITE,';');
  if(handle < 1)
  {
    Comment("Creation of "+nameData+" failed. Error #", GetLastError());
    return(0);
  }
  FileWrite(handle, ServerAddress(), Symbol(), Period());                  // heading
  FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME");   // heading
  int i;
  for (i=length-1; i>=0; i--)
  {
    FileWrite(handle, TimeToStr(Time[i], TIME_DATE), TimeToStr(Time[i], TIME_SECONDS),
                      High[i], Low[i], Close[i], Open[i], Volume[i]);
  }
  FileClose(handle);
  Comment("File "+nameData+" has been created. "+TimeToStr(TimeCurrent(), TIME_SECONDS) );
  return(0);
}
The programm write me :File EURUSD.txt has been created but I not found in files directory(in tester or expert) I use windows vista




Thanks
 

"The programm write me :File EURUSD.txt has been created but I not found in files directory(in tester or expert).

I use windows vista"


Your code works.

Output:

ODL-MT4 Demo;GBPJPY;240
DATE;TIME;HIGH;LOW;CLOSE;OPEN;VOLUME
2008.01.24;20:00:00;212.02;210.73;211.96;210.84;2152
2008.01.25;00:00:00;212.09;211.42;211.92;211.91;1858
2008.01.25;04:00:00;213.49;211.83;213.41;211.93;3531
2008.01.25;08:00:00;214.03;212.55;212.69;213.49;5110
2008.01.25;12:00:00;213.83;212.13;212.76;212.67;4740
.....
.....
 
phy:

"The programm write me :File EURUSD.txt has been created but I not found in files directory(in tester or expert).

I use windows vista"


Your code works.

Output:

ODL-MT4 Demo;GBPJPY;240
DATE;TIME;HIGH;LOW;CLOSE;OPEN;VOLUME
2008.01.24;20:00:00;212.02;210.73;211.96;210.84;2152
2008.01.25;00:00:00;212.09;211.42;211.92;211.91;1858
2008.01.25;04:00:00;213.49;211.83;213.41;211.93;3531
2008.01.25;08:00:00;214.03;212.55;212.69;213.49;5110
2008.01.25;12:00:00;213.83;212.13;212.76;212.67;4740
.....

Thanks!



I run this code in windows xp .And is working!.Now I looking for the problem why don't make files in windows vista.I try run as Administrator.







.....

 

Hi am2008,


I have exactly the same problem as you, it is to say, I use MT4 to create CSV files on windows VISTA. How did you solve the problem?

Thanks,

D.