Simple program write to file

 

Hi all,


I am new to metatrader programming and I would really appreciate if you could give me your opinion. I am trying to write a program that captures the tick by tick prices and writes them to a file. I am using the expert configuration and a file checking process (which is located in the init()) however I think that the variable "handle" is not imported properly in the start() function (if this can happen). i am also getting an error during the compilation that says "init function defined-start function's parameters will be ignored as expert properties". Any ideas?


The code that I am using is listed below.


Thanks for the help,


Modraig


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+

bool handle;

int init()
{
//Open file/////////////////////////////////////////////////////////////////////////////////////////////////////////
handle=FileOpen("data1.csv",FILE_CSV|FILE_READ|FILE_WRITE,',');
if(handle<1)
{
Comment("File data1.csv not found, the last error is ", GetLastError());
return(false);
}
else
{
Comment("Ok");
FileWrite(handle, "Time", "Open", "High", "Low", "Close", "LowBB", "HighBB", "MA", "LowBO", "HighBO",
"DouBO", "DouLowBO", "DouHighBO", "LowHitOR", "HighHitOR", "LowHit", "HighHit");
Comment("1");
}

return(handle);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit(bool handle)
{
//Close file////////////////////////////////////////////////////////////////////////////////////////////////////
FileClose(handle);
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
bool start(bool handle)
{
int i;
//Write to file//////////////////////////////////////////////////////////////////////////////////////////////
for (i=0; i<Bars; i++)
FileWrite(handle, TimeToStr(Time[i]), Open[i], High[i], Low[i], Close[i]);
return(handle);
}
//+------------------------------------------------------------------+

 

If you want to save ticks, try TickCollector expert in CodeBase.

 
modraig:

Hi all,


I am new to metatrader programming and I would really appreciate if you could give me your opinion. I am trying to write a program that captures the tick by tick prices and writes them to a file. I am using the expert configuration and a file checking process (which is located in the init()) however I think that the variable "handle" is not imported properly in the start() function (if this can happen). i am also getting an error during the compilation that says "init function defined-start function's parameters will be ignored as expert properties". Any ideas?


The code that I am using is listed below.

Thanks for the help,


Modraig


File handle is not boolean, here is your code that saves Time, Bid, Ask.


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+

int handle;

int init()
{
//Open file/////////////////////////////////////////////////////////////////////////////////////////////////////////
handle=FileOpen("data1.csv",FILE_CSV|FILE_READ|FILE_WRITE,',');
if(handle<1)
{
Comment("File data1.csv not found, the last error is ", GetLastError());
return(false);
}
else
{
Comment("Ok");
FileWrite(handle, "Time","Bid","Ask");
Comment("1");
}

return(handle);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit(bool handle)
{
//Close file////////////////////////////////////////////////////////////////////////////////////////////////////
FileClose(handle);
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int i;
//Write to file//////////////////////////////////////////////////////////////////////////////////////////////
FileWrite(handle, TimeToStr( TimeCurrent(), TIME_DATE | TIME_SECONDS ), Bid, Ask);
return(0);
}
//+------------------------------------------------------------------+
Enable experts, and attach it to chart. after the expert deataching your file data1.csv will have contents something like this:

Time,Bid,Ask
2010.01.05 16:56:36,1.44106,1.44123
2010.01.05 16:56:37,1.44104,1.4412
2010.01.05 16:56:38,1.44102,1.44116
.....

 

thank you Quantum for your prompt response. I have noticed that it is not necessary to pass on the value of handle through init() to start(). is this because of the way that metatrader is built? i know that it is important to do that in some languages.


Also, when I run the previous code using the loop with the bars, it included not only the forward looking prices but also the old ones i.e. started from the current one and counted backwards. any ideas what was the problem in my code? I would like to try what you have written also by bars.


Thanks again

 
modraig:

Also, when I run the previous code using the loop with the bars, it included not only the forward looking prices but also the old ones i.e. started from the current one and counted backwards. any ideas what was the problem in my code? I would like to try what you have written also by bars.

Please note that the function start() executes at every new tick.

Here are some references, which can help you to learn more:

Program Execution
init(),start(),deinit()

void FileFlush(int handle) - Flushes all data stored in the file buffer to the disk.

 

Quantum, thanks again for your reply. i have read the info at the links you have sent me and also some other documents, however it is not clear to me how to manipulate the incoming data not by tick but by bars in one of the following fixed intervals (1min, 5 min, 10 min......). Any ideas?

 
modraig:

Quantum, thanks again for your reply. i have read the info at the links you have sent me and also some other documents, however it is not clear to me how to manipulate the incoming data not by tick but by bars in one of the following fixed intervals (1min, 5 min, 10 min......). Any ideas?

As I understand, you want to save the bars in some file in a realtime by some expert advisor.

So each time when we have new tick, we have to save the bar completed (for example, M1) to file.

How to understand the moment when the bar just has been completed and it ready to be saved to file?

One of the simple ways is to use the time change of the current bar: Time[0].

you can use the gloabal variable for it, (like the handle you have used above). but it should be a datetime type, and you must set it first as the as bar time during initialization.


Let's learn how to do it using your code (please compile it and attach to M1 EURUSD):

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+

int handle;
datetime MyTime0;

int init()
{
MyTime0=Time[0];
Alert(TimeToStr( TimeCurrent(), TIME_DATE | TIME_SECONDS ));
//Open file/////////////////////////////////////////////////////////////////////////////////////////////////////////
handle=FileOpen("data1.csv",FILE_CSV|FILE_READ|FILE_WRITE,',');
if(handle<1)
{
Comment("File data1.csv not found, the last error is ", GetLastError());
return(false);
}
else
{
Comment("Ok");
FileWrite(handle, "Time","Open","High","Low","Close");
Comment("1");
}

return(handle);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit(bool handle)
{
//Close file////////////////////////////////////////////////////////////////////////////////////////////////////
FileClose(handle);
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int i;
//Write to file//////////////////////////////////////////////////////////////////////////////////////////////

if (Time[0]>MyTime0)
{Alert("New bar time ",TimeToStr( Time[0], TIME_DATE | TIME_SECONDS ),
 " Completed bar time: ",TimeToStr( Time[1], TIME_DATE | TIME_SECONDS ),
" Saved time: ",TimeToStr( MyTime0, TIME_DATE | TIME_SECONDS ));
 FileWrite(handle, TimeToStr( Time[1], TIME_DATE | TIME_SECONDS ), Open[1], High[1], Low[1], Close[1]);
 FileFlush(handle);
 MyTime0=Time[0];
}
//FileWrite(handle, TimeToStr( TimeCurrent(), TIME_DATE | TIME_SECONDS ), Bid, Ask);
return(0);
}
//+------------------------------------------------------------------+

The file output is:

Time,Open,High,Low,Close
2010.01.07 09:13:00,1.43894,1.43909,1.4388,1.43892
2010.01.07 09:14:00,1.43891,1.43908,1.43889,1.43908
2010.01.07 09:15:00,1.43907,1.43922,1.43892,1.43912
2010.01.07 09:16:00,1.43915,1.43915,1.43849,1.43852

And my alerts are:


As we see, we have learned how to save completed M1 bars in a realtime (but the advisor should be attached to M1 chart).

The same with the other timeframes - just attach it to M5, for example.

 

Hi Quantum,


Very useful expert. A question I want to ask:

As I tested this expert, it rewrites existing file line by line, . How it is possible to add new data to existing file ? I have script which creates file from database, so I want to update these files in real time.


Regards,

Edward

 

Quantum, many thanks again. I have tried and works really well.


I wanted to ask you if this is the most safe and secure way to write this code. i am trying to create some code that will do some calculations at the beginning of the new bar, by comparing them with the values of the previous bar. With your code you reset the time at the end of each period (MyTime0=Time[0];) however I have seen many EA that use structures which include indicatorcounted() and Bars to create the what you did above. Just wondering what is the best structure to use as I would also like to call in my code the bollinger bands. Is there any frame on the website that fit the description of what i am trying to do? the write to file functions, would be very useful for the testing of the code.


thanks

 
edas:

A question I want to ask:

As I tested this expert, it rewrites existing file line by line, . How it is possible to add new data to existing file ?

Replace:

handle=FileOpen(Filename, FILE_CSV|FILE_WRITE, ',');


with:

handle=FileOpen(Filename, FILE_CSV|FILE_READ|FILE_WRITE, ',');


CB

 
cloudbreaker:

Replace:

handle=FileOpen(Filename, FILE_CSV|FILE_WRITE, ',');


with:

handle=FileOpen(Filename, FILE_CSV|FILE_READ|FILE_WRITE, ',');


CB

Here is line from expert:

handle=FileOpen("data1.csv",FILE_CSV|FILE_READ|FILE_WRITE,',');

But expert writes line by line from beginning of existing file. Perhaps it is necessary to tell expert to write to end of file with cycle statement, but I don't understand how write such cycle.

Edward

Reason: