#include <default.c>
#include <stdio.h>
#include <windows.h>
typedef struct SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
int _stdcall SystemTimeToVariantTime(SYSTEMTIME *lpSystemTime, DATE *pvtime);
DATE ConvertTime(int year, int month, int day, int hour, int minute, int second)
{
SYSTEMTIME Time;
DATE vTime;
memset(&Time, 0, sizeof(SYSTEMTIME));
Time.wYear = year;
Time.wMonth = month;
Time.wDay = day;
Time.wHour = hour;
Time.wMinute = minute;
Time.wSecond = second;
SystemTimeToVariantTime(&Time, &vTime);
return vTime;
}
function main()
{
FILE *in, *tmp, *out;
if (!(in = fopen("EURUSD_M1_201312.csv", "r"))) {
printf("\ncan't open input file");
return;
}
if (!(tmp = tmpfile())) {
printf("\ncan't open temporary file");
return;
}
if (!(out = fopen("export.bar", "wb"))) {
printf("\ncan't open output file");
return;
}
char line[64];
TICK tick;
int lines = 0, errors = 0;
while (fgets(line, 64, in)) {
int year, month, day, hour, minute, second;
if (sscanf(line, "%4d%2d%2d %2d%2d%2d;%f;%f;%f;%f;",
&year, &month, &day, &hour, &minute, &second,
&tick.fOpen, &tick.fHigh, &tick.fLow, &tick.fClose) == 10) {
tick.time = ConvertTime(year, month, day, hour, minute, second);
if (!fwrite(&tick, sizeof(TICK), 1, tmp)) {
printf("\nwrite error");
return;
}
} else {
errors++;
}
lines++;
}
printf("\nStep 1/2: %d lines parsed (%d errors)", lines, errors);
int offset, bars = 0;
for (offset = ftell(tmp) - sizeof(TICK); offset >= 0; offset -= sizeof(TICK)) {
fseek(tmp, offset, SEEK_SET);
if (fread(&tick, sizeof(TICK), 1, tmp)) {
if (!fwrite(&tick, sizeof(TICK), 1, out)) {
printf("\nwrite error");
return;
}
bars++;
}
}
fclose(out);
printf("\nStep 2/2: %d bars saved", bars);
}