Help with export data

Back to topics list To post a new topic, please log in or register
avatar
29
castann86 2015.06.06 12:31
 
Hi all,
The following code allows me to download data from EURUSD, of timeframe M30, regardless of the timeframe in which it is attached.
The data is exported in real time, ie, each new bar is exported at the time that appears in mt4.

How I have to modify the code to add a second currency, eg USDCHF, so that in "Files", appear two folders with the name of the currency and the period and takes place in real time?

 

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_width1 2
#property indicator_color1 Tomato
double ExtMap[];
extern int       length = 200;

int init()
{
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMap);
   return(0);
}

int start()
{
   static int rea = 0;
   static int old_bars = 0;   // 
   if (old_bars != iBars("EURUSD",PERIOD_M30))
   {
      delete_result();
      clear_graf();
      write_data(); // 
   }
   
   int handle_read = FileOpen("EURUSD"+"_M30"+"_result.csv",FILE_CSV|FILE_READ,';');
   if(handle_read >= 0)
   { 
      Comment("EURUSD"+"_M30" );
      read_n_draw(handle_read);
      delete_result();
      FileClose(handle_read);
      rea=0;
   }
   else 
   {
      rea++;
      Comment("descargar: "+"EURUSD_M30"+"#"+GetLastError()+"#rea"+rea);
      //FileClose(handle_read);
   }
   old_bars = iBars("EURUSD",PERIOD_M30);  // 
   return(0);
}
//+------------------------------------------------------------------+
void read_n_draw(int handle_read)
{
   int i=0;
   while ( !FileIsEnding(handle_read) )
   {
      ExtMap[i] = FileReadNumber(handle_read);
      i++;     
      Comment(ExtMap[0]); 
   }
   ExtMap[i-1] = EMPTY_VALUE;
   
}

void clear_graf()
{
   ArrayInitialize( ExtMap, EMPTY_VALUE);
   return(0);
}

void delete_result()
{
   
   string filename = "EURUSD"+ "_M30" + "_result.csv";
   FileDelete(filename);
   return(0);
} 
 
void write_data()
{
  int handle;
  string EURUSD;
  string _M30;
  string filename =  "EURUSD"+ "_M30"  + ".txt"; // 
  handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';');
  if(handle < 1)
  {
    Print(" #", GetLastError());
    return(0);
    //FileClose(handle);
  }
  
  FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // 
  int i;
  for (i=length-1; i>=0; i--)
  {
  
  double O,H,L,C, T, V;
  
         O = iOpen (EURUSD, PERIOD_M30, i);
         H = iHigh (EURUSD, PERIOD_M30, i);      
         L = iLow  (EURUSD, PERIOD_M30, i);      
         C = iClose(EURUSD, PERIOD_M30, i); 
         T = iTime (EURUSD, PERIOD_M30, i); 
         V = iVolume (EURUSD, PERIOD_M30, i);
                     
        
         
       
         
    FileWrite(handle, TimeToStr(T, TIME_DATE), TimeToStr(T, TIME_SECONDS),
                      H, L, C, O, V);
  }
  FileClose(handle);
  return(0);
}

 Thank you very much in advance.

avatar
29
castann86 2015.06.06 12:57 #
 

I try this, but the txt of GPBUSD has the same values that the txt of EURUSD:

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_width1 2
#property indicator_color1 Tomato
double ExtMap[];
double ExtMap1[];
extern int       length = 200;

int init()
{
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, ExtMap);
   return(0);
}

int start()
{
   static int rea = 0;
   static int old_bars = 0; 
   static int rea1 = 0;
   static int old_bars1 = 0;
   
   if (old_bars != iBars("EURUSD",PERIOD_M30))
   {
      delete_result();
      clear_graf();
      write_data(); // 
   }
   
   int handle_read = FileOpen("EURUSD"+"_M30"+"_result.csv",FILE_CSV|FILE_READ,';');
   if(handle_read >= 0)
   { 
      Comment("EURUSD"+"_M30" );
      read_n_draw(handle_read);
      delete_result();
      FileClose(handle_read);
      rea=0;
   }
   else 
   {
      rea++;
      Comment("descargar: "+"EURUSD_M30"+"#"+GetLastError()+"#rea"+rea);
      //FileClose(handle_read);
   }
   old_bars = iBars("EURUSD",PERIOD_M30);  // 
   
   if (old_bars != iBars("GPBUSD",PERIOD_M30))
   {
      delete_result1();
      clear_graf1();
      write_data1(); // 
   }
   
   int handle_read1 = FileOpen("GPBUSD"+"_M30"+"_result.csv",FILE_CSV|FILE_READ,';');
   if(handle_read1 >= 0)
   { 
      Comment("GPBUSD"+"_M30" );
      read_n_draw1(handle_read1);
      delete_result1();
      FileClose(handle_read1);
      rea1=0;
   }
   else 
   {
      rea1++;
      Comment("descargar: "+"GPBUSD"+"#"+GetLastError()+"#rea"+rea1);
      //FileClose(handle_read);
   }
   old_bars1 = iBars("GPBUSD",PERIOD_M30);  // 
   
   
   return(0);
   
}
//+------------------------------------------------------------------+
void read_n_draw(int handle_read)
{
   int i=0;
   while ( !FileIsEnding(handle_read) )
   {
      ExtMap[i] = FileReadNumber(handle_read);
      i++;     
      Comment(ExtMap[0]); 
   }
   ExtMap[i-1] = EMPTY_VALUE;
   
}

void read_n_draw1(int handle_read1)
{
   int i=0;
   while ( !FileIsEnding(handle_read1) )
   {
      ExtMap1[i] = FileReadNumber(handle_read1);
      i++;     
      Comment(ExtMap1[0]); 
   }
   ExtMap1[i-1] = EMPTY_VALUE;
   
}


void clear_graf()
{
   ArrayInitialize( ExtMap, EMPTY_VALUE);
   return(0);
}

void clear_graf1()
{
   ArrayInitialize( ExtMap1, EMPTY_VALUE);
   return(0);
}


void delete_result()
{
   
   string filename = "EURUSD"+ "_M30" + "_result.csv";
   FileDelete(filename);
   return(0);
} 
 
void delete_result1()
{
   
   string filename1 = "GPBUSD"+ "_M30" + "_result.csv";
   FileDelete(filename1);
   return(0);
}  
 
 
void write_data()
{
  int handle;
  string EURUSD;
  string _M30;
  string filename =  "EURUSD"+ "_M30"  + ".txt"; // 
  handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';');
  if(handle < 1)
  {
    Print(" #", GetLastError());
    return(0);
    //FileClose(handle);
  }
  
  FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // 
  int i;
  for (i=length-1; i>=0; i--)
  {
  
  double O,H,L,C, T, V;
  
         O = iOpen (EURUSD, PERIOD_M30, i);
         H = iHigh (EURUSD, PERIOD_M30, i);      
         L = iLow  (EURUSD, PERIOD_M30, i);      
         C = iClose(EURUSD, PERIOD_M30, i); 
         T = iTime (EURUSD, PERIOD_M30, i); 
         V = iVolume (EURUSD, PERIOD_M30, i);
                     
        
         
       
         
    FileWrite(handle, TimeToStr(T, TIME_DATE), TimeToStr(T, TIME_SECONDS),
                      H, L, C, O, V);
  }
  FileClose(handle);
  return(0);
}


void write_data1()
{
  int handle1;
  string GPBUSD;
  string _M30;
  string filename1 =  "GPBUSD"+ "_M30"  + ".txt"; // 
  handle1 = FileOpen(filename1,FILE_CSV|FILE_WRITE,';');
  if(handle1 < 1)
  {
    Print(" 1#", GetLastError());
    return(0);
    //FileClose(handle);
  }
  
  FileWrite(handle1, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // 
  int i;
  for (i=length-1; i>=0; i--)
  {
  
  double O1,H1,L1,C1, T1, V1;
  
         O1 = iOpen (GPBUSD, PERIOD_M30, i);
         H1 = iHigh (GPBUSD, PERIOD_M30, i);      
         L1 = iLow  (GPBUSD, PERIOD_M30, i);      
         C1 = iClose(GPBUSD, PERIOD_M30, i); 
         T1 = iTime (GPBUSD, PERIOD_M30, i); 
         V1 = iVolume (GPBUSD, PERIOD_M30, i);
                     
        
         
       
         
    FileWrite(handle1, TimeToStr(T1, TIME_DATE), TimeToStr(T1, TIME_SECONDS),
                      H1, L1, C1, O1, V1);
  }
  FileClose(handle1);
  return(0);
}

 And is very code for two currencies

avatar
12985
WHRoeder 2015.06.06 13:39 #
 
Uninitialized variable, bogus values
 Try this (same with GPBUSD)
  string EURUSD;                             // G A R B A G E   I N
:  
         O = iOpen (EURUSD, PERIOD_M30, i);  // G A R B A G E   O U T
  string EURUSD="EURUSD";

avatar
126
Demos 2015.06.06 15:27 #
 

hello,

Please, make up your program structure first before starting to code things out. For that matter, start with something simple, as e.g. copy all existing bars to a file succesfully.  After, try to do it in multiple pairs; hint - for that you can use as a file handler an array, like filehandle[i]=FileOpen(). Then try to make up your program to handle the situation of having to deal with already existent files (which probably already contain a number of bars) and so on

That way you should be more able to resolve issues as, and if, they are arriving, and you will have a more complete "picture" of how complex your software is going to be 

 

best regards 

Back to topics list  

To add comments, please log in or register