Problem opening a file for write and strange behavior of TimeCurrent()

 

When using the following code in a script, it works fine:

CurrentFileName = StringConcatenate(ReportsDir,"\\",title," ",TimeCurrent(),".html");
h = FileOpen(CurrentFileName,FILE_WRITE);

However, using the same code in an expert advisor result in an error opening the file.

When removing the TimeCurrent(), it works fine.

Any suggestions?

 
  1. TimeCurrent to a string is "yyyy.mm.dd hh:mi" and you can't use a colon in filenames.

  2. Don't even try to use any price or server related functions in OnInit as there may be no connection/chart yet:
    1. Terminal starts.
    2. indicators/EAs are loaded.
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called; Now TickValue, TimeCurrent and prices are now valid.
 

The report (and fileOpen command) are executed at OnTimer() event.

When run from a script, TimeCurrent returns a value like 1509638400 (unix time). It works fine and there is no need to convert it to a string.

I am not sure why it is behaving differently in an EA and how can I append the datetime to the file name.

I tried:

string t=TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

t=StringReplace(t,":","");

But it does not seem to work.

 
Hannan Nussbaum:

I am not sure why it is behaving differently in an EA and how can I append the datetime to the file name.

string t=TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

t=StringReplace(t,":","");

But it does not seem to work.

  1. Your script doesn't have #property strict, your EA does.
  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here and our crystal balls are cracked.
    Use the debugger or print out your variables, and find out why.
Reason: