Javier Santiago Gaston De Iriarte Cabrera:
Show the log output while running the posted code.
Hi, I don't know why, but can't manage to read CSV or TXT
I have tried all the possible combinations for reading path, and always get the same results (error 5004 or 5002)
what do I have to change to read the txt?
This:
string filePath = "C:\\Users\\jsgas\\Documents\\todas_las_tasas_2000_2024.txt";
would try (and fails) an access outside of the MQ Sandbox!
Alain Verleyen #:
Show the log output while running the posted code.
Show the log output while running the posted code.
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00 File does NOT exist! 2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00 File does NOT exist! 2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00 File check failed 2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00 Error: No se pudieron cargar las tasas de interés 2024.12.22 20:25:37.034 Core 01 tester stopped because OnInit returns non-zero code 1 2024.12.22 20:25:37.036 Core 01 disconnected
EDITED
Great! Thanks!
bool LoadInterestRatesFromTxt() { string filename = "todas_las_tasas_2000_2024.txt"; // Print detailed file location information string common_folder = TerminalInfoString(TERMINAL_COMMONDATA_PATH); string full_path = common_folder + "\\" + filename; PrintFormat("Attempting to load file:"); PrintFormat("Common Folder: %s", common_folder); PrintFormat("Full File Path: %s", full_path); // Check if file exists before attempting to open bool fileExists = FileIsExist(filename, FILE_COMMON); PrintFormat("File Exists in Common Folder: %s", fileExists ? "Yes" : "No"); // Try different file opening modes int handle = FileOpen(filename, FILE_READ|FILE_TXT|FILE_COMMON); if(handle == INVALID_HANDLE) { int error = GetLastError(); PrintFormat("Error opening file. Error code: %d", error); return false; } // Read the entire file content string file_content = ""; while(!FileIsEnding(handle)) { file_content += FileReadString(handle) + "\n"; } FileClose(handle); // Split the content into lines string lines[]; int lines_count = StringSplit(file_content, '\n', lines); PrintFormat("Total lines in file: %d", lines_count); // Prepare array for interest rates ArrayResize(g_interestRates, 1000); int idx = 0; // Start from 1 to skip header for(int i = 1; i < lines_count; i++) { // Skip empty lines if(lines[i] == "" || lines[i] == "\r") continue; // Split the line using tab string parts[]; int parts_count = StringSplit(lines[i], '\t', parts); // Ensure we have enough parts if(parts_count < 5) { PrintFormat("Skipping line %d: insufficient parts. Line: %s", i, lines[i]); continue; } // Remove leading/trailing whitespace manually string dateStr = StringTrimLeft(parts[0]); string usdStr = StringTrimLeft(parts[1]); string jpyStr = StringTrimLeft(parts[3]); // Remove trailing spaces dateStr = StringSubstr(dateStr, 0, StringLen(dateStr)); usdStr = StringSubstr(usdStr, 0, StringLen(usdStr)); jpyStr = StringSubstr(jpyStr, 0, StringLen(jpyStr)); // Skip lines with empty values if(dateStr == "" || usdStr == "" || jpyStr == "") { PrintFormat("Skipping line %d: empty critical values", i); continue; } datetime fecha = StringToTime(dateStr); double tasaUSD = StringToDouble(usdStr); double tasaJPY = StringToDouble(jpyStr); // Store in structure g_interestRates[idx].date = fecha; g_interestRates[idx].usdRate = tasaUSD; g_interestRates[idx].jpyRate = tasaJPY; g_interestRates[idx].interestDifferential = tasaUSD - tasaJPY; idx++; // Resize array if necessary if(idx >= ArraySize(g_interestRates)) { ArrayResize(g_interestRates, ArraySize(g_interestRates) + 1000); } // Limit to prevent potential infinite loop if(idx >= 300) break; } // Adjust final array size ArrayResize(g_interestRates, idx); PrintFormat("File reading complete:"); PrintFormat("Total lines processed: %d", idx); PrintFormat("Interest rates loaded: %d entries", idx); // Log first and last entries for verification if(idx > 0) { PrintFormat("First entry - Date: %s, USD Rate: %.2f, JPY Rate: %.2f", TimeToString(g_interestRates[0].date, TIME_DATE), g_interestRates[0].usdRate, g_interestRates[0].jpyRate); PrintFormat("Last entry - Date: %s, USD Rate: %.2f, JPY Rate: %.2f", TimeToString(g_interestRates[idx-1].date, TIME_DATE), g_interestRates[idx-1].usdRate, g_interestRates[idx-1].jpyRate); } return idx > 0; }
#property tester_file "todas_las_tasas_2000_2024.txt"
ArrayResize(g_interestRates, 0); // Intentar cargar las tasas de interés if(!LoadInterestRatesFromTxt()) { Print("Error: No se pudieron cargar las tasas de interés"); return(INIT_FAILED); }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi, I don't know why, but can't manage to read CSV or TXT
I have tried all the possible combinations for reading path, and always get the same results (error 5004 or 5002)
what do I have to change to read the txt?