File error 5004

 

Stuck again!!

two problems with the code below.

First is with the toolbar autotrade function switched off, when I attach this EA to the chart the EA still runs. WHY?

Second the EA gives a 5004 error and I cant see why.

What am I doing wrong please

int  targethandle = 0;
double target = 0;
void OnTick()
 { 
  targethandle = FileOpen("target.dat", FILE_READ|FILE_BIN);
  if (targethandle == INVALID_HANDLE)
   {
    Print("targethandle is not present so we must make targefile");
    targethandle = FileOpen("target.dat", FILE_WRITE|FILE_BIN);
    if (targethandle == INVALID_HANDLE)
     {
      Print("target.dat not opened for writing -- error# ", GetLastError());
      return;
     }
    else
     {
      Print("Target file opened to store the value of target");
      target = 123456.78;
     }
    FileWriteDouble(targethandle, target);
    FileClose("targethandle");
    Print("Target created @ ", target);
   }
  targethandle = FileOpen("target.dat", FILE_READ|FILE_BIN);
  if (targethandle == INVALID_HANDLE)
   {
    Print("tagetfile could not be opened for reading back the value of target -- error# ", GetLastError());
    return;
   }
  else
   {
    Print("targethandle is opened for reading");
    target = FileReadDouble(targethandle);
    Print("the value of target was read as ", target);
    FileClose(targethandle);
   }
 }
 
BigAl:

Stuck again!!

two problems with the code below.

First is with the toolbar autotrade function switched off, when I attach this EA to the chart the EA still runs. WHY?

Second the EA gives a 5004 error and I cant see why.

What am I doing wrong please

AutoTrading allow/disallow an EA to take trade (buy, sell) but the EA still is working.

For your second question :

ERR_CANNOT_OPEN_FILE

5004

File opening error


You are probably trying to open an non-existent file. You have to read documentation ;-)

If FILE_READ is specified, an attempt is made to open an existing file. If a file does not exist, file opening fails, a new file is not created.


 
angevoyageur:

AutoTrading allow/disallow an EA to take trade (buy, sell) but the EA still is working.

For your second question :

ERR_CANNOT_OPEN_FILE

5004

File opening error


You are probably trying to open an non-existent file. You have to read documentation ;-)


Hi angevoyageur,

First - Sorry but on MT4 the EA does not run if autotrade has not been selected so I dont understand why this is so on MT5

Second, the EA code above works perfectly correctly on MT4 if you change

           " VoidOnTick()"  to "int start()"

            and the three  occurrences of "== INVALID_HANDLE to " < 0 "

just try the code and see the results please as you should see that the file is opened then closed but fails on the re-opening

 
BigAl:

Hi angevoyageur,

First - Sorry but on MT4 the EA does not run if autotrade has not been selected so I dont understand why this is so on MT5

Second, the EA code above works perfectly correctly on MT4 if you change

           " VoidOnTick()"  to "int start()"

            and the three  occurrences of "== INVALID_HANDLE to " < 0 "

just try the code and see the results please as you should see that the file is opened then closed but fails on the re-opening

1° I can't explain you why MT4 and MT5 are different, but they are.

2° Your first command is FileOpen for reading. I don't have this file so I have 5004 error immediatly.

Where is located your file target.dat ?

 
angevoyageur:

1° I can't explain you why MT4 and MT5 are different, but they are.

2° Your first command is FileOpen for reading. I don't have this file so I have 5004 error immediatly.

Where is located your file target.dat ?

1st - thanks anyway

2nd - I just spotted the problem - if you follow the code it first tries to open for reading to test if the file exists and when it finds it does not it generates it.

the code then opens the file to read it.

unfortunately on the second tick it again tests to see if the file exists which now it does --- but now it tries to open it again but its already been opened successfully so gives error 4003

Thanks for your patience