Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Last active January 1, 2016 16:09
Show Gist options
  • Save currencysecrets/8168911 to your computer and use it in GitHub Desktop.
Save currencysecrets/8168911 to your computer and use it in GitHub Desktop.
Forex MetaTrader Data Downloader - this script is an enhancement to my previously designed script. It will download data to the respective location on your computer and will continually append new data to it as it becomes available. Dependencies: needs WinFile.mqh
//+------------------------------------------------------------------+
//| Example of using WinFile.mqh for reading/writing files |
//| anywhere on the hard disk. WinFile.mqh needs to be present |
//| in the experts\include directory, and "Allow DLL imports" |
//| needs to be turned on. |
//+------------------------------------------------------------------+
#include <WinFile.mqh>
#property copyright "Copyright © 2013 Currency Secrets.com"
#property link "http://www.currencysecrets.com"
// enter the time periods you wish to capture for your currencies into the array below
int PERIODS[5] = { PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1 };
// create a global variable for each time period in the above array
datetime NOWH1, NOWH4, NOWD1, NOWW1, NOWMN1;
// list the location of each time period's containing data folder (in order according
// to how they are listed in the PERIODS array
string FOLDERS[5] = { "c:\\Users\\Administrator\\Dropbox\\DATA\\OANDA\\H1\\",
"c:\\Users\\Administrator\\Dropbox\\DATA\\OANDA\\H4\\",
"c:\\Users\\Administrator\\Dropbox\\DATA\\OANDA\\D1\\",
"c:\\Users\\Administrator\\Dropbox\\DATA\\OANDA\\W1\\",
"c:\\Users\\Administrator\\Dropbox\\DATA\\OANDA\\MN1\\" };
// enter the file extension of each data file here
string FILE_TYPE = ".csv";
// list the currencies you'd like to capture
string CURRENCIES[28] = { "AUDCAD", "AUDCHF", "AUDJPY", "AUDNZD", "AUDUSD",
"CADCHF", "CADJPY",
"CHFJPY",
"EURAUD", "EURCAD", "EURCHF", "EURGBP", "EURJPY", "EURNZD", "EURUSD",
"GBPAUD", "GBPCAD", "GBPCHF", "GBPJPY", "GBPNZD", "GBPUSD",
"NZDCAD", "NZDCHF", "NZDJPY", "NZDUSD",
"USDCAD", "USDCHF", "USDJPY" };
int init() {
// do nothing, but if doing during the weekend you may want to uncomment this
// out so that data begins to be populated:
// start();
}
int start()
{
string sym = Symbol();
if ( NOWH1 != iTime( sym, PERIOD_H1, 0 ) ) {
doPerHour( PERIOD_H1 );
NOWH1 = iTime( sym, PERIOD_H1, 0 );
}
if ( NOWH4 != iTime( sym, PERIOD_H4, 0 ) ) {
doPerHour( PERIOD_H4 );
NOWH4 = iTime( sym, PERIOD_H4, 0 );
}
if ( NOWD1 != iTime( sym, PERIOD_D1, 0 ) ) {
doPerDay( PERIOD_D1 );
NOWD1 = iTime( sym, PERIOD_D1, 0 );
}
if ( NOWW1 != iTime( sym, PERIOD_W1, 0 ) ) {
doPerWeek( PERIOD_W1 );
NOWW1 = iTime( sym, PERIOD_W1, 0 );
}
if ( NOWMN1 != iTime( sym, PERIOD_MN1, 0 ) ) {
doPerMonth( PERIOD_MN1 );
NOWMN1 = iTime( sym, PERIOD_MN1, 0 );
}
}
void doPerHour( int per ) {
writeData( per );
}
void doPerDay( int per ) {
writeData( per );
}
void doPerWeek( int per ) {
writeData( per );
}
void doPerMonth( int per ) {
writeData( per );
}
void writeData( int per ) {
int perCount = ArraySize( PERIODS ),
ccyCount = ArraySize( CURRENCIES ),
p, c, i, b, d, fileHandle;
string fileLoc, lastLine, sym, tempLine;
datetime lastDate;
// loop through each file - getting last bar
// append new data to end of file
// loop through each of the time periods
for ( p = 0; p < perCount; p += 1 ) {
if ( per == PERIODS[p] ) {
// loop through each of the currency pairs
for ( c = 0; c < ccyCount; c += 1 ) {
// get currency pair
sym = CURRENCIES[c];
// get file location
fileLoc = StringConcatenate( FOLDERS[p], sym, FILE_TYPE );
// check if file exists, if it doesn't create it
if ( !DoesFileExist( fileLoc ) ) {
fileHandle = OpenNewFileForWriting( fileLoc, true );
lastDate = 0;
} else {
fileHandle = OpenExistingFileForWriting( fileLoc, true, true );
// loop through contents of file to get last entry
while ( !IsFileAtEnd( fileHandle ) ) {
tempLine = StringTrimRight( ReadLineFromFile( fileHandle ) );
if ( StringLen( tempLine ) > 0 ) lastLine = tempLine;
}
// get date from lastLine
string dataFields[];
StringSplit( lastLine, ",", dataFields );
// date field is first field
lastDate = StrToTime( dataFields[0] );
}
b = iBars( sym, per );
d = MarketInfo( sym, MODE_DIGITS );
// loop through currencies data until you arrive at the next bar
// don't go to current bar as it will NOT be overwritten!
for ( i = b; i > 0; i -= 1 ) {
if ( iTime( sym, per, i ) > lastDate ) {
// you can add conditions on the type of data you want to write to
// file here, such as these:
if ( iHigh( sym, per, i ) != iLow( sym, per, i ) && iVolume( sym, per, i ) > 2 ) {
// write data to file
WriteToFile( fileHandle,
TimeToStr( iTime(sym, per, i), TIME_DATE|TIME_MINUTES) + "," +
sym + "_" + per + "," +
DoubleToStr( iOpen(sym, per, i), d ) + "," +
DoubleToStr( iHigh(sym, per, i), d ) + "," +
DoubleToStr( iLow(sym, per, i), d ) + "," +
DoubleToStr( iClose(sym, per, i), d ) + "," +
DoubleToStr( iVolume( sym, per, i ), 0 ) + "\r\n"
);
}
}
}
// now that we've finished with this currency we will close file and
// move on to the next one!
CloseHandle( fileHandle );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment