// TDBEasyDemo.cpp : 定义控制台应用程序的入口点。
//
#include "tdf/TDBAPI.h"
#include "iostream"
#include <string>
#include <algorithm>
#include <assert.h>
#include <boost/shared_ptr.hpp>
#include <acsy/util/dbconn.h>
#include <acsy/util/acsyapplication.h>
#include <fstream>
#include <boost/tokenizer.hpp>
#include<boost/typeof/typeof.hpp>
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<util::DBaseConn> 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; i<len; i++)
{
if (i == len-1)
{
str += int2str(arr[i]) + " ";
}
else
{
str += int2str(arr[i]) + ",";
}
}
return str;
}
static int ErrNum = 0;
static char ErrCode[100][10] = {0};
static TDBDefine_Code* g_pCodetable = NULL;
static int g_codeCount = 0;
int main()
{
//writeCode();
THANDLE hTdb = NULL;
char* svr_ip = "*.*.*.*"; //new char[20];
int svr_port = *;
char* username = "*"; //new char[20];
char* password = "*"; //new char[20];
//printf("Input IP: ");
//scanf("%s", svr_ip);
//printf("Input Port: ");
//scanf("%d", &svr_port);
//printf("Input Username: ");
//scanf("%s", username);
//printf("Input Password: ");
//scanf("%s", password);
//设置服务器信息
OPEN_SETTINGS settings = {0};
strcpy(settings.szIP, svr_ip);
sprintf(settings.szPort, "%d",svr_port);
strcpy(settings.szUser, username);
strcpy(settings.szPassword, password);
settings.nRetryCount = 100;
settings.nRetryGap = 100;
settings.nTimeOutVal = 100;
TDB_PROXY_SETTING proxy_settings;
proxy_settings.nProxyType = TDB_PROXY_HTTP11;
strcpy(proxy_settings.szProxyHostIp, "10.100.3.42");
sprintf(proxy_settings.szProxyPort, "%d",12345);
strcpy(proxy_settings.szProxyUser, "1");
strcpy(proxy_settings.szProxyPwd, "1");
TDBDefine_ResLogin LoginRes = {0};
//TDB_Open_Proxy
//hTdb = TDB_OpenProxy(&settings,&proxy_settings,&LoginRes);
//TDB_Open
//hTdb = TDB_Open(&settings, &LoginRes);
//// acsy::util::sleep_msec(1000);
// TDB_Close(hTdb);
//// acsy::util::sleep_msec(1000);
// hTdb = NULL;
// hTdb = TDB_Open(&settings, &LoginRes);
//
//int nRet = TDB_SUCCESS;
////用于网络重新连接用
//while (!hTdb && (nRet == TDB_NETWORK_ERROR || nRet == TDB_NETWORK_TIMEOUT)) //去掉的
//{
// nRet = test(hTdb);
//}
// TDB_Close(hTdb);
// hTdb = NULL;
while(1)
{
hTdb = TDB_Open(&settings, &LoginRes);
if (hTdb)
{
break;
}
acsy::util::sleep_msec(3000);
}
if (!hTdb)
{
printf("连接失败!");
getchar();
return 0;
}
/*************************** 请求数据 ***********************************/
//TDB_GetCodeInfo
{
const TDBDefine_Code* pCode = TDB_GetCodeInfo(hTdb, "000001.SZ", "SZ-2-0");
printf("-------------收到代码信息----------------------------\n");
printf("交易所代码 chWindCode:%s \n", pCode->chCode);
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; i<g_codeCount; i++)
{
printf("交易所代码 chWindCode:%s \n", g_pCodetable[i].chCode);
printf("市场代码 chWindCode:%s \n", g_pCodetable[i].chMarket);
printf("证券中文名称 chWindCode:%s \n", g_pCodetable[i].chCNName);
printf("证券英文名称 chWindCode:%s \n", g_pCodetable[i].chENName);
printf("证券类型 chWindCode:%d \n", g_pCodetable[i].nType);
printf("----------------------------------------\n");
}
}
else
{
//print top 10
int i = g_codeCount>10 ? 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;i<pCount;i++)
{
std::string data = acsy::util::strprintf("%s,%s,%d,%d,%d,%d,%d,%d,%llu,%llu,%d,%d\n",
kLine[i].chWindCode,kLine[i].chCode,kLine[i].nDate,kLine[i].nTime,kLine[i].nOpen,kLine[i].nHigh,kLine[i].nLow,kLine[i].nClose,
kLine[i].iVolume,kLine[i].iTurover,kLine[i].nMatchItems,kLine[i].nInterest);
fout << data;
// i +=100;
}
//释放
}
//tick
void GetTickData(THANDLE hTdb, const char* szCode, const char* szMarket, int nDate)
{
//请求信息
TDBDefine_ReqTick req = {0};
strncpy(req.chCode, szCode, sizeof(req.chCode)); //代码写成你想获取的股票代码
strncpy(req.chMarketKey, szMarket, sizeof(req.chMarketKey));
req.nDate = nDate;
req.nBeginTime = 0;
req.nEndTime = 0;
TDBDefine_Tick *pTick = NULL;
int pCount = 0;
// auto conn = connect();
// conn->execute("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; i<pCount; i++)
{
TDBDefine_Tick& pTickCopy = pTick[i];
//买卖盘字段
std::string strOut = array2str(pTickCopy.nAskPrice, ELEMENT_COUNT(pTickCopy.nAskPrice));
strOut = array2str((const int*)pTickCopy.nAskVolume, ELEMENT_COUNT(pTickCopy.nAskVolume));
strOut = array2str(pTickCopy.nBidPrice, ELEMENT_COUNT(pTickCopy.nBidPrice));
strOut = array2str((const int*)pTickCopy.nBidVolume, ELEMENT_COUNT(pTickCopy.nBidVolume));
if (!pTickCopy.chTradeFlag)
pTickCopy.chTradeFlag = '0';
std::string data;
std::string data3 = "";
std::string data1 = acsy::util::strprintf("%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%llu,%llu,%d,%d,%d,%d,%d,%llu,%llu,",
pTickCopy.chWindCode,pTickCopy.nDate, pTickCopy.nTime, pTickCopy.nPrice,
pTickCopy.iVolume, pTickCopy.iTurover, pTickCopy.nMatchItems, pTickCopy.nInterest,
pTickCopy.chTradeFlag, pTickCopy.chBSFlag, pTickCopy.iAccVolume,
pTickCopy.iAccTurover,pTickCopy.nHigh, pTickCopy.nLow, pTickCopy.nOpen,
pTickCopy.nPreClose, pTickCopy.nAskAvPrice,
pTickCopy.iTotalAskVolume, pTickCopy.iTotalBidVolume
);
for (int i = 0; i < 10; i++)
{
std::string data2 = acsy::util::strprintf("%d,%d,%d,%d,",
pTickCopy.nAskPrice[i],pTickCopy.nAskVolume[i],pTickCopy.nBidPrice[i],pTickCopy.nBidVolume[i]);
data3 += data2;
}
data = data1 + data3+"\n";
{
fout << data;
//fout.close();
}
//以下为写入数据库的代码
//std::string sql = "insert into stock_data(chWindCode,nDate,nTime,Price,iVolume,iTurnover,MatchItems,Interest,chTradeFlag,chBSFlag,iAccVolume,"
// "iAccTurover,HighPrice,LowPrice,OpenPrice,PreClose,AskAvPrice,BidAvPrice,TotalAskVolume,TotalBidVolume)"
// " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
////conn->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(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<pCount;i++ )
{
/*const*/ TDBDefine_Transaction& trans = pTransaction[i];
if (!trans.chFunctionCode)
trans.chFunctionCode = '0';
if (!trans.chOrderKind)
trans.chOrderKind = '0';
std::string data = acsy::util::strprintf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", trans.chWindCode, trans.nDate, trans.nTime, trans.nIndex
, trans.chFunctionCode, trans.chOrderKind, trans.chBSFlag, trans.nTradePrice, trans.nTradeVolume, trans.nAskOrder,trans.nBidOrder);
fout << data;
//printf("成交时间(Date): %d \n", trans.nDate);
//printf("成交时间: %d \n", trans.nTime);
//printf("成交代码: %c \n", trans.chFunctionCode);
//printf("委托类别: %c \n", trans.chOrderKind);
//printf("BS标志: %c \n", trans.chBSFlag);
//printf("成交价格: %d \n", trans.nTradePrice);
//printf("成交数量: %d \n", trans.nTradeVolume);
//printf("叫卖序号: %d \n", trans.nAskOrder);
//printf("叫买序号: %d \n", trans.nBidOrder);
//printf("------------------------------------------------------\n");
//#if 0
// printf("成交编号: %d \n", trans.nBidOrder);
//#endif
// i += 10000;
}
//#endif
//释放
}
//逐笔委托
void GetOrder(THANDLE hTdb, char* szCode, char* szMarketKey, int nDate)
{
//请求
TDBDefine_ReqOrder 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;
TDBDefine_Order *pOrder = NULL;
int pCount;
int ret = TDB_GetOrder(hTdb,&req, &pOrder, &pCount);
//printf("---------------------Order Data----------------------\n");
//printf("收到 %d 条逐笔委托消息,打印 1/10000 条\n", pCount);
//for (int i=0; i<pCount; )
//{
//const TDBDefine_Order& order = pOrder[i];
//printf("订单时间(Date): %d \n", order.nDate);
//printf("委托时间(HHMMSSmmm): %d \n", order.nTime);
//printf("委托编号: %d \n", order.nOrder);
//printf("委托类别: %c \n", order.chOrderKind);
//printf("委托代码: %c \n", order.chFunctionCode);
//printf("委托价格: %d \n", order.nOrderPrice);
//printf("委托数量: %d \n", order.nOrderVolume);
//printf("-------------------------------\n");
//i += 10000;
//}
//释放
TDB_Free(pOrder);
}
//委托队列
void GetOrderQueue(THANDLE hTdb, char* szCode, char* szMarketKey, int nDate)
{
//请求
TDBDefine_ReqOrderQueue 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;
TDBDefine_OrderQueue *pOrderQueue = NULL;
int pCount;
TDB_GetOrderQueue(hTdb,&req, &pOrderQueue, &pCount);
printf("-------------------OrderQueue Data-------------\n");
printf("收到 %d 条委托队列消息,打印 1/1000 条\n", pCount);
for (int i=0; i<pCount; i++)
{
const TDBDefine_OrderQueue& que = pOrderQueue[i];
printf("订单时间(Date): %d \n", que.nDate);
printf("订单时间(HHMMSS): %d \n", que.nTime);
printf("买卖方向('B':Bid 'A':Ask): %c \n", que.nSide);
printf("成交价格: %d \n", que.nPrice);
printf("订单数量: %d \n", que.nOrderItems);
printf("明细个数: %d \n", que.nABItems);
printf("订单明细: %s \n", array2str(que.nABVolume, que.nABItems).c_str());
printf("-------------------------------\n");
i += 1000;
}
//释放
TDB_Free(pOrderQueue);
}
//指标公式
void UseEZFFormula(THANDLE hTdb)
{
//公式的编写,请参考<<TRANSEND-TS-M0001 易编公式函数表V1(2).0-20110822.pdf>>;
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; i<nItems; i++)
{
std::string strNameInner(pEZFItem[i].chFormulaName, 0, sizeof(pEZFItem[i].chFormulaName));
std::string strParam(pEZFItem[i].chParam, 0, sizeof(pEZFItem[i].chParam));
printf("公式名称:%s, 参数:%s \n", strNameInner.c_str(), strParam.c_str());
}
struct EZFCycDefine
{
char chName[8];
int nCyc;
int nCyc1;
}
EZFCyc[5]={
{"日线", 2, 0},
{"30分", 0, 30},
{"5分钟", 0, 5},
{"1分钟", 0, 1},
{"15秒", 11, 15}};
//获取公式的计算结果
TDBDefine_ReqCalcFormula reqCalc = {0};
strncpy(reqCalc.chFormulaName, "KDJ", sizeof(reqCalc.chFormulaName));
strncpy(reqCalc.chParam, "N=9,M1=3,M2=3", sizeof(reqCalc.chParam));
strncpy(reqCalc.chCode, "000001.SZ", sizeof(reqCalc.chCode));
strncpy(reqCalc.chMarketKey, "SZ-2-0", sizeof(reqCalc.chMarketKey));
reqCalc.nCycType = (CYCTYPE)(EZFCyc[0].nCyc); //0表示日线
reqCalc.nCycDef = EZFCyc[0].nCyc1;
reqCalc.nCQFlag = (REFILLFLAG)0; //除权标志
reqCalc.nCalcMaxItems = 4000; //计算的最大数据量
reqCalc.nResultMaxItems = 100; //传送的结果的最大数据量
TDBDefine_CalcFormulaRes* pResult = new TDBDefine_CalcFormulaRes;
nErr = TDB_CalcFormula(hTdb, &reqCalc, pResult);
//判断错误代码
printf("计算结果有: %d 条:\n", pResult->nRecordCount);
char szLineBuf[1024] = {0};
//输出字段名
for (int j=0; j<pResult->nFieldCount;j++)
{
std::cout << pResult->chFieldName[j] << " ";
}
std::cout << endl << endl;
//输出数据
for (int i=0; i<pResult->nRecordCount; i++)
{
for (int j=0; j<pResult->nFieldCount;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<util::DBaseConn> connect()
{
std::shared_ptr<util::DBaseConn> db_conn(new util::SQLITE_conn);
std::string url = "E:\\trade\\config\\fileposition.db";
if (db_conn->connect(const_cast<char*>(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<std::string> 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<char> sep("\t");
tokenizer<char_separator<char>> tok(temp, sep);
std::vector<std::string> 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 <<" "<<counter <<std::endl;
std::string sql = "insert into stock_code(stock_code,stock_name) values ('"+code+"','"+name+"')";
//conn->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");
}
万得股票历史数据接口
PublishedCreated: 2018年11月30日星期五 13:26:03 views(1634)