Error 5004 and file open error

 

I wrote an expert that saves the information of my trades in a text file every few seconds.

It works fine in the first few hours, but after a while it gives error 5004.

My code:

//+------------------------------------------------------------------+
//|                                                            g.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
input int time_ontimer=2;
input string filename="2";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(time_ontimer);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {

   int handle=FileOpen(filename+".txt",FILE_TXT|FILE_WRITE|FILE_ANSI);
   if(handle==INVALID_HANDLE)
     {
      Print("handle failed !!! >>> ",GetLastError());
      return;
     }
   long  magic;
   string sym;
   ulong  ticket;
   double sl;
   double tp;
   double lot;
   long total=PositionsTotal();
   for(long i=total-1; i>=0; i--)
     {
      ticket=PositionGetTicket(i);
      sym=PositionGetString(POSITION_SYMBOL);
      magic=PositionGetInteger(POSITION_MAGIC);
      sl=PositionGetDouble(POSITION_SL);
      tp=PositionGetDouble(POSITION_TP);
      lot=PositionGetDouble(POSITION_VOLUME);
      if((datetime)PositionGetInteger(POSITION_TIME)<=D'2023.09.01 11:00:00')continue;
      FileWriteString(handle,(string)(sym)+"\n");
      FileWriteString(handle,(string)(magic)+"\n");
      FileWriteString(handle,(string)(sl)+"\n");
      FileWriteString(handle,(string)(tp)+"\n");
      FileWriteString(handle,(string)(lot)+"\n");
     }
   FileClose(handle);
   int err=GetLastError();
   Print("Files has created error = ",err);
   err=0;
  }
//+------------------------------------------------------------------+


What do you think is the problem? Why should it stop working after a few hours and keep giving error 5004?

Help me please.
 
Click on the automated links underneath your post to get an answer: https://www.mql5.com/en/forum/11189#comment_452517
File error 5004
File error 5004
  • 2013.03.19
  • www.mql5.com
Stuck again!! two problems with the code below...
Reason: