/// <summary> /// 先后尝试MSSQL读取数据,Wind获取数据。若无MSSQL数据,则保存到SQLServer。 /// </summary> /// <param name="code">代码,如股票代码,期权代码</param> /// <param name="date">指定的日期</param> /// <param name="tag">读写文件路径前缀,若为空默认为类名</param> /// <returns></returns> virtual public List <T> fetchFromSQLServerOrWindAndSave(string code, DateTime startDate, DateTime endDate, string sourceServer = "local", string targetSource = "local", string tag = null) { if (tag == null) { tag = typeof(T).ToString(); } List <T> result = null; List <T> lackedInfo = new List <T>(); //记录本地获取的数据 List <DateTime> exitsDates = new List <DateTime>(); List <DateTime> lackedDates = new List <DateTime>(); //根据股票的上市退市日期来调整获取数据的日期 startDate = startDate > StockBasicInfoUtils.getStockListDate(code) ? startDate : StockBasicInfoUtils.getStockListDate(code); endDate = endDate > StockBasicInfoUtils.getStockDelistDate(code) ? StockBasicInfoUtils.getStockDelistDate(code).AddDays(-1) : endDate; var tradeDays = DateUtils.GetTradeDays(startDate, endDate); bool csvHasData = false; return(result); }
/// <summary> /// 尝试从Wind获取数据。 /// </summary> /// <param name="code">代码,如股票代码,期权代码</param> /// <param name="date">指定的日期</param> /// <param name="tag">读写文件路径前缀,若为空默认为类名</param> /// <returns></returns> virtual public List <T> fetchFromWind(string code, DateTime startDate, DateTime endDate, string tag = null) { if (tag == null) { tag = typeof(T).ToString(); } List <T> result = null; log.Debug("正在获取{0}数据列表{1}...", Kit.ToShortName(tag), code); if (Caches.WindConnection == false && Caches.WindConnectionTry == true) { log.Error("无法连接Wind,无法从Wind获取失败!"); return(result); } //尝试从Wind获取 //根据股票的上市退市日期来调整获取数据的日期 startDate = startDate > StockBasicInfoUtils.getStockListDate(code) ? startDate : StockBasicInfoUtils.getStockListDate(code); endDate = endDate > StockBasicInfoUtils.getStockDelistDate(code) ? StockBasicInfoUtils.getStockDelistDate(code) : endDate; if (endDate >= DateTime.Today) { endDate = DateUtils.PreviousTradeDay(DateTime.Today); } if (endDate < startDate) { log.Debug("退市时间过早,无法读取数据!"); return(result); } log.Debug("尝试从Wind获取{0}...", code); try { result = readFromWindOnly(code, startDate, endDate, null, null); } catch (Exception e) { log.Error(e, "尝试从wind读取数据失败!品种{0},时间{1}至{2}", code, startDate.ToShortDateString(), endDate.ToShortDateString()); //debug 输出失败信息 Console.WriteLine("尝试从wind读取数据失败!品种{0},时间{1}至{2}", code, startDate.ToShortDateString(), endDate.ToShortDateString()); } logInfo(code, startDate, endDate, tag, result); return(result); }
/// <summary> /// 先后尝试从本地csv文件,Wind获取数据。若无本地csv,则保存到CacheData文件夹。 /// </summary> /// <param name="code">代码,如股票代码,期权代码</param> /// <param name="date">指定的日期</param> /// <param name="tag">读写文件路径前缀,若为空默认为类名</param> /// <returns></returns> virtual public List <T> fetchFromLocalCsvOrWindAndSave(string code, DateTime startDate, DateTime endDate, string tag = null) { if (tag == null) { tag = typeof(T).ToString(); } List <T> result = null; List <T> lackedInfo = new List <T>(); //记录本地获取的数据 List <DateTime> exitsDates = new List <DateTime>(); List <DateTime> lackedDates = new List <DateTime>(); //根据股票的上市退市日期来调整获取数据的日期 startDate = startDate > StockBasicInfoUtils.getStockListDate(code) ? startDate : StockBasicInfoUtils.getStockListDate(code); endDate = endDate > StockBasicInfoUtils.getStockDelistDate(code) ? StockBasicInfoUtils.getStockDelistDate(code).AddDays(-1) : endDate; var tradeDays = DateUtils.GetTradeDays(startDate, endDate); bool csvHasData = false; result = fetchFromLocalCsv(code, startDate, endDate, tag); if (result != null && result.Count > 0) { csvHasData = true; } if ((result == null || result.Count == 0) && Caches.WindConnection == false && Caches.WindConnectionTry == true) { log.Error("本地无CSV数据并且wind无法连接,故无法获得数据!"); return(result); } if (result == null || result.Count == 0) //数据不完整,必须去万德获取数据 { result = fetchFromWind(code, startDate, endDate); } if (result != null) { exitsDates = result.Select(x => x.time).ToList(); foreach (var date in tradeDays) { if (exitsDates.Contains(date) == false && date < DateTime.Today) { lackedDates.Add(date); } } } if (result != null && lackedDates.Count != 0) { if (lackedDates.Count <= 20) //若缺失数据较少逐日补齐 { foreach (var date in lackedDates) { var lackedList = fetchFromWind(code, date, date); if (lackedList != null) { lackedInfo.AddRange(lackedList); } } } else //若缺失数据较多,整段补齐 { lackedDates.Sort(); var lackedList = fetchFromWind(code, lackedDates[0], lackedDates[lackedDates.Count - 1]); foreach (var item in lackedList) { if (!exitsDates.Contains(item.time)) { lackedInfo.Add(item); } } } } if (!csvHasData && result != null && result.Count() > 0) { //如果数据不是从csv获取的,可保存至本地,存为csv文件 log.Debug("正在保存到本地csv文件..."); saveToLocalCSV(result); } if (lackedInfo.Count > 0) { saveToLocalCSV(lackedInfo); result.AddRange(lackedInfo); result.OrderBy(x => x.time).ToList(); } return(result); }
public static void __Initialize(IContainer container) { //配置NLog日志模块 if (ConfigurationManager.AppSettings["ConsoleLog"] == "on") { MyNLogConfig.ApplyWithConsole(); } else { MyNLogConfig.Apply(); } //初始化CacheData文件夹 var cdPath = ConfigurationManager.AppSettings["RootPath"] + ConfigurationManager.AppSettings["CacheData.RootPath"]; if (!Directory.Exists(cdPath)) { Directory.CreateDirectory(cdPath); } //初始化wind连接 try { WindAPI wapi = Platforms.GetWindAPI(); } catch (Exception e) { log.Error(e, "Wind未连接!"); } //初始化交易日数据 TradeDaysService tradeDaysService = container.Resolve <TradeDaysService>(); if (Caches.WindConnection == true) { tradeDaysService.fetchFromLocalCsvOrWindAndSaveAndCache(); } else { tradeDaysService.fetchFromLocalCsvOnly(); } //初始化交易费用 switch (ConfigurationManager.AppSettings["Setting"]) { case "common": MySettings.CommonSettings(); break; default: break; } switch (ConfigurationManager.AppSettings["DisplayNetWorth"]) { case "on": Caches.DisplayNetWorth = true; break; default: break; } switch (ConfigurationManager.AppSettings["50ETFOptionInfoRecord"]) { case "on": OptionInfoOf50ETFService optionInfoService = container.Resolve <OptionInfoOf50ETFService>(); optionInfoService.fetchFromLocalCsvOrWindAndSaveAndCache(tag: "OptionInfo", code: "510050.SH"); break; default: break; } //程序有问题,暂时注释 switch (ConfigurationManager.AppSettings["StockBasicInfoRecord"]) { case "on": StockBasicInfoService stockInfoService = container.Resolve <StockBasicInfoService>(); stockInfoService.fetchFromLocalCsvOrWindAndSaveAndCache(localCsvExpiration: 0, tag: "StockBasicInfo", code: "allStocks"); var stockInfoList = StockBasicInfoUtils.getAllStockList(); foreach (var stock in stockInfoList) { container.Resolve <StockDailyMarketService>().fetchFromLocalCsvOrWindAndSave(stock.code, new DateTime(2007, 1, 1), DateTime.Today); } break; default: break; } //switch (ConfigurationManager.AppSettings["CommodityOptionInfoRecord"]) //{ // case "on": // OptionInfoDailyService optionInfoService = container.Resolve<OptionInfoDailyService>(); // optionInfoService.fetchFromLocalCsvOrWindAndSave(tag: null, code: "M1707.DCE",date: new DateTime(2017, 05, 15)); // break; // default: // break; //} }