/// <summary> /// 执行方法 /// </summary> /// <param name="context"></param> public override void Execute(CommandContext context) { logger.Debug("执行命令:" + this.ToString()); //取得参数 everydayLinePath = (String)context.GetExternalValue(everydayLinePath); dayLineReposorityPath = (String)context.GetExternalValue(dayLineReposorityPath); mergeDateString = (String)context.GetExternalValue(mergeDateString); mergeDate = DateUtils.Parse(mergeDateString); logger.Debug("取得参数:源路径=" + everydayLinePath + ",合并路径=" + dayLineReposorityPath + ",日期=" + mergeDateString); if (!everydayLinePath.EndsWith("\\")) { everydayLinePath += "\\"; } if (!dayLineReposorityPath.EndsWith("\\")) { dayLineReposorityPath += "\\"; } IndicatorRepository repository = (IndicatorRepository)context.GetExternalValue("{$repository}"); //检查路径 DirectoryInfo dFromInfo = new DirectoryInfo(everydayLinePath); if (!dFromInfo.Exists) { throw new Exception("单日日线存储路径不存在:" + everydayLinePath); } DirectoryInfo dToInfo = new DirectoryInfo(dayLineReposorityPath); if (!dToInfo.Exists) { throw new Exception("日线存储仓库路径不存在:" + dayLineReposorityPath); } FileInfo[] finfos = dFromInfo.GetFiles("*.txt"); logger.Debug("共有" + (finfos == null ? "0" : finfos.Length.ToString()) + "个单日K线数据待合并..."); foreach (FileInfo fInfo in finfos) { logger.Debug("处理" + fInfo.Name + "..."); String market = fInfo.Name.Substring(0, fInfo.Name.IndexOf("#")); String code = fInfo.Name.substring(0, "#", "."); String[] lines = File.ReadAllLines(fInfo.FullName); for (int i = 0; i < lines.Length; i++) { if (lines[i] == null || lines[i] == "") { continue; } if (!lines[i].Contains(",")) { continue; } String[] ss = lines[i].Trim().Split(','); if (ss == null || ss.Length != 7) { continue; } if (ss[0] == null) { continue; } DateTime d = DateUtils.InitDate; if (!DateUtils.TryParse(ss[0].Trim(), out d)) { continue; } if (d.Date != this.mergeDate.Date) { continue; } KLineItem item = new KLineItem(); item.SetValue <String>(KLineItem.PD_CODE.Name, code); item.SetValue <DateTime>(KLineItem.PD_TIME.Name, d); item.SetValue <double>(KLineItem.PD_OPEN.Name, ConvertUtils.ConvertTo <double>(ss[1].Trim())); item.SetValue <double>(KLineItem.PD_HIGH.Name, ConvertUtils.ConvertTo <double>(ss[2].Trim())); item.SetValue <double>(KLineItem.PD_LOW.Name, ConvertUtils.ConvertTo <double>(ss[3].Trim())); item.SetValue <double>(KLineItem.PD_CLOSE.Name, ConvertUtils.ConvertTo <double>(ss[4].Trim())); item.SetValue <double>(KLineItem.PD_VOLUMN.Name, ConvertUtils.ConvertTo <double>(ss[5].Trim())); item.SetValue <double>(KLineItem.PD_TURNOVER.Name, ConvertUtils.ConvertTo <double>(ss[6].Trim())); TimeSerialsDataSet ds = repository[code]; if (ds == null) { continue; } ds.DayKLine[d.Date] = item; ds.Save("kline", TimeUnit.day); logger.Debug("合并到" + market.ToUpper() + code + ".csv"); } } }
/// <summary> /// 导入数据 /// </summary> public bool doMergeToRepository() { showBeginMessage("开始合并数据..."); if (repository == null) { repository = new IndicatorRepository(FileUtils.GetDirectory(props.Get <String>("repository"))); repository.Initilization(); } String datapath = FileUtils.GetDirectory(props.Get <String>("datapath")); DirectoryInfo dInfo = new DirectoryInfo(datapath); FileInfo[] fInfos = dInfo.GetFiles("*.scv"); if (fInfos == null || fInfos.Length <= 0) { showResultMessage("没有需要导入的新文件", 1); return(false); } try { foreach (FileInfo fInfo in fInfos) { String code = fInfo.Name.Substring(3, 6); TimeSerialsDataSet ds = repository[code]; KLine kline = ds.DayKLine; if (ds == null) { continue; } showProgressMessage(code); CSVFile csvFile = new CSVFile(); csvFile.Load(fInfo.FullName, Encoding.UTF8, false, ","); List <String> lines = csvFile.Lines; for (int i = lines.Count - 1; i >= 0; i--) { if (lines[i] == null || lines[i].Trim() == "") { continue; } String[] ss = lines[i].Split(','); if (ss == null || ss.Length < 7) { continue; } DateTime d = DateUtils.Parse(ss[0]); KLineItem item = kline[d]; if (item != null) { break; } double[] v = new double[6]; for (int j = 0; j < v.Length; j++) { v[j] = double.Parse(ss[j + 1]); } item = new KLineItem(); item.Date = d; item.SetValue <double>("OPEN", v[0]); item.SetValue <double>("HIGH", v[1]); item.SetValue <double>("LOW", v[2]); item.SetValue <double>("CLOSE", v[3]); item.SetValue <double>("VOLUME", v[4]); item.SetValue <double>("TURNOVER", v[5]); kline.Add(item); } ds.Save("kline", TimeUnit.day); } showResultMessage(""); return(true); } catch (Exception e) { showResultMessage("导入失败", -1, e.Message); return(false); } }