private void recordNetValue() { DataTable dt = new DataTable(); dt.Columns.Add("日期", Type.GetType("System.Int32")); dt.Columns.Add("序号", Type.GetType("System.Int32")); dt.Columns.Add("净值", Type.GetType("System.Double")); int index = 0; int lastDay = 0; string str = ""; foreach (var item in netValue) { int today = item.Key; double[] net = item.Value; if (index == 0) { str = (today / 100).ToString(); } for (int i = 0; i < net.Count(); i = i + 60) { index += 1; dt.Rows.Add(new object[] { today, index, net[i] }); } if (lastDay != 0 && TradeDays.GetNextTradeDay(lastDay) < today) { index = 0; } lastDay = today; } string str2 = DateTime.Now.ToString("yyMMddHH"); CsvApplication.SaveCSV(dt, "netvalue_" + str + "_" + str2 + ".csv", "new"); }
private void GetStockList() { TradeDays myTradeDays = new TradeDays(20130601, 20160626); int[] changeDate = new int[7] { 20130628, 20131213, 20140613, 20141212, 20150612, 20151211, 20160608 }; w.start(); foreach (int date in changeDate) { int today = date; int tomorrow = TradeDays.GetNextTradeDay(date); string todayStr = DateTime.ParseExact(today.ToString(), "yyyyMMdd", null).ToString("yyyy-MM-dd"); string tomorrowStr = DateTime.ParseExact(tomorrow.ToString(), "yyyyMMdd", null).ToString("yyyy-MM-dd"); List <string> todayList = new List <string>(); List <string> tomorrowList = new List <string>(); List <string> stockIn = new List <string>(); List <string> stockOut = new List <string>(); WindData wd = w.wset("sectorconstituent", "date=" + todayStr + ";windcode=" + indexName); object[] stockList = wd.data as object[]; int num = stockList.Length / 3; for (int i = 0; i < num; i++) { todayList.Add(Convert.ToString(stockList[i * 3 + 1])); } wd = w.wset("sectorconstituent", "date=" + tomorrowStr + ";windcode=" + indexName); stockList = wd.data as object[]; num = stockList.Length / 3; for (int i = 0; i < num; i++) { tomorrowList.Add(Convert.ToString(stockList[i * 3 + 1])); } foreach (string code in tomorrowList) { if (todayList.Contains(code) == false) { stockIn.Add(code); } } foreach (string code in todayList) { if (tomorrowList.Contains(code) == false) { stockOut.Add(code); } } stockModifyList myList = new stockModifyList(today, stockIn, stockOut); changeList.Add(today, myList); } }
/// <summary> /// 根据程序运行日期,获取截止至昨日的指数成分股的列表 /// </summary> /// <returns>指数成分股列表</returns> private Dictionary <string, stockFormat> getExitsStocks() { Dictionary <string, stockFormat> list = new Dictionary <string, stockFormat>(); int yesterday = TradeDays.GetPreviousTradeDay(Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"))); foreach (int day in myTradeDays.myTradeDays) { string todayStr = DateTime.ParseExact(day.ToString(), "yyyyMMdd", null).ToString("yyyy-MM-dd"); if (day <= yesterday) { WindData wd = w.wset("sectorconstituent", "date=" + todayStr + ";windcode=" + indexName); object[] stockList = wd.data as object[]; int num = stockList.Length / 3; for (int i = 0; i < num; i++) { stockFormat myStock = new stockFormat(); myStock.code = Convert.ToString(stockList[i * 3 + 1]); myStock.name = (string)stockList[i * 3 + 2]; if (list.ContainsKey(myStock.code) == false) { myStock.existsDate = new List <int>(); myStock.existsDate.Add(day); myStock.existsDate.Add(day); list.Add(myStock.code, myStock); } else { int enterDate = list[myStock.code].existsDate[list[myStock.code].existsDate.Count() - 2]; int quitDate = list[myStock.code].existsDate[list[myStock.code].existsDate.Count() - 1]; if (day > TradeDays.GetNextTradeDay(quitDate)) { list[myStock.code].existsDate.Add(day); list[myStock.code].existsDate.Add(day); } else { list[myStock.code].existsDate[list[myStock.code].existsDate.Count() - 1] = day; } } } } } //若计算时间不包括历史时间,必须读取昨日的数据作为基准 if (list.Count == 0) { string yesterdayStr = DateTime.ParseExact(yesterday.ToString(), "yyyyMMdd", null).ToString("yyyy-MM-dd"); List <stockFormat> myList = new List <stockFormat>(); WindData wd = w.wset("sectorconstituent", "date=" + yesterdayStr + ";windcode=" + indexName); object[] stockList = wd.data as object[]; int num = stockList.Length / 3; for (int i = 0; i < num; i++) { stockFormat myStock = new stockFormat(); myStock.code = Convert.ToString(stockList[i * 3 + 1]).Substring(0, 6); myStock.name = (string)stockList[i * 3 + 2]; myStock.existsDate = new List <int>(); myStock.existsDate.Add(yesterday); myStock.existsDate.Add(yesterday); } } //从stockModify.csv中读取指数成分股变动股票和日期 DataTable dt = CsvApplication.OpenCSV("stockModify.csv"); SortedDictionary <int, List <stockModify> > stockModifyList = new SortedDictionary <int, List <stockModify> >(); for (int i = 0; i < dt.Rows.Count; i++) { stockModify stock = new stockModify(); stock.code = dt.Rows[i][0].ToString(); stock.name = dt.Rows[i][1].ToString(); stock.date = Convert.ToInt32(dt.Rows[i][2].ToString()); stock.direction = dt.Rows[i][3].ToString(); if (stockModifyList.ContainsKey(stock.date)) { stockModifyList[stock.date].Add(stock); } else { List <stockModify> stockList0 = new List <stockModify>(); stockList0.Add(stock); stockModifyList.Add(stock.date, stockList0); } } //根据文档对我的数据进行处理,如果变动时间大于昨日时间,需要对我的股票列表进行修正 int maxDate = yesterday; foreach (var stockList in stockModifyList) { if (stockList.Value[0].date > yesterday) { foreach (var stock in stockList.Value) { if (stock.direction == "out") { if (list.ContainsKey(stock.code) == true) { list[stock.code].existsDate[list[stock.code].existsDate.Count() - 1] = stock.date; } } if (stock.direction == "in") { if (stockList.Value[0].date > maxDate) { maxDate = stockList.Value[0].date; } if (list.ContainsKey(stock.code) == true) { list[stock.code].existsDate.Add(stock.date); list[stock.code].existsDate.Add(stock.date); } else { stockFormat myStock = new stockFormat(); myStock.name = stock.name; myStock.code = stock.code; myStock.existsDate = new List <int>(); myStock.existsDate.Add(stock.date); myStock.existsDate.Add(stock.date); } } } } } //按照回测日期给股票列表进行修正 foreach (var stock in list) { if (stock.Value.existsDate[stock.Value.existsDate.Count() - 1] == yesterday || stock.Value.existsDate[stock.Value.existsDate.Count() - 1] == maxDate) { stock.Value.existsDate[stock.Value.existsDate.Count() - 1] = endDate; } } return(list); }