Download MetaTrader 5

Offline Charts in the New MQL4

4 February 2014, 07:32
MetaQuotes Software Corp.
6
14 538

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 Software Corp.
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 ?

MQL4 Comments
MQL4 Comments | 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.
Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.

How to create an indicator of non-standard charts for MetaTrader Market How to create an indicator of non-standard charts for MetaTrader Market

Through offline charts, programming in MQL4, and reasonable willingness, you can get a variety of chart types: "Point & Figure", "Renko", "Kagi", "Range bars", equivolume charts, etc. In this article, we will show how this can be achieved without using DLL, and therefore such "two-for-one" indicators can be published and purchased from the Market.