_s_HistoryGen

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
_s_HistoryGen

This script is designed to automate the creation of historical price data for various timeframes based on a primary 1-minute chart. It serves as a data conversion tool, allowing users to generate high-level historical files from granular price movements.

Core Functionality

The primary purpose of this tool is to "compress" 1-minute market data into larger, standard time intervals (such as 5-minute, 15-minute, hourly, or daily candles). Instead of a user having to manually analyze every minute of trading, this script performs the calculations automatically to build out those larger historical records.

How it Works

To ensure accuracy and consistency, the script follows a specific logical workflow:

  • Environment Validation: Before starting, the script checks if it is being run on a 1-minute (M1) chart. If the user attempts to run it on any other timeframe, it will alert the user that only M1 charts are compatible for this specific generation process.
  • Timeframe Mapping: The tool contains a pre-defined list of target timeframes. It iterates through these periods sequentially, creating a corresponding historical file for each one.
  • Data Aggregation Logic: For every target timeframe, the script scans the 1-minute history and performs "aggregation." It looks at the price action within a specific window (e.g., a 15-minute block) and calculates:
    • The opening and closing prices.
    • The highest and lowest price points reached during that timeframe.
    • The cumulative trading volume.
  • File Generation: As it processes the data, it builds a standardized history file for each timeframe. It includes necessary technical headers (such as currency symbols and decimal precision) to ensure that the generated files are compatible with standard trading software.
  • Reporting: Once the process is complete, the script provides a summary in the logs, notifying the user exactly how many records were successfully written for each specific timeframe.

Summary for the User

Essentially, this is a utility for building a comprehensive historical database. By running this on a 1-minute chart, you can instantly populate your history with various larger timeframes, ensuring that your data across different views (from short-term to long-term) is consistent and derived from the same primary source.

Miscellaneous
It opens Message Boxes to the userIt writes information to fileIt writes information to fileIt writes information to fileIt writes information to file
9 Views
0 Downloads
0 Favorites
_s_HistoryGen
//+------------------------------------------------------------------+
//|                                                _s_HistoryGen.mq4 |
//|                                     Copyright © 2005, Doji Starr |
//|                   Based on Period_converter script by metaquotes |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property show_inputs

#include <WinUser32.mqh>



int start()
{
    if (Period() != 1)
    {
        MessageBox("This script is to be applied to M1 charts only.", "_s_HistoryGen", MB_OK);
        return (-1);
    }
    
    int    ExtHandle, period;
    int    periodArray[9] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
    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 ,cnt;
    
    //---- History header
    int    version=400;
    string c_copyright = "(C)opyright 2003, MetaQuotes Software Corp.";
    string c_symbol=Symbol();
    int    i_period;
    int    i_digits=Digits;
    int    i_unused[13];
    
    for (period=1; period<=8; period++)
    {
        ExtHandle=-1;
        hwnd = 0;
        cnt = 0;
        version = 400;
        i_period = Period()*periodArray[period];
    
        ExtHandle = FileOpenHistory(c_symbol+i_period+".hst", FILE_BIN|FILE_WRITE);
        if (ExtHandle < 0)
            return(-1);
            
        //---- write history file header
        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=Open[start_pos];
        d_low=Low[start_pos];
        d_high=High[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--)
        {
            //     Print(i);
            time0=Time[i];
            if(time0>=i_time+periodseconds || i==0)
            {
                if(i==0 && time0<i_time+periodseconds)
                {
                    d_volume+=Volume[0];
                    if (Low[0]<d_low)   d_low=Low[0];
                    if (High[0]>d_high) d_high=High[0];
                    d_close=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=Open[i];
                    d_low=Low[i];
                    d_high=High[i];
                    d_close=Close[i];
                    d_volume=last_volume;
                }
            }
            else
            {
                d_volume+=Volume[i];
                if (Low[i]<d_low)   d_low=Low[i];
                if (High[i]>d_high) d_high=High[i];
                d_close=Close[i];
            }
        } 
        FileFlush(ExtHandle);
        FileClose(ExtHandle);
            
        Print("Period_", periodArray[period], ": ", cnt, " record(s) written");
    }
    
    return(0);
}

Comments