private decimal GetYestclose(string p, DateTime today) { int i = -1; var list = cateService.GetPriceInfo(p, data.TechCycle.day, today.AddDays(i)); while (list == null || list.Count == 0) { list = cateService.GetPriceInfo(p, data.TechCycle.day, today.AddDays(--i)); if (list != null && list.Count > 0) { return(list[0].price ?? 0); } } return(list[0].price ?? 1); }
/// <summary> /// 获取类别数据 /// </summary> /// <param name="p1">类别code</param> /// <param name="p2">类型,day,week,month</param> /// <returns></returns> public IHttpActionResult GetPriceInfo(string p1, string p2) { if (string.IsNullOrEmpty(p1) || string.IsNullOrEmpty(p2)) { return(BadRequest("参数不合规")); } data.TechCycle cycle; if (!Enum.TryParse(p2, true, out cycle)) { return(BadRequest("参数不合规")); } var list = categoryService.GetPriceInfo(p1, cycle); var result = list.Select(p => new PriceInfo { date = p.date, code = p.code, price = p.price ?? 0, yestclose = p.yestclose ?? 0, high = p.high ?? 0, low = p.low ?? 0, open = p.open ?? 0, percent = p.percent ?? 0, updown = p.updown ?? 0, volume = p.volume ?? 0 }).ToList(); return(Ok(result)); }
private void InitCategoryIndexByWeek(string categoryCode) { //获取某只股票的所有价格列表 TechCycle cycle = TechCycle.day; //获取日线数据 var dayList = cateService.GetPriceInfo(categoryCode, TechCycle.day); if (dayList.Count == 0) { return; } var weekList = new List <PriceInfo>(); DateTime startTime = dayList[0].date; PriceInfo lastWeekPrice = null; while (startTime < DateTime.Now) { DateTime nextWeek = startTime.AddDays(7); DateTime Monday = nextWeek.AddDays(DayOfWeek.Monday - nextWeek.DayOfWeek); DateTime endTime = Monday.AddDays(-1); var rangePrice = dayList.Where(p => p.date > startTime && p.date < endTime).OrderBy(p => p.date).ToList(); if (rangePrice.Count == 0) { startTime = endTime; continue; } var lastPrice = rangePrice[rangePrice.Count - 1]; decimal?open = rangePrice[0].price; decimal?close = lastPrice.price; decimal?high = rangePrice.Max(p => p.price); decimal?low = rangePrice.Min(p => p.price); decimal?volume = rangePrice.Sum(p => p.volume); decimal?turnover = rangePrice.Sum(p => p.turnover); decimal?percent = 0; decimal?updown = 0; decimal?yestclose = 0; if (lastWeekPrice != null) { yestclose = lastWeekPrice.price; updown = close - yestclose; if (yestclose != 0) { percent = updown / yestclose; } } var currentPrice = new PriceInfo { code = categoryCode, open = open, price = close, low = low, high = high, date = lastPrice.date, percent = Math.Round(percent ?? 0, 2), turnover = turnover, updown = updown, volume = volume, yestclose = yestclose }; weekList.Add(currentPrice); lastWeekPrice = currentPrice; startTime = endTime; } try { cateService.AddPriceByWeek <data_category_week_latest>(weekList, false); this.Log().Info(string.Format("行业指数计算完成:周期:{0},类别:{1}", TechCycle.week, categoryCode)); } catch (Exception ex) { //(new System.Collections.Generic.Mscorlib_CollectionDebugView<System.Data.Entity.Validation.DbEntityValidationResult>(((System.Data.Entity.Validation.DbEntityValidationException)(ex)).EntityValidationErrors as System.Collections.Generic.List<System.Data.Entity.Validation.DbEntityValidationResult>)).Items[0].ValidationErrors this.Log().Error(ex.Message); } }