/// <summary> /// 技术指标MA /// </summary> public static void CalculateMA() { //取得所有a股安全码 List <ulong> lstSecurityID = MongoDBHelper.AsQueryable <SymbolInfo>().Select(m => m.SecurityID).ToList(); //默认参数20 int maParam = 20; //只要前一交易日 int dataCount = maParam; List <MAResult> lstAll = new List <MAResult>(); foreach (ulong securityID in lstSecurityID) { var lstRaw = (from m in MongoDBHelper.AsQueryable <DataByTime>() where m.SecurityID == securityID && m.CP.HasValue orderby m.TradingDate descending select new MARawData() { SecurityID = m.SecurityID, TradingDate = m.TradingDate, Symbol = m.Symbol, CP = m.CP.Value }).Take(dataCount).OrderBy(m => m.TradingDate).ToList(); List <MAResult> lstResult = CalcMA.CalcOneStock(lstRaw, maParam); lstAll.AddRange(lstResult); } if (lstAll.Count > 0) { MongoDBHelper.DeleteMany <MAResult>("{}"); MongoDBHelper.InsertMany <MAResult>(lstAll); } }
/// <summary> /// 同步所有a股代码 /// </summary> public static void SyncA_PlateIndex() { // 记日志 LogHelper.Info("总运行", "开始:同步a股代码"); try { // 获取所有a股代码数据 var symbols = DSPHelper.GetA_PlateSymbols(); // 获取数据正常 if (symbols.Count() > 0) { MongoDBHelper.DeleteMany <SymbolInfo>("{}"); MongoDBHelper.InsertMany <SymbolInfo>(symbols); } } catch (Exception ex) { LogHelper.Error("异常", "同步所有a股代码异常!", ex); } finally { LogHelper.Info("总运行", "结束:同步a股代码"); } }
///// <summary> ///// 技术指标RSI ///// </summary> //public static void CalculateRSI() //{ // //取得所有a股安全码 // List<ulong> lstSecurityID = MongoDBHelper.AsQueryable<SymbolInfo>().Select(m => m.SecurityID).ToList(); // //默认参数14 // int param = 14; // int dataCount = param; // MongoDBHelper.DeleteMany<RSIHistory>("{}"); // MongoDBHelper.DeleteMany<RSIResult>("{}"); // foreach (ulong securityID in lstSecurityID) // { // var lstRaw = (from m in MongoDBHelper.AsQueryable<DataByTime>() // where m.SecurityID == securityID && m.ChangeRatio.HasValue // orderby m.TradingDate descending // select new RSIHistory() // { // SecurityID = m.SecurityID, // TradingDate = m.TradingDate, // Symbol = m.Symbol, // ChangeRatio = m.ChangeRatio.Value // }).OrderBy(m => m.TradingDate).ToList(); // CalcRSI.CalcOneStock(lstRaw, param); // if (lstRaw.Count > 0) // { // MongoDBHelper.InsertMany<RSIHistory>(lstRaw); // var lstResult = CalcRSI.History2Result(lstRaw); // MongoDBHelper.InsertMany<RSIResult>(lstResult); // } // } // //if (lstAll.Count > 0) // //{ // // MongoDBHelper.DeleteMany<RSIHistory>("{}"); // // MongoDBHelper.InsertMany<RSIHistory>(lstAll); // // //var lstResult = CalcRSI.GetLastResult(lstAll); // // //MongoDBHelper.DeleteMany<RSIResult>("{}"); // // //MongoDBHelper.InsertMany<RSIResult>(lstResult); // //} //} /// <summary> /// 技术指标RSI /// </summary> public static void CalculateRSI() { // 获取所有a股代码数据 var securityIDs = DSPHelper.GetA_PlateSymbols().Select(x => x.SecurityID); //默认参数14 int param = 14; int dataCount = Math.Max(param, 12) + 2; List <RSIResult> lstAll = new List <RSIResult>(); foreach (ulong securityID in securityIDs) { var lstRaw = (from m in MongoDBHelper.AsQueryable <DataByTime>() where m.SecurityID == securityID && m.ChangeRatio.HasValue orderby m.TradingDate descending select new RSIResult() { SecurityID = m.SecurityID, TradingDate = m.TradingDate, Symbol = m.Symbol, ChangeRatio = m.ChangeRatio.Value }).Take(dataCount).OrderBy(m => m.TradingDate).ToList(); CalcRSI.CalcOneStock(lstRaw, param); lstAll.AddRange(lstRaw); } if (lstAll.Count > 0) { var lstResult = CalcRSI.GetLastResult(lstAll); MongoDBHelper.DeleteMany <RSIResult>("{}"); MongoDBHelper.InsertMany <RSIResult>(lstResult); } }
/// <summary> /// 保存日线行情数据 /// </summary> /// <returns></returns> public static object SaveDataByTime(object rep, object reqObj) { ReqDataByTime req = reqObj as ReqDataByTime; try { ulong securityID = Convert.ToUInt64(req.securityIDs[0]); if (rep is Exception) { string msg = string.Format("SecurityID:{0}.{1}", securityID, (rep as Exception).Message); LogHelper.Track("所有日线行情", msg); LstRetryReqDataByTime.Add(req); return(null); } List <DataByTime> lstInfo = DSPHelper.TransferDataByTime(rep, securityID); int count = lstInfo.Count; if (count > 0) { MongoDBHelper.InsertMany <DataByTime>(lstInfo); } LogHelper.Track("所有日线行情", string.Format("SecurityID:{0}. Count:{1}", securityID, count)); } catch (Exception ex) { string errMsg = string.Format("SecurityID:{0},{1}", (reqObj as ReqDataByTime).securityIDs[0], ex.Message); LogHelper.Track("所有日线行情", errMsg); LstRetryReqDataByTime.Add(req); } return(null); }
/// <summary> /// 同步前5个交易日的日线行情数据 /// </summary> public static void SyncDataByTimeIndexInfo() { LogHelper.Info("总运行", "开始:同步前5个交易日的日线行情"); //前5个交易日 List <DateTime> lstTradeDate = MongodbCacheHelper.GetPreTradeDateDescending(5); TimePeriod period = new TimePeriod(); period.begin = TransferHelper.DateTimeToString(lstTradeDate[4]); period.end = TransferHelper.DateTimeToString(lstTradeDate[0]); try { var dataByTime = DSPHelper.GetDataByTimeIndexInfo(period); if (dataByTime.Count > 0) { MongoDBHelper.DeleteMany <DataByTimeIndexInfo>("{}"); MongoDBHelper.InsertMany <DataByTimeIndexInfo>(dataByTime); } } catch (Exception ex) { string errMsg = string.Format("同步前5交易日行情发生异常.begin:{0}; end:{1}", period.begin, period.end); LogHelper.Error("异常", errMsg, ex); } finally { LogHelper.Info("总运行", "结束:同步前5个交易日的日线行情"); } }
/// <summary> /// 保存财务指标 /// </summary> /// <returns></returns> public static object SaveFinaceIndex(object rep, object reqObj) { try { ReqFinance req = reqObj as ReqFinance; ulong securityID = Convert.ToUInt64(req.securityIDs[0]); if (rep is Exception) { string errMsg = string.Format("SecurityID:{0}.{1}", securityID, (rep as Exception).Message); LogHelper.Track("财务指标", errMsg); LstRetryReqFinance.Add(req); return(null); } List <FinanceIndexInfo> lstInfo = DSPHelper.TransferFinance(rep, securityID); int count = lstInfo.Count; if (count > 0) { string delFilter = "{SecurityID:" + securityID + ", EndDate:{$gte:ISODate(\"" + req.dateBegin + "T00:00:00.000+0800\")}}"; MongoDBHelper.DeleteMany <FinanceIndexInfo>(delFilter); MongoDBHelper.InsertMany <FinanceIndexInfo>(lstInfo); } var tttttt = lstInfo.Where(m => m.IndexCode == "BEPS").ToList(); LogHelper.Track("财务指标", string.Format("SecurityID:{0}. Count:{1}", securityID, count)); } catch (Exception ex) { string errMsg = string.Format("SecurityID:{0}.{1}", ((ReqFinance)reqObj).securityIDs[0], ex.Message); LogHelper.Track("财务指标", errMsg); LstRetryReqFinance.Add(reqObj as ReqFinance); } return(null); }
/// <summary> /// 缓存指标树 /// </summary> /// <param name="lstIndexTree">指标树信息</param> private static void CacheIndexTree(List <IndexTreeDTO> lstIndexTree) { //删除原有指标树 MongoDBHelper.DeleteMany <IndexTreeDTO>("{}"); //插入指标树信息 MongoDBHelper.InsertMany <IndexTreeDTO>(lstIndexTree); }
/// <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> /// 同步板块数据 /// </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> /// 技术指标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> /// 技术指标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); } }