AllMinutes_v1

Download script
Author: Copyright � 2006, komposter
AllMinutes_v1

This script is designed to automatically collect and synchronize historical trading data from various currency pairs (or other financial instruments) into dedicated files for long-term storage or external analysis.

Core Functionality

The primary goal of the script is to "scrape" price data (Open, High, Low, Close, and Volume) from your active trading platform and save it into a standardized file format. Unlike a standard chart that only shows you what is currently visible on your screen, this script systematically gathers data across multiple instruments at once.

How It Works

  • Multi-Instrument Tracking: You can define a list of specific currency pairs and timeframes (for example, EURUSD on a 1-minute chart and GBPUSD on a 1-minute chart). The script will automatically handle all pairs listed in that sequence.
  • Data Synchronization: It scans the available history for each pair you've requested. It looks for gaps in the data (missing bars) and ensures that the information saved to the file is continuous and organized chronologically.
  • Weekend Filtering: There is a built-in logic to handle weekends. If enabled, the script can be configured to skip over Saturday and Sunday data to ensure the resulting files only contain active trading sessions.
  • Real-Time Updates: Once the initial historical data is downloaded into the file, the script enters a continuous loop. It monitors the live market "ticks." Every time a new price candle forms or closes, it writes that update immediately to the corresponding file.
  • Automatic Chart Refresh: To ensure your trading platform stays updated with this data, the script can send a signal to the platform to refresh the specific charts where this data is being recorded.

User Experience Features

  • Customizable Speed: You can adjust how frequently the script checks for updates. A higher value reduces the load on your computer's processor, while a lower value provides more frequent data logging.
  • Reporting: After processing the history, the script prints a summary in the log window, telling you exactly how many bars were copied from the platform and how many new bars were added to your files.
  • Automatic File Management: The script automatically creates the file names based on the symbol and timeframe (e.g., "ALLEURUSD1.hst"), making it easy to locate your data without manual renaming.

Summary for the User

Think of this tool as a Data Logger. Instead of you manually watching every chart and recording prices, this script acts as an automated secretary that watches all your specified pairs simultaneously, cleans up the history, fills in the blanks, and writes everything down into permanent files while the market is moving.

