/// <summary> /// 取得指定天数的前交易日(不包括今天,降序) /// </summary> /// <param name="days"></param> /// <returns></returns> public static List <DateTime> GetPreTradeDateDescending(int days) { List <DateTime> lstTradeDate = MongoDBHelper.AsQueryable <TradeCalendarInfo>() .Where(m => m.IsOpen == "Y" && m.Market == "SZSE" && m.CalendarDate != DateTime.Today) .OrderByDescending(m => m.CalendarDate) .Take(days) .Select(m => m.CalendarDate) .ToList(); return(lstTradeDate); }
/// <summary> /// 保存快照信息 /// </summary> /// <param name="list"></param> public static void SaveL1QuoteInfo(List <L1QuoteInfo> list) { foreach (L1QuoteInfo info in list) { L1QuoteInfo updateInfo = MongoDBHelper.FindOneAndReplace <L1QuoteInfo>(m => m.SecurityID == info.SecurityID, info); if (updateInfo == null) { MongoDBHelper.InsertOne <L1QuoteInfo>(info); } } }
/// <summary> /// 删除过期的财务指标 /// (就是已经不用显示的季度) /// </summary> private static void DeleteOverdueFinaceIndex(ulong securityID) { List <DateTime> lstExistDate = MongoDBHelper.AsQueryable <FinanceIndexInfo>().Where(m => m.SecurityID == securityID).Select(m => m.EndDate).ToList(); List <DateTime> listDate = TransferHelper.GetFinanceDate(); List <DateTime> lstOverdueDate = lstExistDate.Except(listDate).ToList(); foreach (DateTime overdueDate in lstOverdueDate) { string delFilter = "EndDate:ISODate(\"" + overdueDate.ToString("yyyy-MM-dd") + "T00:00:00.000+0800\")"; MongoDBHelper.DeleteManyAsync <FinanceIndexInfo>(delFilter); } }
/// <summary> /// 缓存指标菜单 /// </summary> public static void CacheIndexMenu() { LogHelper.Info("总运行", "开始:生成指标菜单"); // 获取板块指标信息 List <PlateInfo> plates = MongoDBHelper.AsQueryable <PlateInfo>().ToList(); //生成指标树信息 List <IndexTreeDTO> lstIndexTree = CreateIndexTreeList(plates); //缓存指标树 CacheIndexTree(lstIndexTree); LogHelper.Info("总运行", "结束:生成指标菜单"); }
/// <summary> /// 从指定开始日开始,获取行情数据 /// </summary> /// <returns></returns> public static List <DataByTimeIndexInfo> GetDataByTimeIndexInfo(TimePeriod period, int retryTime = 0) { var securityIDs = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID); var req = new ReqDataByTime(); req.securityIDs.AddRange(securityIDs); //从json文件取行情指标 string appDataPath = AppDomain.CurrentDomain.BaseDirectory; string jsonFileName = Path.Combine(appDataPath, "Data", "JsonData", "行情数据的字段.json"); string jsonData = File.ReadAllText(jsonFileName); var dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonData); req.fields.AddRange(dic.Keys); req.fields.Add("Symbol"); req.fields.Add("SecurityID"); req.fields.Add("TradingDate"); req.fields.Add("Filling"); req.quoteType = EQuoteType.Day; req.interval = 1; req.timePeriods.Add(period); req.page = new DataPage() { begin = 0, end = 0 }; req.priceAdj = EPriceAdjust.AdjNone; var rep = SyncRequestEx.Instance.SyncSendData(req, _waitTime); if (rep is Exception) { if (retryTime > RetryTime) { string exMsg = string.Format("同步行情异常. 重试{0}次仍未取到值.begin:{1}; end:{2}.", DSPHelper.RetryTime, period.begin, period.end); LogHelper.Error("异常", exMsg); return(null); } retryTime += 1; string errMsg = string.Format("获取行情:begin:{0}; end:{1}异常!重试{2}次!", period.begin, period.end, retryTime); LogHelper.Info("总运行", errMsg); return(GetDataByTimeIndexInfo(period, retryTime)); } var list = TransferHelper.Transfer2ListDataByTimeIndexInfo(rep); list = list.Where(m => m.Filling == "0").ToList(); return(list); }
/// <summary> /// 缓存RSI分布值 /// </summary> private static void CacheTechnicalIndex_RSI() { var lstRSI = MongoDBHelper.AsQueryable <RSIResult>().ToList();; //指标明细 List <IndexDetailDTO> lstDto = new List <IndexDetailDTO>(); IndexDetailDTO dtoRSI = new IndexDetailDTO(); lstDto.Add(dtoRSI); dtoRSI.Code = ConstDefine.Cst_RSI; dtoRSI.Name = ConstDefine.Cst_RSI; dtoRSI.ParamsValues = GetInputParams(ConstDefine.Cst_RSI); dtoRSI.IsDefault = true; //取分布值 double[] arrayIndexValues = lstRSI.Select(m => m.RSI).OrderBy(m => m).ToArray(); //设置最大值、最小值、分布值 SetMaxMinArrValue(dtoRSI, arrayIndexValues); Dictionary <string, string> dicCodeName = new Dictionary <string, string>(); dicCodeName[ConstDefine.Cst_RSI_GoldX] = ConstDefine.Cst_RSI_GoldX_Name; dicCodeName[ConstDefine.Cst_RSI_BlackX] = ConstDefine.Cst_RSI_BlackX_Name; foreach (string indexCode in dicCodeName.Keys) { IndexDetailDTO dto = new IndexDetailDTO(); lstDto.Add(dto); dto.Code = indexCode; dto.Name = dicCodeName[indexCode]; dto.ParamsValues = GetInputParams(ConstDefine.Cst_RSI); dto.IsDefault = true; } //删除原有的 List <string> lstIndexCode = new List <string>(); lstIndexCode.Add(ConstDefine.Cst_RSI); lstIndexCode.AddRange(dicCodeName.Keys); string codes = string.Join("','", lstIndexCode); MongoDBHelper.DeleteMany <IndexDetailDTO>("{Code:{$in:['" + codes + "']}}"); //插入MACD技术指标指标的明细 MongoDBHelper.InsertManyAsync <IndexDetailDTO>(lstDto); }
/// <summary> /// 同步行情快照 /// </summary> public static void SyncQuickData() { // 记日志 //LogHelper.Info("最新行情", "开始:同步最新行情"); // 获取所有a股代码数据 var symbols = MongoDBHelper.AsQueryable <SymbolInfo>(); List <L1QuoteInfo> list = DSPHelper.GetQuickData(symbols); // 数据获取正常 if (list.Count > 0) { SaveL1QuoteInfo(list); } //LogHelper.Info("最新行情", "结束:同步最新行情"); }
/// <summary> /// 缓存所有板块其下的股票(Todo放MongodbCacheHelper) /// </summary> public static void CachePlatesSymbol() { // 记日志 LogHelper.Info("总运行", "开始:同步板块股票信息"); List <Int64> lstPlateID = MongoDBHelper.AsQueryable <IndexTreeDTO>() .Where(m => m.TypeCode == ConstDefine.CstCode_PlateIndex) .Select(m => m.Code) .ToList() .Select(m => Convert.ToInt64(m)).ToList(); List <PlateSymbolInfo> plateSymbols = new List <PlateSymbolInfo>(); foreach (Int64 plateID in lstPlateID) { try { var symbols = DSPHelper.GetPlateSymbols(Convert.ToUInt64(plateID)); if (symbols == null) { continue; } var securityIDs = symbols.Select(x => x.SecurityID); plateSymbols.Add(new PlateSymbolInfo { PlateID = plateID, SecurityIDs = securityIDs }); } catch (Exception ex) { LogHelper.Error("异常", "板块ID:" + plateID, ex); } } if (plateSymbols.Count() > 0) { MongoDBHelper.DeleteMany <PlateSymbolInfo>("{}"); MongoDBHelper.InsertMany <PlateSymbolInfo>(plateSymbols); } LogHelper.Info("总运行", "结束:同步板块股票信息"); }
/// <summary> /// 技术指标WR /// </summary> public static void CalculateWR() { //取得所有a股安全码 List <ulong> lstSecurityID = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID).ToList(); //默认参数14 int nParam = 14; //只要前一交易日 int dataCount = nParam; List <WRResult> lstAll = new List <WRResult>(); foreach (ulong securityID in lstSecurityID) { var lstRaw = (from m in MongoDBHelper.AsQueryable <DataByTime>() where m.SecurityID == securityID && m.CP.HasValue && m.HIP.HasValue && m.LOP.HasValue orderby m.TradingDate descending select new WRResult() { SecurityID = m.SecurityID, TradingDate = m.TradingDate, Symbol = m.Symbol, CP = m.CP.Value, HIP = m.HIP.Value, LOP = m.LOP.Value }).Take(dataCount).OrderBy(m => m.TradingDate).ToList(); List <WRResult> lstResult = CalcWR.CalcOneStock(lstRaw, nParam); lstAll.AddRange(lstResult); } if (lstAll.Count > 0) { var lstResult = CalcWR.GetLastResult(lstAll); MongoDBHelper.DeleteMany <WRResult>("{}"); MongoDBHelper.InsertMany <WRResult>(lstResult); } }
/// <summary> /// 同步板块数据 /// </summary> public static void SyncPlatesIndex() { // 记日志 LogHelper.Info("总运行", "开始:同步板块信息"); try { var plates = DSPHelper.GetPlates(); if (plates.Count() > 0) { MongoDBHelper.DeleteMany <PlateInfo>("{}"); MongoDBHelper.InsertMany <PlateInfo>(plates); } } catch (Exception ex) { LogHelper.Error("异常", "同步板块数据异常!", ex); } finally { LogHelper.Info("总运行", "结束:同步板块信息"); } }
/// <summary> /// 同步交易日历 /// </summary> public static void SyncTradeCalendarIndex() { // 记日志 LogHelper.Info("总运行", "开始:同步交易日历"); try { var tradingDays = DSPHelper.GetTradingDay(); if (tradingDays.Count() > 0) { MongoDBHelper.DeleteMany <TradeCalendarInfo>("{}"); MongoDBHelper.InsertMany <TradeCalendarInfo>(tradingDays); } } catch (Exception ex) { LogHelper.Error("异常", "同步交易日历异常!", ex); } finally { LogHelper.Info("总运行", "结束:同步交易日历"); } }
/// <summary> /// 缓存板块指标的明细 /// </summary> public static void CachePlateIndex() { LogHelper.Info("总运行", "开始:生成板块指标明细"); //取得财务指标编号、名称 var lstIndexLite = from dto in MongoDBHelper.AsQueryable <IndexTreeDTO>() where dto.TypeCode == ConstDefine.CstCode_PlateIndex select new IndexLiteDTO() { Code = dto.Code, Name = dto.Name }; //指标明细 List <IndexDetailDTO> lstDto = new List <IndexDetailDTO>(); //遍历指标编号,生成选股条件信息 foreach (IndexLiteDTO indexLite in lstIndexLite) { IndexDetailDTO dto = new IndexDetailDTO(); lstDto.Add(dto); dto.Code = indexLite.Code; dto.Name = indexLite.Name; dto.IsDefault = true; } //删除原有的 string[] lstCode = lstDto.Select(m => m.Code).ToArray(); string codes = string.Join("','", lstCode); MongoDBHelper.DeleteMany <IndexDetailDTO>("{Code:{$in:['" + codes + "']}}"); //插入板块指标的明细 MongoDBHelper.InsertManyAsync <IndexDetailDTO>(lstDto); LogHelper.Info("总运行", "结束:生成板块指标明细"); }
/// <summary> /// 技术指标MACD /// </summary> public static void CalculateMACD() { //取得所有a股安全码 List <ulong> lstSecurityID = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID).ToList(); List <MACDHistory> lstAllLast5 = new List <MACDHistory>(); foreach (ulong securityID in lstSecurityID) { var lstRet = (from m in MongoDBHelper.AsQueryable <MACDHistory>() where m.SecurityID == securityID orderby m.TradingDate ascending select m).ToList(); List <MACDHistory> lstRaw; if (lstRet.Count == 0) { lstRaw = (from m in MongoDBHelper.AsQueryable <DataByTime>() where m.SecurityID == securityID && m.CP.HasValue orderby m.TradingDate ascending select new MACDHistory() { SecurityID = m.SecurityID, TradingDate = m.TradingDate, Symbol = m.Symbol, CP = m.CP.Value }).ToList(); } else { MACDHistory last = lstRet.Last(); lstRaw = (from m in MongoDBHelper.AsQueryable <DataByTime>() where m.SecurityID == securityID && m.TradingDate > last.TradingDate && m.CP.HasValue orderby m.TradingDate ascending select new MACDHistory() { SecurityID = m.SecurityID, TradingDate = m.TradingDate, Symbol = m.Symbol, CP = m.CP.Value }).ToList(); } int newIndex = lstRet.Count; lstRet.AddRange(lstRaw); CalcMACD.CalcOneStock(lstRet, newIndex, 12, 26, 9); var lstLast5 = lstRet.Skip(lstRet.Count - 5).Take(5).ToList(); lstAllLast5.AddRange(lstLast5); } if (lstAllLast5.Count > 0) { MongoDBHelper.DeleteMany <MACDHistory>("{}"); MongoDBHelper.InsertMany <MACDHistory>(lstAllLast5); var lstResult = CalcMACD.History2Result(lstAllLast5); MongoDBHelper.DeleteMany <MACDResult>("{}"); MongoDBHelper.InsertMany <MACDResult>(lstResult); } }
/// <summary> /// 同步财务指标信息 /// </summary> public static void SyncFinaceIndex() { // 记日志 LogHelper.Info("总运行", "开始:同步财务指标数据"); DspRequest dspRequest = new DspRequest(); dspRequest.DealData = SaveFinaceIndex; LstRetryReqFinance = new List <ReqFinance>(); try { //取得所有a股安全码 List <ulong> lstSecurityID = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID).ToList(); //取得所有财务指标编码 List <string> lstIndexCode = GetAllFinaceIndexCode(); ////删除已过期的数据 //DeleteOverdueFinaceIndex(lstSecurityID[0]); // 根据代码同步所有财务指标数据 foreach (var securityID in lstSecurityID) { try { FinanceIndexInfo info = MongoDBHelper.AsQueryable <FinanceIndexInfo>() .Where(m => m.SecurityID == securityID) .OrderByDescending(m => m.EndDate) .FirstOrDefault(); DateTime lastDate = new DateTime(1990, 1, 1); if (info != null) { lastDate = info.EndDate; } ReqFinance req = DSPHelper.GetReqFinance(securityID, lstIndexCode, lastDate); if (req != null) { dspRequest.AsyncSendData(req, DSPHelper.WaitTime, req); } } catch (Exception ex) { string errMsg = "同步财务指标异常.SecurityID:" + securityID; LogHelper.Error("异常", errMsg, ex); } } //等待DSP任务完成 dspRequest.WaitFinished(); if (LstRetryReqFinance.Count > 0) { //重试 RetrySyncFinaceIndex(dspRequest, 1); //等待DSP任务完成 dspRequest.WaitFinished(); } ////给10秒钟mongodb。 //Thread.Sleep(10000); } catch (Exception ex) { LogHelper.Error("异常", "同步财务指标发生异常.", ex); } finally { dspRequest.Dispose(); LogHelper.Info("总运行", "结束:同步财务指标数据"); } }
/// <summary> /// 缓存行情指标的明细 /// </summary> public static void CacheDataByTimeIndex() { LogHelper.Info("总运行", "开始:生成行情指标明细"); //取得财务指标编号、名称 var lstIndexLite = from dto in MongoDBHelper.AsQueryable <IndexTreeDTO>() where dto.TypeCode == ConstDefine.CstCode_MarketIndex select new IndexLiteDTO() { Code = dto.Code, Name = dto.Name }; //遍历指标编号,生成选股条件信息 foreach (IndexLiteDTO indexLite in lstIndexLite) { //取指定指标的所有指标股票信息 var lstDataByTimeIndex = MongoDBHelper.AsQueryable <DataByTimeIndexInfo>().Where(m => m.IndexCode == indexLite.Code).ToList(); if (lstDataByTimeIndex.Count == 0) { continue; } //按交易日分组 var groupByTradingDateIndex = (from index in lstDataByTimeIndex group index by index.TradingDate into g orderby g.Key descending select new { TradingDate = g.Key, TradingDateName = g.First().TradingDateName, groupList = g }).Take(5); //交易日分组列表 var lstSelectTerm = from groupIndex in groupByTradingDateIndex orderby groupIndex.TradingDate descending select new SelectOption() { SelectItem = TransferHelper.DateTimeToString(groupIndex.TradingDate), SelectDisplay = groupIndex.TradingDateName }; List <SelectOption> selectTermList = lstSelectTerm.ToList(); //指标明细 List <IndexDetailDTO> lstDto = new List <IndexDetailDTO>(); //按交易日遍历 foreach (var groupByEndDate in groupByTradingDateIndex) { IndexDetailDTO dto = new IndexDetailDTO(); lstDto.Add(dto); dto.Code = indexLite.Code; dto.Name = indexLite.Name; dto.SelectTerm = TransferHelper.DateTimeToString(groupByEndDate.TradingDate); dto.SelectTermDisplay = groupByEndDate.TradingDateName; dto.SelectTermList = selectTermList; //取分布值 var arrayIndexValues = groupByEndDate.groupList.Select(m => m.IndexValue).OrderBy(m => m).ToArray(); //设置最大值、最小值、分布值 SetMaxMinArrValue(dto, arrayIndexValues); } //设置默认选项(第一个) if (lstDto.Count > 0) { lstDto[0].IsDefault = true; } //删除原有的 MongoDBHelper.DeleteMany <IndexDetailDTO>("{Code:'" + indexLite.Code + "'}"); //插入行情指标的明细 MongoDBHelper.InsertManyAsync <IndexDetailDTO>(lstDto); } LogHelper.Info("总运行", "结束:生成行情指标明细"); }
/// <summary> /// 缓存KDJ分布值 /// </summary> private static void CacheTechnicalIndex_KDJ() { var lstKDJ = MongoDBHelper.AsQueryable <KDJResult>().ToList();; //指标明细 List <IndexDetailDTO> lstDto = new List <IndexDetailDTO>(); List <SelectOption> lstLeft = GetLeftSelectOption(ConstDefine.Cst_KDJ); foreach (SelectOption option in lstLeft) { IndexDetailDTO dtoKDJ = new IndexDetailDTO(); lstDto.Add(dtoKDJ); dtoKDJ.Code = ConstDefine.Cst_KDJ; dtoKDJ.Name = ConstDefine.Cst_KDJ; dtoKDJ.ParamsValues = GetInputParams(ConstDefine.Cst_KDJ); dtoKDJ.SelectTerm = option.SelectItem; dtoKDJ.SelectTermDisplay = option.SelectItem; dtoKDJ.SelectTermList = lstLeft; //取分布值 double[] arrayIndexValues = null; switch (option.SelectItem) { case ConstDefine.Cst_KDJ_K: dtoKDJ.IsDefault = true; arrayIndexValues = lstKDJ.Select(m => m.K).OrderBy(m => m).ToArray(); break; case ConstDefine.Cst_KDJ_D: arrayIndexValues = lstKDJ.Select(m => m.D).OrderBy(m => m).ToArray(); break; case ConstDefine.Cst_KDJ_J: arrayIndexValues = lstKDJ.Select(m => m.J).OrderBy(m => m).ToArray(); break; } //设置最大值、最小值、分布值 SetMaxMinArrValue(dtoKDJ, arrayIndexValues); } Dictionary <string, string> dicCodeName = new Dictionary <string, string>(); dicCodeName[ConstDefine.Cst_KDJ_GoldX] = ConstDefine.Cst_KDJ_GoldX_Name; dicCodeName[ConstDefine.Cst_KDJ_BlackX] = ConstDefine.Cst_KDJ_BlackX_Name; dicCodeName[ConstDefine.Cst_KDJ_LowGoldX] = ConstDefine.Cst_KDJ_LowGoldX_Name; dicCodeName[ConstDefine.Cst_KDJ_TopDepart] = ConstDefine.Cst_KDJ_TopDepart_Name; dicCodeName[ConstDefine.Cst_KDJ_DownDepart] = ConstDefine.Cst_KDJ_DownDepart_Name; dicCodeName[ConstDefine.Cst_KDJ_Oversold] = ConstDefine.Cst_KDJ_Oversold_Name; dicCodeName[ConstDefine.Cst_KDJ_Overbought] = ConstDefine.Cst_KDJ_Overbought_Name; foreach (string indexCode in dicCodeName.Keys) { IndexDetailDTO dto = new IndexDetailDTO(); lstDto.Add(dto); dto.Code = indexCode; dto.Name = dicCodeName[indexCode]; dto.ParamsValues = GetInputParams(ConstDefine.Cst_KDJ); dto.IsDefault = true; } //删除原有的 List <string> lstIndexCode = new List <string>(); lstIndexCode.Add(ConstDefine.Cst_KDJ); lstIndexCode.AddRange(dicCodeName.Keys); string codes = string.Join("','", lstIndexCode); MongoDBHelper.DeleteMany <IndexDetailDTO>("{Code:{$in:['" + codes + "']}}"); //插入MACD技术指标指标的明细 MongoDBHelper.InsertManyAsync <IndexDetailDTO>(lstDto); }
/// <summary> /// 缓存DMI分布值 /// </summary> private static void CacheTechnicalIndex_DMI() { var lstDMI = MongoDBHelper.AsQueryable <DMIResult>().ToList();; //指标明细 List <IndexDetailDTO> lstDto = new List <IndexDetailDTO>(); List <SelectOption> lstLeft = GetLeftSelectOption(ConstDefine.Cst_DMI); foreach (SelectOption option in lstLeft) { IndexDetailDTO dtoBOLL = new IndexDetailDTO(); lstDto.Add(dtoBOLL); dtoBOLL.Code = ConstDefine.Cst_DMI; dtoBOLL.Name = ConstDefine.Cst_DMI; dtoBOLL.ParamsValues = GetInputParams(ConstDefine.Cst_DMI); dtoBOLL.SelectTerm = option.SelectItem; dtoBOLL.SelectTermDisplay = option.SelectItem; dtoBOLL.SelectTermList = lstLeft; //取分布值 double[] arrayIndexValues = null; switch (option.SelectItem) { case ConstDefine.Cst_DMI_PDI: dtoBOLL.IsDefault = true; arrayIndexValues = lstDMI.Select(m => m.PDI).OrderBy(m => m).ToArray(); break; case ConstDefine.Cst_DMI_MDI: arrayIndexValues = lstDMI.Select(m => m.MDI).OrderBy(m => m).ToArray(); break; case ConstDefine.Cst_DMI_ADX: arrayIndexValues = lstDMI.Where(m => m.ADX.HasValue).Select(m => m.ADX.Value).OrderBy(m => m).ToArray(); break; } //设置最大值、最小值、分布值 SetMaxMinArrValue(dtoBOLL, arrayIndexValues); } Dictionary <string, string> dicCodeName = new Dictionary <string, string>(); dicCodeName[ConstDefine.Cst_DMI_GoldX] = ConstDefine.Cst_DMI_GoldX_Name; dicCodeName[ConstDefine.Cst_DMI_BlackX] = ConstDefine.Cst_DMI_BlackX_Name; foreach (string indexCode in dicCodeName.Keys) { IndexDetailDTO dto = new IndexDetailDTO(); lstDto.Add(dto); dto.Code = indexCode; dto.Name = dicCodeName[indexCode]; dto.ParamsValues = GetInputParams(ConstDefine.Cst_DMI); dto.IsDefault = true; } //删除原有的 List <string> lstIndexCode = new List <string>(); lstIndexCode.Add(ConstDefine.Cst_DMI); lstIndexCode.AddRange(dicCodeName.Keys); string codes = string.Join("','", lstIndexCode); MongoDBHelper.DeleteMany <IndexDetailDTO>("{Code:{$in:['" + codes + "']}}"); //插入MACD技术指标指标的明细 MongoDBHelper.InsertManyAsync <IndexDetailDTO>(lstDto); }
/// <summary> /// 技术指标DMI /// </summary> public static void CalculateDMI() { //默认参数 int N = 14; //取得所有a股安全码 List <ulong> lstSecurityID = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID).ToList(); List <DMIHistory> lstAllLastN = new List <DMIHistory>(); foreach (ulong securityID in lstSecurityID) { var lstRet = (from m in MongoDBHelper.AsQueryable <DMIHistory>() where m.SecurityID == securityID orderby m.TradingDate ascending select m).ToList(); List <DMIHistory> lstRaw; if (lstRet.Count == 0) { lstRaw = (from m in MongoDBHelper.AsQueryable <DataByTime>() where m.SecurityID == securityID && m.CP.HasValue && m.HIP.HasValue && m.LOP.HasValue orderby m.TradingDate ascending select new DMIHistory() { SecurityID = m.SecurityID, TradingDate = m.TradingDate, Symbol = m.Symbol, CP = m.CP.Value, HIP = m.HIP.Value, LOP = m.LOP.Value }).ToList(); } else { DMIHistory last = lstRet.Last(); lstRaw = (from m in MongoDBHelper.AsQueryable <DataByTime>() where m.SecurityID == securityID && m.TradingDate > last.TradingDate && m.CP.HasValue && m.HIP.HasValue && m.LOP.HasValue orderby m.TradingDate ascending select new DMIHistory() { SecurityID = m.SecurityID, TradingDate = m.TradingDate, Symbol = m.Symbol, CP = m.CP.Value, HIP = m.HIP.Value, LOP = m.LOP.Value }).ToList(); } int newIndex = lstRet.Count; lstRet.AddRange(lstRaw); if (lstRet.Count >= N) { CalcDMI.CalcOneStock(lstRet, newIndex, N, 6); var lstLastN = lstRet.Skip(lstRet.Count - N).Take(N); lstAllLastN.AddRange(lstLastN); } } if (lstAllLastN.Count > 0) { MongoDBHelper.DeleteMany <DMIHistory>("{}"); MongoDBHelper.InsertMany <DMIHistory>(lstAllLastN); var lstResult = CalcDMI.History2Result(lstAllLastN); MongoDBHelper.DeleteMany <DMIResult>("{}"); MongoDBHelper.InsertMany <DMIResult>(lstResult); } }
/// <summary> /// 同步所有日线行情数据 /// </summary> public static void SyncDataByTime() { // 记日志 LogHelper.Info("总运行", "开始:同步所有日线行情"); DspRequest dspRequest = new DspRequest(); dspRequest.DealData = SaveDataByTime; LstRetryReqDataByTime = new List <ReqDataByTime>(); try { //取得所有a股安全码 List <ulong> lstSecurityID = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID).ToList(); //根据代码同步所有日频行情数据 foreach (var securityID in lstSecurityID) { try { DataByTime info = MongoDBHelper.AsQueryable <DataByTime>() .Where(m => m.SecurityID == securityID) .OrderByDescending(m => m.TradingDate) .FirstOrDefault(); ReqDataByTime req = null; if (info == null) { req = DSPHelper.GetReqDataByTimeAll(securityID); } else { DateTime beginDate = Convert.ToDateTime(info.TradingDate).AddDays(1); if (beginDate >= DateTime.Today) { continue; } req = DSPHelper.GetReqDataByTimeLast(securityID, beginDate); } dspRequest.AsyncSendData(req, DSPHelper.WaitTime, req); } catch (Exception ex) { string errMsg = string.Format("同步所有日线行情发生异常.securityID:{0}", securityID); LogHelper.Error("异常", errMsg, ex); } } //等待DSP任务完成 dspRequest.WaitFinished(); if (LstRetryReqDataByTime.Count > 0) { //重试 RetrySyncDataByTime(dspRequest, 1); //等待DSP任务完成 dspRequest.WaitFinished(); } ////给10秒钟mongodb。 //Thread.Sleep(10000); } catch (Exception ex) { LogHelper.Error("异常", "同步所有日线行情发生异常.", ex); } finally { dspRequest.Dispose(); LogHelper.Info("总运行", "结束:同步所有日线行情"); } }