예제 #1
0
파일: Task.cs 프로젝트: pkujf/StockHelper
 /// <summary>
 /// 下载指定股票代码行情数据
 /// </summary>
 /// <param name="startDate"></param>
 /// <param name="endDate"></param>
 /// <param name="StockCode"></param>
 ///
 public void DownLoadStockHistoryData(string startDate, string endDate, string stockCode)
 {
     try
     {
         var downFailList = new List <string>();
         foreach (var StockCode in stockCode.Split(','))
         {
             //YahooStockApi ysa = new YahooStockApi();
             //var data = ysa.getDataFromYahoo(StockCode, Convert.ToDateTime(startDate), Convert.ToDateTime(endDate));
             WangYiStockApi wysa = new WangYiStockApi();
             var            data = wysa.getDataFromWangYi(StockCode, Convert.ToDateTime(startDate), Convert.ToDateTime(endDate));
             if (data.Count == 0)
             {
                 downFailList.Add(StockCode);
                 continue;
             }
             var dt  = DataHelper.ToDataTable(data);
             var res = proc.insertStockHistoryData(dt);
             if (res != 1)
             {
                 LogHelper.WriteLog(string.Format("下载股票列表数据失败,股票代码:{0}", StockCode));
             }
             Console.WriteLine(string.Format("时间:{0}下载股票{1}成功", DateTime.Now.ToString(), StockCode));
         }
         Console.WriteLine(string.Format("全量下载股票列表数据失败列表\r\n{0}", string.Join(",", downFailList)));
         LogHelper.WriteLog(string.Format("全量下载股票列表数据失败\r\n{0}", string.Join(",", downFailList)));
     }
     catch (Exception ex)
     {
         LogHelper.WriteLog("下载股票历史数据失败", ex);
     }
 }
예제 #2
0
파일: Task.cs 프로젝트: pkujf/StockHelper
        /// <summary>
        ///
        /// </summary>
        /// <param name="date"></param>
        /// <param name="takeN"></param>
        /// <param name="closeWeight"></param>
        /// <param name="upperShadowWeight"></param>
        /// <param name="lowerShadowWeight"></param>
        /// <param name="entityWeight"></param>
        /// <param name="topN"></param>
        /// <returns></returns>
        public List <String> getSimilarPlate(DateTime date, int takeN, double closeWeight, double upperShadowWeight, double lowerShadowWeight, double entityWeight, int topN)
        {
            WangYiStockApi    wysa = new WangYiStockApi();
            StockPointCompute spc  = new StockPointCompute();
            var ssPlate            = wysa.getDataFromWangYi("000001.ss", date.AddYears(-5), date).Where(w => w.SVolume > 0).OrderBy(o => o.StockHistoryDate);
            var szPlate            = wysa.getDataFromWangYi("399001.sz", date.AddYears(-5), date).Where(w => w.SVolume > 0).OrderBy(o => o.StockHistoryDate);

            if (Convert.ToDateTime(ssPlate.ToList()[ssPlate.Count() - 1].StockHistoryDate) != date)
            {
                return(null);
            }
            Dictionary <string, decimal> meanClose = new Dictionary <string, decimal>();
            Dictionary <string, decimal> meanOpen  = new Dictionary <string, decimal>();
            Dictionary <string, decimal> meanHigh  = new Dictionary <string, decimal>();
            Dictionary <string, decimal> meanLow   = new Dictionary <string, decimal>();

            foreach (var i in ssPlate)
            {
                foreach (var j in szPlate)
                {
                    if (i.StockHistoryDate == j.StockHistoryDate)
                    {
                        meanClose.Add(i.StockHistoryDate, i.SClose + j.SClose);
                        meanOpen.Add(i.StockHistoryDate, i.SOpen + j.SOpen);
                        meanHigh.Add(i.StockHistoryDate, i.SHigh + j.SHigh);
                        meanLow.Add(i.StockHistoryDate, i.SLow + j.SLow);
                        break;
                    }
                }
            }
            var           meanCloseRise = spc.GetIncrease(meanClose.Values.ToList());
            List <double> upperShadow   = new List <double>();
            List <double> lowerShadow   = new List <double>();
            List <double> entity        = new List <double>();

            for (int i = 1; i < meanClose.Count; i++)
            {
                var low   = meanLow.Values.ToList()[i];
                var high  = meanHigh.Values.ToList()[i];
                var open  = meanOpen.Values.ToList()[i];
                var close = meanClose.Values.ToList()[i];
                upperShadow.Add((double)(high - (open > close ? open : close)) / (double)(high - low));
                entity.Add((double)Math.Abs(close - open) / (double)(high - low));
                lowerShadow.Add((double)((open < close ? open : close) - low) / (double)(high - low));
            }
            var closeRise        = meanCloseRise.Skip(meanCloseRise.Count - takeN).ToList();
            var upperSw          = upperShadow.Skip(upperShadow.Count - takeN).ToList();
            var lowerSw          = lowerShadow.Skip(lowerShadow.Count - takeN).ToList();
            var ey               = entity.Skip(entity.Count - takeN).ToList();
            var closeRiseArray   = spc.spiltArray(meanCloseRise, takeN);
            var upperShadowArray = spc.spiltArray(upperShadow, takeN);
            var lowerShadowArray = spc.spiltArray(lowerShadow, takeN);
            var entityArray      = spc.spiltArray(entity, takeN);
            Dictionary <string, double> resultDic = new Dictionary <string, double>();

            for (int i = 1; i < meanClose.Count - 6; i++)
            {
                double distanceClose   = DataHelper.ListDistance(closeRise, closeRiseArray[i]);
                double distanceUpperSw = DataHelper.ListDistance(upperSw, upperShadowArray[i]);
                double distanceLowerSw = DataHelper.ListDistance(lowerSw, lowerShadowArray[i]);
                double distanceEntity  = DataHelper.ListDistance(ey, entityArray[i]);
                resultDic.Add(meanClose.Keys.ToList()[i], distanceClose * closeWeight + distanceEntity * entityWeight + distanceLowerSw * lowerShadowWeight + distanceUpperSw * upperShadowWeight);
            }
            return(resultDic.OrderBy(o => o.Value).Take(topN).Select(s => s.Key).ToList());
        }