Problem with File Funtion

 
//+------------------------------------------------------------------+
//|                                                   Invincible.mq4 |
//|                                                            N.Huy |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "N.Huy"
#property link      ""


extern string MaximumEquityFileName = "Maximum Equity.txt";


extern int LossLevel = 20;
extern int MaximumLossLevel = 30;
extern int PriceDiffLevel = 300;

double FinalEquity;
double MaximumEquity;
double LossEquity_1;
double LossEquity_2;


int FileHandle_1;
int FileHandle_2;
int FileHandle_3;

int init ()
        {               
                MaximumEquity = AccountEquity();
                
                int FileHandle_1 = FileOpen(MaximumEquityFileName, FILE_WRITE | FILE_CSV, '\t' );
                FileWriteDouble(FileHandle_1,MaximumEquity,DOUBLE_VALUE);
                FileClose(FileHandle_1);
                
                FinalEquity = AccountEquity() + (AccountEquity() * ProfitLevel / 100);
        }
        
int start ()
        {
                if (AccountEquity() < FinalEquity) 
                        {       
                           FileHandle_2 = FileOpen(MaximumEquityFileName, FILE_READ | FILE_CSV, '\t' );
                                MaximumEquity = FileReadDouble(FileHandle_2,DOUBLE_VALUE);
                                                                 
                                if(AccountEquity()> MaximumEquity)
                                        {  
                                                FileDelete(MaximumEquityFileName);                                          
                                                FileHandle_3 = FileOpen(MaximumEquityFileName, FILE_WRITE | FILE_CSV, '\t' );
                                                MaximumEquity = AccountEquity();
                                                FileWriteDouble(FileHandle_3,MaximumEquity,DOUBLE_VALUE);
                                                FileClose(FileHandle_3);
                                        }       
                                        
                                if(AccountEquity() <= MaximumEquity)
                                        {
                                                LossEquity_1 = MaximumEquity - (MaximumEquity * LossLevel/100);
                                                LossEquity_2 = MaximumEquity - (MaximumEquity * MaximumLossLevel/100);
                                        }
                                
                                if (AccountEquity() < LossEquity_2)
                                        {
                                                CloseAll();
                                                init ();
                                        }
                                

        }
}  
I can compile my ea but it doesn't work. I don't know why it don't work. Help me !!!
 
HuyTitan:
I can compile my ea but it doesn't work. I don't know why it don't work. Help me !!!


Hi !

I can not compile the file as the variable "ProfitLevel" is not defined.

And what exactly does not work in your EA ?

Regards, Klaus

 

Try using FILE_BIN instead of FILE_CSV

int FileHandle_1 = FileOpen(MaximumEquityFileName, FILE_BIN|FILE_WRITE);

int FileWriteDouble( int handle, double value, int size=DOUBLE_VALUE)
The function writes a double value with floating point to a binary file. If the format is specified as FLOAT_VALUE, the value will be written as a 4-bytes floating point number (of the float type), otherwise, it will be written in the 8-bytes floating point format (of the double type).
Returns the actually written bytes count or a negative value if an error occurs.
To get the detailed error information, one has to call the GetLastError() function.

 
kennyhubbard:

Try using FILE_BIN instead of FILE_CSV

int FileWriteDouble( int handle, double value, int size=DOUBLE_VALUE)
The function writes a double value with floating point to a binary file. If the format is specified as FLOAT_VALUE, the value will be written as a 4-bytes floating point number (of the float type), otherwise, it will be written in the 8-bytes floating point format (of the double type).
Returns the actually written bytes count or a negative value if an error occurs.
To get the detailed error information, one has to call the GetLastError() function.


or u can use FileWrite() & FileReadString() & StrToDouble()
 

kennyhubbard is of course absolutely right, FileWriteDouble only works with binary files.

FileWrite is the only way to write in a CSV file.

Check the documentation for details.

 
knatterton:


Hi !

I can not compile the file as the variable "ProfitLevel" is not defined.

And what exactly does not work in your EA ?

Regards, Klaus


My EA create a file but it doesn't write account equity into this file.
 

Use "FILE_BIN" in your "FileOpen" command !
NOT "FILE_CSV" - this is only for creation of text files, not useable to write double values in your file !

 
Thank! But I have one more question:"How can I read and edit this bin file?"
 

Bin-files are not thought to be read.

You can use an HEX editor to get an idea of whats inside (values are stored binary, you will find strings in there), but if you want to get a text file which you can open eg. in Excel or with an editor, you must write a CSV file.
But then you can not use FileWriteDouble, only FileWrite is abvailable.

For details check the MQL website, like here: https://book.mql4.com/functions/files

 
Ok! Thank
Reason: