Download MetaTrader 5

QUESTION - File Function

To add comments, please log in or register
Wackena
648
Wackena 2010.11.02 19:41 

I am using FileOpen(filename,FILE_BIN|FILE_WRITE ) to write EA produced data to a file in files folder and is automatically updated as data values change. Then use FileOpen(filename,FILE_BIN|FILE_READ ) in an Indicator to use this data. This works great. The problem is when using the Strategy Tester. FileOpen(filename,FILE_BIN|FILE_WRITE ) files are in the tester/files folder and Indicators can only access files in the experts/file folder. I would like to use visual mode to test EA with Indicator attached to visual mode chart.

Is there a way for Indicators to use FileOpen(filename,FILE_BIN|FILE_READ) to access files in the tester/files folder??

zzuegg
1506
zzuegg 2010.11.02 19:54  
wackena:

I am using FileOpen(filename,FILE_BIN|FILE_WRITE ) to write EA produced data to a file in files folder and is automatically updated as data values change. Then use FileOpen(filename,FILE_BIN|FILE_READ ) in an Indicator to use this data. This works great. The problem is when using the Strategy Tester. FileOpen(filename,FILE_BIN|FILE_WRITE ) files are in the tester/files folder and Indicators can only access files in the experts/file folder. I would like to use visual mode to test EA with Indicator attached to visual mode chart.

Is there a way for Indicators to use FileOpen(filename,FILE_BIN|FILE_READ) to access files in the tester/files folder??

easy: should be possible trough symlinks. (not tested but i cannot see why it shouldn't work)

hard: trough dll's


//z

Ahmed Soliman
15897
Ahmed Soliman 2010.11.02 21:14  

Yes you can access files anywhere with my DLL

You can download and find more details here:

MT4 Files Functions Replacement

Wackena
648
Wackena 2010.11.03 15:32  
codersguru:

Yes you can access files anywhere with my DLL

You can download and find more details here:

MT4 Files Functions Replacement



Thanks. I've been testing and have not found solution with your code for the following:

// Write data to files folder
int handle=FileOpen("DataFile.dat",FILE_BIN|FILE_WRITE);
   if(handle>0)
      {
         FileWriteInteger(handle,1);
         FileWriteDouble(handle,1.1111);
         FileWriteDouble(handle,2.1111);
         FileWriteDouble(handle,3.1111);
         FileWriteDouble(handle,3.1111);
         FileClose(handle);
      }
// Read data from files folder
int data1;
double data2, data3, data4, data5;
int handle1=FileOpen("DataFile.dat",FILE_BIN|FILE_READ);
if(handle1>0)
   {
     data1=FileReadInteger(handle1);
     data2=FileReadDouble(handle1);
     data3=FileReadDouble(handle1);
     data4=FileReadDouble(handle1);
     data5=FileReadDouble(handle1);
     FileClose(handle1);
   }
else Print("File ",name1," failed to open");

Your code writes file to new location and works great, but appears to only write string data. I have not been able to read the string data back into needed variables.

// write data to experts/files folder from Strategy Tester
int handle1=gFileOpen("c:\Program Files\MetaTrader 4\experts\files\FileData.dat",FILE_BIN|FILE_WRITE);
if(handle1>0)
   {
     gFileWrite(handle1,1);
     gFileWrite(handle1,1.1111);
     gFileWrite(handle1,2.1111);
     gFileWrite(handle1,3.1111);
     gFileWrite(handle1,4.1111);
     gFileClose(handle1);
   }

This part does not work to get needed assigned variable values.

// Read data from files folder
int data1;
double data2, data3, data4, data5;
int handle1=gFileOpen("c:\Program Files\MetaTrader 4\experts\files\DataFile.dat",FILE_BIN|FILE_READ);
if(handle1>0)
   {
     string data6=gFileRead(handle1,0);
     string data7=(gFileRead(handle,0);
     string data 8=gFileRead(handle1,0);
     string data9=gFileRead(handle1,0);
     string data10=gFileRead(handle1,0);
     gFileClose(handle1);
   }
data1=StrToInteger(OpenOrders1);
data2=StrToDouble(PreviousClose1);
data3=StrToDouble(OpenPrice1);
data4=StrToDouble(TradeStopLoss1);
data5=StrToDouble(step1);
Ahmed Soliman
15897
Ahmed Soliman 2010.11.03 21:03  

And what do you see when you open the file? I mean the "c:\Program Files\MetaTrader 4\experts\files\DataFile.dat"

I think you should try to enclose the double number into ""

gFileWrite(handle1,"1.1111");
Wackena
648
Wackena 2010.11.04 00:51  

DataFile.dat output.

íÀHø}Ù?íÀHø}Ù?· ì©ÇQ˜?jzÆG·¬?


