File Path for saving .csv files in MT5

 

Is there a way of saving the location of .csv files other than in the TERMINAL_DATA_PATH ? I would like to save to a folder off the root.

Such as:

FilePath = "C:" + "\\" + "MT5_Export_History" + "\\" + Symbol() + ".csv";

Thank you!

 
Frank Jarrett :

Is there a way of saving the location of .csv files other than in the TERMINAL_DATA_PATH ? I would like to save to a folder off the root.

Such as:

Thank you!

Use WinAPI


Add: 

[data folder]\MQL5\Include\WinAPI\fileapi.mqh

 

Create a link in the common folder via command shell:

mklink /D MT5_Export_History C:\MT5_Export_History

Then save your file with:

string filename="MT5_Export_History" + "\\" + Symbol() + ".csv";
int fh=FileOpen(filename,FILE_WRITE|FILE_CSV|FILE_ANSI|FILE_COMMON);

You get into the common data folder by File - Open Common Data Folder (MetaEditor menu)

 

I have so far managed to create a directory:

//+------------------------------------------------------------------+
//|                                                   CreateFile.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#property script_show_inputs
#include <WinAPI\fileapi.mqh>
//--- input parameters
input string   InpDirectory="E:\\MT5_Export_History\\";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string path_name=InpDirectory;
   PVOID security_attributes=0;

   int result=CreateDirectoryW(path_name,security_attributes);
   int d=0;
  }
//+------------------------------------------------------------------+

but creating a file in a directory fails.

Files:
 

In general, I can, using WinAPI, create a directory and create a file in it:

//+------------------------------------------------------------------+
//|                                                   CreateFile.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
#property script_show_inputs
#include <WinAPI\fileapi.mqh>
//--- input parameters
input string   InpDirectory="E:\\MT5_Export_History\\";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string path_name=InpDirectory;
   PVOID security_attributes=0;

   int result=CreateDirectoryW(path_name,security_attributes);

   HANDLE handle=-1;
   string file_name=Symbol()+".csv";

   uint desired_access=0;
   
//+------------------------------------------------------------------+
//| share_mode                                                       |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew                               |
//|  0x00000000 Prevents other processes from opening a file or device if they request delete, read, or write access |
//|  0x00000004 FILE_SHARE_DELETE Enables subsequent open operations on a file or device to request delete access    |
//+------------------------------------------------------------------+
   uint share_mode=0x00000004;
   
//+------------------------------------------------------------------+
//| security_attributes                                              |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew                               |
//|   This parameter can be NULL                                     |
//+------------------------------------------------------------------+
   security_attributes=0;
   
//+------------------------------------------------------------------+
//| creation_disposition                                             |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew                               |
//|  2 CREATE_ALWAYS                                                 |
//+------------------------------------------------------------------+   
   uint creation_disposition=2;
   
//+------------------------------------------------------------------+
//| flags_and_attributes                                             |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew                               |
//|  128 (0x80) FILE_ATTRIBUTE_NORMAL                                |
//+------------------------------------------------------------------+
   uint flags_and_attributes=0x80;
   HANDLE template_file=0;

   handle=CreateFileW(path_name+"\\"+file_name,desired_access,share_mode,security_attributes,creation_disposition,flags_and_attributes,template_file);
   if(handle!=INVALID_HANDLE)
      FileClose(handle);
   int d=0;
  }
//+------------------------------------------------------------------+
Files:
 
Vladimir Karputov:

In general, I can, using WinAPI, create a directory and create a file in it:

Hi Vladimir,

I was successful in implementing your code into my EA and creating the desired data path for the csv files to be written, in my case: C:\MT5_Export_History\

Would you please expand a little further and show the proper way to define and call the WriteFile() Function?

HANDLE handle = -1;

uint number_of_bytes_to_write = ???;

uint &number_of_bytes_written = ???;

OVERLAPPED &overlapped = ???;  or  PVOID overlapped = ???;

Witch one of these to use?

int WriteFile(HANDLE file,const ushort &buffer[],uint number_of_bytes_to_write,uint &number_of_bytes_written,OVERLAPPED &overlapped);

int WriteFile(HANDLE file,const ushort &buffer[],uint number_of_bytes_to_write,uint &number_of_bytes_written,PVOID overlapped);


It would be great if you could add the WriteFile() Function to your CreateFile.mq5 code example.

Thank you so much for your excellent help!

 
Frank Jarrett :

Hi Vladimir,

I was successful in implementing your code into my EA and creating the desired data path for the csv files to be written, in my case: C:\MT5_Export_History\

Would you please expand a little further and show the proper way to define and call the WriteFile() Function ?

HANDLE handle = -1;

uint number_of_bytes_to_write = ???;

uint &number_of_bytes_written = ???;

OVERLAPPED &overlapped = ???;  or  PVOID overlapped = ???;

Witch one of these to use?

int WriteFile(HANDLE file,const ushort &buffer[],uint number_of_bytes_to_write,uint &number_of_bytes_written,OVERLAPPED &overlapped);

int WriteFile(HANDLE file,const ushort &buffer[],uint number_of_bytes_to_write,uint &number_of_bytes_written,PVOID overlapped);


It would be great if you could add the WriteFile() Function to your CreateFile.mq5 code example.

Thank you so much for your excellent help!

You need to do:

  1. using MQL5 tools (without WinAPI) create a file, write information to a file, close a file
  2. using WinAPI, simply transfer the file to a new location
LifeHack for trader: four backtests are better than one
LifeHack for trader: four backtests are better than one
  • www.mql5.com
Before the first single test, every trader faces the same question — "Which of the four modes to use?" Each of the provided modes has its advantages and features, so we will do it the easy way — run all four modes at once with a single button! The article shows how to use the Win API and a little magic to see all four testing chart at the same...
 
Vladimir Karputov:

You need to do:

  1. using MQL5 tools (without WinAPI) create a file, write information to a file, close a file
  2. using WinAPI, simply transfer the file to a new location

Ok, I'll do as you instruct but does that mean the WinAPI WriteFile() Function does not work in MQL5?

I tried several different attempts writing to a file but failed:

HANDLE FileHandle = -1;
string FirstLine = "First line of csv file";
uint number_of_bytes_to_write = StringLen(FirstLine);
uint number_of_bytes_written = 0;
//uint number_of_bytes_written[]={0};
PVOID overlapped = 0;

FileHandle = CreateFileW(FileName,desired_access,share_mode,security_attributes,creation_disposition,flags_and_attributes,template_file);

WriteFile(FileHandle,FirstLine,number_of_bytes_to_write,number_of_bytes_written,overlapped);

CreateFileW() is working fine as in your example code. However, I get this same error every time for WriteFile():

'WriteFile' - no one of the overloads can be applied to the function call Currency_Loader V2.3.mq5 352 1

could be one of 2 function(s) Currency_Loader V2.3.mq5 352 1

   int WriteFile(long,const ushort&[],uint,uint&,OVERLAPPED&)   fileapi.mqh 141 8

   int WriteFile(long,const ushort&[],uint,uint&,long)  fileapi.mqh 142 8


 
Frank Jarrett :

Why torture yourself with the WinAPI WriteFile () function if you can safely use MQL5: create a file, write information to a file, and then (if it is VERY necessary) use WinAPI CopyFileW?


Added:

Since there is an easy way through MQL5 (I described this way above), I never tried to find out: "How WinAPI WriteFile Works".

That is, I don’t even have a clue how WinAPI WriteFile works.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
Reason: