How to add column names when writing to CSV File?

 

Hello, I cannot figure out how to add column names to my csv file when mql4 outputs it. Has anyone managed to get around this?

My EA outputs the chart data every 30 seconds in a CSV, the idea is then to be able to process this in python, column names would be very helpful for this. Can someone please take a look at this?

Thank you.


static datetime Switch;

int OnInit()
  {
//--- create timer
   Switch = TimeCurrent();
   
//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
    double Spread = AskPrice - BidPrice;
   
    
    
    if(TimeCurrent() > (Switch+30)){
    Alert("Symbol: ", Symbol(), "DateTime: " ,CurrentTime ,",", "Ask: ",AskPrice,",","High:",HighPrice, ",", "Low: ",LowPrice, "," , "Volumes:",Volumes ,", " , "Spread: " , Spread);
    
    string file="export_"+Symbol()+"_"+Period()+".csv";
    int f=FileOpen(file,FILE_CSV|FILE_WRITE,",");
     if(f<1) {
     Alert("File opening error");
     return(0);
            }
     for(int i=0;i<Bars;i++) {
      FileWrite(f,StrToTime(Time[i]),
       Open[i],High[i],Low[i],Close[i],Volume[i],Spread);
        }

         Alert("Export "+Symbol()+" finished. Exported: "+Bars+" records");

          FileFlush(f);
          FileClose(f);
     
            
    Switch = TimeCurrent();
    }
 
Rahul Shaji Parmeshwar: I cannot figure out how to add column names to my csv file when mql4 outputs it. Has anyone managed to get around this?
    int f=FileOpen(file,FILE_CSV|FILE_WRITE,","); if    for(int i=0;i<Bars;i++)  FileWrite(…}
    FileClose(f);

There is no getting around anything: PICNIC. Open the file, write the headers, write the data. Close the file. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help (2017.04.21)

 

@William Roeder Thank you for your clarification, I managed to figure it out with your help. Now however, I need to split the date from the time and since mql4 outputs the date and time as a string it is a bit difficult. Could you provide any clarity on this please? I've also attached the fixed code for the data exporter with corrected column names in case anybody needs to use it in the future. 

int OnInit()
  {

   Switch = TimeCurrent();
   

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
    
    if(TimeCurrent() > (Switch+30)){
    
    
    string file="export_"+Symbol()+"_"+Period()+".csv";
    int f=FileOpen(file,FILE_CSV|FILE_WRITE,",");
     if(f<1) {
     Alert("File opening error");
     return(0);
            }
     if(f>0){
      FileSeek(f,0,SEEK_SET);
      FileWrite(f,"DateTime","Open","High","Low","Close","Volume","Spread");
       }
     for(int i=0;i<Bars;i++) {
      FileWrite(f,StrToTime(Time[i]),
       Open[i],High[i],Low[i],Close[i],Volume[i],Spread);
        }

         Alert("Export "+Symbol()+" finished. Exported: "+Bars+" records");

          FileFlush(f);
          FileClose(f);
     
            
    Switch = TimeCurrent();
    }
    
    
  }