Mql5自定义品种 中国金融产品: A股票、期货、债券、数字货币的应用(2) 新评论 Mars Yuan 2020.01.07 15:15 4、读取A股个股数据结构及实例 ---- //+------------------------------------------------------------------+ //| Astock_Stock.mq5 | //| Copyright 2019, MarsYuan 苑兵 | //| Mars.yuan@gmail.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2019, MarsYuan" #property link "https://www.mql5.com/zh/users/marsyuan" #property version "1.00" //+------------------------------------------------------------------+ input string Token="e5282e6eee67e52c4b0b420c96d5ac6dcb7cf291b06c080547649086"; //input string Adj="qfq"; //---复权类型(只针对股票):None未复权 qfq前复权 hfq后复权 ---- input string Adj="None"; //---除权 input string Freq="D"; input string StockName="300015.SZ"; //注意SZ、SH要大写 input datetime TBegin = D'20000101 00:00:00'; // 数据开始时间 //input datetime TEnd = D'20191219 00:00:00'; // 数据结束时间 datetime TEnd=TimeLocal(); //获取当前时间 //+------------------------------------------------------------------+ void OnStart() { string list[]; ReadList(Token,list); int n=ArraySize(list); MessageBox("Astock:"+n); ReadStock(Token,StockName,TBegin,TEnd); //-----如果调用个股打开此句,如果是所有股票就关闭//这句 for(int i=0;i<n;i++) { //ReadStock(Token,list[i],TBegin,TEnd); //-----如果调用个股关闭此句,如果是所有股票就打开//这句 } //MessageBox(StockName+","+TBegin+"~"+TEnd+" Data Finish!"); } //----------------------------------------------------------------------------------------------------------------- //-------------------Http协议Post方式读取股票名称列表并存入数组list------Http post download stock list------------- void ReadList(const string &token,string &list[]) { //---------Http post Josn string to char Arr-------------------------------------------------------------- char message_data[], result[]; string RequestMethod ="POST"; string END_POINT_URL ="http://api.waditu.com"; string headers = "content-type:application/json"+"\r\n"; headers += ",charset=UTF-8" + "\r\n"; int timeout = 10000; string result_headers = NULL; string message_text ; //--------------------将字符整合成Post接受的Json标准的char 字符串--------- StringConcatenate(message_text, "{", "\"api_name\"", ":", "\"stock_basic\"", ",", "\"token\"", ":", "\"", token, "\"", ",", "\"params\"", ":", "{", "\"list_stauts\"", ":", "\"L\"", "}", ",", "\"fields\"", ":", "\"ts_code,name,area,industry,list_date\"", "}" ); StringToCharArray(message_text,message_data,0, StringLen(message_text)); //string _jt=CharArrayToString(message_data); //MessageBox(_jt); //-------------------------------------------------------------------------------------------------------------- //------------------------------------------Http Post 请求服务器下载数据---------------------------------------- ResetLastError(); int res =WebRequest(RequestMethod, END_POINT_URL, headers, timeout, message_data, result, 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 '"+END_POINT_URL+"' to the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION); } else { if (res == 200) { //------------------------拆分result[] char数组串将导入list数组---------- //---------------------将Tushare返回的char数组合并成字符串处理过程-------------------------------------- string str=CharArrayToString(result,0,-1,0); string str2=StringSubstr(str,172); //Alert(str2); //int rc=StringReplace(str2,"\"",""); int rc=StringReplace(str2,"[",""); rc+=StringReplace(str2,"]","\r\n"); //----------------------将处理好的字符串分装到周期字符串数组------------------------------------------- string sep="\r\n"; // 分隔符为字符 ushort u_sep; // 分隔符字符代码 //--- 获得分隔符代码 u_sep=StringGetCharacter(sep,0); //--- 字符串分为子字符串 int k=StringSplit(str2,u_sep,list); //---用分隔符切分周期字符串并装入数组 list[0]=","+list[0]; //----------------进一步处理周期字符串数组提取股票代码装入list数组中------------------------------------- k=k-2; for(int i=0;i<k;i++) { string list_Arr[]; sep=","; u_sep=StringGetCharacter(sep,0); int m=StringSplit(list[i],u_sep,list_Arr); //提取股票代码装入list数组中 list[i]=list_Arr[1]; rc+=StringReplace(list[i],"\"",""); StringTrimLeft(list[i]); //MessageBox(list[i]); } //------------------------------------------------------------------------------------------------------- } else PrintFormat("Error in download '%s', code %d", END_POINT_URL, res); } } //----------------------------------------------------------------------------------------------------------------- //------------------------读取个股数据----download stock data------------------------------------- void ReadStock(const string &token, const string &stockName,const datetime &tBegin,const datetime &tEnd) { char message_data[], result[]; string RequestMethod ="POST"; string END_POINT_URL ="http://api.waditu.com"; string headers = "content-type:application/json"+"\r\n"; headers += ",charset=UTF-8" + "\r\n"; int timeout = 10000; string result_headers = NULL; string message_text ; string stringresult = ""; string Ts,Te; Ts=TimeToString(tBegin,3); Te=TimeToString(tEnd,3); StringReplace(Ts,".",""); StringReplace(Te,".",""); //--------------------将字符整合成Post接受的Json标准的char 字符串--------- StringConcatenate(message_text, "{", "\"api_name\"", ":", "\"daily\"", ",", "\"token\"", ":", "\"", token, "\"", ",", "\"params\"", ":", "{", "\"ts_code\"", ":", "\"", stockName, "\"", ",", "\"asset\"", //-----------资产类型---------- ":", "\"E\"", ",", "\"adj\"", //--------复权--------- ":", "\"", Adj, "\"", ",", "\"freq\"", //-----------周期------------ ":", "\"", Freq, "\"", ",", "\"start_date\"", ":", "\"", Ts, "\"", ",", "\"end_date\"", ":", "\"", Te, "\"", "}", ",", "\"fields\"", ":", "\"\"", "}" ); StringToCharArray(message_text,message_data,0, StringLen(message_text)); string _jt=CharArrayToString(message_data); //Alert(_jt); //MessageBox(_jt);//--------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------- //------------------------------------------Http Post 请求服务器下载数据---------------------------------------- ResetLastError(); int res =WebRequest(RequestMethod, END_POINT_URL, headers, timeout, message_data, result, 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 '"+END_POINT_URL+"' to the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION); } else { if (res == 200) { //--- Succesfully downloaded //PrintFormat("Succesfully downloaded, size %d.", ArraySize(result)); //MessageBox(ArraySize(result),"ok",MB_ICONINFORMATION); //--- Save result[] to file //int filehandle = FileOpen("debug_output6.csv", FILE_WRITE | FILE_BIN); //if (filehandle != INVALID_HANDLE) //{ //FileWriteArray(filehandle, result, 0, ArraySize(result)); //FileClose(filehandle); MqlRates Rates[]; Tushare_CharArrayToStruct(result,Rates);//---将HTTP POST返回char数组拆分到MQL自定义品种结构中-- CustomSymbolCreate(stockName, "\\Astock");//----自定义Astock品类目录---- int reg=CustomRatesReplace(stockName,tBegin,tEnd,Rates,WHOLE_ARRAY );//----将MqlRates数组数据建立并导入自定义品类目录下的产品中 MessageBox(stockName+" "+"Downloaded"+" "+Freq+" "+"OK!" ); //} //else //{ // Print("Error in FileOpen. Error code =", GetLastError()); //} } else PrintFormat("Error in download '%s', code %d", END_POINT_URL, res); } } //----------------------------------------------------------------------------------------------------------------- //----------------------------------将HTTP POST返回char数组拆分到MQL自定义品种结构中--------------------------------------------- void Tushare_CharArrayToStruct(const char &result[],MqlRates &Rates[]) { //---------------------将Tushare返回的char数组合并成字符串处理过程-------------------------------------- string str=CharArrayToString(result,0,-1,0); //Alert(str); string str2=StringSubstr(str,228); int rc=StringReplace(str2,"\"",""); rc+=StringReplace(str2,"[",""); rc+=StringReplace(str2,"]","\r\n"); //Alert(str2); //----------------------将处理好的字符串分装到周期字符串数组-------------------------------------------------- string sep="\r\n"; // 分隔符为字符 ushort u_sep; // 分隔符字符代码 string trade_Arr[]; // 获得字符串数组 //--- 获得分隔符代码 u_sep=StringGetCharacter(sep,0); //--- 字符串分为子字符串 int k=StringSplit(str2,u_sep,trade_Arr); //---用分隔符切分周期字符串并装入数组 trade_Arr[0]=","+trade_Arr[0]; //----------------进一步处理周期字符串数组装入MqlRates结构数组中-------------------------------------------------------------------------------- //MessageBox(trade_Arr[2]); k=k-2; ArrayResize(Rates,k); for(int i=0;i<k;i++) { string su_Arr[]; sep=","; u_sep=StringGetCharacter(sep,0); int m=StringSplit(trade_Arr[i],u_sep,su_Arr); //MessageBox(trade_Arr[i]); //按照MQL5历史数据格式重新整合数组格式 //trade_Arr[i]=su_Arr[2]+" "+"09:30:00"+" "+su_Arr[3]+" "+su_Arr[4]+" "+su_Arr[5]+" "+su_Arr[6]+" "+"0"+" "+su_Arr[10]+" "+"0"+"\r\n"; datetime stm=D'1980.07.19 12:30:27'; stm=StringToTime(su_Arr[2]+" "+"09:30:01"); //ArraySetAsSeries(Rates,false); Rates[i].time=stm; //Rates[i].time=StringToTime("09:30:00"); Rates[i].open=StringToDouble(su_Arr[3]); Rates[i].high=StringToDouble(su_Arr[4]); Rates[i].low=StringToDouble(su_Arr[5]); Rates[i].close=StringToDouble(su_Arr[6]); Rates[i].tick_volume=StringToDouble(su_Arr[10]); Rates[i].real_volume=StringToDouble(su_Arr[11]); //MessageBox(Rates[i].time+","+Rates[i].close+","+Rates[i].real_volume); } //MessageBox(TimeToString( Rates[0].open)); //-------------------------------------------------------------------------------------------------------------------------------------------------------- } //----------------------------------------------------------------------------------------------------------------------------------------------------------- 图例:注意2019年6月4日与6月5日的没有复权即除权数据K线 发生不连续的断裂 新评论 您错过了交易机会: 免费交易应用程序 8,000+信号可供复制 探索金融市场的经济新闻 注册 登录 拉丁字符(不带空格) 密码将被发送至该邮箱 发生错误 使用 Google 登录 您同意网站政策和使用条款 如果您没有帐号,请注册 可以使用cookies登录MQL5.com网站。 请在您的浏览器中启用必要的设置,否则您将无法登录。 忘记您的登录名/密码? 使用 Google 登录
4、读取A股个股数据结构及实例
图例:注意2019年6月4日与6月5日的没有复权即除权数据K线 发生不连续的断裂