Error 5011 - File is Empty

 

Hello,


I am trying to store one small value into file.


Simple, Rule


If file exist, read the value else create new file and store the value

When OnTick condition executed, Rewrite the value.


But i am getting an error 5011 and when i open the .txt file, it always empty and blank. How to solve this?


extern string inc_MoneyManagement = "***Money Management***";
extern bool Money_Management = FALSE;
enum MoneyManagement_enum {Fixed_Money, Balance_Percentage};
extern MoneyManagement_enum MoneyManagement_Type = Balance_Percentage;
extern double Target_Revenue = 1000.0;
double gTargeted_Revenue;

// File to store gTargeted_Revenue value
string MM_File = _Symbol+"targeted_revenue.txt";

int OnInit() {
   // Load gTargeted_Revenue value from file if it exists
   int fileHandle = FileOpen(MM_File, FILE_READ);
   if (fileHandle != INVALID_HANDLE) {
      gTargeted_Revenue = FileReadDouble(fileHandle);
      FileClose(fileHandle);
      Print("Loaded gTargeted_Revenue from file: ", gTargeted_Revenue);
   } else {
      // Create a new file and calculate gTargeted_Revenue
      ResetLastError();
      fileHandle = FileOpen(MM_File, FILE_WRITE);
      if (fileHandle != INVALID_HANDLE) {
         if(MoneyManagement_Type == Balance_Percentage)
            gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance();
         else
            gTargeted_Revenue = Target_Revenue + AccountBalance();

         FileWriteDouble(fileHandle, gTargeted_Revenue);
         FileClose(fileHandle);
         Print("Created new file and wrote gTargeted_Revenue: ", gTargeted_Revenue);
         Print(" File creation Error : " + GetLastError());
      } else {
         Print("Failed to create file: ", MM_File);
         // Handle file creation failure here
      }
   }
}


void OnTick() {
   if(Money_Management && AccountEquity() >= gTargeted_Revenue) {
      for (int i = OrdersTotal() - 1; i >= 0; i--) {
         if (OrderSelect(i, SELECT_BY_POS)==false) continue;
         if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE) == false)
            Print("Order close failed for ticket ", OrderTicket());
      }

      Print("All The Trade Closed due to it exceeded defined Money Management (Target Revenue) : " + AccountEquity());

      // Update gTargeted_Revenue value
      if(MoneyManagement_Type == Balance_Percentage)
         gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance();
      else
         gTargeted_Revenue = Target_Revenue + AccountBalance();

      // Save gTargeted_Revenue value to file
      int fileHandle = FileOpen(MM_File, FILE_WRITE);
      if (fileHandle != INVALID_HANDLE) {
         FileWriteDouble(fileHandle, gTargeted_Revenue);
         FileClose(fileHandle);
      } else {
         Print("Failed to open file for writing: ", MM_File);
         // Handle file writing failure here
      }
   }
}
 
anuj71:

Hello,


I am trying to store one small value into file.


Simple, Rule


If file exist, read the value else create new file and store the value

When OnTick condition executed, Rewrite the value.


But i am getting an error 5011 and when i open the .txt file, it always empty and blank. How to solve this?


Have you ever checked what the error code means?

You need to check your file extension and your opening flags such that they coincide. A .bin file needs to be opened as binary, a txt or CSV file need to be opened as text or csv file...

There are some undocumented limitations, and I am not sure since when they exist.


 
Dominik Egert #:
Have you ever checked what the error code means?

You need to check your file extension and your opening flags such that they coincide. A .bin file needs to be opened as binary, a txt or CSV file need to be opened as text or csv file...

There are some undocumented limitations, and I am not sure since when they exist.


I have already seen that URL and some forum post but still i did not understand how can i code this. Can you help with sample code or more explanation, how can i achieve  

 
anuj71 #:

I have already seen that URL and some forum post but still i did not understand how can i code this. Can you help with sample code or more explanation, how can i achieve  

You need to add a flag to specify the file type

FileOpen(MM_File, FILE_READ |FILE_TXT );

EDIT:

I think FileWriteDouble requires a binary file type.

If you want a text file, you need to convert the double to a string and then write the string to the text file.
 
Dominik Egert #:
You need to add a flag to specify the file type



All i need to add FILE_TXT with FileOpen?


Like this FILE_READ | FILE_TXT and FILE_WRITE | FILE_TXT?


