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

 

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);
}
//+------------------------------------------------------------------+
 
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()
 
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.......
 
i know, but i dont care. all i want is 1 bid and ask price every second and that is it.
 
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 ?
 

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!

 
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.
 
Simon Gniadkowski:
If you never exit start() Bid and Ask are never updated . . . unless you RefreshRates()
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!

What is it wrong? (this is MT5). How can I test in backtest?


//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+


int handle;

bool testing=true;

MqlTick tick;


int OnInit()


{

//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,TimeToString( TimeCurrent(), TIME_DATE | TIME_SECONDS ), tick.bid,tick.ask);

 Sleep(1000);

 


return(0);

}

//+------------------------------------------------------------------+