vendredi 20 mai 2011

How to export historical quotes

Metatrader allows you to export historical quotes by hand. But it's rather cumbersome. Here's how to do it by script:

 //+------------------------------------------------------------------+  
 //| script program start function                  |  
 //+------------------------------------------------------------------+  
 int start()  
  {  
 //----  
   string symbols[]={"EURUSD","EURGBP"};  
   int periods[] = {1,5,15,60,240,1440,10080,43200};  
   int count = ArraySize(symbols);  
   int counttf = ArraySize(periods);   
   string symbol;  
   int period;  
   for (int ii=0; ii<count; ii++){    
    symbol = symbols[ii];  
    for (int jj=0; jj<counttf; jj++){   
      period = periods[jj];  
      export(symbol, period);  
    }  
   }    
 //----  
   Alert("the end");  
   return(0);  
  }  
 //+------------------------------------------------------------------+  
 int export(string symbol, int theperiod)  
  {  
   string filename = StringSubstr(symbol,0,6) + theperiod + ".csv";  
   int handle = FileOpen(filename, FILE_CSV|FILE_WRITE, ";");  
   if(handle>0)  
   {  
    // table column headers recording  
    FileWrite(handle, "Time;Open;High;Low;Close;Volume");  
    // data recording  
    for(int i=iBars(symbol,theperiod); i>=0; i--) {  
     datetime t = iTime(symbol,theperiod,i);  
     // int year = TimeYear(Time[i]);  
     int year = TimeYear(t);  
     if (year > 1970) {  
      string strdatetime = TimeToStr(t);  
      strdatetime = StringSetChar(strdatetime,4,'-');  
      strdatetime = StringSetChar(strdatetime,7,'-');  
      double Open_i = iOpen(symbol,theperiod,i);  
      double High_i = iHigh(symbol,theperiod,i);  
      double Low_i = iLow(symbol,theperiod,i);  
      double Close_i = iClose(symbol,theperiod,i);  
      double Volume_i = iVolume(symbol,theperiod,i);  
      FileWrite(handle, strdatetime, Open_i, High_i, Low_i, Close_i, Volume_i);  
     }  
    }  
    FileClose(handle);  
   }  
   return(0);  
  }  

Source code formatted with http://codeformatter.blogspot.com

Aucun commentaire:

Enregistrer un commentaire