Writing to a text file in metatrader 4

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

#property indicator_chart_window
//+------------------------------------------------------------------+
//| 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();
//----
int handle;
  string file_name = "Data for "+Symbol()+" of period "+Period();
  handle = FileOpen("test/"+file_name+".txt", FILE_CSV|FILE_WRITE|FILE_READ, ';');
  if(handle>0)
    {
    FileWrite(handle, "Bar #", "Symbol", "Close", "Open", "High", "Low", "Time");
    for (int pos = 0; pos < Bars; pos ++)
      {
      FileWrite(handle, pos, Symbol(), Close[pos], Open[pos], High[pos], Low[pos], TimeToStr(Time[pos], TIME_DATE|TIME_SECONDS));
      }
    FileClose(handle);
    }
  PlaySound("wait.wav");
  
  
  
//----
   return(0);
  }
//+------------------------------------------------------------------+

//+——————————————————————+
//| printtest.mq4 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+——————————————————————+
#property copyright “Copyright 2012, MetaQuotes Software Corp.”
#property link “http://www.metaquotes.net”

#property indicator_chart_window
//+——————————————————————+
//| 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();
//—-
int handle;
string file_name = “Data for “+Symbol()+” of period “+Period();
handle = FileOpen(“test/”+file_name+”.txt”, FILE_CSV|FILE_WRITE|FILE_READ, ‘;’);
if(handle>0)
{
FileWrite(handle, “Bar #”, “Symbol”, “Close”, “Open”, “High”, “Low”, “Time”);
for (int pos = 0; pos < Bars; pos ++) { FileWrite(handle, pos, Symbol(), Close[pos], Open[pos], High[pos], Low[pos], TimeToStr(Time[pos], TIME_DATE|TIME_SECONDS)); } FileClose(handle); } PlaySound("wait.wav"); //---- return(0); } //+------------------------------------------------------------------+

Leave a Reply