Exp - Convertor

Download script
Author: Copyright � 2007, MetaQuotes Software Corp.
Exp - Convertor

This script functions as a data processing tool designed to aggregate and convert market price history into a custom file format. Its primary objective is to take historical price data from a specific financial instrument and reorganize it based on set time intervals, effectively "translating" the data for use in other applications or for specialized analysis.

Core Functional Logic

1. Initialization and File Creation When the script begins, it identifies the specific currency pair (symbol) and the current time period you are viewing on your chart. It then creates a dedicated history file. This file is structured with a standardized header containing essential metadata, such as copyright information, symbol names, and the number of digits used in the pricing, ensuring the output file is readable by compatible systems.

2. Historical Data Aggregation The script scans through your existing historical price records. Because market data often arrives in small "ticks," the script performs a mathematical grouping process:

  • It calculates the duration of the time period (e.g., how many seconds are in a specific timeframe).
  • It groups multiple smaller price movements into a single consolidated record for that timeframe.
  • During this grouping, it tracks key statistics: the opening price, the highest and lowest prices reached during that interval, the closing price, and the total volume traded.

3. Real-Time Synchronization Once the historical data is processed, the script enters an active monitoring mode. It continuously listens for new "ticks" or live price updates coming from the market.

  • If a price update occurs within the current timeframe, the script updates the running high, low, and volume totals.
  • As soon as a new timeframe begins, the script finalizes the previous record, writes it to the file, and starts a fresh calculation for the new period.

4. Continuous Updating and Feedback To ensure the data stays current without overloading your computer's resources, the script runs in a continuous loop with a very brief pause between checks. It also includes a mechanism to communicate with the trading platform's interface, occasionally sending a "refresh" command to ensure that any connected visual elements stay synchronized with the newly written data.

5. Resource Management The script is designed to be "clean"—it properly manages the file connection by ensuring the history file is correctly closed and released once the script is no longer running, preventing data corruption or memory leaks.

Miscellaneous
It writes information to fileIt writes information to fileIt writes information to fileIt writes information to file
9 Views
0 Downloads
0 Favorites
Exp - Convertor
//+------------------------------------------------------------------+
//|                                             Period_Converter.mq4 |
//|                 Copyright © 2005-2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property show_inputs
#include <WinUser32.mqh>

   extern string sym="CHFEUR";
int        ExtHandle=-1;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int    i, start_pos, i_time, time0, last_fpos, periodseconds;
   double d_open, d_low, d_high, d_close, d_volume, last_volume;
   int    hwnd=0,cnt=0;
//---- History header
   int    version=400;
   string c_copyright;
   string c_symbol=sym;
   int    i_period=Period();
   int    i_digits=Digits;
   int    i_unused[13];
//----  
   ExtHandle=FileOpenHistory(c_symbol+i_period+".hst", FILE_BIN|FILE_WRITE);
   if(ExtHandle < 0) return(-1);
//---- write history file header
   c_copyright="(C)opyright 2003, MetaQuotes Software Corp.";
   FileWriteInteger(ExtHandle, version, LONG_VALUE);
   FileWriteString(ExtHandle, c_copyright, 64);
   FileWriteString(ExtHandle, c_symbol, 12);
   FileWriteInteger(ExtHandle, i_period, LONG_VALUE);
   FileWriteInteger(ExtHandle, i_digits, LONG_VALUE);
   FileWriteInteger(ExtHandle, 0, LONG_VALUE);       //timesign
   FileWriteInteger(ExtHandle, 0, LONG_VALUE);       //last_sync
   FileWriteArray(ExtHandle, i_unused, 0, 13);
//---- write history file
   periodseconds=i_period*60;
   start_pos=Bars-1;
   d_open=1/Open[start_pos];
   d_low=1/High[start_pos];
   d_high=1/Low[start_pos];
   
   
   d_volume=Volume[start_pos];
   //---- normalize open time
   i_time=Time[start_pos]/periodseconds;
   i_time*=periodseconds;
   for(i=start_pos-1;i>=0; i--)
     {
      time0=Time[i];
      //---- history may be updated
      if(i==0)
        {
         //---- modify index if history was updated
         if(RefreshRates())
            i=iBarShift(NULL,0,time0);
        }
      //----
      if(time0>=i_time+periodseconds || i==0)
        {
         if(i==0 && time0<i_time+periodseconds)
           {
            d_volume+=Volume[0];
            if (1/High[0]<d_low)   d_low=1/High[0];
            if (1/Low[0]>d_high) d_high=1/Low[0];
            
            
            
            d_close=1/Close[0];
           }
         last_fpos=FileTell(ExtHandle);
         last_volume=Volume[i];
         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);
         FileFlush(ExtHandle);
         cnt++;
         if(time0>=i_time+periodseconds)
           {
            i_time=time0/periodseconds;
            i_time*=periodseconds;
            d_open=1/Open[i];
            d_low=1/High[i];
            d_high=1/Low[i];
            d_close=1/Close[i];
            d_volume=last_volume;
           }
        }
       else
        {
         d_volume+=Volume[i];
         if (1/High[i]<d_low)   d_low=1/High[i];
         if (1/Low[i]>d_high) d_high=1/Low[i];
         d_close=1/Close[i];
        }
     } 
   FileFlush(ExtHandle);
   Print(cnt," record(s) written");
//---- collect incoming ticks
   int last_time=LocalTime()-5;
   while(IsStopped()==false)
     {
      int cur_time=LocalTime();
      //---- check for new rates
      if(RefreshRates())
        {
         time0=Time[0];
         FileSeek(ExtHandle,last_fpos,SEEK_SET);
         //---- is there current bar?
         if(time0<i_time+periodseconds)
           {
            d_volume+=Volume[0]-last_volume;
            last_volume=Volume[0]; 
            if (1/High[0]<d_low) d_low=1/High[0];
            if (1/Low[0]>d_high) d_high=1/Low[0];
            d_close=1/Close[0];
           }
         else
           {
            //---- no, there is new bar
            d_volume+=Volume[1]-last_volume;
            if (1/High[1]<d_low) d_low=1/High[1];
            if (1/Low[1]>d_high) d_high=1/Low[1];
            //---- write previous bar remains
            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);
            last_fpos=FileTell(ExtHandle);
            //----
            i_time=time0/periodseconds;
            i_time*=periodseconds;
            d_open=1/Open[0];
            d_low=1/High[0];
            d_high=1/Low[0];
            d_close=1/Close[0];
            d_volume=Volume[0];
            last_volume=d_volume;
           }
         //----
         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);
         FileFlush(ExtHandle);
         //----
         if(hwnd==0)
           {
            hwnd=WindowHandle(Symbol(),i_period);
            if(hwnd!=0) Print("Chart window detected");
           }
         //---- refresh window not frequently than 1 time in 2 seconds
         if(hwnd!=0 && cur_time-last_time>=2)
           {
            PostMessageA(hwnd,WM_COMMAND,33324,0);
            last_time=cur_time;
           }
        }
      Sleep(50); 
     }      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit()
  {
   if(ExtHandle>=0) { FileClose(ExtHandle); ExtHandle=-1; }
  }
//+------------------------------------------------------------------+

Comments