//+------------------------------------------------------------------+ //| HISTORYDATA-MTP.mq4 | //| Copyright © 2008, Muhammad Hamizi Jaminan | //| http://www.fxsentral.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Muhammad Hamizi Jaminan." #property link "http://www.fxsentral.com" #property indicator_chart_window /* Filename: HISTORYDATA-MTP.mq4 Description: Export history data Author: Muhammad Hamizi Jaminan aka hymns Version History *************** Version 1.0.5 - add string time frame on file instead of minute numbers. Version 1.0.4 - add new frequency update for reduce memory usage on writing. Version 1.0.3 - fixed exausting memory usage on writing for all pair (19 pair 5 timeframe). Version 1.0.2 - add completed bar parameter - add debug mode parameter - add timeframe 60, 30 & 15 minute data Version 1.0.1 - add custom extension for filename - add custom bar numbers Version 1.0.0 - Enjoy my first release - Export D1 & H4 chart history */ //external input extern int frequency_update = 60; extern string file_extension = ".csv"; //bar input extern int number_bars = 2000; extern int from_year = 1978; extern bool completed_bar = false; extern bool enable_debug = false; //period input extern bool period_weekly = true; extern bool period_daily = true; extern bool period_4hour = false; extern bool period_1hour = false; extern bool period_30min = false; extern bool period_15min = false; //writing permissions bool writedata = false; //file handler int handler, cnt, shift; int current_time = 0; //string handle string strline, filename; //start function int start() { //timer controller if (current_time == 0 || current_time < StrToTime(TimeToStr(TimeCurrent(), TIME_MINUTES))) { current_time = StrToTime(TimeToStr(TimeCurrent()+frequency_update, TIME_MINUTES)); writedata = true; } //check writing permissions if (writedata == false) { return(0); } //check complete bar if (completed_bar) { shift = 1; } else { shift = 0; } //weekly period if (period_weekly) { write_history(PERIOD_W1); } //24 hour period if (period_daily) { write_history(PERIOD_D1); } //4 hour period if (period_4hour) { write_history(PERIOD_H4); } //1 hour period if (period_1hour) { write_history(PERIOD_H1); } //30 minute period if (period_30min) { write_history(PERIOD_M30); } //15 minute period if (period_15min) { write_history(PERIOD_M15); } //reset permission back writedata = false; return(0); } //write history data void write_history(int period) { //switch period string string period_string; switch(period) { case 10080 : period_string = "W1"; break; case 1440 : period_string = "D1"; break; case 240 : period_string = "H4"; break; case 60 : period_string = "H1"; break; case 30 : period_string = "M30"; break; case 15 : period_string = "M15"; break; } //filename filename = Symbol() + "_" + period_string + file_extension; //debug : open file if (enable_debug) { Print("Debug: Accessing file ",filename,"..."); } //open file : assign handler handler = FileOpen(filename, FILE_CSV|FILE_WRITE, "\t"); //failed create handle if(handler < 1) { //debug : file open failed if (enable_debug) { Print("Debug: Cannot open file ", filename," : error_",GetLastError()); } return(0); } //debug : file open if (enable_debug) { Print("Debug: Success accessing file ",filename,"..."); } //reset string strline = ""; //read existing bars for (cnt = number_bars; cnt >= shift; cnt--) { //check year if (from_year < TimeYear(iTime(Symbol(), period, cnt)) ) { //assign contents strline = TimeToStr(iTime(Symbol(), period, cnt),TIME_DATE|TIME_SECONDS); strline = StringSubstr(strline, 0, 4) + StringSubstr(strline, 5, 2) + StringSubstr(strline, 8, 2) + "," + StringSubstr(strline, 11, 5); strline = strline + "," + DoubleToStr(iOpen(Symbol(), period, cnt), 4) + "," + DoubleToStr(iHigh(Symbol(), period, cnt),4) + "," + DoubleToStr(iLow(Symbol(), period, cnt),4) + "," + DoubleToStr(iClose(Symbol(), period, cnt),4) + "," + DoubleToStr(iVolume(Symbol(), period, cnt),0); //writing contents FileWrite(handler, strline); } } //debug : complete if (enable_debug) { Print("Debug: Writing file ",filename," completed!"); } //close handler FileClose(handler); //debug : closing if (enable_debug) { Print("Debug: Closing file ",filename,"..."); } return(0); }