/// <summary> /// 构造函数。 /// </summary> /// <param name="market">市场</param> /// <param name="startDate">开始时间</param> /// <param name="endDate">结束时间</param> public TreasuryBondFutures(string market, int startDate, int endDate = 0) { this.market = market.ToUpper(); this.startDate = startDate; if (endDate == 0) { endDate = startDate; } this.endDate = endDate; //对接口类进行初始化。 tdbSource = new TDBDataSource(mySource.IP, mySource.port, mySource.account, mySource.password); if (CheckConnection()) { Console.WriteLine("Connect Success!"); myTradeDays = new TradeDays(startDate, endDate); Console.WriteLine("Tradedays Collect!"); bondList = GetbondList(market); Console.WriteLine("bondList Collect!"); logName = DateTime.Now.ToString() + "_log.txt"; logName = logName.Replace("/", "_"); logName = logName.Replace(" ", "_"); logName = logName.Replace(":", "_"); StoreData(); } else { Console.WriteLine("Please Input Valid Parameters!"); } //工作完毕之后,关闭万德TDB数据库的连接。 //关闭连接 tdbSource.DisConnect(); }
/// <summary> /// 按日期遍历,添加期权信息。写入静态哈希表myOptionList。 /// </summary> /// <param name="startDate">开始日期</param> /// <param name="endDate">结束日期</param> private void GetOptionInformationList(int startDate, int endDate) { //定义交易日期的类。 TradeDays myTradeDays = new TradeDays(startDate, endDate); //从数据库表中获取信息,如果已经是完整信息就不需要连接万德数据库。 if (GetOptionListFromTable(startDate, endDate) == true) { return; } //按日期遍历,添加期权信息。 WindAPI w = new WindAPI(); w.start(); foreach (int today in myTradeDays.myTradeDays) { if (TradeDays.IsOptionExerciseDate(today) || today == myTradeDays.myTradeDays[myTradeDays.myTradeDays.Count - 1]) { WindData optionToday = w.wset("OptionChain", "date=" + today.ToString() + ";us_code=" + Configuration.underlyingAsset + ";option_var=;month=全部;call_put=全部"); object[] optionList = optionToday.data as object[]; int num = optionList.Length / 13; for (int i = 0; i < num; i++) { optionFormat option = new optionFormat(); string codeString = (string)optionList[i * 13 + 4 - 1]; option.optionCode = Convert.ToInt32(codeString.Substring(0, 8)); option.market = codeString.Substring(9, 2); option.optionName = (string)optionList[i * 13 + 5 - 1]; option.executeType = (string)optionList[i * 13 + 6 - 1]; option.strike = (double)optionList[i * 13 + 7 - 1]; option.optionType = (string)optionList[i * 13 + 9 - 1]; option.startDate = TradeDays.DateTimeToInt((DateTime)optionList[i * 13 + 10 - 1]); option.endDate = TradeDays.DateTimeToInt((DateTime)optionList[i * 13 + 11 - 1]); if (myOptionList.ContainsKey(option.optionCode) == false) { myOptionList.Add(option.optionCode, option); } } } } w.stop(); }
/// <summary> /// 核心存储函数。根据市场信息等存储商品期货数据。 /// </summary> public void StoreData() { foreach (var bond in bondList) { string tableName = "MarketData_" + bond.code + "_" + bond.market + "E"; int maxRecordDate = 0; foreach (int today in myTradeDays.myTradeDays) { if (today > bond.endDate) //如果在期货交割之后,不再进行记录 { break; } int yesterday = TradeDays.GetPreviousTradeDay(today); string todayDataBase = "TradeMarket" + (today / 100).ToString(); //string todayConnectString = "server=(local);database=" + todayDataBase + ";Integrated Security=true;"; //string todayConnectString = "server=192.168.38.217;database=" + todayDataBase + ";uid =sa;pwd=maoheng0;"; string todayConnectString = "server=145.146.30.152;database=" + todayDataBase + ";uid =spqhhqld;pwd=spqhhqld;"; if (SqlApplication.CheckDataBaseExist(todayDataBase, orignalConnectString) == false) { maxRecordDate = 0; } else if (yesterday / 100 != today / 100 || today == startDate || maxRecordDate == 0) { if (SqlApplication.CheckExist(todayDataBase, tableName, todayConnectString) == true) { maxRecordDate = MaxRecordDate(tableName, todayConnectString); } } if (maxRecordDate < today) //若没有记录数据,需要重新记录 //若数据存在,存储数据,否则需要跳过 { TDBReqFuture reqFuture = new TDBReqFuture(bond.contractName, today, today); TDBFutureAB[] futureABArr; TDBErrNo nErrInner = tdbSource.GetFutureAB(reqFuture, out futureABArr); if (futureABArr.Length == 0) { continue; } //string yesterdayDataBase = "TradeMarket" + (yesterday / 100).ToString(); //string yesterdayConnectString = "server=(local);database=" + yesterdayDataBase + ";Integrated Security=true;"; //string yesterdayConnectString = "server=192.168.38.217;database=" + yesterdayDataBase + ";uid =sa;pwd=maoheng0;";Security=true;"; if (SqlApplication.CheckDataBaseExist(todayDataBase, orignalConnectString) == false) //检测当日对应的数据库是否存在 { CreateDataBase(todayDataBase, orignalConnectString); } if (SqlApplication.CheckExist(todayDataBase, tableName, orignalConnectString) == false) { CreateTable(tableName, todayConnectString); } //if (yesterdayDataBase != todayDataBase) ////如果前一交易日所在月份和当日不同,检测上一个月的数据库是否存在 //{ // if (SqlApplication.CheckDataBaseExist(yesterdayDataBase, orignalConnectString) == false) // { // CreateDataBase(yesterdayDataBase, orignalConnectString); // } // if (SqlApplication.CheckExist(yesterdayDataBase, tableName, orignalConnectString) == false) // { // CreateTable(tableName, yesterdayConnectString); // } //} //判断数据是否已经存储,若数据存在,默认已经记录,仅记录数据条数,写入日志文档,靠人工来校对 // int alreadyRecord = CountRecordNumber(tableName, todayConnectString, today); bondShot[] dataList; dataList = ModifyData(futureABArr, bond.contractName, today); StoreDataDaily(tableName, todayConnectString, dataList); Console.WriteLine("Date:{0}, table:{1}, MaxRecordDate:{2}, Wind:{3}", today, tableName, maxRecordDate, futureABArr.Length); string log = "Date:" + today.ToString() + ", table:" + tableName + ", MaxRecordDate:" + maxRecordDate.ToString() + ", Wind:" + futureABArr.Length.ToString(); MyApplication.TxtWrite(logName, log); maxRecordDate = today; } else { Console.WriteLine("Date:{0}, table:{1}, MaxRecordDate:{2}", today, tableName, maxRecordDate); string log = "Date:" + today.ToString() + ", table:" + tableName + ", MaxRecordDate:" + maxRecordDate.ToString(); MyApplication.TxtWrite(logName, log); } } } }
/// <summary> /// 核心存储函数。根据市场信息等存储股票数据。 /// </summary> public void StoreData() { foreach (var stock in stockList) { string tableName = "MarketData_" + stock.code + "_" + stock.market; int maxRecordDate = 0; foreach (int today in myTradeDays.myTradeDays) { int yesterday = TradeDays.GetPreviousTradeDay(today); string todayDataBase = "TradeMarket" + (today / 100).ToString(); //string todayConnectString = "server=192.168.38.217;database=" + todayDataBase + ";uid =sa;pwd=maoheng0;"; //string todayConnectString = "server=(local);database=" + todayDataBase + ";Integrated Security=true;"; string todayConnectString = "server=192.168.38.209;database=" + todayDataBase + ";uid =sa;pwd=280514;"; if (SqlApplication.CheckDataBaseExist(todayDataBase, orignalConnectString) == false) { maxRecordDate = 0; } if (yesterday / 100 != today / 100 || today == startDate || maxRecordDate == 0) { if (SqlApplication.CheckDataBaseExist(todayDataBase, orignalConnectString) == true && SqlApplication.CheckExist(todayDataBase, tableName, orignalConnectString) == true && SqlApplication.CheckExist(todayDataBase, tableName, todayConnectString) == true) { maxRecordDate = MaxRecordDate(tableName, todayConnectString); } } if (maxRecordDate < today) //若没有记录数据,需要重新记录 //若数据存在,存储数据,否则需要跳过 { TDBReq reqTick = new TDBReq(stock.code + "." + stock.market, today, today); TDBTickAB[] tickArr; TDBErrNo nErrInner = tdbSource.GetTickAB(reqTick, out tickArr); if (tickArr.Length == 0) { continue; } if (SqlApplication.CheckDataBaseExist(todayDataBase, orignalConnectString) == false) //检测当日对应的数据库是否存在 { CreateDataBase(todayDataBase, orignalConnectString); } if (SqlApplication.CheckExist(todayDataBase, tableName, orignalConnectString) == false) { CreateTable(tableName, todayConnectString); } //判断数据是否已经存储,若数据存在,默认已经记录,仅记录数据条数,写入日志文档,靠人工来校对 // int alreadyRecord = CountRecordNumber(tableName, todayConnectString, today); stockShot[] dataList = ModifyData(tickArr, stock.code, today); StoreDataDaily(tableName, todayConnectString, dataList); Console.WriteLine("Date:{0}, table:{1}, MaxRecordDate:{2}, Wind:{3}", today, tableName, maxRecordDate, tickArr.Length); string log = "Date:" + today.ToString() + ", table:" + tableName + ", MaxRecordDate:" + maxRecordDate.ToString() + ", Wind:" + tickArr.Length.ToString(); MyApplication.TxtWrite(logName, log); maxRecordDate = today; } else { Console.WriteLine("Date:{0}, table:{1}, MaxRecordDate:{2}", today, tableName, maxRecordDate); string log = "Date:" + today.ToString() + ", table:" + tableName + ", MaxRecordDate:" + maxRecordDate.ToString(); MyApplication.TxtWrite(logName, log); } } } }