If handle >=0 (succesful ) then
fileWrite(handle, "DATE"......
else
fileclose(handle)
??
Si handle is succesful, then we write in file, but if handle is not correct, we close the file
It is correct?
Thank yoy very much for your patience
If it is successful the function returns the file handle, and if it is unsuccessfull returns invalid handle:
void write_data() { int handle; string PAIR = "EURUSD"; string _MN1; string filename = PAIR + "_D1"+"_"+EURUSD + ".txt"; // handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';'); if(handle != INVALID_HANDLE) { FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // int i; for (i=EURUSD-1; i>=0; i--) { double O,H,L,C, T, V; O = iOpen (PAIR, PERIOD_D1, i); H = iHigh (PAIR, PERIOD_D1, i); L = iLow (PAIR, PERIOD_D1, i); C = iClose(PAIR, PERIOD_D1, i); T = iTime (PAIR, PERIOD_D1, i); V = iVolume (PAIR, PERIOD_D1, i); FileWrite(handle, TimeToStr(T, TIME_DATE), TimeToStr(T, TIME_SECONDS), H, L, C, O, V); } FileClose(handle); } return(0); }
Is it correct?
Thank you very much
If it is successful the function returns the file handle, and if it is unsuccessfull returns invalid handle:
Is it correct?
- Now you've change the code again, you never answered the question with the original code.What does FileOpen return when successful and unsuccessful? what do you do?
You still don't understand what was wrong with the original code.
- If it returns an invalid handle, how can you close it (after the if?)
- Check your return codes (FileWrite and FileClose) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Hi,
I'm trying to get export data, but building the code correctly.
If FileOpen is succesfull, then FileWrite.
If FileOpen is unsuccesfull, then "error"
I read about functions "return" but still do not understand.
In theory, "return" to "avoid" can be omitted.
The following code is on which I am working, although it is not correct because it does not write correctly the values in the file:
#property copyright "Copyright © 2007, dkoloskov" #property link "dkoloskov@rambler.ru" #property indicator_chart_window #property indicator_buffers 1 #property indicator_width1 2 #property indicator_color1 Tomato double ExtMap[]; ; extern int EURUSD = 200; int init() { SetIndexStyle(0, DRAW_LINE); SetIndexBuffer(0, ExtMap); return(0); } int start() { static int rea = 0; static int old_bars = 0; static int rea1 = 0; if (old_bars != iBars("EURUSD",PERIOD_D1)) { delete_result(); clear_graf(); write_data(); // } int handle_read = FileOpen("EURUSD"+"_D1"+"_result.csv",FILE_CSV|FILE_READ,';'); if(handle_read >= 0) { Comment("EURUSD"+"_D1" ); read_n_draw(handle_read); delete_result(); FileClose(handle_read); rea=0; } else { rea++; Comment("descargar: "+"EURUSD_D1"+"#"+GetLastError()+"#rea"+rea); //FileClose(handle_read); } old_bars = iBars("EURUSD",PERIOD_D1); // return(0); } ////////////////////////////////// //+------------------------------------------------------------------+ void read_n_draw(int handle_read) { int i=0; while ( !FileIsEnding(handle_read) ) { ExtMap[i] = FileReadNumber(handle_read); i++; Comment(ExtMap[0]); } ExtMap[i-1] = EMPTY_VALUE; } ///////////////////////////////////////////////////// void clear_graf() { ArrayInitialize( ExtMap, EMPTY_VALUE); } ///////////////////////////////////////////////////////7 void delete_result() { string filename = "EURUSD"+ "_D1" + "_result.csv"; FileDelete(filename); } ///////////////////////////////////////////////////////////////////// void write_data() { int handle; string PAIR = "EURUSD"; string filename = PAIR + "_D1" +"_"+EURUSD + ".txt"; // handle = FileOpen(filename,FILE_CSV|FILE_WRITE,';'); if(handle != INVALID_HANDLE) { FileWrite(handle, "DATE","TIME","HIGH","LOW","CLOSE","OPEN","VOLUME"); // int i; for (i=EURUSD-1; i>=0; i--) { double O,H,L,C, T, V; O = iOpen (PAIR, PERIOD_D1, i); H = iHigh (PAIR, PERIOD_D1, i); L = iLow (PAIR, PERIOD_D1, i); C = iClose(PAIR, PERIOD_D1, i); T = iTime (PAIR, PERIOD_D1, i); V = iVolume (PAIR, PERIOD_D1, i); FileWrite(handle, TimeToStr(T, TIME_DATE), TimeToStr(T, TIME_SECONDS), H, L, C, O, V); FileClose(handle); } } else { Print(" error", GetLastError()); } }
Thank you very much
Any comment?
Thank you very much
castann86: I'm trying to get export data, but building the code correctly. The following code is on which I am working, although it is not correct because it does not write correctly the values in the file: If FileOpen is succesfull, then FileWrite. Any comment? |
|
I think you are not reading it right or I'm not expressing clearly enough.
When I say that the successful return = writing on file. I think it is clear enough what I want.
I'm learning to encode, while I'm learning many other things, so you can not question what I'm willing to learn or not, just because you know a little more than others does not mean you're better than others in other ways.
I do not need your help, much less. Whenever there is a solution, I'll find her. To learn how to "codify" must see code. That's what I intend.
thank you very much

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
I have doubts whether this code is correct.
The code is to export data in real time. I had still not successful because I think that the code is not correct. But still I could not fix.
Can anyone explain why the code is not correct?
He had a few days ago said the following:
" You try open the file. If it is successful, you return, loosing the handle, keeping the file open, and nothing works after that."
But I'm still learning and I could not do the correct code.
Thank you very much in advance!