// TDBEasyDemo.cpp : 定义控制台应用程序的入口点。
//
#include "tdf/TDBAPI.h"
#include "iostream"
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#ifdef NDEBUG
#define AssertEx(expr) expr;
#else
#define AssertEx(expr) {int n = (int)(expr); assert(n);}
#endif
#define ELEMENT_COUNT(arr) (sizeof(arr)/sizeof(*arr))
void GetKData(THANDLE hTdb, char* szCode, char* szMarket, int nBeginDate, int nEndDate, int nCycle, int nUserDef, int nCQFlag, int nAutoComplete);
void GetTickData(THANDLE hTdb, const char* szCode, const char* szMarket, int nDate);//带买卖盘的tick
void GetTransaction(THANDLE hTdb, char* szCode, char* szMarket, int nDate); //逐笔成交
void GetOrder(THANDLE hTdb, char* szCode, char* szMarket, int nDate);//逐笔委托
void GetOrderQueue(THANDLE hTdb, char* szCode, char* szMarket, int nDate);//委托队列
void UseEZFFormula(THANDLE hTdb);
void GetCodeTable(THANDLE hTdb, char* szMarket);
int test(THANDLE hTdb);
void writeCode();
using namespace acsy;
std::shared_ptr connect();
std::string int2str(int n)
{
char szBuf[32];
_snprintf(szBuf, sizeof(szBuf)/sizeof(szBuf[0]), "%d", n);
return std::string(szBuf);
}
std::string array2str(const int* arr, int len)
{
std::string str;
for (int i=0; ichCode);
printf("市场代码 chWindCode:%s \n", pCode->chMarket);
printf("证券中文名称 chWindCode:%s \n", pCode->chCNName);
printf("证券英文名称 chWindCode:%s \n", pCode->chENName);
printf("证券类型 chWindCode:%d \n", pCode->nType);
}
//TDB_GetCodeTable
{
//GetCodeTable(hTdb, "SH-2-0");
}
//---//
{
// GetKData(hTdb, "000001.sz", "SZ-2-0", 20150910, 20150915, CYC_MINUTE, 0, 0, 0);
// GetKData(hTdb, "000001.sz", "SZ-2-0", 20150110, 20150915, CYC_DAY, 0, 0, 0);
// GetKData(hTdb, "000001.sz", "SZ-2-0", 20150910, 20150915, CYC_SECOND, 0, 0, 1);
//GetTransaction(hTdb, "000001.sz", "SZ-2-0", 20151913);
int ic = 0;
//for (ic = 0; ic < g_codeCount; ++ic )
//{
// GetTickData(hTdb, g_pCodetable[ic].chCode, g_pCodetable[ic].chMarket, 20150910);//带买卖盘的tick
// if ( ic > 10)
// break;
//}
ic = 0;
std::ifstream ifs(R"(E:\data\tdb\code\all_stock.csv)");
while (ifs && !ifs.eof())
{
std::string scode;
std::getline(ifs, scode);
if (scode.length() < 9)
continue;
std::string mkt = scode.substr(7, 2)+ "-2-0";
for (int iday = 20151001; iday < 20160401; ++iday)
{
if (iday % 100 > 31)
continue;
if (iday % 10000 > 1231)
continue;
GetTickData(hTdb, scode.c_str(), mkt.c_str(), iday);//带买卖盘的tick
GetTransaction(hTdb, (char*)scode.c_str(), (char*)mkt.c_str(), iday);
}
GetKData(hTdb, (char*)scode.c_str(), (char*)mkt.c_str(), 20151001, 20160401, CYC_MINUTE, 0, 0, 0);
//if (++ic > 10)break;
}
//GetOrder(hTdb, "000001.sz", "SZ-2-0", 20150910);//带买卖盘的tick
//GetOrderQueue(hTdb, "000001.sz", "SZ-2-0", 20150910);//带买卖盘的tick
//UseEZFFormula(hTdb);
}
//释放
if (g_pCodetable)
TDB_Free(g_pCodetable);
printf("输入任意键结束程序");
getchar();
int nRet2 = -1;
if (hTdb)
nRet2 = TDB_Close(hTdb);
return 0;
}
class NeedFreeTick
{
public:
NeedFreeTick(void *tick) :tick_(tick){}
~NeedFreeTick(){
TDB_Free(tick_);
}
private:
void *tick_ = nullptr;
};
int test(THANDLE hTdb)
{
while (1)
{
//请求K线
TDBDefine_ReqKLine* req = new TDBDefine_ReqKLine;
strncpy(req->chCode, "000001.SZ", ELEMENT_COUNT(req->chCode));
strncpy(req->chMarketKey, "SZ-2-0", ELEMENT_COUNT(req->chMarketKey));
req->nCQFlag = (REFILLFLAG)0;//除权标志,由用户定义
req->nBeginDate = 20151201;//开始日期
req->nEndDate = 20151205;//结束日期
req->nBeginTime = 0;//开始时间
req->nEndTime = 0;//结束时间
req->nCycType = (CYCTYPE)CYC_DAY;
req->nCycDef = 0;
//返回结构体指针
TDBDefine_KLine* kLine = NULL;
//返回数
int pCount;
//API请求K线
int nRet = TDB_GetKLine(hTdb,req,&kLine,&pCount);
delete req;
req = NULL;
if (nRet == TDB_NETWORK_TIMEOUT || nRet == TDB_NETWORK_ERROR)
{
return nRet;
}
}
return 0;
}
//请求代码表
void GetCodeTable(THANDLE hTdb, char* szMarket)
{
bool outPutTable = false;
int ret = TDB_GetCodeTable(hTdb, szMarket, &g_pCodetable, &g_codeCount);
if (ret == TDB_NO_DATA)
{
printf("无代码表!");
return;
}
printf("---------------------------Code Table--------------------\n");
printf("收到代码表项数:%d,\n\n", g_codeCount);
//输出
if(outPutTable)
{
for (int i = 0; i10 ? 10 : g_codeCount;
for (; i > 0 ;i--)
{
printf("交易所代码 chWindCode:%s \n", g_pCodetable[i-1].chCode);
printf("市场代码 chWindCode:%s \n", g_pCodetable[i-1].chMarket);
printf("证券中文名称 chWindCode:%s \n", g_pCodetable[i-1].chCNName);
printf("证券英文名称 chWindCode:%s \n", g_pCodetable[i-1].chENName);
printf("证券类型 chWindCode:%d \n", g_pCodetable[i-1].nType);
printf("----------------------------------------\n");
}
}
}
void GetKData(THANDLE hTdb, char* szCode, char* szMarket, int nBeginDate, int nEndDate, int nCycle, int nUserDef, int nCQFlag, int nAutoComplete)
{
//请求K线
TDBDefine_ReqKLine* req = new TDBDefine_ReqKLine;
strncpy(req->chCode, szCode, ELEMENT_COUNT(req->chCode));
strncpy(req->chMarketKey, szMarket, ELEMENT_COUNT(req->chMarketKey));
req->nCQFlag = (REFILLFLAG)nCQFlag;//除权标志,由用户定义
req->nBeginDate = nBeginDate;//开始日期
req->nEndDate = nEndDate;//结束日期
req->nBeginTime = 0;//开始时间
req->nEndTime = 0;//结束时间
req->nCycType = (CYCTYPE)nCycle;
req->nCycDef = 0;
namespace fs = boost::filesystem;
std::string dir = R"(E:\data\tdb\kline)";
std::string floder = util::path_append(dir, szCode);
bool result = boost::filesystem::is_directory(floder);
if (result == false)
{
try{ fs::create_directories(floder); }
catch (std::exception& e)
{
std::cout << "exception!!!![" << e.what() << "]" << std::endl;
}
}
std::string filenametemp = "kline_" + (string)szCode + ".csv";
std::string filename1 = util::path_append(floder, filenametemp);
bool hasdir = boost::filesystem::exists(filename1);
if (hasdir == true)
{
return;
}
std::ofstream fout(filename1, std::ios::app);
//返回结构体指针
TDBDefine_KLine* kLine = NULL;
//返回数
int pCount;
//API请求K线
TDB_GetKLine(hTdb,req,&kLine,&pCount);
NeedFreeTick SS(kLine);
if (pCount <= 0)
{
return;
}
delete req;
req = NULL;
printf("---------------------------K Data--------------------\n");
printf("数据条数:%d,打印 1/100 条\n\n",pCount);
for(int i=0;iexecute("begin transaction");
namespace fs = boost::filesystem;
stringstream stream;
stream << nDate;
std::string date = stream.str();
if (date == "")
{
return;
}
std::string dir = R"(E:\data\tdb\tick)";
std::string floder = util::path_append(dir, date);
bool result = boost::filesystem::is_directory(floder);
if (result == false)
{
try{ fs::create_directories(floder); }
catch (std::exception& e)
{
std::cout << "exception!!!![" << e.what() << "]" << std::endl;
}
}
std::string filenametemp = "snapshot_" + (string)szCode + ".csv";
std::string filename1 = util::path_append(floder, filenametemp);
bool ishas = boost::filesystem::exists(filename1);
if (ishas == true)
return;
int ret = TDB_GetTick(hTdb, &req, &pTick, &pCount);
NeedFreeTick needfree(pTick);
if (pCount <= 0)
{
return;
}
printf("---------------------------------------Tick Data------------------------------------------\n");
printf("共收到 %d 条Tick数据,[%d], 打印 1/100 条:\n", pCount, ret);
std::ofstream fout(filename1, std::ios::app);
for (int i = 0; iexecute(sql.c_str());
//acsy::util::DbPrepare *ins = conn->prepare(sql.c_str());
//if (ins == nullptr)
//{
// std::cout << conn->getLastError() << std::endl;
// return;
//}
//ins->bind(pTickCopy.chWindCode);
//ins->bindInt(pTickCopy.nDate);
//ins->bindInt(pTickCopy.nTime);
//ins->bindInt(pTickCopy.nPrice);
//ins->bindInt(pTickCopy.iVolume);
//ins->bindInt(pTickCopy.iTurover);
//ins->bindInt(pTickCopy.nMatchItems);
//ins->bindInt(pTickCopy.nInterest);
//ins->bindInt(pTickCopy.chTradeFlag);
//ins->bindInt(pTickCopy.chBSFlag);
//ins->bindDouble(pTickCopy.iAccVolume);
//ins->bindDouble(pTickCopy.iAccTurover);
//ins->bindInt(pTickCopy.nHigh);
//ins->bindInt(pTickCopy.nLow);
//ins->bindInt(pTickCopy.nOpen);
//ins->bindInt(pTickCopy.nPreClose);
//ins->bindInt(pTickCopy.nAskAvPrice);
//ins->bindInt(pTickCopy.nBidAvPrice);
//ins->bindDouble(pTickCopy.iTotalAskVolume);
//ins->bindDouble(pTickCopy.iTotalBidVolume);
//int j = ins->execute();
//if (0 != j)
//{
// std::cout << "j=[" << j << "] insert data error!!!" << conn->getLastError() << std::endl;
//}
//ins->close();
//ins = nullptr;
//printf("万得代码 chWindCode:%s \n", pTickCopy.chWindCode);
//printf("日期 nDate:%d \n", pTickCopy.nDate);
//printf("时间 nTime:%d \n", pTickCopy.nTime);
//printf("成交价 nPrice:%d \n", pTickCopy.nPrice);
//printf("成交量 iVolume:%I64d \n", pTickCopy.iVolume);
//printf("成交额(元) iTurover:%I64d \n", pTickCopy.iTurover);
//printf("成交笔数 nMatchItems:%d \n", pTickCopy.nMatchItems);
//printf(" nInterest:%d \n", pTickCopy.nInterest);
//printf("成交标志: chTradeFlag:%c \n", pTickCopy.chTradeFlag);
//printf("BS标志: chBSFlag:%c \n", pTickCopy.chBSFlag);
//printf("当日成交量: iAccVolume:%I64d \n", pTickCopy.iAccVolume);
//printf("当日成交额: iAccTurover:%I64d \n", pTickCopy.iAccTurover);
//printf("最高 nHigh:%d \n", pTickCopy.nHigh);
//printf("最低 nLow:%d \n", pTickCopy.nLow);
//printf("开盘 nOpen:%d \n", pTickCopy.nOpen);
//printf("前收盘 nPreClose:%d \n", pTickCopy.nPreClose);
//printf("加权平均叫卖价 nAskAvPrice:%d \n", pTickCopy.nAskAvPrice);
//printf("加权平均叫买价 nBidAvPrice:%d \n", pTickCopy.nBidAvPrice);
//printf("叫卖总量 iTotalAskVolume:%I64d \n", pTickCopy.iTotalAskVolume);
//printf("叫买总量 iTotalBidVolume:%I64d \n", pTickCopy.iTotalBidVolume);
#if 0
//期货字段
printf("结算价 nSettle:%d \n", pTickCopy.nSettle);
printf("持仓量 nPosition:%d \n", pTickCopy.nPosition);
printf("虚实度 nCurDelta:%d \n", pTickCopy.nCurDelta);
printf("昨结算 nPreSettle:%d \n", pTickCopy.nPreSettle);
printf("昨持仓 nPrePosition:%d \n", pTickCopy.nPrePosition);
//指数
printf("不加权指数 nIndex:%d \n", pTickCopy.nIndex);
printf("品种总数 nStocks:%d \n", pTickCopy.nStocks);
printf("上涨品种数 nUps:%d \n", pTickCopy.nUps);
printf("下跌品种数 nDowns:%d \n", pTickCopy.nDowns);
printf("持平品种数 nHoldLines:%d \n", pTickCopy.nHoldLines);
printf("-------------------------------\n");
#endif
//i += 1000;
}
// conn->execute("commit");
//释放
//TDB_Free(pTick);
}
//逐笔成交
void GetTransaction(THANDLE hTdb, char* szCode, char* szMarketKey, int nDate)
{
//请求
TDBDefine_ReqTransaction req = {0};
strncpy(req.chCode, szCode, sizeof(req.chCode)); //代码写成你想获取的股票代码
strncpy(req.chMarketKey, szMarketKey, sizeof(req.chMarketKey));
req.nDate = nDate;
req.nBeginTime = 0;
req.nEndTime = 0;
namespace fs = boost::filesystem;
stringstream stream;
stream << nDate;
std::string date = stream.str();
if (date == "")
{
return;
}
std::string dir = R"(E:\data\tdb\transaction)";
std::string floder = util::path_append(dir, date);
bool result = boost::filesystem::is_directory(floder);
if (result == false)
{
try{ fs::create_directories(floder); }
catch (std::exception& e)
{
std::cout << "exception!!!![" << e.what() << "]" << std::endl;
}
}
std::string filenametemp = "transaction_" + (string)szCode + ".csv";
std::string filename1 = util::path_append(floder, filenametemp);
bool ishas = boost::filesystem::exists(filename1);
if (ishas == true)
return;
std::ofstream fout(filename1, std::ios::app);
TDBDefine_Transaction *pTransaction = NULL;
int pCount;
int ret = TDB_GetTransaction(hTdb,&req, &pTransaction, &pCount);
NeedFreeTick needFree(pTransaction);
if (pCount <= 0)
{
return;
}
if (pTransaction && pCount && pTransaction[pCount-1].nTradeVolume > 100000)
{
printf("错误code:%s\n", req.chCode);
strncpy(ErrCode[ErrNum++], req.chCode, sizeof(req.chCode));
}
//#if 0
printf("---------------------------------------Transaction Data------------------------------------------\n");
printf("收到 %d 条逐笔成交消息,打印 1/10000 条\n", pCount);
for (int i=0; i>;
std::string strName = "KDJ";
std::string strContent = "INPUT:N(9), M1(3,1,100,2), M2(3);"
"RSV:=(CLOSE-LLV(LOW,N))/(HHV(HIGH,N)-LLV(LOW,N))*100;"
"K:SMA(RSV,M1,1);"
"D:SMA(K,M2,1);"
"J:3*K-2*D;";
//添加公式到服务器并编译,若不过,会有错误返回
TDBDefine_AddFormulaRes* addRes = new TDBDefine_AddFormulaRes;
int nErr = TDB_AddFormula(hTdb, strName.c_str(), strContent.c_str(),addRes);
printf("Add Formula Result:%s",addRes->chInfo);
//查询服务器上的公式,能看到我们刚才上传的"KDJ"
TDBDefine_FormulaItem* pEZFItem = NULL;
int nItems = 0;
//名字为空表示查询服务器上所有的公式
nErr = TDB_GetFormula(hTdb, NULL, &pEZFItem, &nItems);
for (int i=0; inRecordCount);
char szLineBuf[1024] = {0};
//输出字段名
for (int j=0; jnFieldCount;j++)
{
std::cout << pResult->chFieldName[j] << " ";
}
std::cout << endl << endl;
//输出数据
for (int i=0; inRecordCount; i++)
{
for (int j=0; jnFieldCount;j++)
{
std::cout << (pResult->dataFileds)[j][i] << " ";
}
std::cout << endl;
}
//删除之前上传的公式指标
TDBDefine_DelFormulaRes pDel = {0};
nErr = TDB_DeleteFormula(hTdb, "KDJ", &pDel);
printf("删除指标信息:%s", pDel.chInfo);
//释放内存
delete pEZFItem;
TDB_ReleaseCalcFormula(pResult);
}
using namespace acsy;
std::shared_ptr connect()
{
std::shared_ptr db_conn(new util::SQLITE_conn);
std::string url = "E:\\trade\\config\\fileposition.db";
if (db_conn->connect(const_cast(url.c_str())) == 0)
return db_conn;
else
return nullptr;
}
void writeCode()
{
std::string content = "C:\\Users\\025\\Desktop\\Table.txt",code,temp,name;
ifstream ifs(content,fstream::in);
std::vector vecall;
char *p;
int counter = 0;
auto conn = connect();
conn->execute("begin transaction");
while (!ifs.eof())
{
getline(ifs, temp);
// std::cout << temp << std::endl;
using namespace boost;
char_separator sep("\t");
tokenizer> tok(temp, sep);
std::vector vecBuf;
for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)
{
vecBuf.push_back(*pos);
}
if (vecBuf.size() >= 2)
{
counter++;
code = vecBuf[0];
name = vecBuf[1];
std::cout << code << " " << name <<" "<execute(sql.c_str());
acsy::util::DbPrepare *ins = conn->prepare(sql.c_str());
if (ins == nullptr)
{
std::cout << conn->getLastError() << std::endl;
return;
}
ins->bind(code.c_str());
ins->bind(name.c_str());
int i = ins->execute();
if (0 != i)
{
std::cout << "i=[" << i << "] insert data error!!!" << conn->getLastError() << std::endl;
}
ins->close();
ins = nullptr;
}
}
conn->execute("commit");
}