//初始化股票基本信息 public void Init(string file) { filename = file; StreamReader sr = new StreamReader(file, Encoding.Default); String line; string[] s; if ((line = sr.ReadLine()) != null) { s = line.Split(' '); int size = s.Length; if (s.Length < 2) //从python过来得到的数据 { code = file.Substring(file.Length - 10, 6); name = code; } else //从通达信过来的数据 { code = 's' + s[0]; name = s[1]; } } InitItem(); UtilLog.AddInfo(TAG, code + " init item completed."); }
public static StockData GetStock(string code) { StockData stock = (StockData)allstock[code]; if (stock.items == null) { stock = StockSQL.GetStockDetail_2(code, stock.name); allstock[code] = stock; UtilLog.AddInfo("StockApp", "Stock " + code + " initialized."); } return(stock); }
//得到分析时间段股票本身平均涨幅 public static double GetAverageGrowth(int startdate, int enddate) { if (startdate < Util.getIntDate(Util.getDate(StockApp.getAnalysisStartDate).AddMonths(StockApp.STOCK_START_DATE_SHITE_MONTH))) { startdate = Util.getIntDate(Util.getDate(StockApp.getAnalysisStartDate).AddMonths(StockApp.STOCK_START_DATE_SHITE_MONTH)); } string sql = "select isnull(max(date),0) from stock_full where [date] < " + startdate + ";"; object ret = db.GetFirstRow(sql)[0]; int minid = Convert.ToInt32(ret); if (enddate < startdate) { enddate = startdate; } sql = "select isnull(max(date),0) from stock_full where [date] <= " + enddate + ";"; ret = db.GetFirstRow(sql)[0]; int maxid = Convert.ToInt32(db.GetFirstRow(sql)[0]); UtilLog.AddInfo(TAG, "Calculate ave growth start"); //pivot 用法 /* * select code, "20151231", "20180126" from * ( * select code,[date], [end] from stock_Full where [DATE] in ('20151231', '20180126') * and code in (select code from stock_full group by code having MIN([date])< '20151231') * ) T1 pivot (MAX([end]) for [date] in ("20151231", "20180126")) as tp; */ sql = "select code, "; sql += " isnull(SUM(CASE [date] WHEN " + minid + " THEN [end] END),0) AS 'start', "; sql += " isnull(SUM(CASE [date] WHEN " + maxid + " THEN [end] END),0) AS 'end' "; sql += " from ( "; sql += " select code,[date], [end] from stock_Full where [DATE] in ('" + minid + "', '" + maxid + "') "; sql += " and code in (select code from stock_full group by code having MIN([date])< '" + minid + "') "; sql += " ) T1 group by code "; System.Data.DataTable dt = db.GetTable(sql); int count = 0; double retvalue = 0; for (int i = 0; i < dt.Rows.Count; i++) { System.Data.DataRow row = dt.Rows[i]; double start = Convert.ToDouble(row["start"]); double end = Convert.ToDouble(row["end"]); if (start < 0.1 || end < 0.1) { continue; } count++; retvalue += end / start; } try { return(retvalue / count); } catch (Exception e) { Console.WriteLine(e.ToString()); return(0); } finally { UtilLog.AddInfo(TAG, "Calculate ave growth end"); } }