/// <summary> /// 创建用户状态服务 /// </summary> public UserCookieService() { String dataDir = DataCenter.getUserPath(); if (!FCFile.isDirectoryExist(dataDir)) { FCFile.createDirectory(dataDir); } String dataBasePath = DataCenter.getUserPath() + "\\usercookies.db"; m_connectStr = "Data Source = " + dataBasePath; if (!FCFile.isFileExist(dataBasePath)) { createTable(); } }
/// <summary> /// 获取分时数据 /// </summary> public static void getMinuteDatas() { if (m_minuteDatas.Count > 0) { return; } String appPath = DataCenter.getAppPath(); foreach (String code in m_codedMap.Keys) { String fileName = m_newFileDir + FCStrEx.convertDBCodeToFileName(code); if (!FCFile.isFileExist(fileName)) { fileName = m_newFileDir + FCStrEx.convertDBCodeToSinaCode(code).ToUpper() + ".txt"; } if (FCFile.isFileExist(fileName)) { String text = ""; FCFile.read(fileName, ref text); List <SecurityData> datas = new List <SecurityData>(); StockService.getHistoryDatasByMinuteStr(text, datas); if (datas.Count > 0) { int rindex = 0; int dataSize = datas.Count; while (rindex < dataSize) { SecurityData d = datas[rindex]; if (rindex == 0) { d.m_avgPrice = d.m_close; } else { SecurityData ld = datas[rindex - 1]; d.m_avgPrice = (ld.m_avgPrice * rindex + d.m_close) / (rindex + 1); } rindex++; } m_minuteDatas[code] = datas; } } } }
/// <summary> /// 获取或设置是否需要创建表 /// </summary> public void CreateTable() { String dataBasePath = DataCenter.GetUserPath() + "\\data\\" + DATABASENAME; if (!FCFile.isFileExist(dataBasePath)) { //创建数据库文件 SQLiteConnection.CreateFile(dataBasePath); } //创建表 SQLiteConnection conn = new SQLiteConnection(m_connectStr); conn.Open(); SQLiteCommand cmd = conn.CreateCommand(); cmd.CommandText = CREATETABLESQL; cmd.ExecuteNonQuery(); conn.Close(); }
/// <summary> /// 创建图形控件 /// </summary> public MainForm() { InitializeComponent(); m_juhe = new Juhe(); m_juhe.createNative(); m_juhe.Script = new FaceCatScript(m_juhe); m_native = m_juhe.Native; m_native.Paint = new GdiPlusPaintEx(); m_host = new WinHostEx(); m_host.Native = m_native; m_native.Host = m_host; m_host.HWnd = Handle; m_native.AllowScaleSize = true; m_native.DisplaySize = new FCSize(ClientSize.Width, ClientSize.Height); m_juhe.resetScaleSize(getClientSize()); Invalidate(); m_juhe.load(DataCenter.getAppPath() + "\\config\\MainFrame.html"); m_native.update(); }
/// <summary> /// 获取证券列表 /// </summary> /// <returns>证券列表</returns> public List <Security> getSecurities() { List <Security> securities = new List <Security>(); String codesStr = ""; FCFile.read(DataCenter.getAppPath() + "\\codes.txt", ref codesStr); String[] strs = codesStr.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; for (int i = 0; i < strsSize; i++) { String[] subStrs = strs[i].Split(','); Security security = new Security(); security.m_code = subStrs[0]; security.m_name = subStrs[1]; securities.Add(security); } return(securities); }
/// <summary> /// 加载历史数据 /// </summary> /// <param name="history"></param> public static void loadHistoryDatas() { if (m_historyDatas.Count > 0) { return; } foreach (String code in m_codedMap.Keys) { String fileName = DataCenter.getAppPath() + "\\day\\" + FCStrEx.convertDBCodeToSinaCode(code).ToUpper() + ".txt"; if (File.Exists(fileName)) { StreamReader sra = new StreamReader(fileName, Encoding.Default); String text = sra.ReadToEnd(); List <SecurityData> datas = new List <SecurityData>(); StockService.getHistoryDatasByTdxStr(text, datas); if (datas.Count > 0) { m_historyDatas[code] = datas; } } } }
/// <summary> /// 开始服务 /// </summary> public override void start() { base.start(); String dir = DataCenter.getDataPath() + "\\sinanews"; if (!FCFile.isDirectoryExist(dir)) { FCFile.createDirectory(dir); } List <Security> securities = DataCenter.SecurityService.getSecurities(); int securitiesSize = securities.Count; List <WorkDataInfo> dataInfos = new List <WorkDataInfo>(); for (int i = 0; i < securitiesSize; i++) { SecurityDataInfo dataInfo = new SecurityDataInfo(); dataInfo.m_id = i; dataInfo.m_security = securities[i]; dataInfos.Add(dataInfo); } m_workThread.startWork(dataInfos); }
/// <summary> /// 根据时间下载新浪xls数据文件 /// </summary> public static void downLoadSinaXlsDataByDate(String dateStr) { String contentPath = DataCenter.getAppPath() + "\\download\\xls\\"; String urlTemplate = "http://market.finance.sina.com.cn/downxls.php?date={0}&symbol={1}"; foreach (String code in m_codedMap.Keys) { String filePath = contentPath + code + "-" + dateStr + ".xls"; String url = String.Format(urlTemplate, dateStr, code.ToLower()); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); byte[] bArr = new byte[1024]; int size = responseStream.Read(bArr, 0, (int)bArr.Length); Stream fs = new FileStream(filePath, FileMode.Create); while (size > 0) { fs.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, (int)bArr.Length); } fs.Close(); responseStream.Close(); } }
/// <summary> /// 数据落地线程工作 /// </summary> public static void startWork3() { //复制数据 loadHistoryDatas(); //getMinuteDatas(); //新旧数据合并 foreach (String oCode in m_historyDatas.Keys) { if (!m_latestDatas.ContainsKey(oCode) || !m_historyDatas.ContainsKey(oCode)) { continue; } SecurityLatestData securityLatestData = m_latestDatas[oCode]; List <SecurityData> oldSecurityDatas = m_historyDatas[oCode]; SecurityData oldSecurityData = oldSecurityDatas[oldSecurityDatas.Count - 1]; int myear = 0, mmonth = 0, mday = 0, mhour = 0, mmin = 0, msec = 0, mmsec = 0; FCStr.getDateByNum(oldSecurityData.m_date, ref myear, ref mmonth, ref mday, ref mhour, ref mmin, ref msec, ref mmsec); int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, msec2 = 0; FCStr.getDateByNum(securityLatestData.m_date, ref year, ref month, ref day, ref hour, ref min, ref sec, ref msec2); if (year >= myear && month >= mmonth && day >= mday) { SecurityData nSecurityData = new SecurityData(); nSecurityData.m_amount = securityLatestData.m_amount; nSecurityData.m_close = securityLatestData.m_close; nSecurityData.m_date = securityLatestData.m_date; nSecurityData.m_high = securityLatestData.m_high; nSecurityData.m_low = securityLatestData.m_low; nSecurityData.m_open = securityLatestData.m_open; nSecurityData.m_volume = securityLatestData.m_volume; if (day == mday) { m_historyDatas[oCode].RemoveAt(m_historyDatas[oCode].Count - 1); } m_historyDatas[oCode].Add(nSecurityData); } } String outputFileTemplate = DataCenter.getAppPath() + "\\day\\{0}.txt"; String fileInfo = "{0} {1} 日线 前复权\r\n"; String title = " 日期 开盘 最高 最低 收盘 成交量 成交额\r\n"; String lineTemp = "{0},{1},{2},{3},{4},{5},{6}\r\n"; String timeFormatStr = "yyyy-MM-dd"; //写入文件 foreach (String code in m_historyDatas.Keys) { List <SecurityData> temp3 = m_historyDatas[code]; StringBuilder strbuff = new StringBuilder(); strbuff.Append(String.Format(fileInfo, m_codedMap[code].m_code, m_codedMap[code].m_name)); strbuff.Append(title); foreach (SecurityData sdt in temp3) { strbuff.Append(String.Format(lineTemp, // FCStr.convertNumToDate(sdt.m_date).ToString(timeFormatStr), // sdt.m_open, // sdt.m_high, // sdt.m_low, // sdt.m_close, // sdt.m_volume, // sdt.m_amount)); } strbuff.Append("数据来源:通达信\r\n"); FCFile.write(String.Format(outputFileTemplate, code), strbuff.ToString()); } }
/// <summary> /// 工作中 /// </summary> /// <param name="dataInfo">信息</param> /// <returns>状态</returns> public override int onWorking(WorkDataInfo dataInfo) { Console.WriteLine(dataInfo.m_id.ToString()); SecurityDataInfo securityDataInfo = dataInfo as SecurityDataInfo; String url = String.Format(m_listUrl, FCStrEx.ConvertDBCodeToSinaCode(securityDataInfo.m_security.m_code)); Log = String.Format("下载:{0}", securityDataInfo.m_security.m_code); String html = DataCenter.getHttpWebRequest(url, "GB2312"); if (html != null && html.Length > 0) { NewsInfo newsInfo = new NewsInfo("sinanews"); newsInfo.m_code = securityDataInfo.m_security.m_code; String dir = newsInfo.getDirectory(); if (!FCFile.isDirectoryExist(dir)) { FCFile.createDirectory(dir); } String identifier = "<div class=\"datelist\">"; int pos = html.IndexOf(identifier); if (pos != -1) { html = html.Substring(pos + identifier.Length); html = html.Substring(0, html.IndexOf("</ul>")); html = html.Replace("<ul>\r\n\t\t\t", "").Replace(" ", " "); String[] strs = html.Split(new String[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; for (int i = 0; i < strsSize; i++) { try { newsInfo.m_content = ""; String str = strs[i]; String[] subStrs = str.Split(new String[] { "<a target='_blank' href='" }, StringSplitOptions.RemoveEmptyEntries); if (subStrs.Length >= 2) { newsInfo.m_time = subStrs[0].Trim(); String[] sunStrs = subStrs[1].Split(new String[] { "'>" }, StringSplitOptions.RemoveEmptyEntries); newsInfo.m_url = sunStrs[0]; newsInfo.m_title = sunStrs[1].Replace("</a>", ""); String fileName = newsInfo.getFileName(); if (!FCFile.isFileExist(fileName)) { Log = String.Format("下载:{0}", securityDataInfo.m_security.m_code); String contentHtml = DataCenter.getHttpWebRequest(newsInfo.m_url, "UTF-8"); String sIdentifier = "<!-- 原始正文start -->", eIdentifier = "<!-- 原始正文end -->"; int sPos = contentHtml.IndexOf(sIdentifier); if (sPos != -1) { String content = contentHtml.Substring(sPos + sIdentifier.Length); int ePos = content.IndexOf(eIdentifier); newsInfo.m_content = content.Substring(0, ePos); FCFile.write(fileName, newsInfo.ToString()); } } } } catch (Exception ex) { } } } } return(1); }