//      File: MT4-HST.bt
//   Authors: jjnet
//    E-mail: jj.net@163.com
//   Version: 0.1
//   Purpose: MetaTrader 4 HST (versions 400 and 401) for 010 Editor.
//  Category: Financial
// File Mask: *.hst
//   History:
//   0.1 2026-11-8 jjnet: Public release.

typedef struct {
    uint version;
    char copyright[64];
    char symbol[12];
    uint periodMinutes;
    uint digits;
    time_t timeSign;
    uint lastSync;
    uint reserved[13];
} HST_HEADER;

typedef struct {
    time_t barTimeSeconds;
    double open;
    double low;
    double high;
    double close;
    double tickVolume;
} HST_BAR_400;

typedef struct {
    time64_t barTimeSeconds;
    double open;
    double high;
    double low;
    double close;
    int64 tickVolume;
    int spreadPoints;
    int64 realVolume;
} HST_BAR_401;

LittleEndian();
FSeek(0);

if (FileSize() < 148) {
    Warning("File is shorter than the 148-byte HST header.");
    return;
}

HST_HEADER header;
local int recordSize;

switch (header.version) {
    case 400: recordSize = 44; break;
    case 401: recordSize = 60; break;
    default:
        Warning("Unsupported HST version. Expected 400 or 401.");
        return;
}

local int64 dataSize = FileSize() - FTell();
local int64 barCount = dataSize / recordSize;
local int64 trailingSize = dataSize % recordSize;

if (barCount > 0) {
    if (header.version == 400)
        HST_BAR_400 bars400[barCount];
    else
        HST_BAR_401 bars401[barCount];
}

if (trailingSize > 0) {
    uchar trailingBytes[trailingSize];
    Warning("File ends with an incomplete HST bar record.");
}
