Русский 中文 Español Deutsch 日本語 Português
Offline Charts in the New MQL4

Offline Charts in the New MQL4

MetaTrader 4Examples | 4 February 2014, 07:32
43 250 6

Updated MQL4 has the new format for storing historical data and provides the appropriate MqlRates structure for convenient storage of Time, Open, Low, High, Close and Volume values. For many years, traders have developed their MQL4 applications that collect and store their data in HST files for generating offline charts. We can assure you that all previously compiled EX4 files will work in the new MetaTrader 4 terminal the same way as before.

All Old EX4 Programs Work in the New MetaTrader 4 Terminal

As already mentioned, not a single custom executable EX4 file compiled by the old compiler will be deleted or changed when upgrading the terminal. This means that all your tried and tested applications not included in the terminal's standard delivery will be automatically copied to the new location and remain available for work.

Offline Charts in MetaTrader 4 Terminal

The standard delivery of the new client terminal contains the updated PeriodConverter script that generates HST files in the new format. However, if you have programs based on the older period_converter script and compiled with the older compiler, they will work as usual.

Suppose that we have the compiled period_converter_509.ex4 script developed by the older version. During the upgrade, it has automatically been copied to the new storage location and is now available for launch in Navigator window. We apply it to EURUSD M1 chart for creating EURUSD M2 custom history.

Multiplier is set to 3 by default. Let's change it to 2. We should also be sure to enable DLL call to be able to manage offline EURUSD M2 chart we are going to open after the appropriate history is formed.

As soon as the script reports on generating the data file to Experts journal, go to File - Open Offline and find EURUSD M2 line. That is the data prepared by our script.

After the offline chart is opened, the script goes on working with EURUSD M1 and processing newly arrived real time ticks. After it finds out that the offline chart with the specified timeframe has been opened, it starts sending update commands to that chart once per two seconds.

Thus, the old script compiled by the older compiler version successfully operates in the new terminal activating all built-in functionality.

Accomplish all described steps: download attached executable period_converter_509.ex4 file and put it to <data folder>\MQL4\Scripts.

Updating the Old Script for the New Compiler

At some point, you may need to change the source code of your application and compile it in the new MetaEditor. Here we will show how to consider the changes in MQL4 language by two small corrections using period_converter_509 script as an example. Of course, we can compile the source code of period_conveter_509.mq4 without any changes, and it will work. But it would be better to take the language changes into account and make minor corrections to the code.

Shared access mode should be specified explicitly when opening the file
As you may remember, all files were opened in shared access mode in the old MQL4. In the new MQL4, FILE_SHARE_WRITE and FILE_SHARE_READ flags should explicitly be specified for shared use when opening files. If the flags are absent, the file is opened in exclusive mode and cannot be opened by anyone else till it is closed by the user who opened it. That is exactly the behavior we need, since we want to open the historical data file generated by the script in MetaTrader 4 terminal as an offline chart. Thus, we simply add these two flags to FileOpenHistory() function call:

   // 1-st change - add share flags

Now, FileFlush() flushes stored data to the disk immediately
FileFlush() function is used too often in the old script. There is no need to call it after each recording of the bar data during the first file generation. It would be enough to do that in the very end when all prepared data on a non-standard symbol and/or timeframe is recorded. The fact is that FileFlush() function implementation has been changed in the new version of MQL4 language to enable instant data flush to the physical drive. The old version used data writeback, and multiple calls did not lead to script operation slowdown.

//--- write generated bar data
FileWriteInteger(ExtHandle, i_time, LONG_VALUE);
FileWriteDouble(ExtHandle, d_open, DOUBLE_VALUE);
FileWriteDouble(ExtHandle, d_low, DOUBLE_VALUE);
FileWriteDouble(ExtHandle, d_high, DOUBLE_VALUE);
FileWriteDouble(ExtHandle, d_close, DOUBLE_VALUE);
FileWriteDouble(ExtHandle, d_volume, DOUBLE_VALUE);
// 2-nd change - remove FileFlush from here

Let's save the file obtained after these two corrections as period_converter_580.mq4 and compile it. You can download it from the files attached to this article. Now, we open another EURUSD M1 chart and run the script with parameter 3 in order to create a three-minute non-standard EURUSD chart.

After the data file is generated, we open it as an offline EURUSD M3 chart.

As expected, after the chart was opened, the launched period_converter_580 script has detected it and sent a message to the journal. From now on, it will send the update command to the offline chart not more often than once per 2 seconds.

Thus, we have tested working with the previous historical data storage format and made sure that everything works as before.

New PeriodConverter Script

The standard delivery of the new MetaTrader 4 client terminal version also contains PeriodConverter script that performs the same tasks as its predecessor. However, it has some minor differences from the old one, as it has been developed using the new source code style. Also, it features the new possibilities of MQL4 language.

The most important difference from the previous version is the use of the new MqlRates structure developed for working with price bar data in the new format:

if(time0>=rate.time+periodseconds || i==0)
   if(i==0 && time0<rate.time+periodseconds)

