//逐笔委托 public TDBErrNo GetOrder(TDBReq reqOrder, out TDBOrder[] tdbOrder) { TDBErrNo nVerifyRet = SimpleVerifyReqInput(reqOrder); tdbOrder = new TDBOrder[0]; if (nVerifyRet != TDBErrNo.TDB_SUCCESS) { return(nVerifyRet); } LibTDBWrap.TDBDefine_ReqTransaction reqAPIOrder = reqOrder.ToAPIReqTransaction(); IntPtr pUnmanagedReqAPIOrder = LibWrapHelper.CopyStructToGlobalMem(reqAPIOrder, typeof(LibTDBWrap.TDBDefine_ReqTransaction)); IntPtr ppOrder = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); IntPtr pCount = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32))); int nRet = LibTDBWrap.TDB_GetOrder(m_hTdb, pUnmanagedReqAPIOrder, ppOrder, pCount); IntPtr pOrder = (IntPtr)Marshal.PtrToStructure(ppOrder, typeof(IntPtr)); int nCount = (Int32)Marshal.PtrToStructure(pCount, typeof(Int32)); if ((UInt64)pOrder != 0 && nCount > 0 && nRet == 0) { tdbOrder = new TDBOrder[nCount]; int nElemLen = Marshal.SizeOf(typeof(LibTDBWrap.TDBDefine_Order)); for (int i = 0; i < nCount; i++) { IntPtr pCurRecord = (IntPtr)((UInt64)pOrder + (UInt64)(nElemLen * i)); LibTDBWrap.TDBDefine_Order apiOrder = (LibTDBWrap.TDBDefine_Order)Marshal.PtrToStructure(pCurRecord, typeof(LibTDBWrap.TDBDefine_Order)); tdbOrder[i] = new TDBOrder(); tdbOrder[i].FromAPIOrder(ref apiOrder); } } else { //如果网络连接断掉,则关闭连接 if (nRet == (int)TDBErrNo.TDB_NETWORK_ERROR) { DisConnect(); } } if ((UInt64)pOrder != 0) { LibTDBWrap.TDB_Free(pOrder); } Marshal.FreeHGlobal(pUnmanagedReqAPIOrder); Marshal.FreeHGlobal(ppOrder); Marshal.FreeHGlobal(pCount); return((TDBErrNo)nRet); }
//获取普通股票的行情数据(不带买卖盘),本接口不支持期货,对于期货(CF市场和商品期货),需要调用GetFuture或GetFutureAB public TDBErrNo GetTick(TDBReq reqTick, out TDBTick[] tdbTick) { TDBErrNo nVerifyRet = SimpleVerifyReqInput(reqTick); tdbTick = new TDBTick[0]; if (nVerifyRet != TDBErrNo.TDB_SUCCESS) { return(nVerifyRet); } LibTDBWrap.TDBDefine_ReqTick reqAPITick = reqTick.ToAPIReqTick(); IntPtr pUnmanagedReqAPITick = LibWrapHelper.CopyStructToGlobalMem(reqAPITick, typeof(LibTDBWrap.TDBDefine_ReqTick)); IntPtr ppTick = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); IntPtr pCount = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32))); int nRet = LibTDBWrap.TDB_GetTick(m_hTdb, pUnmanagedReqAPITick, ppTick, pCount); IntPtr pTick = (IntPtr)Marshal.PtrToStructure(ppTick, typeof(IntPtr)); int nCount = (Int32)Marshal.PtrToStructure(pCount, typeof(Int32)); if ((UInt64)pTick != 0 && nCount > 0 && nRet == 0) { tdbTick = new TDBTick[nCount]; int nElemLen = Marshal.SizeOf(typeof(LibTDBWrap.TDBDefine_Tick)); for (int i = 0; i < nCount; i++) { IntPtr pCurRecord = (IntPtr)((UInt64)pTick + (UInt64)(nElemLen * i)); LibTDBWrap.TDBDefine_Tick apiTick = (LibTDBWrap.TDBDefine_Tick)Marshal.PtrToStructure(pCurRecord, typeof(LibTDBWrap.TDBDefine_Tick)); tdbTick[i] = new TDBTick(); tdbTick[i].FromAPITick(ref apiTick); } } else { //如果网络连接断掉,则关闭连接 if (nRet == (int)TDBErrNo.TDB_NETWORK_ERROR) { DisConnect(); } } if ((UInt64)pTick != 0) { LibTDBWrap.TDB_Free(pTick); } Marshal.FreeHGlobal(pUnmanagedReqAPITick); Marshal.FreeHGlobal(ppTick); Marshal.FreeHGlobal(pCount); return((TDBErrNo)nRet); }
public int Start() { if (_isRunning == true) { App.Logger.Info("TDB数据服务:已启动!"); return(1); } try { tdbSource = new TDBDataSource(_config.Ip, _config.Port.ToString(), _config.Username, _config.Password, 30, 1, 1); TDBLoginResult loginRes; TDBErrNo nErr = tdbSource.Connect(out loginRes); if (nErr == TDBErrNo.TDB_OPEN_FAILED) { App.Logger.Error(string.Format("TDB数据服务:连接失败,错误代码{0}", nErr)); } else { App.Logger.Info("TDB数据服务:登陆成功!"); TDBCode[] codeArr1; //输出全部市场的代码表 TDBErrNo nRetInner = tdbSource.GetCodeTable("", out codeArr1); if (nRetInner == TDBErrNo.TDB_SUCCESS) { var mcList = new List <MarketCode>(); foreach (var code in codeArr1) { MarketCode mc = new MarketCode() { WindCode = code.m_strWindCode, Market = code.m_strMarket, Code = code.m_strCode, EnName = code.m_strENName, CnName = code.m_strCNName, Type = code.m_nType }; mcList.Add(mc); } _marketCodes.AddRange(mcList); } } } catch (Exception ex) { App.Logger.Error(ex); App.Logger.Error("TDB数据服务:初始化错误!" + ex.Message); return(100); } return(0); }
//获取K线 public TDBErrNo GetKLine(TDBReqKLine reqKLine, out TDBKLine[] tdbKLine) { TDBErrNo nVerifyRet = SimpleVerifyReqInput(reqKLine); tdbKLine = new TDBKLine[0]; if (nVerifyRet != TDBErrNo.TDB_SUCCESS) { return(nVerifyRet); } LibTDBWrap.TDBDefine_ReqKLine reqKLineInner = reqKLine.ToAPIReqKLine(); IntPtr pUnmanagedAPIReqK = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LibTDBWrap.TDBDefine_ReqKLine))); Marshal.StructureToPtr(reqKLineInner, pUnmanagedAPIReqK, false); IntPtr ppKLine = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); IntPtr pCount = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32))); int nRet = LibTDBWrap.TDB_GetKLine(m_hTdb, pUnmanagedAPIReqK, ppKLine, pCount); IntPtr PKLine = (IntPtr)Marshal.PtrToStructure(ppKLine, typeof(IntPtr)); int nCount = (int)Marshal.PtrToStructure(pCount, typeof(Int32)); if ((UInt64)PKLine != 0 && nRet == 0 && nCount > 0) { tdbKLine = new TDBKLine[nCount]; int nElemLen = Marshal.SizeOf(typeof(LibTDBWrap.TDBDefine_KLine)); for (int i = 0; i < nCount; i++) { LibTDBWrap.TDBDefine_KLine apiKLine = (LibTDBWrap.TDBDefine_KLine)Marshal.PtrToStructure((IntPtr)((UInt64)PKLine + (UInt64)(nElemLen * i)), typeof(LibTDBWrap.TDBDefine_KLine)); tdbKLine[i] = new TDBKLine(); tdbKLine[i].FromAPIKLine(ref apiKLine); } } else { //如果网络连接断掉,则关闭连接 if (nRet == (int)TDBErrNo.TDB_NETWORK_ERROR) { DisConnect(); } } if ((UInt64)PKLine != 0) { LibTDBWrap.TDB_Free(PKLine); } Marshal.FreeHGlobal(pUnmanagedAPIReqK); Marshal.FreeHGlobal(ppKLine); Marshal.FreeHGlobal(pCount); return((TDBErrNo)nRet); }
//获取某个市场或者全部市场的代码表。strMarket取值: "SH"、"SZ"、"CF"、"SHF"、"CZC"、"DCE",全部市场:"" public TDBErrNo GetCodeTable(string strMarket, out TDBCode[] codeArr) { TDBErrNo nVerifyRet = SimpleVerifyReqInput(strMarket); codeArr = new TDBCode[0]; if (nVerifyRet != TDBErrNo.TDB_SUCCESS) { return(nVerifyRet); } int nArrLen = 128; byte[] btMarketArr = LibWrapHelper.String2AnsiArr(strMarket, nArrLen); IntPtr pUnmanagedMarket = Marshal.AllocHGlobal(btMarketArr.Length); Marshal.Copy(btMarketArr, 0, pUnmanagedMarket, nArrLen); IntPtr ppCodeTable = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); IntPtr pCount = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32))); int nRet = LibTDBWrap.TDB_GetCodeTable(m_hTdb, pUnmanagedMarket, ppCodeTable, pCount); IntPtr pCodeTable = (IntPtr)Marshal.PtrToStructure(ppCodeTable, typeof(IntPtr)); int nCount = (Int32)Marshal.PtrToStructure(pCount, typeof(Int32)); if (nRet == 0 && (UInt64)pCodeTable != 0 && nCount > 0) { codeArr = new TDBCode[nCount]; int nElemLen = Marshal.SizeOf(typeof(LibTDBWrap.TDBDefine_Code)); for (int i = 0; i < nCount; i++) { LibTDBWrap.TDBDefine_Code apiCode = (LibTDBWrap.TDBDefine_Code)Marshal.PtrToStructure((IntPtr)((UInt64)pCodeTable + (UInt64)(nElemLen * i)), typeof(LibTDBWrap.TDBDefine_Code)); codeArr[i] = new TDBCode(); codeArr[i].FromAPICode(ref apiCode); } } else { //如果网络连接断掉,则关闭连接 if (nRet == (int)TDBErrNo.TDB_NETWORK_ERROR) { DisConnect(); } } if ((UInt16)pCodeTable != 0) { LibTDBWrap.TDB_Free(pCodeTable); } Marshal.FreeHGlobal(pUnmanagedMarket); Marshal.FreeHGlobal(ppCodeTable); Marshal.FreeHGlobal(pCount); return((TDBErrNo)nRet); }
/// <summary> /// 判断TDB数据库是否连接成功。 /// </summary> /// <returns>返回是否连接成功。</returns> public bool CheckConnection() { TDBLoginResult loginRes; TDBErrNo nErr = tdbSource.Connect(out loginRes); //输出登陆结果 if (nErr == TDBErrNo.TDB_OPEN_FAILED) { Console.WriteLine("open failed, reason:{0}", loginRes.m_strInfo); Console.WriteLine(); return(false); } return(true); }
//如果查询的代码不存在,连接已经断掉、未连接,则返回null public TDBCode GetCodeInfo(string strWindCode, string strMarketKey) { TDBErrNo nVerifyRet = SimpleVerifyReqInput(strWindCode); if (nVerifyRet != TDBErrNo.TDB_SUCCESS) { return(null); } int nMaxWindCodeLen = 64; int nMaxmarketLen = 48; IntPtr pszWindCode = Marshal.AllocHGlobal(nMaxWindCodeLen); byte[] btWindCode = LibWrapHelper.String2AnsiArr(strWindCode, nMaxWindCodeLen); btWindCode[btWindCode.Length - 1] = 0; Marshal.Copy(btWindCode, 0, pszWindCode, btWindCode.Length); IntPtr pszMarket = Marshal.AllocHGlobal(nMaxmarketLen); byte[] btMarket = LibWrapHelper.String2AnsiArr(strMarketKey, nMaxmarketLen); btMarket[btMarket.Length - 1] = 0; Marshal.Copy(btMarket, 0, pszMarket, btMarket.Length); IntPtr pCode = LibTDBWrap.TDB_GetCodeInfo(m_hTdb, pszWindCode, pszMarket); Marshal.FreeHGlobal(pszWindCode); Marshal.FreeHGlobal(pszMarket); if ((UInt64)pCode != 0) { LibTDBWrap.TDBDefine_Code apiCode = (LibTDBWrap.TDBDefine_Code)Marshal.PtrToStructure(pCode, typeof(LibTDBWrap.TDBDefine_Code)); TDBCode tdbCode = new TDBCode(); tdbCode.FromAPICode(ref apiCode); return(tdbCode); } else { return(null); } }
/// <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); } } } }