DataFile.dat with "".

11.11112.11113.11114.1111


Still have problem reading back individual variable values from both files.

zzuegg
1506
zzuegg 2010.11.04 00:53  

i still vote for symlink.

Start-> cmd

cd to your directory.

mklink filename targetFile

done.

Wackena
648
Wackena 2010.11.04 14:39  
zzuegg:

i still vote for symlink.


done.


I'm running Windows XP. "mklink" is for Vista and Windows 7. I believe "junction points" are for symlink in XP. I will need to review if there is a resource app to do this.

Wackena
648
Wackena 2010.11.04 15:53  

codersguru,

Can you modify the mtguru1.dll and gFiles.mqh files to include the following?

int FileWriteInteger(   int handle, int value, int size=LONG_VALUE)
int FileWriteDouble(    int handle, double value, int size=DOUBLE_VALUE)

int FileReadInteger(    int handle, int size=LONG_VALUE)
double FileReadDouble(  int handle, int size=DOUBLE_VALUE)
JC
1456
JC 2010.11.04 17:33  
wackena:

codersguru,

Can you modify the mtguru1.dll and gFiles.mqh files to include the following?

As an alternative...

A while ago I posted a library for reading and writing files anywhere on the hard disk (https://www.mql5.com/en/forum/118999). This uses Windows API calls rather than requiring its own bespoke DLL (but does still require "Allow DLL imports" to be turned on). It could already handle double arrays, but I have updated it to allow reads and writes of individual doubles and ints - updated version attached.

The following test script demonstrates the equivalent of FileWriteInteger() etc:

#property show_inputs

#include <WinFile.mqh>

// User configurable test values to write and then read
extern int  WriteInt1 = 1234567890;
extern int  WriteDouble1 = 12345.6789;
extern int  WriteInt2 = 9876543210;
extern int  WriteDouble2 = 9876.54321;

int start()
{
   string strTestFilename = "c:\\temp\\test.dat";
   
   int fWrite = OpenNewFileForWriting(strTestFilename, true);
   if (!IsValidFileHandle(fWrite)) {
      MessageBox("Unable to open " + strTestFilename + " for writing");
   } else {
      // Write the four parameters in the order int-double-int-double
      WriteIntToFile(fWrite, WriteInt1);
      WriteDoubleToFile(fWrite, WriteDouble1);
      WriteIntToFile(fWrite, WriteInt2);
      WriteDoubleToFile(fWrite, WriteDouble2);
      CloseFile(fWrite);
   }


   int fRead = OpenExistingFileForReading(strTestFilename);
   if (!IsValidFileHandle(fRead)) {
      MessageBox("Unable to open " + strTestFilename + " for reading");
   } else {
      if (ReadIntFromFile(fRead) == WriteInt1) {
         if (ReadDoubleFromFile(fRead) == WriteDouble1) {
            if (ReadIntFromFile(fRead) == WriteInt2) {
               if (ReadDoubleFromFile(fRead) == WriteDouble2) {
                  MessageBox("The two ints and doubles were successfully read back from the file!");
               } else {
                  MessageBox("Read of double 2 failed");
               }
            } else {
               MessageBox("Read of int 2 failed");
            }
         } else {
            MessageBox("Read of double 1 failed");
         }
      } else {
         MessageBox("Read of int 1 failed");
      }

      CloseFile(fRead);
   }
}
Files:
winfile.mqh 28 kb
Wackena
648
Wackena 2010.11.04 19:17  
jjc:

As an alternative...

A while ago I posted a library for reading and writing files anywhere on the hard disk (https://www.mql5.com/en/forum/118999). This uses Windows API calls rather than requiring its own bespoke DLL (but does still require "Allow DLL imports" to be turned on). It could already handle double arrays, but I have updated it to allow reads and writes of individual doubles and ints - updated version attached.

The following test script demonstrates the equivalent of FileWriteInteger() etc:

Thanks a lot. The fWrite code worked great. Did not need to use the fRead code as standard MT-4 embedded file functions can read the fWrite produced file OK. I will use fRead code when file location is outside of standard MT-4 files folders.
12
To add comments, please log in or register