period_converter_ALL
Download scriptThis script is designed as an automated data processing tool for MetaTrader users who need to convert historical trading data from a high-frequency timeframe into multiple higher timeframes simultaneously.
Core Functionality
The primary purpose of this tool is to take "Minute 1" (M1) price data—which represents every individual minute of market activity—and consolidate it into larger blocks of time. Instead of the user manually downloading or calculating data for different intervals, this script automatically generates new history files for several standard periods at once:
- 5-Minute (M5)
- 15-Minute (M15)
- 30-Minute (M30)
- 1-Hour (H1)
- 4-Hour (H4)
- Daily (D1)
- Weekly (W1)
- Monthly (MN1)
How the Logic Works
To ensure accuracy, the script follows a specific logical flow to reconstruct these larger timeframes from the smaller minute-by-minute data:
1. Validation Check Before starting any work, the script checks if the chart you are currently viewing is set to the 1-Minute (M1) period. If it is not, the script will stop immediately and notify the user, as it requires the highest level of detail to perform the conversion correctly.
2. Data Aggregation (The "Grouping" Process) The script looks at each minute of history and begins "stacking" them into the target timeframes. It doesn't just pick a random point; it calculates the mathematical boundaries for each new period. For example, to create a 5-minute bar, it groups five consecutive M1 bars together.
3. Price Calculation Rules While grouping the data, the script identifies four key pieces of information for every new bar created:
- Open: It captures the price at the exact start of the time block.
- High/Low: It scans all the individual minutes within that block to find the absolute highest and lowest prices reached.
- Close: It identifies the price at the very last minute of that time block.
- Volume: It adds up the total trading volume from every minute included in that specific period.
4. Automated File Creation As the script processes the data, it creates and saves individual history files for every timeframe mentioned above. Each file is formatted correctly so that the MetaTrader platform can read them as native history files. This allows you to have the converted data ready for use in backtesting or technical analysis without having to manually compile the data yourself.
Summary for the User
Essentially, this is a "bulk converter." If you have a lot of M1 data but need to see the trends on a Daily or 4-Hour chart, you can run this script once. It will do the heavy lifting by scanning through every minute of history, calculating the correct highs, lows, and volumes, and saving all the resulting timeframes into your system in one continuous process.
//+------------------------------------------------------------------+
//| Period_ConverterALL.mq4 |
//| Original Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//| Only OffLine convert history from 1M Period |
//| to M5, M15, M30, 1H, 4H, 1D in one time |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
//#property show_inputs
#include <WinUser32.mqh>
//extern int ExtPeriodMultiplier=5; // new period multiplier factor
int ExtHandle = -1;
static int ArrPeriod[];
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
if(Period() != PERIOD_M1)
{
Print("Start Period Must be M1.");
return(0);
}
ArrayResize(ArrPeriod, 8);
ArrPeriod[0] = 5;
ArrPeriod[1] = 15;
ArrPeriod[2] = 30;
ArrPeriod[3] = 60;
ArrPeriod[4] = 240;
ArrPeriod[5] = 1440;
ArrPeriod[6] = 10080;
ArrPeriod[7] = 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 = 0, cnt = 0;
//---- History header
int version = 400;
string c_copyright = "(C)opyright 2003, MetaQuotes Software Corp.";
string c_symbol = Symbol();
int i_period = 1;
int i_digits = Digits;
int i_unused[13];
string Comm = "";
for(int qq = 0; qq < ArraySize(ArrPeriod); qq++)
{
i_period = Period()*ArrPeriod[qq];
Comm = "Converting to Period (" + i_period + "), bars to end: ";
//----
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--)
{
if(MathMod(i, 1000) == 0)
Comment(Comm + " " + 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);
if(ExtHandle >= 0)
{
FileClose(ExtHandle);
ExtHandle = -1;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void deinit()
{
if(ExtHandle >= 0)
{
FileClose(ExtHandle); ExtHandle = -1;
}
Comment("");
}
//+------------------------------------------------------------------+
Comments