Need help on CSV file write

 

I want to record spread when spread is larger than 10 point ( 5 digits).

but it only one line record

Please anyone has experience in file write/read.......Thanks

 
//+------------------------------------------------------------------+
//|                                                 recordspread.mq4 |                    
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010 MetaQuotes Software Corp"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

extern int spreadlimit = 10;// if spread is more than 10 point ( 5 Digits),then record it

  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
    if(Period()!=PERIOD_M1) return(0);  // only use it on M1 time frame
    
    int handle;
    datetime t1;
     
    int spread = (Ask-Bid)*MathPow(10,Digits);
   
   handle = FileOpen("recordspread.csv",FILE_CSV|FILE_WRITE,",");  
    
   if( spread >spreadlimit )   // 
      {
         t1 = TimeCurrent();
         FileWrite(handle,TimeToStr(t1),Ask,Bid,spread); 
      }
    
     FileClose(handle);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

1) y open & close the file every time for writing (every tick)

2) no need the calculation

int spread = (Ask-Bid)*MathPow(10,Digits);

use

int spread = MarketInfo(Symbol(),MODE_SPREAD);

3) add

FileFlush(handle);
 

Thank you gjol.

I revised as below,but it record only one line data.

Please advise more,thank you.

  int spread = MarketInfo(Symbol(),MODE_SPREAD);
   
   handle = FileOpen("recordspread.csv",FILE_CSV|FILE_WRITE,",");  
    
   if( spread >spreadlimit )   // 
      {
         t1 = TimeCurrent();
         
         FileWrite(handle,TimeToStr(t1),Ask,Bid,spread); 
         FileFlush(handle);          
      }
    
     FileClose(handle); 
 
//+------------------------------------------------------------------+
//|                                                 recordspread.mq4 |                    
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010 MetaQuotes Software Corp"
#property link      "http://www.metaquotes.net"

#property indicator_chart_window

extern int spreadlimit = 10;// if spread is more than 10 point ( 5 Digits),then record it

int handle;  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   handle = FileOpen("recordspread.csv",FILE_CSV|FILE_READ|FILE_WRITE,",");  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
        FileClose(handle);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
    if(Period()!=PERIOD_M1) return(0);  // only use it on M1 time frame
    
    datetime t1;
     
int spread = MarketInfo(Symbol(),MODE_SPREAD);
   
   if( spread >spreadlimit )   // 
      {
         t1 = TimeCurrent();
         FileWrite(handle,TimeToStr(t1),Ask,Bid,spread); 
         FileFlush(handle);    
      }
    
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

Thank you qjol,but it does not work yet.

Reason: