EqualVolumeBars

Download script
Author: Copyright � 2008, komposter

This script is designed to transform raw market data into a specialized type of price chart based on volume activity rather than standard time intervals. Instead of looking at bars that form every few minutes or hours, this tool creates "Equal Volume Bars."

Core Functionality

The primary purpose of the logic is to group price movements until a specific number of individual transactions (ticks) have occurred. For example, if you set the requirement to 2 ticks, the script will wait for exactly two price updates to happen before finishing one candle and starting the next. This allows traders to see a visual representation of price action that is driven by the frequency of market activity.

How the Logic Works

1. Historical Data Processing When the script is first activated, it performs a "backfill." It searches through your existing historical CSV tick files (organized by year and month). It reads every single price change recorded in the past and organizes them into the new "Equal Volume" format. This ensures that when you open the chart, the history is already populated with correctly formed bars based on your chosen tick count.

2. Real-Time Monitoring Once the historical data is processed, the script switches to live mode. It monitors every incoming price update from the market. For every new price received:

  • It checks if the current candle has reached the required number of ticks.
  • If the limit hasn't been reached, it updates the current candle’s high, low, and closing prices.
  • Once the limit is hit, it "closes" that bar, saves the final data to a file, and immediately starts a new bar for the next set of ticks.

3. Data Storage and Visualization The script handles its own data management by creating a custom history file on your computer. This file stores the specific price points (Open, High, Low, Close) and the volume for each generated bar.

To ensure you can see these results, the script also interacts with the trading platform's windowing system. It identifies the correct chart window and sends a refresh signal to the interface, ensuring that as new bars are completed in the background, they appear on your screen automatically.

4. Automation Summary

  • Initialization: Scans historical files $\rightarrow$ Groups ticks into bars $\rightarrow$ Saves to a custom history file.
  • Live Execution: Watches every tick $\rightarrow$ Counts volume $\rightarrow$ Completes bar at specific count $\rightarrow$ Refreshes the chart display.
  • Cleanup: Properly closes all data connections and files when you stop using the script to ensure no data is lost or corrupted.
Miscellaneous
It issuies visual alerts to the screenUses files from the file systemIt reads information from a file
13 Views
0 Downloads
0 Favorites
EqualVolumeBars
//+------------------------------------------------------------------+
//|                                              EqualVolumeBars.mq4 |
//|                                      Copyright © 2008, komposter |
//|                                      mailto:komposterius@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, komposter"
#property link      "mailto:komposterius@mail.ru"

#include <WinUser32.mqh>

//---- Êîëè÷åñòâî òèêîâ â îäíîì áàðå
extern int		TicksInBar	= 2;
extern int		StartYear	= 2008;
extern int		StartMonth	= 01;

int HistoryHandle = -1, HistoryFilePos = 0, hwnd = 0, TicksFilePos = 0, now_time;
double now_close, now_open, now_low, now_high, now_volume;
string _Symbol;

int _Period, _PeriodSec, pre_time, last_fpos = 0;
double pre_close;

int init()
{
	int    _GetLastError = 0, cnt_ticks = 0, cnt_bars = 0, temp[13];

	// çàïîìèíàåì ñèìâîë ãðàôèêà, îáíóëÿåì õýíäë îêíà off-line ãðàôèêà
	_Symbol = Symbol();
   hwnd = 0;

	// îòêðûâàåì ôàéë, â êîòîðûé áóäåì çàïèñûâàòü èñòîðèþ
	string file_name = StringConcatenate( "!Eqv", _Symbol, TicksInBar, ".hst" );
	HistoryHandle = FileOpenHistory( file_name, FILE_BIN | FILE_WRITE );
	if ( HistoryHandle < 0 )
	{
		_GetLastError = GetLastError();
		Alert( "FileOpenHistory( \"", file_name, "\", FILE_BIN | FILE_WRITE )", " - Error #", _GetLastError );
		return(-1);
	}

	//---- Çàïèñûâàåì çàãîëîâîê ôàéëà
	FileWriteInteger	( HistoryHandle, 400, LONG_VALUE );
	FileWriteString	( HistoryHandle, "Copyright © 2008, komposter", 64 );
	FileWriteString	( HistoryHandle, StringConcatenate( "!Eqv", _Symbol ), 12 );
	FileWriteInteger	( HistoryHandle, TicksInBar, LONG_VALUE );
	FileWriteInteger	( HistoryHandle, Digits, LONG_VALUE );
	FileWriteInteger	( HistoryHandle, 0, LONG_VALUE );       //timesign
	FileWriteInteger	( HistoryHandle, 0, LONG_VALUE );       //last_sync
	FileWriteArray		( HistoryHandle, temp, 0, 13 );

	//+------------------------------------------------------------------+
	//| Îáðàáàòûâàåì èñòîðèþ
	//+------------------------------------------------------------------+
	int year = StartYear, month = StartMonth;
	int cur_date = year*100+month, end_date = Year()*100+Month();

	datetime time; double bid; string tmp_str1, tmp_str2;
	now_time = 60; now_close = 0; now_open = 0; now_low = 0; now_high = 0; now_volume = 0;

	while ( cur_date <= end_date )
	{
		string ticks_file_name = StringConcatenate( "[Ticks]\\", AccountServer(), "\\", Symbol(), "_", year, ".", strMonth( month ), ".csv" );
		int ticks_file_handle = FileOpen( ticks_file_name, FILE_READ | FILE_CSV );

		//---- Åñëè âîçíèêëà îøèáêà
		if ( ticks_file_handle < 0 )
		{
			Alert( "Îøèáêà ïðè îòêðûòèè ôàéëà \"", ticks_file_name, "\" #", GetLastError(), "!" );
		}
		else
		{
			while ( !FileIsEnding( ticks_file_handle ) )
			{
				if ( GetLastError() == 4099 ) break;

				tmp_str1 = FileReadString( ticks_file_handle );
				if ( StringLen( tmp_str1 ) < 19 ) continue;

				tmp_str2 = FileReadString( ticks_file_handle );
				if ( StringLen( tmp_str2 ) < Digits+2 ) continue;

				time = StrToTime  ( tmp_str1 );
				bid  = StrToDouble( tmp_str2 );

				// ñôîðìèðîâàëñÿ áàð èëè ïåðâûé áàð
				if ( now_volume >= TicksInBar || now_volume < 1 )
				{
					if ( now_volume >= TicksInBar )
					{
						FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
						FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
						FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
						FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
						FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
						FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
						FileFlush			( HistoryHandle );

						cnt_bars ++;
					}

					//now_time		= time;
					now_time		+= 60;	// âðåìÿ ïîäñòàâëÿåì óñëîâíîå, ÷òîá íå ïîëó÷èëîñü 2 áàðà â îäíó ìèíóòó
					now_open		= bid;
					now_low		= bid;
					now_high		= bid;
					now_close	= bid;
					now_volume	= 1;
				}
				else
				{
					if ( bid < now_low  ) now_low  = bid;
					if ( bid > now_high ) now_high = bid;
					now_close = bid;
					now_volume ++;
				}
				cnt_ticks ++;
			}

			//---- Çàêðûâàåì ôàéë
			FileClose( ticks_file_handle );
			GetLastError();
		}

		month ++;
		if ( month > 12 )
		{
			month = 1;
			year ++;
		}
		cur_date = year*100+month;
	}


	// çàïîìèíàåì ìåñòî â ôàéëå, ïåðåä çàïèñüþ 0-ãî áàðà
	HistoryFilePos = FileTell( HistoryHandle);

	// çàïèñûâàåì 0-é áàð äëÿ îòîáàæåíèÿ íà ãðàôèêå
	FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
	FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
	FileFlush			( HistoryHandle );

	// âûâîäèì ñòàòèñòèêó
	Print( "< - - - Îáðàáîòàíî òèêîâ: ", cnt_ticks, ", ïîñòðîåíî ïîëíûõ áàðîâ: ", cnt_bars, " - - - >" );
	Print( "< - - - Äëÿ ïðîñìîòðà ðåçóëüòàòîâ îòêðîéòå ãðàôèê \"!Eqv", _Symbol, TicksInBar, "\" - - - >" );
	
	// îáíîâëÿåì ãðàôèê
	RefreshWindow();

	return(0);
}

