/// <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 IEnumerable <TradeCalendarInfo> GetTradingDay(int retryTime = 0) { var req = new ReqTradeCalendar(); req.exchangeCode.Add("SZSE"); req.fields.Add("*"); req.securityType = "S0101"; req.dateBegin = TransferHelper.DateTimeToString(DateTime.Today.AddMonths(-1)); req.dateEnd = TransferHelper.DateTimeToString(DateTime.Today); req.page = new DataPage() { begin = 0, end = 0 }; var rep = SyncRequestEx.Instance.SyncSendData(req, _waitTime); if (rep is Exception) { if (retryTime > RetryTime) { string exMsg = string.Format("同步交易日历异常. 重试{0}次仍未取到值.", DSPHelper.RetryTime); LogHelper.Error("异常", exMsg); return(new List <TradeCalendarInfo>()); } retryTime += 1; string errMsg = string.Format("获取交易日历发生异常!重试{0}次!", retryTime); LogHelper.Info("总运行", errMsg); return(GetTradingDay(retryTime)); } return(TransferHelper.Transfer2List <TradeCalendarInfo>(rep)); }
/// <summary> /// 根据代码取财务数据的请求参数 /// </summary> /// <returns></returns> public static ReqFinance GetReqFinance(ulong securityID, List <string> lstIndexCode, DateTime lastDate) { ReqFinance req = new ReqFinance(); req.securityIDs.Add(securityID); req.fields.Add("Symbol"); req.fields.Add("EndDate"); req.fields.AddRange(lstIndexCode); //List<DateTime> listDate = TransferHelper.GetFinanceDate(); //DateTime maxDate = listDate.Max(); //DateTime startDate = maxDate; //foreach(DateTime date in listDate) //{ // if(date >= lastDate) // { // startDate = date; // break; // } //} List <DateTime> listDate = TransferHelper.GetFinanceDate(); DateTime maxDate = listDate.Max(); if (lastDate >= maxDate) { return(null); } DateTime startDate = listDate[0]; foreach (DateTime date in listDate) { if (date > lastDate) { startDate = date; break; } } req.dateBegin = TransferHelper.DateTimeToString(startDate); req.dateEnd = TransferHelper.DateTimeToString(maxDate); // 财报统计的截止日期 req.dateType = ERptDateType.ERptDateClose; // 合并本期 req.reportType = EReportType.MergeCur; // TTM,12个月滚动累计 或者是 季度累计 req.trailType = ETrailType.TrailAddup; req.page = new DataPage() { begin = 0, end = 0 }; return(req); }
/// <summary> /// 获取行情数据的请求条件 /// </summary> /// <returns></returns> public static ReqDataByTime GetReqDataByTime(ulong securityID, DateTime beginDate, DateTime endDate) { ReqDataByTime req = new ReqDataByTime(); req.securityIDs.Add(securityID); req.fields.Add("*"); req.quoteType = EQuoteType.Day; req.interval = 1; req.timePeriods.Add(new TimePeriod() { begin = TransferHelper.DateTimeToString(beginDate), // 只取在此时间之后的数据 end = TransferHelper.DateTimeToString(endDate) }); req.page = new DataPage() { begin = 0, end = 0 }; req.priceAdj = EPriceAdjust.AdjNone; return(req); }
/// <summary> /// 转换成指定的模板 /// (自适应T的string型) /// </summary> /// <param name="obj"></param> /// <returns></returns> public static List <T> Transfer2List <T>(object obj) { List <T> lstModel = new List <T>(); PropertyInfo[] properties = obj.GetType().GetProperties(); foreach (PropertyInfo pro in properties) { if (pro.Name.EndsWith("Specified")) { continue; } if (pro.PropertyType.IsGenericType) { if (pro.PropertyType.GetGenericArguments()[0] == typeof(DataColumn)) { List <DataColumn> lstCol = pro.GetValue(obj, null) as List <DataColumn>; Dictionary <string, string[]> dicStr = new Dictionary <string, string[]>(); PropertyInfo[] modelProperties = typeof(T).GetProperties(); var modelPropertieNames = modelProperties.Select(m => m.Name); Dictionary <string, int> dicNameIndex = GetColIndexAndStringValue(lstCol, ref dicStr, modelPropertieNames.ToArray()); uint count = lstCol[0].count; for (int i = 0; i < count; i++) { T model = Activator.CreateInstance <T>(); lstModel.Add(model); Type typeModel = model.GetType(); foreach (string colName in dicNameIndex.Keys) { int colIndex = dicNameIndex[colName]; DataColumn col = lstCol[colIndex]; string[] strValues = dicStr.ContainsKey(colName) ? dicStr[colName] : null; object objValue = GetValueObj(i, col.type, col.data, strValues); if (IsInvalidValue(col.type, objValue)) //判断是否非法数值 { continue; } if (col.type != EDataType.TypeChar && typeModel.GetProperty(colName).PropertyType == typeof(string)) { if (col.type == EDataType.TypeDateTime) { string strValue = TransferHelper.DateTimeToString(Convert.ToDateTime(objValue)); typeModel.GetProperty(colName).SetValue(model, strValue); } else { string strValue = Convert.ToString(objValue); typeModel.GetProperty(colName).SetValue(model, strValue); } } else if (col.type == EDataType.TypeInt64 && typeModel.GetProperty(colName).PropertyType == typeof(ulong)) { ulong ulongValue = Convert.ToUInt64(objValue); typeModel.GetProperty(colName).SetValue(model, ulongValue); } else { typeModel.GetProperty(colName).SetValue(model, objValue); } } } } else { throw new Exception("未处理的类型"); } } } return(lstModel); }
/// <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("总运行", "结束:生成行情指标明细"); }