Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1549

 
Sergey Izhutov #:

Now I want to

3. Create an array to store deviations

I don't understand the best way to do this. I have two arrays with prices now (MA and Hai values).So I need to loop over the "totalBars" bars? And when the high was greater than the MA200 and store that deviation in the array?

Almost everything is correct. Declare a new array. Set its size by calling ArrayResize(totalBars). Then, in the loop, you simply assign the corresponding difference between the elements from high and MA to each element of the new array. And then do everything else with them - sort and loop through the first 50 elements and save them to a file.

 
Aleksandr Slavskii #:

Didn't notice the conversation was about the script.

If it's a script, then it is:

Don't forget to allow dll in the settings

Alexander, good afternoon!

You helped me a lot by advising me to use kernel32.dll with the following command:

DeleteFile(TerminalInfoString(TERMINAL_DATA_PATH) + "\\Tester\\\cache\\"); // Clearing the Cache to guarantee a real run (and not a replay of the data memorised earlier from the opt file)

Now my programme works perfectly, thank you very much!


If possible, I would like to ask you to give one more command for recording.

My script generates a set of optimal Settings and writes it to the Files sandbox with the name NameSettings, which is generated in the code.

Here is an example of the generated name: 701-2.3-15097.set.

I need to write it to the tester folder: C:\Users\clips\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3\MQL5\Profiles\Tester

I think it can be done with the WriteFile() command from kernel32.dll.

But I don't understand what parameters to write it with.


#import "kernel32.dll"

int DeleteFileW(const string file_name);

long FindFirstFileW(const string file_name, FIND_DATAWW &find_file_data);

int FindNextFileW(long find_file, FIND_DATAWWW &find_file_data);

int FindClose(long find_file);

int WriteFile();

#import


Here is a code snippet of my script:


Now = TimeLocal(); // The moment the name for Settings is formed (this string has already been formed earlier)

TimeToStruct(Now,dt_struct); // Decomposition of the moment of formation

string mnth = dt_struct.mon; // Month, day

int dyy = dt_struct.day;

string dy = dt_struct.day;

if(dyy <= 9)

dy = "0" + dy;

string LimPos = MTTESTER::GetValue(Settings, "LimitPosition"); // LimitPosition is read out

int PosVert = StringFind(LimPos,"|",0);

string razn = StringSubstr(LimPos,PosVert,-1);

StringReplace(LimPos,razn,"");

NameSettings = mnth+dy+"-"+LimPos+"-"+DoubleToString(ProfitNew,0)+".set"; // The name for Settings is formed.

int file_handle=FileOpen("//"+NameSettings,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);

FileWriteString(file_handle,Settings+"\r\n"); // The Settings string is written to the Files sandbox.


This is where I would like to add this write command to the Tester folder.


Help, please!


Regards, Alexander


 
klycko #:

Alexander, good afternoon!

I've taken it out of my code, try to figure it out.

If it doesn't work out, write to me, we'll think together.

//+------------------------------------------------------------------+
#import "user32.dll"
bool CopyFileW(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
#import
//+------------------------------------------------------------------+
void CopyFile(string Name)
  {
   string PathName = TerminalInfoString(TERMINAL_DATA_PATH);
   string Path = PathName + "\\Tester\\cache\\";
   string File = GetFileName(Path);
   string FileNew = PathName + "\\MQL5\\Files\\ResOpt\\" + Name + "\\" + File;
   int aaa = CopyFileW(Path + File, PathName + "\\MQL5\\Files\\ResOpt\\" + File, false);
  }
//+------------------------------------------------------------------+

there is also a function to search for a file

//+------------------------------------------------------------------+
// Получает имя файла оптимизации
string GetFileName(string Path)
  {
   FIND_DATAW FindData;
   FindData.cFileName[0] = 0;
   const long handle = FindFirstFileW(Path + "*.opt*", FindData);
   string FileName = "";
   if(handle != INVALID_HANDLE)
     {
      do
        {
         FileName = ShortArrayToString(FindData.cFileName);
        }
      while(FindNextFileW(handle, FindData));

      FindClose(handle);
     }

   return(FileName);
  }
 
klycko #:

Alexander, good afternoon!

Here I have copied from my code how to copy a file from folder to folder.

I don't remember what I copied and where to, but I think it's what you need))))))

Try to figure it out.

#define  MAX_PATH                       260

struct FILETIME
  {
   uint              dwLowDateTime;
   uint              dwHighDateTime;
  };
