My code does not get executed at the starting time

My code does not get executed at the starting time

Manage alerts

Loading saved threads...

emptyfullstack · External communityPost link
External question — Stack Overflow Stack Exchange Author: emptyfullstack Original post: https://stackoverflow.com/questions/76953488 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. What I would like to achieve with the following code is to export a csv file containing the bit of USD/JPY recorded every 100ms. This file can successfully compiled, but not get executed when it's the starting time. Could anyone tell me why that is? I have placed .ex5 file under EA and attach it with the USD/JPY chart displayed in the platform. input datetime StartTime = D'2023.08.17 10:00'; // Example start time input datetime EndTime = D'2023.08.17 10:05'; // Example end time, 5 minutes later bool isTimeToFetch = false; string csvData = "Time,Price\n"; // CSV header //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { EventSetTimer(0.1); // Set timer to trigger every 100ms return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { datetime currentTime = TimeCurrent(); if(currentTime >= StartTime && currentTime <= EndTime) { if(!isTimeToFetch) { isTimeToFetch = true; // Flag to ensure we only start fetching once Print("Fetching data..."); } double price = SymbolInfoDouble("USDJPY", SYMBOL_BID); // Get current bid price, change to SYMBOL_ASK for ask price csvData += TimeToString(currentTime, TIME_DATE|TIME_MINUTES) + "," + DoubleToString(price, 5) + "\n"; // 5 decimal precision } else if(isTimeToFetch) // Once the fetching started and we're now outside of the time range { isTimeToFetch = false; // Stop fetching Print("Finished fetching data."); // Save to CSV SaveToCSV("USDJPY_data.csv"); } } void SaveToCSV(string filename) { Kc ResetLastError(); int fileHandle = FileOpen(path, FILE_WRITE|FILE_CSV, ','); if(fileHandle != INVALID_HANDLE) { FileWriteString(fileHandle, csvData); FileClose(fileHandle); Print("Data saved to: ", path); } else { Print("Error in saving CSV file: ", GetLastError()); } } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { EventKillTimer(); } //+------------------------------------------------------------------+
Quote
Report
Dale Woods · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Dale Woods Original post: https://stackoverflow.com/a/76962088 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Because EventSetTimer accepts an integer and you're passing 0.1 which will get truncated to 0 and never fire. Use EventSetMillisecondTimer instead https://docs.mql4.com/eventfunctions/eventsetmillisecondtimer
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: emptyfullstack Source score (net votes, not local likes): -1 Original post: https://stackoverflow.com/questions/76953488 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. What I would like to achieve with the following code is to export a csv file containing the bit of USD/JPY recorded every 100ms. This file can successfully compiled, but not get executed when it's the starting time. Could anyone tell me why that is? I have placed .ex5 file under EA and attach it with the USD/JPY chart displayed in the platform. input datetime StartTime = D'2023.08.17 10:00'; // Example start time input datetime EndTime = D'2023.08.17 10:05'; // Example end time, 5 minutes later bool isTimeToFetch = false; string csvData = "Time,Price\n"; // CSV header //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { EventSetTimer(0.1); // Set timer to trigger every 100ms return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { datetime currentTime = TimeCurrent(); if(currentTime >= StartTime && currentTime <= EndTime) { if(!isTimeToFetch) { isTimeToFetch = true; // Flag to ensure we only start fetching once Print("Fetching data..."); } double price = SymbolInfoDouble("USDJPY", SYMBOL_BID); // Get current bid price, change to SYMBOL_ASK for ask price csvData += TimeToString(currentTime, TIME_DATE|TIME_MINUTES) + "," + DoubleToString(price, 5) + "\n"; // 5 decimal precision } else if(isTimeToFetch) // Once the fetching started and we're now outside of the time range { isTimeToFetch = false; // Stop fetching Print("Finished fetching data."); // Save to CSV SaveToCSV("USDJPY_data.csv"); } } void SaveToCSV(string filename) { Kc ResetLastError(); int fileHandle = FileOpen(path, FILE_WRITE|FILE_CSV, ','); if(fileHandle != INVALID_HANDLE) { FileWriteString(fileHandle, csvData); FileClose(fileHandle); Print("Data saved to: ", path); } else { Print("Error in saving CSV file: ", GetLastError()); } } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { EventKillTimer(); } //+------------------------------------------------------------------+

Cancel quote

Checking account access…