Reading file in Strategy Tester using MQL5

To add comments, please log in or register
jaffer wilson
755
jaffer wilson  

I have created a file using the Python Pandas library like this:

out.to_csv("modular.csv",index=False,encoding="utf-16")

Now I am trying to read the file through the Strategy Tester of the Metatrader5. But getting the following error on the Tester Window:

#property tester_file "modular.csv"
int file;
int OnInit()
  {
   file = FileOpen("modular.csv",FILE_READ|FILE_CSV,',');
   if(file != INVALID_HANDLE)
   {
      Print("File size resising number: ",(int)FileSize(file));
   }
   else{Print("File Not found.");}

   return(INIT_SUCCEEDED);
  }

I am getting the following error:

login (build 1870)
3860 bytes of account info loaded
deposit currency is USD
1482 bytes of tester parameters loaded
7868 bytes of input parameters loaded
4257 bytes of symbols list loaded
expert file added: Experts\modularexpert.ex5. 23022 bytes loaded
file Files\Data\modular.csv read error
requested data synchronization error

See the image:

Strategy Tester

Here is the link for the file: Trying to open this file

Please help me what I should do?

Marco vd Heijden
Moderator
8151
Marco vd Heijden  

You can also just generate it as a txt file.

How are you treating EOL ?

":" ",""\n"" "

jaffer wilson
755
jaffer wilson  
Marco vd Heijden:

You can also just generate it as a txt file.

How are you treating EOL ?

":" ",""\n"" "

The end of line is, what I guess, \n

The newline character.

But sir had you tried it on your end. As I am guessing that the error is the codec of the file or may be the size of the file.

And sir, I would love to use it as csv instead of text file. I love csv format. Pretty good to understand and formulate for me.

Marco vd Heijden
Moderator
8151
Marco vd Heijden  

I mean you can make it as a text file and save it as csv.

This way you can treat it as a normal txt file in Python.

Just add the delimiter hardcoded in between the data in one line.

Or you can create a csv file in mql, save it , and try to alter it in Python to see if it works the way around.

jaffer wilson
755
jaffer wilson  
Marco vd Heijden:

I mean you can make it as a text file and save it as csv.

This way you can treat it as a normal txt file in Python.

Just add the delimiter hardcoded in between the data in one line.

Or you can create a csv file in mql, save it , and try to alter it in Python to see if it works the way around.

I guess, I didn't write the proper question. Let me explain to you.

I have created the data file using MQL5 and saved it as csv file in the folder.

Then I tried to modify the files using the Python code using the pandas library.

After the data file was proper I try to read it through the MQL5 code and then use it in my program.

But the result it was you see , that is , error in reading the file. 

I hope I have made the scenario clear to you.

Please let me know what I can do.

Marco vd Heijden
Moderator
8151
Marco vd Heijden  

Ok i don't really know the answer but it's usually a mismatch in encoding.

I think that there are two options.

The first to alter the encoding

encoding="latin-1")

The second is to specify the encoding at the beginning of your Python script.

# coding: latin1

So that it knows which one to use.

Stanislav Korotky
24522
Stanislav Korotky  

Too much info left unspecified.

Is the file probably locked by other software? In which folder is it placed? Why FILE_SHARE_READ and FILE_UNICODE not flagged? Why utf-16 encoding in python?

Also you save model.csv but attach and read modular.csv.

jaffer wilson
755
jaffer wilson  
Stanislav Korotky:

Too much info left unspecified.

Is the file probably locked by other software? In which folder is it placed? Why FILE_SHARE_READ and FILE_UNICODE not flagged? Why utf-16 encoding in python?

Also you save model.csv but attach and read modular.csv.

Sorry for the confusion regarding the model and modular. I have edited the name of the file.

Is the file probably locked by other software?

I don't know what you mean by the statement. What I understood is that whether the other software is using the same file, then no it is not possible. The file is written onDeinit () and since it is not completely written, it is no use to read it through other software. Please let me know anything required.

In which folder is it placed? 

The file is in the Data folder which in the Files of the MQL5 folder.

Why FILE_SHARE_READ and FILE_UNICODE not flagged?

I don't know what for these flag you want me to include. But I just need to write a CSV file hence, I used only FILE_CSV flag.

Why utf-16 encoding in python?

Because, I was trying to read the file through Pandas library in Python and it was showing encoding issues. After modifying the file then I tried to save it so I used the same encoding what it was before.

Hence, you can see the 'utf-16' encoding.

jaffer wilson
755
jaffer wilson  
The issue comes with the line: #property tester_file "modular.csv"
Stanislav Korotky
24522
Stanislav Korotky  

I see some inconsistency in your words. From the initial post it's clear that you're writing file in python and then reading it in MQL - and this is the point where the problem happens. Yes, you mentioned later that initialy the file was created from MQL, but the problem is about reading back into MQL. Right? Now you say you write file from MQL in the context of MQL's FileOpen(... FILE_READ) for reading. I don't get you.

I don't know what is Pandas, but as any other program, I suppose, it can keep files open, and then it may be locked, and hence inaccessible from MQL (hence FILE_SHARE_READ, but it can be insufficient, if the other program prevents reading as well).

You should probably read about the file flags and apply them accordingly. Since you're using unicode encoding you can't expect it'll work properly without FILE_UNICODE. Also you should fix the issue in python (because usage of utf-16 just as a workaround sounds error prone), or show here the csv-file. Without this it's hard to tell what's wrong.

Also your log reads "Files\Data\modular.csv" while you say the file is in MQL5/Files.

Stanislav Korotky
24522
Stanislav Korotky  

I see you linked a file, but it's too large.

Try to reproduce the problem with a small file. Possibly the size is the matter.

12
To add comments, please log in or register