int start()
{
	if ( HistoryHandle < 0 ) return(-1);

	//+------------------------------------------------------------------+
	//| Îáðàáàòûâàåì ïîñòóïàþùèå òèêè
	//+------------------------------------------------------------------+
	//---- ñòàâèì "êóðñîð" ïåðåä ïîñëåäíèì áàðîì
	FileSeek( HistoryHandle, HistoryFilePos, SEEK_SET );

	now_volume ++;

	// áàð ïðîäîëæàåòñÿ
	if ( now_volume <= TicksInBar )
	{
		if ( Bid < now_low  ) now_low  = Bid;
		if ( Bid > now_high ) now_high = Bid;
		now_close = Bid;

		// çàïèñûâàåì 0-é áàð äëÿ îòîáàæåíèÿ íà ãðàôèêå
		FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
		FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
		FileFlush			( HistoryHandle );
	}
	// ïðèøåë òèê íîâîãî áàðà
	else
	{
		// çàïèñûâàåì 1-é áàð
		FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
		FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_volume-1,DOUBLE_VALUE);
		FileFlush			( HistoryHandle );

//		now_time		= TimeCurrent();
		now_time		+= 60;	// âðåìÿ ïîäñòàâëÿåì óñëîâíîå, ÷òîá íå ïîëó÷èëîñü 2 áàðà â îäíó ìèíóòó
		now_open		= Bid;
		now_low		= Bid;
		now_high		= Bid;
		now_close	= Bid;
		now_volume	= 1;

		// çàïîìèíàåì ìåñòî â ôàéëå, ïåðåä çàïèñüþ 0-ãî áàðà
		HistoryFilePos = FileTell( HistoryHandle);

		// çàïèñûâàåì 0-é áàð
		FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
		FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
		FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
		FileFlush			( HistoryHandle );
	}
/*
// Print( "now_time = ", now_time, ", now_open = ", now_open, ", now_low = ", now_low, ", now_high = ", now_high, ", now_close = ", now_close, ", now_volume = ", now_volume );
// Print( "5:  file_pos = ", FileTell( HistoryHandle) );
	// çàïèñûâàåì äëÿ îòîáàæåíèÿ íà ãðàôèêå
	FileWriteInteger	( HistoryHandle, now_time,		LONG_VALUE	);
	FileWriteDouble	( HistoryHandle, now_open,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_low,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_high,		DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_close,	DOUBLE_VALUE);
	FileWriteDouble	( HistoryHandle, now_volume,	DOUBLE_VALUE);
	FileFlush			( HistoryHandle );

// Print( "6:  file_pos = ", FileTell( HistoryHandle) );
*/
	// îáíîâëÿåì ãðàôèê
	RefreshWindow();
}

void RefreshWindow()
{
	//---- íàõîäèì îêíî, â êîòîðîå áóäåì "îòïðàâëÿòü" ñâåæèå êîòèðîâêè
	if ( hwnd == 0 )
	{
		hwnd = WindowHandle( StringConcatenate( "!Eqv", _Symbol ), TicksInBar );
		if ( hwnd != 0 ) { Print( "< - - - Ãðàôèê ", "!Eqv", _Symbol, TicksInBar, " íàéäåí! - - - >" ); }
	}
	//---- è, åñëè íàøëè, îáíîâëÿåì åãî
	if ( hwnd != 0 ) { PostMessageA( hwnd, WM_COMMAND, 33324, 0 ); }
}

int deinit()
{
	if ( HistoryHandle > 0 )
	{
		//---- çàêðûâàåì ôàéë
		FileClose( HistoryHandle );
		HistoryHandle = -1;
	}
	return(0);
}

string strMonth( int m )
{
	if ( m < 10 ) return( StringConcatenate( "0", m ) );
	return(m);
}

Comments