FileWriteDouble() generates wrong output (strange symbols)

 

Hi

I try to copy the CLOSE price value of a serie of 1 min BARS to a CSV file.

But the CSV file generates wrong output. see below:


Here is my code:


#property indicator_chart_window

#property indicator_buffers 1
#property indicator_plots   1

//--- parameters for writing data to the file
input string             InpFileName      = "Bars.csv"; // file name
input string             InpDirectoryName = "Data";   // directory name

int file_handle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
   {
  
   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[])
   {
 
   static bool bData_Calculation_Check = false;
   
   if(bData_Calculation_Check == false)
         {
   
         static int nHistoryPeriod = 1;
         nHistoryPeriod = Bars(Symbol(), PERIOD_M1) - 1;
         
         double   aData_Close[]; 
       
         ArrayResize(aData_Close, nHistoryPeriod); 
         CopyClose(Symbol(), PERIOD_M1, 1, nHistoryPeriod, aData_Close);
            
         file_handle = FileOpen(InpDirectoryName + "//" + InpFileName, FILE_READ|FILE_WRITE|FILE_BIN, ',');   
         if(file_handle != INVALID_HANDLE)
               {

//--- first, write the size of data sample
               FileWriteDouble(file_handle,(double)nHistoryPeriod);
         
               for(int i = nHistoryPeriod - 1; i >= 0; i--)
                     {
            
                     double dValue = aData_Close[i]; 
            
                     FileWriteDouble(file_handle, dValue);
                                                              
                     }
      
//--- close the file
               FileClose(file_handle);          
               
               bData_Calculation_Check = true; 
               }
         }
   


//--- return value of prev_calculated for next call
   return(rates_total);
   }

What is wrong with this code.

Can somebody help me, please?

 
snelle_moda:

Hi

I try to copy the CLOSE price value of a serie of 1 min BARS to a CSV file.

But the CSV file generates wrong output. see below:


Here is my code:


What is wrong with this code.

Can somebody help me, please?

  • This line is not needed and lead to an error in Journal tab :
         ArrayResize(aData_Close, nHistoryPeriod); 
  • You are creating a binary file, not a csv :
         file_handle = FileOpen(InpDirectoryName + "//" + InpFileName, FILE_READ|FILE_WRITE|FILE_BIN, ',');
 

Thanks for your input.

I have Changed the code with your suggestions but now I get an empty CSV file with no data.

I have searched the forum for a few days but I don't understand what I'm doing wrong.

#property indicator_chart_window

#property indicator_buffers 1
#property indicator_plots   1

//--- parameters for writing data to the file
input string             InpFileName      = "Bars.csv"; // file name
input string             InpDirectoryName = "Data";   // directory name

int file_handle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
   {
  
   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[])
   {

// boolean check (The indicator only needs to be calculated 1 time). 
   static bool bData_Calculation_Check = false;

// Check if the indicator has not yet been calculated.   
   if(bData_Calculation_Check == false)
         {

// Number of BARS to copy   
         static int nHistoryPeriod = 1;
         nHistoryPeriod = Bars(Symbol(), PERIOD_M1) - 1;

// Data array for storage.         
         double   aData_Close[]; 

// Copy data to array.              
         CopyClose(Symbol(), PERIOD_M1, 1, nHistoryPeriod, aData_Close);
      
// Check/create CSV file      
         file_handle = FileOpen(InpDirectoryName + "//" + InpFileName, FILE_READ|FILE_WRITE|FILE_CSV, ',');   

// Check if file is valid.         
         if(file_handle != INVALID_HANDLE)
               {

// Write the size of data sample. (Do I need this????)
               FileWriteDouble(file_handle,(double)nHistoryPeriod);
         
// Loop calculation.
               for(int i = nHistoryPeriod - 1; i >= 0; i--)
                     {

// Variable for CLOSE price value.            
                     double dValue = aData_Close[i];             
                     FileWriteDouble(file_handle, dValue);                                                              
                     }
      
// Close the file.
               FileClose(file_handle);          

// Activate the calculation boolean check (The indicator only needs to be calculated 1 time).               
               bData_Calculation_Check = true; 
               }
         }
   


//--- return value of prev_calculated for next call
   return(rates_total);
   }
 
snelle_moda:

Thanks for your input.

I have Changed the code with your suggestions but now I get an empty CSV file with no data.

I have searched the forum for a few days but I don't understand what I'm doing wrong.

FileWriteDouble doesn't work with a CSV file. Use FileWrite.
 
angevoyageur:
FileWriteDouble doesn't work with a CSV file. Use FileWrite.


Yes it works, thank you.