private void SaveFuture(string strDstDir, DateTime month) { if (month == default(DateTime)) { return; } foreach (string futureid in _bufferFuture.Keys) { //如果K线根数为0,表示没有K线信息,则直接跳过 if (_bufferFuture[futureid].Count == 0) { continue; } string fullname = strDstDir + "\\" + month.ToString("yyyyMM"); if (!Directory.Exists(fullname)) { Directory.CreateDirectory(fullname); } fullname = fullname + "\\" + futureid + ".day"; if (Market == EnumMarket.期货) { FutureDayFile dayfile = new FutureDayFile(); dayfile.Init(fullname); BarSeries daybarlist = new BarSeries(); if (_prgMq.Contains(futureid)) { var starttime = _prgMq.BeginTime(futureid); var endtime = _prgMq.EndTime(futureid); var tradingdate = _bufferFuture[futureid][0].TradingDate; //如果换月,则需要从当月的1号开始 if (starttime.Month < tradingdate.Month) { starttime = new DateTime(tradingdate.Year, tradingdate.Month, 1); } //如果换月,则不需要读取 if (endtime.Month == tradingdate.Month) { if (_prgMq.EndTime(futureid).Date == _bufferFuture[futureid][0].TradingDate) { endtime = endtime.AddDays(-1); if (endtime >= starttime) { dayfile.Read(daybarlist, starttime, endtime); } else { _logger.Error(starttime + "没有读取到日线数据" + endtime + fullname); } } else { dayfile.Read(daybarlist, _prgMq.BeginTime(futureid), _prgMq.EndTime(futureid)); } } daybarlist.AddRange(_bufferFuture[futureid]); } else { daybarlist = _bufferFuture[futureid]; } if (File.Exists(fullname)) { File.Delete(fullname); } if (daybarlist.Count > 0) { dayfile.Write(daybarlist); } else { _logger.Error("品种" + futureid + "在" + month + "没有对应的分钟线数据,请查询对应的数据是否有错!"); } } } _bufferFuture.Clear(); }
//public LogReporter logrpt = null; public void Convert(string strSrcDir, string strDstDir, DateTime startTime = default(DateTime)) { // 创建目录 if (!Directory.Exists(strDstDir)) { Directory.CreateDirectory(strDstDir); } string progName = strDstDir + "\\prog.mq"; if (!File.Exists(progName)) { _logger.Error(progName + "文件不存在"); } // 读取目录下已经存在的数据 _prgMq = new ProgMqFile(progName, startTime); string[] tmpdirlist = Directory.GetDirectories(strSrcDir); List <string> dirlist = new List <string>(tmpdirlist); dirlist.Sort(); dirlist.Reverse(); int month = 0; DateTime lastDay = default(DateTime); foreach (string strDir in dirlist) { DateTime day; string strshort = strDir.Substring(strDir.Length - 8); if (!DateTime.TryParseExact(strshort, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out day)) { continue; } if (month != day.Month) { SaveFuture(strDstDir, lastDay); month = day.Month; _logger.Info("进度: " + day.ToString("yyyy-MM-dd")); //if (logrpt != null) // logrpt.Print("进度: " + day.ToString("yyyy-MM-dd")); } lastDay = day; if (ConvertDay(strDir, day)) { break; } } Save(strDstDir); SaveFuture(strDstDir, lastDay); foreach (string futureid in _beginlist.Keys) { DateTime begintime = _prgMq.BeginTime(futureid); DateTime endtime = _prgMq.EndTime(futureid); if (begintime == default(DateTime)) { begintime = _beginlist[futureid]; } else if (begintime > _beginlist[futureid]) { begintime = _beginlist[futureid]; } if (endtime == default(DateTime)) { endtime = _endlist[futureid]; } else if (endtime < _endlist[futureid]) { endtime = _endlist[futureid]; } _prgMq.Add(futureid, begintime, endtime); } _prgMq.Save(strDstDir + "\\prog.mq"); _logger.Info("完成T2D"); //if (logrpt != null) // logrpt.Print("完成T2D"); }
private void Save(string strDstDir, DateTime month) { if (month == default(DateTime)) { return; } foreach (string futureid in _buffer.Keys) { //如果K线根数为0,表示没有K线信息,则直接跳过 if (_buffer[futureid].Count == 0) { continue; } string fullname = strDstDir + "\\" + month.ToString("yyyyMM"); if (!Directory.Exists(fullname)) { Directory.CreateDirectory(fullname); } fullname = fullname + "\\" + futureid + ".min"; try { BarSeries daybarlist = new BarSeries(); if (_prgMq.Contains(futureid)) { //TimeSlice livingtime = fmgr.GetLivingTime(futureid); using (var barfile = new BarFileV1(fullname, EnumMarket.期货)) { var starttime = _prgMq.BeginTime(futureid); var endtime = _prgMq.EndTime(futureid); var tradingdate = _buffer[futureid][0].TradingDate; //如果换月,则需要从当月的1号开始 if (starttime.Month < tradingdate.Month) { starttime = new DateTime(tradingdate.Year, tradingdate.Month, 1); } //如果换月,则不需要读取 if (endtime.Month == tradingdate.Month) { if (_prgMq.EndTime(futureid).Date == _buffer[futureid][0].TradingDate) { endtime = endtime.AddDays(-1); if (endtime >= starttime) { barfile.ReadTradingDays(daybarlist, starttime, endtime); } } else { barfile.ReadTradingDays(daybarlist, _prgMq.BeginTime(futureid), _prgMq.EndTime(futureid)); } } } daybarlist.AddRange(_buffer[futureid]); } else { daybarlist = _buffer[futureid]; } if (File.Exists(fullname)) { File.Delete(fullname); } if (daybarlist.Count > 0) { using (var barfile = new BarFileV1(fullname, EnumMarket.期货, EnumBarType.分钟, 1)) { barfile.Write(daybarlist); } } else { _logger.Error("品种" + futureid + "在" + month + "没有对应的分钟线数据,请查询对应的数据是否有错!"); } } catch (Exception ex) { _logger.Error(fullname + ex.ToString()); throw new Exception("读取文件出错" + fullname); } } _buffer.Clear(); }