File Write Problem (Error: 5002)

 

Hello Folks,

I'm very new to MQL programming.

I just start coding to write LAST TRADED PRICE in external text file. All my effort are failed.

Below is my code. Can anyone please correct my code.


//+------------------------------------------------------------------+
//|                                                       ZimLTP.mq4 |
//|                                                           ZimTek |
//|                                             simson.fdo@gmail.com |
//+------------------------------------------------------------------+
#property copyright "ZimTek"
#property link      "simson.fdo@gmail.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

input bool Record       = False;
input int  Precision    = 2;

int      _Decimals      =  Precision;
long     _ChartID       =  ChartID();
string   _ObjName       =  "";
string   _DirectoryPath =  TerminalInfoString(TERMINAL_DATA_PATH);
string   _DirectorySub  = "\\MQL4\\Experts\\Notes\\"; 
string   _FileName      =  _Symbol+".txt";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {                      
   if (Record==True) 
   {
      ResetLastError();
      //Open the file for writing the LTP value (if the file is absent, it will be created automatically)        
      int file_handle=FileOpen(_DirectoryPath+_DirectorySub+_FileName,FILE_TXT|FILE_READ|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ);
      if(file_handle!=INVALID_HANDLE)
      {
        // 
        FileWrite(file_handle,Close[0]);
        FileClose(file_handle);
        PrintFormat("Data is written, %s file is closed",_DirectoryPath+_DirectorySub+_FileName);
      }
      else
      {        
        //Print error
        PrintFormat("Failed to open %s file in specified location, Error code = %d",_DirectoryPath+_DirectorySub+_FileName,GetLastError());
      }
    }  
    _ObjName = "LTP";
    ObjectCreate(_ChartID,_ObjName, OBJ_LABEL, 0, 0, 0);
    ObjectSetText(_ObjName,"LTP: "+DoubleToStr(Close[0],_Decimals),20, "Calibri", Yellow);
    ObjectSet(_ObjName, OBJPROP_CORNER, 0);
    ObjectSet(_ObjName, OBJPROP_XDISTANCE, 050);
    ObjectSet(_ObjName, OBJPROP_YDISTANCE, 620);
        
   return(rates_total);
  }
//+------------------------------------------------------------------+

Thanks in advance.

 
MQL file operations are restrictive to specific folders only.
RTFM
 
string   _DirectoryPath =  TerminalInfoString(TERMINAL_DATA_PATH);
string   _DirectorySub  = "\\MQL4\\Experts\\Notes\\"; 
⋮
FileOpen(_DirectoryPath+_DirectorySub+_FileName, …

You are trying to write to C:\Users\«username»\AppData\Roaming\MetaQuotes\Terminal\«hexString»\MQL4\Files\C:\Users\«username»\AppData\Roaming\MetaQuotes\Terminal\«hexString»\MQL4\Experts\Notes\«fileName» which is an invalid path (semicolon.)

Perhaps you should read the manual.
and FolderDelete using TERMINAL_DATA_PATH - General - MQL5 programming forum 2017.12.13

 

You are trying to write a file to a directory which is not allowed. Only certain directories predefined by MQL4/5 are allowed.

Read this page of the manual:  https://www.mql5.com/en/docs/files

Documentation on MQL5: File Functions
Documentation on MQL5: File Functions
  • www.mql5.com
For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means cannot be outside the file sandbox. the common folder for all the terminals installed on a computer - usually located in the directory C:\Documents and Settings\All Users\Application...