Hi ,
I have amended the file as followed. After each execution , I tried to open the csv excel file (Data101.csv) and it said error or read only, I have to edit the csv file to a new name , says Data102 and rerun it , then I can open Data101.csv. Can anyone advise what is the problem? It seems the csv file is being open by MT4?
//+------------------------------------------------------------------+ //| File.mq4 | //| | //+------------------------------------------------------------------+ #property link "http://www.forexfactory.com/showthread.php?t=29419" #property indicator_chart_window int handle; int init() { handle=FileOpen("Data101.csv",FILE_CSV|FILE_READ|FILE_WRITE,','); FileWrite(handle,"Symbol","Time","Close","Open","High","Low"); return(handle); } int deinit() { FileClose(handle); return(0); } int start() { FileWrite(handle, Symbol(), TimeToStr(Time[0],TIME_DATE|TIME_SECONDS),Close[0], Open[0], High[0], Low[0]); return(0); }
When your file is being opened in OnInit, it will be locked for exclusive access by your indicator. That is no one else can access it, you will not be able to open it with a text viewer, say. This lock will be removed as soon as FileClose in OnDeinit is called. Only thereafter you may look into it. So viewing this file content would require the indicator to unload from the chart, which is probably not what you wanted.
A solution to fix this would be to open and close the file together with the FileWrite, like this:
int start() { int hnd=FileOpen("Data101.csv",FILE_CSV|FILE_READ|FILE_WRITE,','); FileSeek(hnd, 0, SEEK_END); FileWrite(hnd, Symbol(), TimeToStr(Time[0],TIME_DATE|TIME_SECONDS),Close[0], Open[0], High[0], Low[0]); FileClose(hnd); return(0); }
But this is just a quick fix. It has a minor flaw in that the file is opened and closed very frequently, depending on the tick speed. This places quite a burden on your file system. If you experience frequent ticks it would be more economical to StringFormat() into a string buffer and flush this buffer to the file when its full.
Some approach with a string buffer. You should also check the file handle for errors.
string LineBuffer[100]; int LineNr=0; int start() { string line=StringFormat("%s,%s,%s,%s,%s,%s", _Symbol, TimeToStr(Time[0],TIME_DATE|TIME_SECONDS), DoubleToStr(Open[0],_Digits), DoubleToStr(High[0],_Digits), DoubleToStr(Low[0],_Digits), DoubleToStr(Close[0],_Digits)); LineBuffer[LineNr++]=line; if(LineNr==ArraySize(LineBuffer)) FlushLineBuffer(); return(0); } void OnDeinit(const int reason) { if(LineNr>0) FlushLineBuffer(); } void FlushLineBuffer() { int hnd=FileOpen("Data101.csv",FILE_TEXT|FILE_READ|FILE_WRITE); FileSeek(hnd, 0, SEEK_END); for(int i=0; i<LineNr; i++) FileWrite(hnd, LineBuffer[i]); FileClose(hnd); LineNr=0; }
Hi,
Is there a way I can close file when there is no new incoming ticks (when i finished backtest )?
Hi,
The present situation is I have to exit MT4 in order to view the file.
Hi,
The string buffer seems complicated, i try to understand the logic behind. Thanks a lot
The present situation is I have to exit MT4 in order to view the file.
Yes that's what I said. The solution I gave you allows to view the file anytime.
Hi,
I saw the FileFlush in this code, does it serve any purpose?
//+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { FileWrite(handle, TimeToStr( Time[1], TIME_DATE | TIME_SECONDS ), Open[1], High[1], Low[1], Close[1]); FileFlush(handle); MyTime0=Time[0]; } return(0); } //+------------------------------------------------------------------+
- 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,
I have the file function as in coding section, it will work when the following is not inserted. The following code is inserted to only write once every hour. Kindly help why it doesnt work when code to write once every hour is inserted ? Also I would like to ask is it a must to use FileClose(handle) for every file writing? Can I not close it until I finished the last writing?
if (iTime(Symbol(),PERIOD_H1,0)!=PE600412)
{
PE600412=iTime(Symbol(),PERIOD_H1,0);
}