Price Data Components
Series array that contains close prices for each barSeries array that contains open prices of each barSeries array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains tick volumes of each bar
Miscellaneous
It issuies visual alerts to the screenIt writes information to fileIt writes information to fileIt writes information to file
10 Views
0 Downloads
0 Favorites
AllMinutes_v1
//+------------------------------------------------------------------+
//|                                             		    AllMinutes.mq4 |
//|                                      Copyright © 2006, komposter |
//|                                      mailto:komposterius@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, komposter"
#property link      "mailto:komposterius@mail.ru"
//----
#include <WinUser32.mqh>
// Ñïèñîê ãðàôèêîâ êîòîðûå íåîáõîäèìî îáðàáàòûâàòü, ðàçäåë¸ííûé çàïÿòîé (",")
extern string	ChartList	= "EURUSD1,GBPUSD1";
// Ðàçðåøèòü/çàïðåòèòü ðèñîâàòü áàðû â âûõîäíûå
// Åñëè == true, âûõîäíûå îñòàíóòñÿ íåçàïîëíåíûìè
// Åñëè == false, âûõîäíûå áóäóò çàïîëíåíû áàðàìè O=H=L=C
extern bool		SkipWeekEnd	= true;
// ×àñòîòà, ñ êîòîðîé áóäóò îáíîâëÿòüñÿ ãðàôèêè â ìèëèñåêóíäàõ
//- ×åì áîëüøå çíà÷åíèå, òåì ìåíüøå ðåñóðñîâ áóäåò èñïîëüçîâàòü ñêðèïò.
extern int		RefreshLuft	= 1000;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init() 
  { 
   start(); 
   return(0); 
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
	  int		_GetLastError = 0, cnt_copy = 0, cnt_add = 0, temp[13];
	  int		Charts = 0, pos = 0, curchar = 0, len = StringLen(ChartList);
	  string	cur_symbol = "", cur_period = "", file_name = "";
	  string	_Symbol[100]; int _Period[100], _PeriodSec[], _Bars[];
	  int		HistoryHandle[], hwnd[], last_fpos[], pre_time[], now_time[];
	  double	now_close[], now_open[], now_low[], now_high[], now_volume[];
	  double	pre_close[], pre_open[], pre_low[], pre_high[], pre_volume[];
//---- ñ÷èòàåì êîëè÷åñòâî ãðàôèêîâ, êîòîðûå íåîáõîäèìî îáðàáîòàòü
	  while(pos <= len)
	    {
		     curchar = StringGetChar( ChartList, pos);
		     if(curchar > 47 && curchar < 58)
		         cur_period = cur_period + CharToStr( curchar ); 
		     else
		       {
			        if(curchar == ',' || pos == len)
			          { 
				           MarketInfo(cur_symbol, MODE_BID);
				           if(GetLastError() == 4106)
				             {
					              Alert("Íåèçâåñòíûé ñèìâîë ", cur_symbol, "!!!");
					              return(-1);				
				             }
				           if(iClose(cur_symbol, StrToInteger( cur_period ), 0) <= 0)
				             {
					              Alert( "Íåèçâåñòíûé ïåðèîä ", cur_period, "!!!" );
					              return(-1);				
				             }
				           _Symbol[Charts] = cur_symbol; _Period[Charts] = StrToInteger(cur_period);
				           cur_symbol = ""; cur_period = "";
				           Charts ++;
			          }
			        else
			            cur_symbol = cur_symbol + CharToStr(curchar); 
		       }
		     pos++;
	    }
	  Print("< - - - Íàéäåíî ", Charts, " êîððåêòíûõ ãðàôèêîâ. - - - >");
	  ArrayResize(_Symbol,	Charts); 
	  ArrayResize(_Period,	Charts);
	  ArrayResize(HistoryHandle,	Charts); 
	  ArrayResize( hwnd,	Charts);
	  ArrayResize(last_fpos,	Charts); 
	  ArrayResize( pre_time,	Charts );
	  ArrayResize(now_time,	Charts); 
	  ArrayResize( now_close,	Charts );
	  ArrayResize(now_open,	Charts); 
	  ArrayResize( now_low,	Charts );
	  ArrayResize(now_high,	Charts); 
	  ArrayResize( now_volume,	Charts );
	  ArrayResize(pre_close,	Charts); 
	  ArrayResize( pre_open,		Charts );
	  ArrayResize(pre_low,	Charts); 
	  ArrayResize( pre_high,	Charts );
	  ArrayResize(pre_volume,	Charts); 
	  ArrayResize( _PeriodSec,	Charts );
	  ArrayResize(_Bars,	Charts);
	  for(int curChart = 0; curChart < Charts; curChart++)
	    {
		     _PeriodSec[curChart] = _Period[curChart]*60;
		     //---- îòêðûâàåì ôàéë, â êîòîðûé áóäåì çàïèñûâàòü èñòîðèþ
		     file_name = StringConcatenate("ALL", _Symbol[curChart], _Period[curChart], ".hst");
		     HistoryHandle[curChart] = FileOpenHistory(file_name, FILE_BIN | FILE_WRITE);
		     if(HistoryHandle[curChart] < 0)
		       {
			        _GetLastError = GetLastError();
			        Alert("FileOpenHistory( \"", file_name, "\", FILE_BIN | FILE_WRITE )",
																	" - Error #", _GetLastError);
			        continue;
		       }
		     //---- Çàïèñûâàåì çàãîëîâîê ôàéëà
		     FileWriteInteger(HistoryHandle[curChart], 400, LONG_VALUE);
		     FileWriteString(HistoryHandle[curChart], "Copyright © 2006, komposter", 64);
		     FileWriteString(HistoryHandle[curChart], 
							                StringConcatenate("ALL", _Symbol[curChart]), 12);
		     FileWriteInteger(HistoryHandle[curChart], _Period[curChart], LONG_VALUE);
		     FileWriteInteger(HistoryHandle[curChart], 
									               MarketInfo(_Symbol[curChart], MODE_DIGITS), LONG_VALUE);
		     FileWriteInteger(HistoryHandle[curChart], 0, LONG_VALUE);       //timesign
		     FileWriteInteger(HistoryHandle[curChart], 0, LONG_VALUE);       //last_sync
		     FileWriteArray	(HistoryHandle[curChart], temp, 0, 13);
		     //+------------------------------------------------------------------+
		     //| Îáðàáàòûâàåì èñòîðèþ                                             |
		     //+------------------------------------------------------------------+
		     _Bars[curChart] = iBars(_Symbol[curChart], _Period[curChart]);
		     pre_time[curChart] = iTime(_Symbol[curChart], _Period[curChart], _Bars[curChart] - 1);
		     for(int i = _Bars[curChart] - 1; i >= 1; i--)
		       {
			        //---- Çàïîìèíàåì ïàðàìåòðû áàðà
			        now_open[curChart] = iOpen(_Symbol[curChart], _Period[curChart], i);
			        now_high[curChart] = iHigh(_Symbol[curChart], _Period[curChart], i);
			        now_low[curChart] = iLow(_Symbol[curChart], _Period[curChart], i);
			        now_close[curChart] = iClose(_Symbol[curChart], _Period[curChart], i);
			        now_volume[curChart] = iVolume(_Symbol[curChart], _Period[curChart], i);
			        now_time[curChart] = iTime(_Symbol[curChart], _Period[curChart], i) / 
			                                   _PeriodSec[curChart];
			        now_time		[curChart] *= _PeriodSec[curChart];
			        //---- åñëè åñòü ïðîïóùåííûå áàðû,
			        while(now_time[curChart] > pre_time[curChart] + _PeriodSec[curChart])
			          {
				           pre_time[curChart] += _PeriodSec[curChart];
				           pre_time[curChart] /= _PeriodSec[curChart];
				           pre_time[curChart] *= _PeriodSec[curChart];
				           //---- åñëè ýòî íå âûõîäíûå,
				           if(SkipWeekEnd)
				             {
					              if(TimeDayOfWeek(pre_time[curChart]) <= 0 || 
							               TimeDayOfWeek(pre_time[curChart]) > 5) 
							                continue; 
					              if(TimeDayOfWeek(pre_time[curChart]) == 5)
					                {
						                 if(TimeHour(pre_time[curChart]) == 23 || 
						                    TimeHour(pre_time[curChart] + 
						                    _PeriodSec[curChart]) == 23) 
						                     continue; 
					                }
				             }
				           //---- çàïèñûâàåì ïðîïóùåííûé áàð â ôàéë
				           FileWriteInteger(HistoryHandle[curChart], pre_time[curChart],	LONG_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], 1,	DOUBLE_VALUE);
				           FileFlush(HistoryHandle[curChart]);
				           cnt_add ++;
			          }
			        //---- çàïèñûâàåì íîâûé áàð â ôàéë
			        FileWriteInteger(HistoryHandle[curChart], now_time[curChart],	LONG_VALUE);
			        FileWriteDouble(HistoryHandle[curChart], now_open[curChart],	DOUBLE_VALUE);
			        FileWriteDouble(HistoryHandle[curChart], now_low[curChart],		DOUBLE_VALUE);
			        FileWriteDouble(HistoryHandle[curChart], now_high[curChart],		DOUBLE_VALUE);
			        FileWriteDouble(HistoryHandle[curChart], now_close[curChart],		DOUBLE_VALUE);
			        FileWriteDouble(HistoryHandle[curChart], now_volume[curChart],	DOUBLE_VALUE);
			        FileFlush(HistoryHandle[curChart]);
			        cnt_copy ++;
			        //---- çàïîìèíàåì çíà÷åíèå âðåìåíè è öåíó çàêðûòèÿ çàïèñàííîãî áàðà
			        pre_close[curChart]	= now_close[curChart];
			        pre_time[curChart]	= now_time[curChart] / _PeriodSec[curChart];
			        pre_time[curChart]	*= _PeriodSec[curChart];
 		      }
		     last_fpos[curChart] = FileTell(HistoryHandle[curChart]);
	      //---- âûâîäèì ñòàòèñòèêó
	      Print("< - - - ", _Symbol[curChart], _Period[curChart], ": áûëî ", cnt_copy, 
									    " áàðîâ, äîáàâëåíî ", cnt_add, " áàðîâ - - - >");
	      Print("< - - - Äëÿ ïðîñìîòðà ðåçóëüòàòîâ, îòêðîéòå ãðàôèê \"ALL", 
									    _Symbol[curChart], _Period[curChart], "\" - - - >");
	    }
	  //+------------------------------------------------------------------+
	  //| Îáðàáàòûâàåì ïîñòóïàþùèå òèêè
	  //+------------------------------------------------------------------+
	  while(!IsStopped())
	    {
		     RefreshRates();
		     for(curChart = 0; curChart < Charts; curChart ++)
		       {
			        //---- ñòàâèì "êóðñîð" ïåðåä ïîñëåäíèì áàðîì
			        //---- (ýòî íåîáõîäèìî íà âñåõ çàïóñêàõ, êðîìå ïåðâîãî)
			        FileSeek(HistoryHandle[curChart], last_fpos[curChart], SEEK_SET);
			        //---- Çàïîìèíàåì ïàðàìåòðû áàðà
			        now_open[curChart] = iOpen(_Symbol[curChart], _Period[curChart], 0);
			        now_high[curChart] = iHigh(_Symbol[curChart], _Period[curChart], 0);
			        now_low[curChart] = iLow(_Symbol[curChart], _Period[curChart], 0);
			        now_close[curChart] = iClose(_Symbol[curChart], _Period[curChart], 0);
			        now_volume[curChart] = iVolume(_Symbol[curChart], _Period[curChart], 0);
			        now_time[curChart] = iTime(_Symbol[curChart], _Period[curChart], 0) / 
			                                   _PeriodSec[curChart];
			        now_time[curChart] *= _PeriodSec[curChart];
			        //---- åñëè áàð ñôîðìèðîâàëñÿ, 
			        if(now_time[curChart] >= pre_time[curChart] + _PeriodSec[curChart])
			          {
				           //---- çàïèñûâàåì ñôîðìèðîâàâøèéñÿ áàð
				           FileWriteInteger(HistoryHandle[curChart], pre_time[curChart],	LONG_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_open[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_low[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_high[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_volume[curChart], DOUBLE_VALUE);
				           FileFlush(HistoryHandle[curChart]);
				           //---- çàïîìèíàåì ìåñòî â ôàéëå, ïåðåä çàïèñüþ 0-ãî áàðà
				           last_fpos[curChart] = FileTell( HistoryHandle[curChart] );
			          }
			        //---- åñëè ïîÿâèëèñü ïðîïóùåííûå áàðû,
			        while(now_time[curChart] > pre_time[curChart] + _PeriodSec[curChart])
			          {
				           pre_time[curChart] += _PeriodSec[curChart];
				           pre_time[curChart] /= _PeriodSec[curChart];
				           pre_time[curChart] *= _PeriodSec[curChart];
				           //---- åñëè ýòî íå âûõîäíûå,
				           if(SkipWeekEnd)
				             {
					              if(TimeDayOfWeek(pre_time[curChart]) <= 0 || 
						                TimeDayOfWeek(pre_time[curChart]) > 5 ) 
						                 continue; 
					              if(TimeDayOfWeek(pre_time[curChart]) == 5)
					                {
						                 if(TimeHour(pre_time[curChart]) == 23 || 
							                   TimeHour(pre_time[curChart] + _PeriodSec[curChart]) == 23) 
							                    continue; 
					                }
				             }
				           //---- çàïèñûâàåì ïðîïóùåííûé áàð â ôàéë
				           FileWriteInteger(HistoryHandle[curChart], pre_time[curChart],	LONG_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], pre_close[curChart],	DOUBLE_VALUE);
				           FileWriteDouble(HistoryHandle[curChart], 1,	DOUBLE_VALUE);
				           FileFlush(HistoryHandle[curChart]);
				           //---- çàïîìèíàåì ìåñòî â ôàéëå, ïåðåä çàïèñüþ 0-ãî áàðà
				           last_fpos[curChart] = FileTell(HistoryHandle[curChart]);
			          }
		         //---- çàïèñûâàåì òåêóùèé áàð
		         FileWriteInteger(HistoryHandle[curChart], now_time[curChart],	LONG_VALUE);
		         FileWriteDouble(HistoryHandle[curChart], now_open[curChart],	DOUBLE_VALUE);
		         FileWriteDouble(HistoryHandle[curChart], now_low[curChart],	DOUBLE_VALUE);
		         FileWriteDouble(HistoryHandle[curChart], now_high[curChart],	DOUBLE_VALUE);
		         FileWriteDouble(HistoryHandle[curChart], now_close[curChart],	DOUBLE_VALUE);
		         FileWriteDouble(HistoryHandle[curChart], now_volume[curChart],	DOUBLE_VALUE);
		         FileFlush(HistoryHandle[curChart]);
			        //---- çàïîìèíàåì ïàðàìåòðû çàïèñàííîãî áàðà
			        pre_open[curChart]	= now_open[curChart];
			        pre_high[curChart]	= now_high[curChart];
			        pre_low[curChart]	= now_low[curChart];
			        pre_close[curChart]	= now_close[curChart];
			        pre_volume[curChart]	= now_volume[curChart];
			        pre_time[curChart]	= now_time[curChart] / _PeriodSec[curChart];
			        pre_time[curChart]	*= _PeriodSec[curChart];
			        //---- íàõîäèì îêíî, â êîòîðîå áóäåì "îòïðàâëÿòü" ñâåæèå êîòèðîâêè
			        if(hwnd[curChart] == 0)
			          {
				           hwnd[curChart] = WindowHandle(StringConcatenate( "ALL", _Symbol[curChart]), 
																								                     _Period[curChart]);
				           if(hwnd[curChart] != 0) 
				             { 
				               Print("< - - - Ãðàôèê ", "ALL" + _Symbol[curChart], 
																	        _Period[curChart], " íàéäåí! - - - >"); 
																	}
			          }
			        //---- è, åñëè íàøëè, îáíîâëÿåì åãî
			        if(hwnd[curChart] != 0) 
			            PostMessageA(hwnd[curChart], WM_COMMAND, 33324, 0); 
		       }
		     Sleep(RefreshLuft);
	    }
   for(curChart = 0; curChart < Charts; curChart ++)
     {
	      if(HistoryHandle[curChart] >= 0)
	        {
		        //---- çàêðûâàåì ôàéë
		        FileClose(HistoryHandle[curChart]);
		        HistoryHandle[curChart] = -1;
	        }
	    }
	  return(0);
  }
//+------------------------------------------------------------------+


Comments