Retreiving data from a file

 

Hello, I'v sent data to a file through the following code,

int deinit()
{
//----
int handle;
string name = AccountNumber()+Symbol();
handle=FileOpen(name, FILE_CSV|FILE_WRITE, ';');
if(handle>0)
{
FileWrite(handle, l, s, InitialShort);
FileClose(handle);
}
//----
return(0);
}

now I have to read that data when init() but I don´t know how to reasign the values to the variables. Note that the values stored are 3, all of them double variables in global scope. The file is created ok and of course I do need to reasign those values to the same variables when reopening the EA.

I think it has to do with fileopen and fileread but I can´t make it up from the available documentation.

Any help welcome, thanks

 
 
int deinit()
{
//----
    int handle;    
    string name = AccountNumber()+Symbol();
    handle = FileOpen(name, FILE_CSV|FILE_WRITE, ';');
    if(handle>0)
    {
        FileWrite(handle, l, s, InitialShort);
        FileClose(handle);
    }
    //----
    return(0);
}
 
int init()
{
    //...whatever work needs doing - like "should I be opening this file?"...
    //-
    int    handle = FileOpen(name, FILE_CSV|FILE_READ, ';');
    
    //read previously saved double values from CSV file into EA global variables
    //-
    l = FileReadNumber(handle);
    s = FileReadNumber(handle);
    InitialShort = FileReadNumber(handle);
    FileClose(handle);
    
    //...whatever work doing prior function return
    //-
    
    return(0);
}//init()

of course - the reading of 3 doubles in init() assumes that they are valid, know what I mean?

personally, I'd do some checks to ensure data integrity - is your deposit after all...!

eg, since assume you saving prices, then you could easily enough range check each one

1. like is it <=0 ? OR is it so far away from expected data range (say, for the symbol pair they came from (eg, Symbol() )

Some sort of checks no biggie, yes?

That way you at least have a fighting chance that what read from file is most likely what you wrote in first place!

(it does happen now 'n then that what read is noway near what wrote ;)


hth


please look at mql4 book - is the only way to fly!

for instance: File Operations...

 

Thanks again ukt, I'll place a check and I'll add the EA name to the file name to be more sure.

I looked at the fileread but I didn't find how to place the values to different variables.

Thanks again and have a nice day.

 

good.

yes... I not even pickup on name, agreed - is good to tie down filename to the creator (eg, WindowExpertName() )

also, maybe consider throwing into filename the EA's unique magic# (ummmm, you of course are gonna use... :)

why? IF you run >1 EA on ClientTerminal AND just happens that 2 or more run in identical chart environments AND they also happen to do file I/O to same directory THEN chaos reigns ;)

I mean: A/C#+symbol [and even EA name] will not potentially make for uniqueness because whatIf M30 and H1 charts for symbol xyz have same EA (2 instances of...) ?

So... assuming one always strict about allocating different magic# for every simultaneous run of a same EA then - the uniqueness factor should be catered for.

Each EA instantiation uniqueness concept is imho VIP to practice at all times... call it a safety valve or something...

excuse the ramble...

but - thoughts appear so gotta push 'da keys :o))

Reason: