Download MetaTrader 5

how to save data of bid and ask price to a csv file per second, not every tick. ( code inside )

To add comments, please log in or register
Do you want to become a well-known developer? Execute the orders of traders in the Freelance service!
ricov3ry
18
ricov3ry 2013.12.17 22:25 

what i like to achieve is that instead of every tick the bid and ask price will be saved to a csv file, i like to have the bid and ask price saved in a csv file for every second.

i tried the sleepcommand for and add 1000 milliseconds but that does not work. could anybody help me fix the code? ( code is below )

thanks in advance

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

int handle;
bool testing=true;
int init()
{
//Open file/////////////////////////////////////////////////////////////////////////////////////////////////////////
handle=FileOpen("test.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
if(handle<1)
{
Comment("File test.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=0;
//Write to file//////////////////////////////////////////////////////////////////////////////////////////////
while(testing){
 
 FileWrite(handle, TimeToStr( TimeCurrent(), TIME_DATE | TIME_SECONDS ), Bid, Ask);
 Sleep(1000);
} 
 

return(0);
}
//+------------------------------------------------------------------+
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.17 22:39  
ricov3ry:

what i like to achieve is that instead of every tick the bid and ask price will be saved to a csv file, i like to have the bid and ask price saved in a csv file for every second.

i tried the sleepcommand for and add 1000 milliseconds but that does not work. could anybody help me fix the code? ( code is below )

thanks in advance

If you never exit start() Bid and Ask are never updated . . . unless you RefreshRates()
Tjipke de Vries
6316
Tjipke de Vries 2013.12.17 22:48  
ricov3ry:

what i like to achieve is that instead of every tick the bid and ask price will be saved to a csv file, i like to have the bid and ask price saved in a csv file for every second.

i tried the sleepcommand for and add 1000 milliseconds but that does not work. could anybody help me fix the code? ( code is below )

thanks in advance


there is not every second a new tick.......
ricov3ry
18
ricov3ry 2013.12.18 08:32  
i know, but i dont care. all i want is 1 bid and ask price every second and that is it.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.18 10:58  
ricov3ry:
i know, but i dont care. all i want is 1 bid and ask price every second and that is it.
So . . did you add what I suggested ?
ricov3ry
18
ricov3ry 2013.12.18 13:12  

sorry raptorUK i didnt read your part.

i just tried it and seems to work.

however when i add it on the GBPUSD for example i get some mismatches.

so lets say price is 1.63450 i get then like 1.6345 and 0 is missing. but i can easily fix this in excel myself.

again thanks for the help! and for fast reply!

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.18 13:17  
ricov3ry:

sorry raptorUK i didnt read your part.

i just tried it and seems to work.

however when i add it on the GBPUSD for example i get some mismatches.

so lets say price is 1.63450 i get then like 1.6345 and 0 is missing. but i can easily fix this in excel myself.

again thanks for the help! and for fast reply!


1.63450 is the same as 1.6345 is the same as 1.634500000 is the same as 1.6345000000000000000000000000000000000000000 just set the number of digits for the cell in excel.
To add comments, please log in or register