Offline Charts in the New MQL4

MetaQuotes | 4 February, 2014

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