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

Offline Charts in the New MQL4

MetaTrader 4Examples | 4 February 2014, 07:32
44 828 6
MetaQuotes
MetaQuotes

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
   ExtHandle=FileOpenHistory(c_symbol+i_period+".hst",FILE_BIN|FILE_WRITE|FILE_SHARE_WRITE|FILE_SHARE_READ);

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.

last_fpos=FileTell(ExtHandle);
last_volume=Volume[i];
//--- 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
//FileFlush(ExtHandle);

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)
     {
      rate.tick_volume+=(long)Volume[0];
      if(rate.low>Low[0])
         rate.low=Low[0];
      if(rate.high<High[0])
         rate.high=High[0];
      rate.close=Close[0];
     }
   last_fpos=FileTell(ExtHandle);
   last_volume=(long)Volume[i];
   FileWriteStruct(ExtHandle,rate);
   cnt++;
   if(time0>=rate.time+periodseconds)
     {
      rate.time=time0/periodseconds;
      rate.time*=periodseconds;
      rate.open=Open[i];
      rate.low=Low[i];
      rate.high=High[i];
      rate.close=Close[i];
      rate.tick_volume=last_volume;
     }
  }

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.


Conclusion

  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: https://www.mql5.com/ru/articles/1387

Last comments | Go to discussion (6)
okwh
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
galsa:

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 ?

thanks

gal


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
bah12ram | 31 Dec 2015 at 18:26
galsa:

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 ?

thanks

gal

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
bah12ram:
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.
Creating Non-Lagging Digital Filters Creating Non-Lagging Digital Filters
The article describes one of the approaches to determining a useful signal (trend) in stream data. Small filtering (smoothing) tests applied to market quotes demonstrate the potential for creating non-lagging digital filters (indicators) that are not redrawn on the last bars.
MQL5 Programming Basics: Lists MQL5 Programming Basics: Lists
The new version of the programming language for trading strategy development, MQL [MQL5], provides more powerful and effective features as compared with the previous version [MQL4]. The advantage essentially lies in the object-oriented programming features. This article looks into the possibility of using complex custom data types, such as nodes and lists. It also provides an example of using lists in practical programming in MQL5.
MQL5 Cookbook: Developing a Multi-Symbol Volatility Indicator in MQL5 MQL5 Cookbook: Developing a Multi-Symbol Volatility Indicator in MQL5
In this article, we will consider the development of a multi-symbol volatility indicator. The development of multi-symbol indicators may present some difficulties for novice MQL5 developers which this article helps to clarify. The major issues arising in the course of development of a multi-symbol indicator have to do with the synchronization of other symbols' data with respect to the current symbol, the lack of some indicator data and the identification of the beginning of 'true' bars of a given time frame. All of these issues will be closely considered in the article.