-1

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();
  }
//+------------------------------------------------------------------+

1 Answers1

0

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

Dale Woods
  • 784
  • 1
  • 13
  • 31