ohlc expert too export realtime price to csv in mql5

 

hi! 

im writnig a kinda expert that get every price change and store it to a array and export it to a csv file! 

i have some problem to not too copy duplicated data, but i dont get it why its happening

can any one help me pleas


   double      m_open[];        //Open
   double      m_high[];        //High
   double      m_low[];         //Low
   double      m_close[];       //Close
   datetime    m_time[];        //Time
   long        m_rvol[];        //Real Volume
   long        m_tvol[];        //Tick Volume
   string m_row;
int m_file_handle  = FileOpen("filewrite-7.csv",FILE_READ|FILE_WRITE|FILE_CSV);
void OnTick()
  {

//--- get ohlc & volume data. Use one period behind chosen date for heiken-ashi
   CopyOpen(Symbol(),Period(),SYMBOL_TIME-1,SYMBOL_TIME,m_open);
   CopyHigh(Symbol(),Period(),SYMBOL_TIME-1,SYMBOL_TIME,m_high);
   CopyLow(Symbol(),Period(),SYMBOL_TIME-1,SYMBOL_TIME,m_low);
   CopyClose(Symbol(),Period(),SYMBOL_TIME-1,SYMBOL_TIME,m_close);
   CopyTime(Symbol(),Period(),SYMBOL_TIME-1,SYMBOL_TIME,m_time);
   CopyTickVolume(Symbol(),Period(),SYMBOL_TIME-1,SYMBOL_TIME,m_tvol);
   CopyRealVolume(Symbol(),Period(),SYMBOL_TIME-1,SYMBOL_TIME,m_rvol);
   
   for(int x=1;x<ArraySize(m_time);x++)
      {
         StringAdd(m_row,StringFormat("\t%G",m_time[x]));
         StringAdd(m_row,StringFormat("\t%G",m_open[x]));
         StringAdd(m_row,StringFormat("\t%G",m_high[x]));
         StringAdd(m_row,StringFormat("\t%G",m_low[x]));
         StringAdd(m_row,StringFormat("\t%G",m_close[x]));
   
         
         FileWrite(m_file_handle,m_row);
      }
   
}
 

Remember: OHLC data is received from the bar! Example for PERIOD_M1

//+------------------------------------------------------------------+
//|                                                  OHLC to csv.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.00"
//--- input parameters
input string   InpFileName="filewrite-7.csv";
//---
int filehandle=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   filehandle=FileOpen(InpFileName,FILE_WRITE|FILE_READ|FILE_CSV);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   FileClose(filehandle);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlRates rates[];
   datetime time=(datetime)SymbolInfoInteger(Symbol(),SYMBOL_TIME);
   int result=CopyRates(Symbol(),Period(),time-65,time,rates);
   if(result>0)
     {
      for(int i=0; i<result; i++)
        {
         string m_row="";
         
         StringAdd(m_row,TimeToString(rates[i].time));
         StringAdd(m_row,StringFormat("\t%G",rates[i].open));
         StringAdd(m_row,StringFormat("\t%G",rates[i].high));
         StringAdd(m_row,StringFormat("\t%G",rates[i].close));
         StringAdd(m_row,StringFormat("\t%G",rates[i].low));

         FileWrite(filehandle,m_row);
        }
     }
  }
//+------------------------------------------------------------------+
Files:
 
Thanks for the code!! 
 

Hello  Vladimir Karputov and Rezashahabi

I am new at MQL5/MT5.

Thanks for the code, when I compile it in MQL5 it works without any problem but I cound't find the "filewrite-7.csv" folder in my file system. Could you please guide me on this?

 
caglaris:

Hello  Vladimir Karputov and Rezashahabi

I am new at MQL5/MT5.

Thanks for the code, when I compile it in MQL5 it works without any problem but I cound't find the "filewrite-7.csv" folder in my file system. Could you please guide me on this?

Files (unless given a path) are created in the MQL5\Files directory.

More specifically in C:\Users\<username>\AppData\Roaming\MetaQuotes\Terminal\<SomeLongCodeFolderName>\MQL5\Files.

The "Open Data Folder" option in the Meta Editor : File menu takes you to the folder containing the MQL5 directory.

Hope that helps...

Martyn

Reason: