#define NUM_PAIRS 28 // Number of currency pairs
#define NUM_TIMEFRAMES 2 // Number of timeframes to process
// Arrays to store the currency pairs and timeframes
string Pairs[NUM_PAIRS];
string Timeframes[NUM_TIMEFRAMES];
/*
* Format string for parsing CSV data.
* The '+' indicates the CSV file has a header line to skip.
* %Y - Year (4 digits)
* %m - Month (2 digits)
* %d - Day (2 digits)
* %H - Hour (2 digits, 24-hour format)
* %M - Minute (2 digits)
* %f3 - Open price (floating point with 3 decimals)
* %f1 - High price (floating point with 1 decimal)
* %f2 - Low price (floating point with 2 decimals)
* %f4 - Close price (floating point with 4 decimals)
* %f6 - Volume (floating point with 6 decimals)
*
* This must match the structure of your CSV files.
*/
string Format = "+%Y.%m.%d,%H:%M,%f3,%f1,%f2,%f4,%f6";
// Function to initialize currency pairs and timeframes
// Modify this to include/remove pairs or adjust timeframes as needed.
function initializePairsAndTimeframes() {
// Currency pairs
Pairs[0] = "EURUSD";
Pairs[1] = "GBPUSD";
Pairs[2] = "USDJPY";
Pairs[3] = "USDCHF";
Pairs[4] = "USDCAD";
Pairs[5] = "AUDUSD";
Pairs[6] = "NZDUSD";
Pairs[7] = "EURGBP";
Pairs[8] = "EURJPY";
Pairs[9] = "EURCHF";
Pairs[10] = "GBPJPY";
Pairs[11] = "GBPCHF";
Pairs[12] = "AUDJPY";
Pairs[13] = "AUDCHF";
Pairs[14] = "NZDCAD";
Pairs[15] = "NZDJPY";
Pairs[16] = "NZDCHF";
Pairs[17] = "CADJPY";
Pairs[18] = "CADCHF";
Pairs[19] = "CHFJPY";
Pairs[20] = "EURAUD";
Pairs[21] = "EURNZD";
Pairs[22] = "EURCAD";
Pairs[23] = "GBPAUD";
Pairs[24] = "GBPNZD";
Pairs[25] = "GBPCAD";
Pairs[26] = "AUDNZD";
Pairs[27] = 0; // End marker
// Timeframes in minutes (e.g., 60 = 1 hour, 240 = 4 hours)
Timeframes[0] = "60";
Timeframes[1] = "240";
}
/*
* Function to convert a CSV file to a .t6 file.
* This version splits the CSV data by year and saves each year as a separate .t6 file.
*
* Parameters:
* InName - The path and name of the input CSV file.
* Pair - The currency pair string to include in the output filename.
*
* Outputs:
* Files are saved as {CurrencyPair}_{Year}.t6, e.g., EURAUD_2025.t6
*/
function ConvertCSV(string InName, string Pair) {
int Records = dataParse(1, Format, InName); // Parse the CSV with the defined format
printf("\n%d lines read from %s", Records, InName); // Print the number of records read
if(Records) {
int i, Start = 0, Year, LastYear = 0;
for(i = 0; i < Records; i++) {
Year = ymd(dataVar(1, i, 0)) / 10000; // Extract the year from the date
if(!LastYear) LastYear = Year; // Set the first encountered year
// Handle the last record
if(i == Records - 1) {
LastYear = Year;
Year = 0;
i++;
}
// When the year changes, save the data segment to a new .t6 file
if(Year != LastYear) {
// Construct the output file name as {Pair}_{Year}.t6
string OutName = strf("C:\\Users\\**username**\\Zorro\\History\\%s_%4i.t6", Pair, LastYear);
printf("\nSaving file: %s", OutName);
dataSave(1, OutName, Start, i - Start); // Save the data segment to .t6
Start = i; // Update the start index for the next segment
LastYear = Year; // Update the current year
}
}
}
}
/*
* Main function:
* Loops through all specified currency pairs and timeframes,
* checks for CSV files in the specified directory, and converts them to .t6 files.
*/
function main() {
initializePairsAndTimeframes(); // Initialize pairs and timeframes
int p, t; // Loop counters for pairs and timeframes
// Loop through each currency pair
for(p = 0; Pairs[p]; p++) {
// Loop through each timeframe
for(t = 0; t < NUM_TIMEFRAMES; t++) {
// Construct the CSV file path dynamically
// Path: C:\Users\**username**\Zorro\History\{CurrencyPair}{Timeframe}.csv
string FileName = strf("C:\\Users\\**user//name**\\Zorro\\History\\%s%s.csv", Pairs[p], Timeframes[t]);
printf("\nChecking file: %s", FileName); // Log the file being checked
if(file_length(FileName)) { // Check if the file exists
printf("\nConverting %s...", FileName); // Log the conversion process
ConvertCSV(FileName, Pairs[p]); // Call the conversion function with the pair name
} else {
printf("\nFile not found: %s", FileName); // Log missing files
}
}
}
quit("Conversion done!"); // Exit the script when all files are processed
}