/// <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 + CStrA.ConvertDBCodeToFileName(code); if (!CFileA.IsFileExist(fileName)) { fileName = m_newFileDir + CStrA.ConvertDBCodeToSinaCode(code).ToUpper() + ".txt"; } if (CFileA.IsFileExist(fileName)) { String text = ""; CFileA.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> /// <param name="id">指标ID</param> /// <param name="code">代码</param> /// <returns>返回数据</returns> public static Dictionary <String, double> CalculateIndicatorExtern(int id, String code) { Dictionary <String, double> list = new Dictionary <String, double>(); if (m_indicators.ContainsKey(id) && SecurityService.m_historyDatas.ContainsKey(code) && SecurityService.m_latestDatas.ContainsKey(code)) { CIndicator indicator = m_indicators[id]; List <CIndicator> indicators = new List <CIndicator>(); indicators.Add(indicator); List <SecurityData> datas = new List <SecurityData>(); List <SecurityData> oldSecurityDatas = SecurityService.m_historyDatas[code]; int oldSecurityDatasSize = oldSecurityDatas.Count; for (int i = 0; i < oldSecurityDatasSize; i++) { datas.Add(oldSecurityDatas[i]); } SecurityLatestData latestData = SecurityService.m_latestDatas[code]; SecurityData newData = new SecurityData(); StockService.GetSecurityData(latestData, latestData.m_lastClose, 1440, 1, ref newData); datas.Add(newData); CTable dataSource = indicator.DataSource; int[] fields = new int[] { KeyFields.CLOSE_INDEX, KeyFields.HIGH_INDEX, KeyFields.LOW_INDEX, KeyFields.OPEN_INDEX, KeyFields.VOL_INDEX, KeyFields.AMOUNT_INDEX }; SecurityDataHelper.BindHistoryDatas(m_chart, dataSource, indicators, fields, datas); datas.Clear(); int rowsCount = dataSource.RowsCount; int variablesSize = indicator.MainVariables.Count; if (rowsCount > 0) { foreach (String name in indicator.MainVariables.Keys) { int field = indicator.MainVariables[name]; double value = dataSource.Get2(rowsCount - 1, field); list[name] = value; } } dataSource.Clear(); } return(list); }
/// <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\\" + CStrA.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> private static void StartWork() { //加载代码表//step 1 String codes = ""; if (m_codedMap.Count == 0) { Dictionary <String, KwItem> items = EMSecurityService.KwItems; foreach (KwItem item in items.Values) { GSecurity security = new GSecurity(); security.m_code = item.Code; security.m_name = item.Name; security.m_pingyin = item.Pingyin; security.m_type = item.Type; if (!security.m_code.StartsWith("A")) { m_codedMap[security.m_code] = security; codes += security.m_code; codes += ","; } } } while (true) { if (codes != null && codes.Length > 0) { if (codes.EndsWith(",")) { codes.Remove(codes.Length - 1); } String[] strCodes = codes.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); int codesSize = strCodes.Length; String latestCodes = ""; for (int i = 0; i < codesSize; i++) { latestCodes += strCodes[i]; if (i == codesSize - 1 || (i > 0 && i % 50 == 0)) { String latestDatasResult = StockService.GetSinaLatestDatasStrByCodes(latestCodes); if (latestDatasResult != null && latestDatasResult.Length > 0) { List <SecurityLatestData> latestDatas = new List <SecurityLatestData>(); StockService.GetLatestDatasBySinaStr(latestDatasResult, 0, latestDatas); String[] subStrs = latestDatasResult.Split(new String[] { ";\n" }, StringSplitOptions.RemoveEmptyEntries); int latestDatasSize = latestDatas.Count; for (int j = 0; j < latestDatasSize; j++) { SecurityLatestData latestData = latestDatas[j]; if (latestData.m_close == 0) { latestData.m_close = latestData.m_buyPrice1; } if (latestData.m_close == 0) { latestData.m_close = latestData.m_sellPrice1; } lock (m_latestDatas) { m_latestDatasStr[latestData.m_code] = subStrs[j]; //bool append = true; //if (m_latestDatas.ContainsKey(latestData.m_code)) //{ // if (!m_latestDatas[CStrA.ConvertFileCodeToMemoryCode(latestData.m_code)].Equals(latestData)) // { // append = false; // } //} //if(append) //{ // long today = (long)DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds / 86400000; // if (m_today < today) // { // m_today = today; // String nPath = DataCenter.GetAppPath() + "\\tick\\" + DateTime.Now.ToString("yyyy-MM-dd"); // if (!Directory.Exists(nPath)) // { // Directory.CreateDirectory(nPath); // } // m_newFileDir = nPath + "\\"; // } // String line = String.Format("{0},{1},{2},{3}\r\n", latestData.m_date,// // latestData.m_close, latestData.m_volume, latestData.m_amount); // CFileA.Append(m_newFileDir + latestData.m_code + ".txt", line); //} if (!m_latestDatas.ContainsKey(latestData.m_code)) { m_latestDatas[latestData.m_code] = latestData; } else { m_latestDatas[latestData.m_code].Copy(latestData); } if (latestData.m_code == "000001.SH") { m_shTradeTime = latestData.m_date; } } } latestDatas.Clear(); } latestCodes = ""; } else { latestCodes += ","; } } } Thread.Sleep(1); } }