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"); }
public EventOfIndex50(int startDate, int endDate) { GetStockChange change = new GetStockChange(); changeList = GetStockChange.changeList; myTradeDays = new TradeDays(startDate, endDate); computePosition(startDate, endDate); recordNetValue(); }
public GetStockData(string code, int startDate, int endDate = 0) { if (endDate == 0) { endDate = startDate; } this.code = code; stockId = code.Substring(0, 6); stockMarket = code.Substring(7, 2); this.startDate = startDate; this.endDate = endDate; myTradeDays = new TradeDays(startDate, endDate); GetDataFromSql(IP, account, password); }
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> /// <param name="date">指定日期</param> public GetStocks(int startDate, int endDate, string indexName) { this.startDate = startDate; this.endDate = endDate; this.indexName = indexName; if (myTradeDays == null) { myTradeDays = new TradeDays(startDate, endDate); } if (stockList == null) { w.start(); stockList = getExitsStocks(); } }
public GetStockBonus(string code, int startDate, int endDate = 0) { if (endDate == 0) { endDate = startDate; } this.code = code; stockId = code.Substring(0, 6); stockMarket = code.Substring(7, 2); this.startDate = startDate; this.endDate = endDate; myTradeDays = new TradeDays(startDate, endDate); InitializeWind(); GetBonusFromWind(); }
/// <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); }
private void computePosition(int startDate, int endDate) { foreach (var item in changeList) { if (item.Key < startDate || item.Key > endDate) { continue; } SortedDictionary <string, List <stockChangeFormat> > stockChangeList = new SortedDictionary <string, List <stockChangeFormat> >(); SortedDictionary <string, SortedDictionary <int, stockDataFormat[]> > stockDataList = new SortedDictionary <string, SortedDictionary <int, stockDataFormat[]> >(); int date = item.Key; stockModifyList list = item.Value; List <stockPosition> stockList = new List <stockPosition>(); int firstDate = Math.Max(TradeDays.GetNTradeDaysBefore(date, 15), startDate); int lastDate = Math.Min(TradeDays.GetNTradeDaysLater(date, 15), endDate); TradeDays myDays = new TradeDays(firstDate, lastDate); foreach (string stockIn in list.stockIn) { stockPosition stock = new stockPosition(); stock.code = stockIn; stock.position = 1; stockList.Add(stock); } foreach (string stockOut in list.stockOut) { stockPosition stock = new stockPosition(); stock.code = stockOut; stock.position = -1; stockList.Add(stock); } foreach (stockPosition stock in stockList) { GetStockBonus bonus = new GetStockBonus(stock.code, firstDate, lastDate); GetStockData data = new GetStockData(stock.code, firstDate, lastDate); stockDataList.Add(stock.code, data.stockData); stockChangeList.Add(stock.code, bonus.stockChangeList); } //先确定权重 Dictionary <string, double> weight = new Dictionary <string, double>(); foreach (int today in myDays.myTradeDays) { List <stockPosition> stockListToday = new List <stockPosition>(); foreach (stockPosition stock in stockList) { List <stockChangeFormat> myChange = stockChangeList[stock.code]; double position = stock.position; double cash = stock.cash; for (int i = 0; i < myChange.Count; i++) { if (myChange[i].date <= today && myChange[i].date > firstDate) { cash += position * myChange[i].bonus; position *= myChange[i].divisor; } } stockListToday.Add(new stockPosition(stock.code, today, position, cash)); } positionList.Add(today, stockListToday); //计算净值曲线 double[] todayNetValue = new double[14402];//一天分成14402秒 //先计算权重 if (today == firstDate) { double stockInNum = 0; double stockOutNum = 0; Dictionary <string, double> stockOpen = new Dictionary <string, double>(); foreach (stockPosition myStock in stockListToday) { if (stockDataList.ContainsKey(myStock.code) && stockDataList[myStock.code].ContainsKey(today)) { double[] data = dataModify(stockDataList[myStock.code][today]); stockOpen.Add(myStock.code, data[0]); } else { stockOpen.Add(myStock.code, 0); } } foreach (stockPosition myStock in stockListToday) { if (myStock.position > 0 && stockOpen[myStock.code] > 0) { stockInNum += 1; } if (myStock.position < 0 && stockOpen[myStock.code] > 0) { stockOutNum += 1; } } foreach (stockPosition myStock in stockListToday) { if (myStock.position > 0 && stockOpen[myStock.code] > 0) { weight.Add(myStock.code, 1.0 / stockInNum / stockOpen[myStock.code]); } if (myStock.position < 0 && stockOpen[myStock.code] > 0) { weight.Add(myStock.code, 1.0 / stockOutNum / stockOpen[myStock.code]); } } } foreach (stockPosition myStock in stockListToday) { if (weight.ContainsKey(myStock.code) == false) { continue; } stockDataFormat[] myStockData = (stockDataList.ContainsKey(myStock.code) && stockDataList[myStock.code].ContainsKey(today))? stockDataList[myStock.code][today]:new stockDataFormat[14402]; double[] data = dataModify(myStockData); for (int i = 0; i < 14402; i++) { todayNetValue[i] += (myStock.position * data[i] + myStock.cash) * weight[myStock.code]; } } netValue.Add(today, todayNetValue); } } }