//+------------------------------------------------------------------+ //| TestLoadHistory.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property script_show_inputs //--- input parameters input string InpLoadedSymbol="NZDUSD"; // Symbol to be load input ENUM_TIMEFRAMES InpLoadedPeriod=PERIOD_H1; // Period to be load input datetime InpStartDate=D'2009.01.01'; // Start date //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { Print("Start load",InpLoadedSymbol+","+GetPeriodName(InpLoadedPeriod),"from",InpStartDate); //--- int res=CheckLoadHistory(InpLoadedSymbol,InpLoadedPeriod,InpStartDate); switch(res) { case -1 : Print("Unknown symbol",InpLoadedSymbol); break; case -2 : Print("Requested bars more than max bars in chart"); break; case -3 : Print("Program was stopped"); break; case -4 : Print("Indicator shouldn't load its own data"); break; case 0 : Print("Loaded OK"); break; case 1 : Print("Loaded previously"); break; case 2 : Print("Loaded previously and built"); break; default : Print("Unknown result"); } //--- datetime first_date; SeriesInfoInteger(InpLoadedSymbol,InpLoadedPeriod,SERIES_FIRSTDATE,first_date); int bars=Bars(InpLoadedSymbol,InpLoadedPeriod); Print("First date",first_date,"-",bars,"bars"); //--- } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CheckLoadHistory(string symbol,ENUM_TIMEFRAMES period,datetime start_date) { datetime first_date=0; datetime times[100]; //--- check symbol & period if(symbol==NULL || symbol=="") symbol=Symbol(); if(period==PERIOD_CURRENT) period=Period(); //--- check if symbol is selected in the MarketWatch if(!SymbolInfoInteger(symbol,SYMBOL_SELECT)) { if(GetLastError()==ERR_MARKET_UNKNOWN_SYMBOL) return(-1); SymbolSelect(symbol,true); } //--- check if data is present SeriesInfoInteger(symbol,period,SERIES_FIRSTDATE,first_date); if(first_date>0 && first_date<=start_date) return(1); //--- don't ask for load of its own data if it is indicator if(MQL5InfoInteger(MQL5_PROGRAM_TYPE)==PROGRAM_INDICATOR && Period()==period && Symbol()==symbol) return(-4); //--- second attempt if(SeriesInfoInteger(symbol,PERIOD_M1,SERIES_TERMINAL_FIRSTDATE,first_date)) { if(first_date>0) { //--- force timeseries build CopyTime(symbol,period,first_date+PeriodSeconds(period),1,times); //--- check date if(SeriesInfoInteger(symbol,period,SERIES_FIRSTDATE,first_date)) if(first_date>0 && first_date<=start_date) return(2); } } //--- max bars in chart from terminal options int max_bars=TerminalInfoInteger(TERMINAL_MAXBARS); //--- wait for load symbol history info first_date=0; while(!SeriesInfoInteger(symbol,PERIOD_M1,SERIES_SERVER_FIRSTDATE,first_date) && !IsStopped()) Sleep(5); //--- fix start date for loading if(first_date>start_date) start_date=first_date; //--- load data step by step while(!IsStopped()) { //--- wait for timeseries build while(!SeriesInfoInteger(symbol,period,SERIES_SYNCRONIZED) && !IsStopped()) Sleep(5); //--- ask for built bars int bars=Bars(symbol,period); if(bars>0) { if(bars>=max_bars) return(-2); //--- ask for first date if(SeriesInfoInteger(symbol,period,SERIES_FIRSTDATE,first_date)) if(first_date>0 && first_date<=start_date) return(0); } //--- copying of next part forces data loading int copied=CopyTime(symbol,period,bars,100,times); if(copied>0) { //--- check for data if(times[0]<=start_date) return(0); if(bars+copied>=max_bars) return(-2); } } //--- stopped return(-3); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ string GetPeriodName(ENUM_TIMEFRAMES period) { string res; //--- if(period==PERIOD_CURRENT) period=Period(); switch(period) { case PERIOD_M1: res="M1"; break; case PERIOD_M2: res="M2"; break; case PERIOD_M3: res="M3"; break; case PERIOD_M4: res="M4"; break; case PERIOD_M5: res="M5"; break; case PERIOD_M6: res="M6"; break; case PERIOD_M10: res="M10"; break; case PERIOD_M12: res="M12"; break; case PERIOD_M15: res="M15"; break; case PERIOD_M20: res="M20"; break; case PERIOD_M30: res="M30"; break; case PERIOD_H1: res="H1"; break; case PERIOD_H2: res="H2"; break; case PERIOD_H3: res="H3"; break; case PERIOD_H4: res="H4"; break; case PERIOD_H6: res="H6"; break; case PERIOD_H8: res="H8"; break; case PERIOD_H12: res="H12"; break; case PERIOD_D1: res="Daily"; break; case PERIOD_W1: res="Weekly"; break; case PERIOD_MN1: res="Monthly"; break; default : res="unknown period"; } //--- return(res); } //+------------------------------------------------------------------+