Download MetaTrader 5

FileWriteDouble() generates wrong output (strange symbols)

To add comments, please log in or register
Thousands of developers have already published their products in the Market. Join them!
Snelle Moda
602
Snelle Moda 2014.03.27 12:12 

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?

Alain Verleyen
Moderator
30737
Alain Verleyen 2014.03.27 15:35  
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, ',');
Snelle Moda
602
Snelle Moda 2014.03.27 16:09  

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);
   }
Alain Verleyen
Moderator
30737
Alain Verleyen 2014.03.27 17:17  
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.
Snelle Moda
602
Snelle Moda 2014.03.27 17:51  
angevoyageur:
FileWriteDouble doesn't work with a CSV file. Use FileWrite.


Yes it works, thank you.

To add comments, please log in or register