Author: Copyright � 2006, komposter
AllMinutes

This script is designed to act as an automated data logger and history aggregator for MetaTrader 4. Its primary purpose is to gather historical price data from multiple currency pairs (or other assets) and save that information into dedicated, organized files on your computer.

Here is a breakdown of how the logic works:

Multi-Asset Configuration

The script begins by looking at a user-defined list of symbols and timeframes (for example, EURUSD on a 1-hour chart). It parses this list to understand exactly which assets need to be monitored. Instead of just looking at what is currently open on your screen, it can handle multiple pairs simultaneously based on the provided instructions.

Data Logging & File Creation

For every asset identified in your list, the script creates a specific history file. These files are named using a standard format (e.g., "ALL[Symbol][Period].hst").

  • File Headers: It automatically writes the necessary metadata to these files, including the copyright information, the specific symbol name, and the timeframe.
  • Continuous Writing: As the script runs, it constantly records the Open, High, Low, Close, and Volume prices for each bar.

Smart Handling of Time Gaps

One of the core features of this logic is its ability to handle "missing" data. If there is a gap in the price history (for instance, if your platform didn't have data for a specific period), the script identifies these missing pieces and attempts to fill them in the file sequentially so that the resulting historical record remains continuous and usable.

Weekend and Holiday Filters

The script includes a specific logic gate to handle non-trading days. If the "SkipWeekEnd" option is enabled, the tool will recognize Saturdays and Sundays (as well as specific late-Friday hours) and skip them during the logging process. This ensures that your exported files contain only relevant trading data without being cluttered by inactive market periods.

Real-Time Updates & Synchronization

Once the initial historical data is loaded and saved, the script enters a continuous loop to monitor live market ticks:

  • Live Tracking: It tracks the "current" candle as it forms in real-time.
  • Auto-Finalization: The moment a new candle begins (a new bar is formed), the script automatically finalizes the data for the previous candle and writes it into the file.
  • Performance Management: To ensure the computer doesn't slow down, the script includes a "Refresh" delay. You can adjust this to balance how fast the data is saved versus how much processing power the script uses.

Visual Updates (Optional)

The script also contains a feature to interact with the MetaTrader interface. If configured, it can send a command to refresh the visual charts of the generated history files. This allows you to see the "ALL" files update visually as the data is being logged.

Price Data Components
Series array that contains close prices for each bar
Miscellaneous
It issuies visual alerts to the screen
8 Views
0 Downloads
0 Favorites
AllMinutes
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                   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], 0,							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], 0,							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