MQL5 Exposrt Historical Data
File Directory:
C:\Documents and Settings\barnix\Application Data\MetaQuotes\Tester\D0E8209F77C8CF37AD8BF550E51FF075\Agent-127.0.0.1-3000\MQL5\Files
//+------------------------------------------------------------------+
//| MetaTrader5.mq5 |
//| Copyright 2010, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
string FileName="";
int copied=0;
int FileHandle=0;
MqlDateTime tm;
MqlRates rates[1];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
// file name formation, (Symbol+Period+Month) EURUSD_M1_09.txt
FileName=Symbol()+""+fTimeFrameName(_Period)+".csv";
FileHandle=FileOpen(FileName,FILE_WRITE|FILE_ANSI,',');
//---
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
FileClose(FileHandle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
/*
struct MqlRates
{
datetime time; // Period start time
double open; // Open price
double high; // The highest price of the period
double low; // The lowest price of the period
double close; // Close price
long tick_volume; // Tick volume
int spread; // Spread
long real_volume; // Trade volume
};
*/
void OnTick()
{
//---
if(FileHandle!=INVALID_HANDLE)
{
if(isNewBar(PERIOD_M1))
{
CopyRates(Symbol(),_Period,0,1,rates);
TimeToStruct(rates[0].time,tm);
FileWrite(FileHandle, // write data to file
DoubleToString(rates[0].time,0), // number of seconds, passed from 1st January 1970
rates[0].open, // Open
rates[0].high, // High
rates[0].low, // Low
rates[0].close, // Close
rates[0].tick_volume,
rates[0].spread, // Spread
rates[0].real_volume // Trade volume
// tm.year, // your
// tm.mon, // month
// tm.day, // day
// tm.hour, // hour
// tm.min, // minutes
// tm.day_of_week, // week day (0-sunday, 1-monday)
// tm.day_of_year // day index in the year (1st January is the 0-th day of the year)
);
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Returns true if a new bar has appeared, overwise return false |
//+------------------------------------------------------------------+
bool isNewBar(ENUM_TIMEFRAMES timeFrame)
{
//----
static datetime old_Times[21];// an array for old time values
bool res=false; // variable for the result
int i; // index of old_Times[] array
datetime new_Time[1]; // time of a new bar
switch(timeFrame)
{
case PERIOD_M1: i= 0; break;
case PERIOD_M2: i= 1; break;
case PERIOD_M3: i= 2; break;
case PERIOD_M4: i= 3; break;
case PERIOD_M5: i= 4; break;
case PERIOD_M6: i= 5; break;
case PERIOD_M10: i= 6; break;
case PERIOD_M12: i= 7; break;
case PERIOD_M15: i= 8; break;
case PERIOD_M20: i= 9; break;
case PERIOD_M30: i=10; break;
case PERIOD_H1: i=11; break;
case PERIOD_H2: i=12; break;
case PERIOD_H3: i=13; break;
case PERIOD_H4: i=14; break;
case PERIOD_H6: i=15; break;
case PERIOD_H8: i=16; break;
case PERIOD_H12: i=17; break;
case PERIOD_D1: i=18; break;
case PERIOD_W1: i=19; break;
case PERIOD_MN1: i=20; break;
}
// copying the last bar time to the element new_Time[0]
int copied=CopyTime(_Symbol,timeFrame,0,1,new_Time);
if(copied>0) // ok, the data has been copied successfully
{
if(old_Times[i]!=new_Time[0]) // if old time isn't equal to new bar time
{
if(old_Times[i]!=0) res=true; // if it isn't a first call, the new bar has appeared
old_Times[i]=new_Time[0]; // saving bar time
}
}
//----
return(res);
}
string fTimeFrameName(int arg)
{
int v;
if(arg==0)
{
v=_Period;
}
else
{
v=arg;
}
switch(v)
{
case PERIOD_M1: return("M1");
case PERIOD_M2: return("M2");
case PERIOD_M3: return("M3");
case PERIOD_M4: return("M4");
case PERIOD_M5: return("M5");
case PERIOD_M6: return("M6");
case PERIOD_M10: return("M10");
case PERIOD_M12: return("M12");
case PERIOD_M15: return("M15");
case PERIOD_M20: return("M20");
case PERIOD_M30: return("M30");
case PERIOD_H1: return("H1");
case PERIOD_H2: return("H2");
case PERIOD_H3: return("H3");
case PERIOD_H4: return("H4");
case PERIOD_H6: return("H6");
case PERIOD_H8: return("H8");
case PERIOD_H12: return("H12");
case PERIOD_D1: return("D1");
case PERIOD_W1: return("W1");
case PERIOD_MN1: return("MN1");
default: return("?");
}
} // end fTimeFrameName