Example #1
0
        private void Save(string strDstDir)
        {
            foreach (string futureid in _buffer.Keys)
            {
                string fullname = strDstDir + "\\" + futureid + ".day";
                var    isfile   = File.Exists(fullname);
                bool   isRead   = true;
                if (Market == EnumMarket.期货)
                {
                    FutureDayFile dayfile = new FutureDayFile();
                    dayfile.Init(fullname);
                    BarSeries daybarlist = new BarSeries();
                    if (_prgMq.Contains(futureid))
                    {
                        if (_prgMq.EndTime(futureid).Date == _buffer[futureid][0].TradingDate)
                        {
                            var endtime = _prgMq.EndTime(futureid);
                            endtime = endtime.AddDays(-1);
                            if (endtime > new DateTime())
                            {
                                isRead = dayfile.Read(daybarlist, new DateTime(), endtime);
                            }
                        }
                        else
                        {
                            isRead = dayfile.Read(daybarlist, new DateTime(), _prgMq.EndTime(futureid));
                        }

                        if (isRead == false)
                        {
                            _logger.Error("读取文件失败,不准备写入" + fullname + futureid);
                            continue;
                        }
                        daybarlist.AddRange(_buffer[futureid]);
                        dayfile.Write(daybarlist);
                    }
                    else
                    {
                        dayfile.Write(_buffer[futureid]);
                    }
                }
            }
        }
Example #2
0
        //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");
        }
Example #3
0
        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();
        }