extern string inc_MoneyManagement = "***Money Management***";
extern bool Money_Management = FALSE;
enum MoneyManagement_enum {Fixed_Money, Balance_Percentage};
extern MoneyManagement_enum MoneyManagement_Type = Balance_Percentage;
extern double Target_Revenue = 1000.0;
double gTargeted_Revenue;

// File to store gTargeted_Revenue value
string MM_File = _Symbol+"targeted_revenue.txt";

int OnInit() {
   // Load gTargeted_Revenue value from file if it exists
   int fileHandle = FileOpen(MM_File, FILE_READ |FILE_TXT);
   if (fileHandle != INVALID_HANDLE) {
      gTargeted_Revenue = FileReadDouble(fileHandle);
      FileClose(fileHandle);
      Print("Loaded gTargeted_Revenue from file: ", gTargeted_Revenue);
   } else {
      // Create a new file and calculate gTargeted_Revenue
      ResetLastError();
      fileHandle = FileOpen(MM_File, FILE_WRITE |FILE_TXT);
      if (fileHandle != INVALID_HANDLE) {
         if(MoneyManagement_Type == Balance_Percentage)
            gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance();
         else
            gTargeted_Revenue = Target_Revenue + AccountBalance();

         FileWriteDouble(fileHandle, gTargeted_Revenue);
         FileClose(fileHandle);
         Print("Created new file and wrote gTargeted_Revenue: ", gTargeted_Revenue);
         Print(" File creation Error : " + GetLastError());
      } else {
         Print("Failed to create file: ", MM_File);
         // Handle file creation failure here
      }
   }
}


void OnTick() {
   if(Money_Management && AccountEquity() >= gTargeted_Revenue) {
      for (int i = OrdersTotal() - 1; i >= 0; i--) {
         if (OrderSelect(i, SELECT_BY_POS)==false) continue;
         if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE) == false)
            Print("Order close failed for ticket ", OrderTicket());
      }

      Print("All The Trade Closed due to it exceeded defined Money Management (Target Revenue) : " + AccountEquity());

      // Update gTargeted_Revenue value
      if(MoneyManagement_Type == Balance_Percentage)
         gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance();
      else
         gTargeted_Revenue = Target_Revenue + AccountBalance();

      // Save gTargeted_Revenue value to file
      int fileHandle = FileOpen(MM_File, FILE_WRITE |FILE_TXT);
      if (fileHandle != INVALID_HANDLE) {
         FileWriteDouble(fileHandle, gTargeted_Revenue);
         FileClose(fileHandle);
      } else {
         Print("Failed to open file for writing: ", MM_File);
         // Handle file writing failure here
      }
   }
}
 
anuj71 #:


All i need to add FILE_TXT with FileOpen?


Like this FILE_READ | FILE_TXT and FILE_WRITE | FILE_TXT?


See edit of my previous post, please.

You need to align the data types. The file type with the FileOpen flags, and the data you want to write to the file...


 
anuj71 #:


All i need to add FILE_TXT with FileOpen?


Like this FILE_READ | FILE_TXT and FILE_WRITE | FILE_TXT?


I have moved your thread, because error code 5011 only exists in MQL5, but you posted in MQL4 section.

Also, do a research about the keyword "extern", it has a different meaning in MQL5 than in MQL4.


 
Dominik Egert #:
I have moved your thread, because error code 5011 only exists in MQL5, but you posted in MQL4 section.

Also, do a research about the keyword "extern", it has a different meaning in MQL5 than in MQL4.


My EA is MQL4 EA and i am working on MT4 Platform. I am getting this error on MT4


Dominik Egert #:
See edit of my previous post, please.

You need to align the data types. The file type with the FileOpen flags, and the data you want to write to the file...



I saw your code based on it. i made an edit and posted next to your post. Is my code is correct?

 
anuj71 #:

My EA is MQL4 EA and i am working on MT4 Platform. I am getting this error on MT4



I saw your code based on it. i made an edit and posted next to your post. Is my code is correct?

Yes, you are right, it's MQL4 as well... I've checked the wrong doc. It's one of those undocumented error codes. I moved the thread back to MQL4 section.

Please test end debug your code yourself, I am not sure about FileWriteDouble, as I think it requires a binary file...
 
Dominik Egert #:
I am not sure about FileWriteDouble, as I think it requires a binary file...
I have to add FILE_READ | FILE_TXT in FileWriteDouble?
 
anuj71 #:
I have to add FILE_READ | FILE_TXT in FileWriteDouble?
Please refer to the documentation, everything is explained there for your needs.


Reason: