- WebRequest to download ForexFactory News
- Errors, bugs, questions
- help with FileWriteArray and FileReadArray code
What you posted is not a CSV file. It is code that reads a URL. That could be anything. You did not post a downloaded file.
Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
How To Ask Questions The Smart Way. (2004)
Be precise and informative about your problem
What you posted is not a CSV file. It is code that reads a URL. That could be anything. You did not post a downloaded file.
Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
How To Ask Questions The Smart Way. (2004)
Be precise and informative about your problem
It's not? I see the CSV file on my pc though, so it is being downloaded from the link. I thought the fileopen part of the code is writing what it reads to a file.
int filehandle=FileOpen("test.csv",FILE_WRITE|FILE_BIN); //--- Checking errors if(filehandle!=INVALID_HANDLE) { //--- Save the contents of the result[] array to a file FileWriteArray(filehandle,result,0,ArraySize(result)); //--- Close the file FileClose(filehandle); }
-
int filehandle=FileOpen("test.csv",FILE_WRITE|FILE_BIN);
-
It is ANSI not UNICODE, but you didn't specify either.
-
Time,bitfinex,bitso,bitstamp,btcmarkets,coinbase,exmo,gemini,korbit,kraken,others 2022-08-22 18:00:00 UTC,753.411434689989,10.88726352,142.67348607,2.60915712,1476.08998657986,…
You haven't specified the comma separator.
-
The first line is seven (7) fields (headers). You have to read and toss them.
-
The subsequent lines contain a timestamp and six (6) doubles. The doubles are easy (FileReadNumber). The timestamp is not formatted for FileReadDatetime
-
You will have to read it into a string and use StringToTime to convert it to a datetime.
- Second, the time is UTC, you have to adjust it to your brokers timezone, by adding: ( TimeCurrent()-TimeGMT() +900)/1800*1800
-
It is ANSI not UNICODE, but you didn't specify either.
-
You haven't specified the comma separator.
-
The first line is seven (7) fields (headers). You have to read and toss them.
-
The sequent lines contain a timestamp and six (6) doubles. The doubles are easy (FileReadNumber). The timestamp is not formatted for FileReadDatetime
-
You will have to read it into a string and use StringToTime to convert it to a datetime.
- Second, the time is UTC, you have to adjust it to your brokers timezone, by adding: ( TimeCurrent()-TimeGMT() +900)/1800*1800
Thank you so much. I really appreciate the help. The mql5 documentation is so confusing sometimes for a noob like me.
void WebData() { string cookie=NULL, headers; string reqheaders="User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19042\r\n"; char post[],result[]; double r[]; string url; if(Period()>=PERIOD_H1) { url=url60; } else { url=url1; } // url60= https://data.bitcoinity.org/export_data.csv?c=e&data_type=volume&r=hour&t=b×pan=7d // url1=https://data.bitcoinity.org/export_data.csv?c=e&data_type=volume&r=minute&t=b×pan=24h int res; ResetLastError(); int timeout=5000; res=WebRequest("GET",url,reqheaders,timeout,post,result,headers); if(res==-1) { Print("Error in WebRequest. Error code =",GetLastError()); //--- Perhaps the URL is not listed, display a message about the necessity to add the address MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION); } else //{ //--- Load successfully PrintFormat("The file has been successfully downloaded, File size =%d bytes.",ArraySize(result)); //--- Save the data to a file string test=CharArrayToString(result,0,-1,CP_ACP); int filehandle=FileOpen("BTC_volume.csv",FILE_WRITE|FILE_CSV|FILE_ANSI,",",CP_ACP); //--- Checking errors if(filehandle!=INVALID_HANDLE) { //--- Save the contents of the result[] array to a file FileWriteString(filehandle,test); //--- Close the file FileClose(filehandle); } else Print("Error in FileOpen. Error code=",GetLastError()); int h_file=FileOpen("BTC_volume.csv",FILE_READ|FILE_CSV|FILE_ANSI,",",CP_ACP); if(h_file!=INVALID_HANDLE) { int v=0; double value[]; datetime time[]; double val=0; string minute_bar_time; for(int k=0; k<11; k++) //ignore the headers { FileReadString(h_file); } while(!FileIsEnding(h_file) && !_StopFlag) { minute_bar_time = FileReadString(h_file); ArrayResize(time,v+1); time[v]=StringToTime(minute_bar_time)+(TimeCurrent()-TimeGMT()+900)/1800*1800; //format and save date to buffer while(!FileIsLineEnding(h_file)) { double num = FileReadNumber(h_file); if(num>0) val += num; //total volumes from all exchanges each line (each bar) } ArrayResize(value,v+1); value[v]=val; v++; val=0; } ArrayResize(btcvl,ArraySize(value)-1); //don't include "current" bar with incomplete data, hence arraysize-1 for(int i=0; i<ArraySize(btcvl); i++) //copy buffers to structure. { btcvl[i].vol_tot=value[i]; btcvl[i].voltime=time[i]; } //Print("last value ",value[ArraySize(value)-1]); FileClose(h_file); }
Since I am not certain of the amount of data being downloaded, I have kept the arrays dynamic. However, I wonder if I am using arrayresize too often and if there is a more economical way to do it.
It is working beautifully though so thank you again for the help.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use