struct FIND_DATAW
  {
   uint              dwFileAttributes;
   FILETIME          ftCreationTime;
   FILETIME          ftLastAccessTime;
   FILETIME          ftLastWriteTime;
   uint              nFileSizeHigh;
   uint              nFileSizeLow;
   uint              dwReserved0;
   uint              dwReserved1;
   short             cFileName[MAX_PATH];
   short             cAlternateFileName[14];
  };

#import "user32.dll"
bool CopyFileW(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
long FindFirstFileW(const string file_name, FIND_DATAW &find_file_data);
int  FindNextFileW(long find_file, FIND_DATAW &find_file_data);
int  FindClose(long find_file);
#import
//+------------------------------------------------------------------+
bool IsCopi(string Name)
  {
//---находим файл с фреймами
   string FileName = "";
   string FileNameTemp = "";
   int count = 0;
   long search_handle = FileFindFirst("Tester\\*.mqd", FileNameTemp);
   do
     {
      if(FileNameTemp != "")
         count++;
      if(count > 1)
        {
         if(FileGetInteger("Tester\\" + FileNameTemp, FILE_CREATE_DATE, false) > FileGetInteger("Tester\\" + FileName, FILE_CREATE_DATE, false))
            FileName = FileNameTemp;
        }
      else
         FileName = FileNameTemp;
     }
   while(FileFindNext(search_handle, FileNameTemp));
   FileFindClose(search_handle);

   FileCopy("Tester\\" + FileName, 0, "ResOpt\\" + Name + "\\" + FileName, FILE_REWRITE);
//---
   string PathName = TerminalInfoString(TERMINAL_DATA_PATH);
   string Path = PathName + "\\Tester\\cache\\";
   string File = GetFileName(Path);
   string FileNew = PathName + "\\MQL5\\Files\\ResOpt\\" + Name + "\\" + File;

   int aaa = CopyFileW(Path + File, PathName + "\\MQL5\\Files\\ResOpt\\" + File, false);
   Sleep(300);
   FileCopy("ResOpt\\" + File, 0, "ResOpt\\" + Name + "\\" + File, FILE_REWRITE);
   FileDelete("ResOpt\\" + File);

   return true;
  }
//+------------------------------------------------------------------+
// Получает имя файла оптимизации
string GetFileName(string Path)
  {
   FIND_DATAW FindData;
   FindData.cFileName[0] = 0;
   const long handle = FindFirstFileW(Path + "*.opt*", FindData);
   string FileName = "";
   if(handle != INVALID_HANDLE)
     {
      do
        {
         FileName = ShortArrayToString(FindData.cFileName);
        }
      while(FindNextFileW(handle, FindData));

      FindClose(handle);
     }

   return(FileName);
  }
//+------------------------------------------------------------------+


Yeah, I remembered a little bit. The code is redundant for you. It copies the optimisation file from the tester folder to somewhere. And it chooses the most recent file. I don't think you need this freshness estimation).

ZY all you need from all this is CopyFileW, all the rest is water))))))

 

I added such commands:

string PathName = TerminalInfoString(TERMINAL_DATA_PATH);

string Path = PathName + "\\Files\\";

string FileNew = PathName + "\\MQL5\Profiles\\Tester\" + NameSettings + "\\\";


Print("PathName= ",PathName);

Print("Path= ",Path);

Print("FileNew= ",FileNew);


bool aaa = CopyFileW(Path + NameSettings, FileNew, false);

PrintFormat("Error %d",GetLastError()); // information about failed operation

Print("aaa= ",aaa);


The result is:

PathName= C:\Users\клыков\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3

Path= C:\Users\клыков\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3\Files\

FileNew= C:\Users\клыков\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3\MQL5\Profiles\Tester\702-2.3-15097.set\

Error 4009

aaa= false

ERR_NOTINITIALISED_STRING

4009

Uninitialised string


All paths seem to be defined correctly, but copying does not happen.

The procedure itself is imported from:

#import "kernel32.dll"

int DeleteFileW(const string file_name);

long FindFirstFileW(const string file_name, FIND_DATAWW &find_file_data);

int FindNextFileW(long find_file, FIND_DATAWWW &find_file_data);

int FindClose(long find_file);

bool CopyFileW(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);

//int WriteFile();

#import

 
klycko #:

Added commands such as:

Huz.

Well, for starters, I'd remove the slash at the end of that line.

FileNew= C:\Users\клыков\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3\MQL5\Profiles\Tester\702-2.3-15097.set\

like this.

string FileNew = PathName + "\\MQL5\\Profiles\\Tester\\" + NameSettings;
 

It didn't work.

Same error 4009.


Tried just rewriting the file inside the sandbox. The error is the same.

string PathName = TerminalInfoString(TERMINAL_DATA_PATH);

string Path = PathName + "\\Files\\";

// string FileNew = PathName + "\\\MQL5\\Profiles\\\Tester\\" + NameSettings;

string FileNew = PathName + "\\MQL5\\Files\" + "f "+ NameSettings;

Print("PathName= ",PathName);

Print("Path= ",Path);

Print("FileNew= ",FileNew);


bool aaa = kernel32::CopyFileW(Path + NameSettings, FileNew, false);

PrintFormat("Error %d",GetLastError()); // information about failed operation

Print("aaa= ",aaa);


And the result is as follows:

PathName= C:\Users\lizak\AppData\Roaming\MetaQuotes\Terminal\FA97EA291D4188820508F9D2B5AAD50F

Path= C:\Users\lizak\AppData\Roaming\MetaQuotes\Terminal\FA97EA291D4188820508F9D2B5AAD50F\Files\

FileNew= C:\Users\lizak\AppData\Roaming\MetaQuotes\Terminal\FA97EA291D4188820508F9D2B5AAD50F\MQL5\Files\f702-2.3-15097.set

Error 4009

aaa= false


 
klycko #:

Didn't help.

I created a file in the folder C:\Users\Ya\AppData\Roaming\MetaQuotes\Terminal\FA97EA291D418888820508F9D2B5AAD50F\MQL5\Files with this name File.csv.

I run the script.

The file is copied to the folder C:\Users\Ya\AppData\Roaming\MetaQuotes\Terminal\FA97EA291D418882020508F9D2B5AAD50F as planned.

Everything works.

#import "kernel32.dll"
bool CopyFileW(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
#import
//+------------------------------------------------------------------+
void OnStart()
  {
   string File = "File.csv";
   string Path = TerminalInfoString(TERMINAL_DATA_PATH);
   string LastPath = Path + "\\MQL5\\Files\\" + File;  
   string NewPath = Path + "\\" + File;                
   bool res = kernel32::CopyFileW(LastPath, NewPath, false);
   Print(res);
  }
//+------------------------------------------------------------------+
 
klycko #:

By the way.

123589

 
Aleksandr Slavskii #:

I created a file in the folder C:\Users\Ya\AppData\Roaming\MetaQuotes\Terminal\FA97EA291D418888820508F9D2B5AAD50F\MQL5\Files with the following name File.csv.

Running the script.

The file is copied to the folder C:\Users\Ya\AppData\Roaming\MetaQuotes\Terminal\FA97EA291D418882020508F9D2B5AAD50F as planned.

It's working.

Unfortunately, your code did not work for me.

Then I made it even easier: rewrite the file inside the sandbox, but with a new name:

//+------------------------------------------------------------------+
//|                                                        proba.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#import "kernel32.dll"
bool CopyFileW(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
#import
//+------------------------------------------------------------------+
void OnStart()
  {
   string File = "File.csv"; 
   string Path = TerminalInfoString(TERMINAL_DATA_PATH);
   string LastPath = Path + "\\MQL5\\Files\\" + File;
   string NewPath = Path + "\\MQL5\\Files\\" + "New_" + File;
//   string NewPath = Path + "\\" + File;
   Print("\nPath = ",Path);
   Print("LastPath = ",LastPath);
   Print("NewPath = ",NewPath);

   ResetLastError();
   bool res = kernel32::CopyFileW(LastPath, NewPath, false);
   PrintFormat("Error %d",GetLastError());                          // информация о неудачной операции
   Print("res = ",res);
  }
//+------------------------------------------------------------------+

Here's what it printed:

Path = C:\Users\клыков\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3

LastPath = C:\Users\клыков\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3\MQL5\Files\File.csv

NewPath = C:\Users\клыков\AppData\Roaming\MetaQuotes\Terminal\F58594996208E46D021F1E606AD322F3\MQL5\Files\New_File.csv

Error 0

res = false


No new file appeared in the Files folder.

It is surprising that Error 0 (i.e. there is no error), but res = false


Ichecked on two computers and the result is the same.


I wonder if this code will work for you?


I have dlls allowed.

But still it must be some other settings?