MQL4 developers who actively use offline charts will quickly appreciate the convenience of the new approach.

Structure for storing information about prices, volumes and spread

struct MqlRates
   datetime time;    // period start time
   double open;      // Open price
   double high;      // High price for the period
   double low;       // Low price for the period
   double close;     // Close price
   long tick_volume; // tick volume
   int spread;       // spread
   long real_volume; // trade volume

Let's compile the new PeriodConverter script and launch it on the new EURUSD M1 chart, like the previous ones.

At this time, we are preparing the data for the offline EURUSD M4 chart. Thus, the multiplier is equal to 4.

After the data has been prepared, we are opening the offline chart the same way.

As you can see, all three versions of the script are working similarly. All traders working with custom symbol charts or non-standard timeframes will be able to use them in the new version of MetaTrader 4 terminal, as well as improve existing source codes and develop the new ones. No fundamental changes or difficulties await you while switching to the new version.


  1. The offline charts in the new terminal work the same way as before. Both new and previous historical data formats are supported.
  2. Old EX4 files preserve their functionality in the new terminal.
  3. Start developing new MQL4 applications using all new language features and reveal their full potential.

Related articles:

  1. Testing Expert Advisors on Non-Standard Time Frames
  2. Principles of Time Transformation in Intraday Trading
  3. Equivolume Charting Revisited
  4. Synthetic Bars - A New Dimension to Displaying Graphical Information on Prices
  5. "Free-of-Holes" Charts

Translated from Russian by MetaQuotes Ltd.
Original article:

Last comments | Go to discussion (6)
okwh | 28 Feb 2014 at 07:42

hst file ? That is not new data file. hcc is !

How to write hcc file ?

[Deleted] | 26 Apr 2014 at 10:47

Thanks for a Great article :)

I have an EA that produce me a Renko offline chart, and I understood that it is imposible to produce this kind of chart in MQL5 .

Will i be able to use this EA on MQL4 build 6.0 and higher ?

If it can be done on MQL4, why can't we do it on MQL5 ?



Do you mind to share how do you write the hst.file format ??? The header and the body content . I've been trying to write mine ( as an EA too ) but I just can't get the offline chart working . The bars it returns is wrong and the From and To date goes the other way round, meaning FROM 2002 TO 1970 ( Something's wrong obviously ) . I post a question but there's still no reply yet .
Rasoul Mojtahedzadeh
Rasoul Mojtahedzadeh | 20 Jan 2015 at 23:54
Can we run EAs on offline charts?
bah12ram | 31 Dec 2015 at 18:26

Thanks for a Great article :)

I have an EA that produce me a Renko offline chart, and I understood that it is imposible to produce this kind of chart in MQL5 .

Will i be able to use this EA on MQL4 build 6.0 and higher ?

If it can be done on MQL4, why can't we do it on MQL5 ?



I am looking for such an EA that produce me a Renko offline chart. would you please let me know what is the name of this EA?
Stanislav Korotky
Stanislav Korotky | 31 Dec 2015 at 19:44
I am looking for such an EA that produce me a Renko offline chart. would you please let me know what is the name of this EA?
This is impossible on MQL5 due to technical limitations of the platform. As for MQL4 version, you can find a lot such EAs - just use search ("renko") here on the site.
Data Structure in MetaTrader 4 Build 600 and Higher Data Structure in MetaTrader 4 Build 600 and Higher
MetaTarder 4 build 600 features the new structure and location of the client terminal files. Now, MQL4 applications are placed in separate directories according to the program type (Expert Advisors, indicators or scripts). In most cases, the terminal data is now stored in a special data folder separated from the terminal installation location. In this article, we will describe in details how data is transferred, as well as the reasons for introducing the new storage system.
Testing and Optimization of Expert Advisors Testing and Optimization of Expert Advisors
The article provides a detailed description of the process of testing and optimizing Expert Advisors in the MetaTrader 4 Strategy Tester. The importance of such information and the need for this publication cannot be underestimated. A lot of users who only get started with the MetaTrader 4 trading platform have a very vague idea of what and how needs to be done when working with Expert Advisors. The proposed article gives simple and clear answers to all of these questions and provides a slightly more professional approach to handling these issues using a specific example.
Upgrade to MetaTrader 4 Build 600 and Higher Upgrade to MetaTrader 4 Build 600 and Higher
The new version of the MetaTrader 4 terminal features the updated structure of user data storage. In earlier versions all programs, templates, profiles etc. were stored directly in terminal installation folder. Now all necessary data required for a particular user are stored in a separate directory called data folder. Read the article to find answers to frequently asked questions.
Lite_EXPERT2.mqh: Expert Advisor Implementation Examples Lite_EXPERT2.mqh: Expert Advisor Implementation Examples
In this article, the author continues to familiarize the readers with the Lite_EXPERT2.mqh functions using real Expert Advisor implementation examples. The article deals with the idea of using floating pending orders and pending orders that vary dynamically from deal to deal which are determined based on Average True Range (ATR) indicator values.