├── .gitignore ├── hello ├── hello.h ├── Makefile └── hello.cpp ├── cpphelper.pyc ├── lib ├── code_convert.o ├── thostmduserapi.so └── thosttraderapi.so ├── OrderInsert ├── execute └── Makefile ├── QryInvestor.1 ├── execute ├── Makefile └── QryInvestor.cpp ├── api ├── thostmduserapi.so ├── thosttraderapi.so ├── md5.txt ├── error.dtd ├── ThostFtdcMdApi.h └── error.xml ├── common ├── code_convert.o ├── code_convert.h ├── Makefile └── code_convert.cpp ├── QryExchange.all ├── execute ├── Makefile └── QryExchange.cpp ├── QryParkedOrder ├── execute └── Makefile ├── QryInstrument.all ├── execute ├── Makefile └── QryInstrument.cpp ├── QryTradingCode.1 ├── execute ├── Makefile └── QryTradingCode.cpp ├── QryTransferBank.1 ├── execute ├── Makefile └── QryTransferBank.cpp ├── OrderInsert.buy.open ├── execute └── Makefile ├── QryTradingAccount.1 ├── execute ├── Makefile └── QryTradingAccount.cpp ├── thread ├── Makefile └── thread.cpp ├── OrderInsert.buy.close ├── execute └── Makefile ├── OrderInsert.sell.close ├── execute └── Makefile ├── OrderInsert.sell.open ├── execute └── Makefile ├── QryBrokerTradingParams ├── execute └── Makefile ├── QryDepthMarketData.all ├── execute ├── Makefile └── QryDepthMarketData.cpp ├── QryInvestorPosition.1 ├── execute ├── Makefile └── QryInvestorPosition.cpp ├── SettlementInfoConfirm.1 ├── execute ├── Makefile └── SettlementInfoConfirm.cpp ├── OrderInsert.buylimit.open ├── execute └── Makefile ├── QryDepthMarketData.timer ├── execute └── Makefile ├── QryInstrument.ExchangeID ├── execute ├── Makefile └── QryInstrument.cpp ├── QryDepthMarketData.timer.all ├── execute ├── Makefile └── QryDepthMarketData.cpp ├── QryInstrument.InstrumentID ├── execute ├── Makefile └── QryInstrument.cpp ├── QrySettlementInfoConfirm.1 ├── execute ├── Makefile └── QrySettlementInfoConfirm.cpp ├── QryDepthMarketData.InstrumentID ├── execute ├── Makefile └── QryDepthMarketData.cpp ├── login ├── Makefile └── login.cpp ├── include ├── code_convert.h ├── error.dtd ├── ThostFtdcMdApi.h └── error.xml ├── tradeapitest ├── Makefile ├── tradeapitest.h └── tradeapitest.cpp ├── QryOrder.1 └── Makefile ├── OrderAction.delete └── Makefile ├── QueryMaxOrderVolume.1 ├── Makefile └── QueryMaxOrderVolume.cpp ├── templates └── Makefile.tpl ├── README.md ├── cpphelper.py └── create_api_tester.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | *.con 3 | *.orig 4 | -------------------------------------------------------------------------------- /hello/hello.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | -------------------------------------------------------------------------------- /cpphelper.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/cpphelper.pyc -------------------------------------------------------------------------------- /lib/code_convert.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/lib/code_convert.o -------------------------------------------------------------------------------- /OrderInsert/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/OrderInsert/execute -------------------------------------------------------------------------------- /QryInvestor.1/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryInvestor.1/execute -------------------------------------------------------------------------------- /api/thostmduserapi.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/api/thostmduserapi.so -------------------------------------------------------------------------------- /api/thosttraderapi.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/api/thosttraderapi.so -------------------------------------------------------------------------------- /common/code_convert.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/common/code_convert.o -------------------------------------------------------------------------------- /lib/thostmduserapi.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/lib/thostmduserapi.so -------------------------------------------------------------------------------- /lib/thosttraderapi.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/lib/thosttraderapi.so -------------------------------------------------------------------------------- /QryExchange.all/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryExchange.all/execute -------------------------------------------------------------------------------- /QryParkedOrder/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryParkedOrder/execute -------------------------------------------------------------------------------- /QryInstrument.all/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryInstrument.all/execute -------------------------------------------------------------------------------- /QryTradingCode.1/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryTradingCode.1/execute -------------------------------------------------------------------------------- /QryTransferBank.1/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryTransferBank.1/execute -------------------------------------------------------------------------------- /OrderInsert.buy.open/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/OrderInsert.buy.open/execute -------------------------------------------------------------------------------- /QryTradingAccount.1/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryTradingAccount.1/execute -------------------------------------------------------------------------------- /thread/Makefile: -------------------------------------------------------------------------------- 1 | all : *.cpp 2 | g++ -pthread *.cpp -o thread 3 | 4 | clean : 5 | rm -f *.o *.out thread 6 | -------------------------------------------------------------------------------- /OrderInsert.buy.close/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/OrderInsert.buy.close/execute -------------------------------------------------------------------------------- /OrderInsert.sell.close/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/OrderInsert.sell.close/execute -------------------------------------------------------------------------------- /OrderInsert.sell.open/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/OrderInsert.sell.open/execute -------------------------------------------------------------------------------- /QryBrokerTradingParams/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryBrokerTradingParams/execute -------------------------------------------------------------------------------- /QryDepthMarketData.all/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryDepthMarketData.all/execute -------------------------------------------------------------------------------- /QryInvestorPosition.1/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryInvestorPosition.1/execute -------------------------------------------------------------------------------- /SettlementInfoConfirm.1/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/SettlementInfoConfirm.1/execute -------------------------------------------------------------------------------- /OrderInsert.buylimit.open/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/OrderInsert.buylimit.open/execute -------------------------------------------------------------------------------- /QryDepthMarketData.timer/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryDepthMarketData.timer/execute -------------------------------------------------------------------------------- /QryInstrument.ExchangeID/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryInstrument.ExchangeID/execute -------------------------------------------------------------------------------- /api/md5.txt: -------------------------------------------------------------------------------- 1 | 5a99572dcfab37a3b4a4c6cac1a02366 thostmduserapi.so 2 | 2d4b54c13fd45b54c7623d84b7e42af5 thosttraderapi.so 3 | -------------------------------------------------------------------------------- /QryDepthMarketData.timer.all/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryDepthMarketData.timer.all/execute -------------------------------------------------------------------------------- /QryInstrument.InstrumentID/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryInstrument.InstrumentID/execute -------------------------------------------------------------------------------- /QrySettlementInfoConfirm.1/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QrySettlementInfoConfirm.1/execute -------------------------------------------------------------------------------- /QryDepthMarketData.InstrumentID/execute: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xmduhan/ctp_examples/HEAD/QryDepthMarketData.InstrumentID/execute -------------------------------------------------------------------------------- /hello/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all : *.cpp *.h 3 | g++ *.cpp /etc/ctp/lib/*.so -o hello 4 | 5 | 6 | clean : 7 | rm -f *.o *.out *.con hello 8 | -------------------------------------------------------------------------------- /login/Makefile: -------------------------------------------------------------------------------- 1 | all : *.cpp 2 | g++ -pthread *.cpp /etc/ctp/lib/*.so -o login 3 | 4 | 5 | clean : 6 | rm -f *.o rm *.out *.con login 7 | -------------------------------------------------------------------------------- /common/code_convert.h: -------------------------------------------------------------------------------- 1 | void codeConvert(char * from,char * to,char * src,char * dst,size_t dstSize); 2 | void gbk2utf8(char * src,char * dst,size_t dstSize); 3 | -------------------------------------------------------------------------------- /include/code_convert.h: -------------------------------------------------------------------------------- 1 | void codeConvert(char * from,char * to,char * src,char * dst,size_t dstSize); 2 | void gbk2utf8(char * src,char * dst,size_t dstSize); 3 | -------------------------------------------------------------------------------- /tradeapitest/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all : *.cpp *.h 3 | g++ -g *.cpp /etc/ctp/lib/*.so -o tradeapitest 4 | 5 | 6 | clean : 7 | rm -f *.o *.out *.con tradeapitest 8 | -------------------------------------------------------------------------------- /common/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all : compile copy 3 | 4 | compile : *.cpp 5 | g++ -c *.cpp 6 | 7 | copy : *.o *.h 8 | cp *.o ../lib 9 | cp *.h ../include 10 | 11 | 12 | 13 | clean : 14 | rm -f *.o *.out 15 | -------------------------------------------------------------------------------- /api/error.dtd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /include/error.dtd: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /OrderInsert/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute -------------------------------------------------------------------------------- /QryOrder.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryExchange.all/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryInvestor.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryParkedOrder/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryTradingCode.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /OrderAction.delete/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /OrderInsert.buy.close/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /OrderInsert.buy.open/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /OrderInsert.sell.open/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryInstrument.all/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryInvestorPosition.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryTradingAccount.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryTransferBank.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QueryMaxOrderVolume.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /hello/hello.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "hello.h" 3 | 4 | int main(){ 5 | // 测试调用CTP的API 6 | // 这里访问了CTP的库函数,如果程序能过正常编译并执行,说明头文件引用及库连接正常 7 | CThostFtdcMdApi *pUserApi = CThostFtdcMdApi::CreateFtdcMdApi(); 8 | printf("Hello CTP!\n"); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /OrderInsert.buylimit.open/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /OrderInsert.sell.close/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryBrokerTradingParams/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryDepthMarketData.all/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryDepthMarketData.timer/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryInstrument.ExchangeID/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryInstrument.InstrumentID/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QrySettlementInfoConfirm.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /SettlementInfoConfirm.1/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryDepthMarketData.InstrumentID/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /QryDepthMarketData.timer.all/Makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute 12 | -------------------------------------------------------------------------------- /templates/Makefile.tpl: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | LIBS =../lib/*.o ../lib/*.so 3 | CC=g++ 4 | CFLAGS=-pthread -I$(IDIR) $(LIBS) 5 | 6 | all : *.cpp 7 | $(CC) *.cpp $(CFLAGS) -o execute {# {{ apiName }} #} 8 | 9 | 10 | clean : 11 | rm -f *.o rm *.out *.con *.orig execute {# {{ apiName }} #} 12 | -------------------------------------------------------------------------------- /tradeapitest/tradeapitest.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | //#include 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | -------------------------------------------------------------------------------- /thread/thread.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | sem_t sem; 7 | pthread_t tid; 8 | 9 | static void * thread_function(void * arg){ 10 | sleep(5); 11 | sem_post(&sem); 12 | } 13 | 14 | int main(){ 15 | 16 | sem_init(&sem,0,0); 17 | pthread_create(&tid,NULL,thread_function,NULL); 18 | 19 | // 等待子进程释放信号 20 | sem_wait(&sem); 21 | printf("wait return,thread finished."); 22 | 23 | return(0); 24 | } 25 | -------------------------------------------------------------------------------- /common/code_convert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void codeConvert(char * from,char * to,char * src,char * dst,size_t dstSize){ 6 | size_t sl,dl; 7 | sl = strlen(src); 8 | dl = dstSize; 9 | char * pIn = src; 10 | char * pOut = dst; 11 | memset(dst,'\0',dstSize); 12 | iconv_t conv = iconv_open(to,from); 13 | iconv(conv, &pIn, &sl, &pOut, &dl); 14 | iconv_close(conv); 15 | } 16 | 17 | void gbk2utf8(char * src,char * dst,size_t dstSize){ 18 | char * gbk = (char*)"GBK"; 19 | char * utf8 = (char*)"UTF-8"; 20 | codeConvert(gbk,utf8,src,dst,dstSize); 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CTP API 使用范例 2 | - 该项目主要是为帮助生成CTP API测试片段,降低CTP接口的学习难度。 3 | - 目前仅支持在linux下用gcc编译使用。 4 | 5 | # 安装CppHeaderParser 6 | sudo pip install ply 7 | sudo pip install CppHeaderParser 8 | 9 | # 安装模板生成工 jinja2 10 | sudo pip install jinja2 11 | 12 | # 安装astyle代码美化工具 13 | sudo apt-get install astyle 14 | 15 | 16 | # 安装接口api 17 | - 解压 18 | - unzip 6.2.5_20140811_apitraderapi_linux64.zip 19 | 20 | - 创建安装目录 21 | - sudo mkdir -p /etc/ctp/include 22 | - sudo mkdir -p /etc/ctp/lib 23 | 24 | - 拷贝文件 25 | - sudo cp *.h /etc/ctp/include 26 | - sudo cp *.so *.xml *.dtd /etc/ctp/lib 27 | 28 | 29 | # 使用前需要设置环境变量 30 | - export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/etc/ctp/include 31 | - export CTP_FrontAddress="tcp://x.x.x.x:51205" 32 | - export CTP_BrokerId=xxxxxx 33 | - export CTP_UserId=xxxxxxxx 34 | - export CTP_Password=xxxxxx 35 | 36 | # 创建API调用框架 37 | - create_api_tester.py这个命令可以帮我们生成一个特定CTP接口API的调用测试框架,其具体使用方法如下: 38 | - $ python create_api_tester.py API名称 39 | - 其中API名称形如QryTradingAccount,对应的请求函数为ReqQryTradingAccount,对应的相应函数为OnRspQryTradingAccount。 40 | - create_api_tester.py执行成功会创建以API名称命名的目录(如:QryTradingAccount,OrderInsert,QryDepthMarketData等),目录中有一个c++源文件和一个makefile。用编辑器打开c++源文件,设置调用参数,已经对响应函数编写相应的处理逻辑(比如:在屏幕上打印返回的参数信息)。 41 | - 然后可以使用以下命令进行编译和执行: 42 | - $ make 43 | - ./execute 44 | 45 | # 基本测试例子 46 | - 除了API调用生成框架外,项目还提供了几个例子程序 47 | - hello 测试CTP API环境的安装和工作是否正常 48 | - tradeapitest 官方的API参考文档中的例子(程序只能运行,但订单无法提交成功,需要提交逻辑,使用创建API调用框架生成OrderInsert目录。 49 | - login 一个登录登出的例子 50 | - thread linux线程同步例子 51 | -------------------------------------------------------------------------------- /login/login.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 服务器地址 18 | char * serverAddress = (char *)"tcp://101.231.96.18:51205"; 19 | // 登录请求结构体 20 | CThostFtdcReqUserLoginField userLoginField; 21 | // 用户请求结构体 22 | CThostFtdcUserLogoutField userLogoutField; 23 | // 线程同步标志 24 | sem_t sem; 25 | 26 | 27 | 28 | class CTraderHandler : public CThostFtdcTraderSpi{ 29 | 30 | public: 31 | 32 | CTraderHandler(){ 33 | printf("CTraderHandler:called.\n"); 34 | } 35 | 36 | virtual void OnFrontConnected() { 37 | static int i = 0; 38 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 39 | if (i++==0) { 40 | printf("OnFrontConnected:called.\n"); 41 | sem_post(&sem); 42 | } 43 | } 44 | 45 | 46 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 47 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast){ 48 | printf("OnRspUserLogin:called\n"); 49 | if (pRspInfo->ErrorID == 0) { 50 | printf("登录成功!\n"); 51 | sem_post(&sem); 52 | }else{ 53 | printf("登录失败!\n"); 54 | } 55 | } 56 | 57 | 58 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 59 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 60 | printf("OnReqUserLogout:called\n"); 61 | if (pRspInfo->ErrorID == 0) { 62 | printf("登出成功!\n"); 63 | sem_post(&sem); 64 | }else{ 65 | printf("登出失败!\n"); 66 | } 67 | } 68 | 69 | }; 70 | 71 | 72 | int main(){ 73 | 74 | // 初始化线程同步变量 75 | sem_init(&sem,0,0); 76 | 77 | 78 | // 创建TraderAPI和回调响应控制器的实例 79 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 80 | CTraderHandler traderHandler = CTraderHandler(); 81 | CTraderHandler * pTraderHandler = &traderHandler; 82 | pTraderApi->RegisterSpi(pTraderHandler); 83 | 84 | // 设置服务器地址 85 | pTraderApi->RegisterFront(serverAddress); 86 | // 链接交易系统 87 | pTraderApi->Init(); 88 | 89 | // 等待服务器发出登录消息 90 | sem_wait(&sem); 91 | 92 | // 读取登录信息 93 | printf("BrokerID:"); 94 | scanf("%s", (char*) &userLoginField.BrokerID); 95 | 96 | printf("userid:"); 97 | scanf("%s", (char*) &userLoginField.UserID); 98 | 99 | printf("password:"); 100 | scanf("%s", (char*) &userLoginField.Password); 101 | 102 | // 发出登陆请求 103 | pTraderApi->ReqUserLogin(&userLoginField, 0); 104 | 105 | // 等待登录成功消息 106 | sem_wait(&sem); 107 | 108 | // 等待5秒钟 109 | sleep(5); 110 | printf("等待5秒完毕\n"); 111 | 112 | // 拷贝用户登录信息到登出信息 113 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 114 | strcpy(userLogoutField.UserID, userLoginField.UserID); 115 | pTraderApi->ReqUserLogout(&userLogoutField, 1); 116 | 117 | // 等待登出成功 118 | sem_wait(&sem); 119 | 120 | 121 | printf("主线程执行完毕!\n"); 122 | return(0); 123 | } 124 | -------------------------------------------------------------------------------- /cpphelper.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import re 4 | import CppHeaderParser 5 | 6 | 7 | def getCppHeader(filename,ignoreSymbols=[]): 8 | ''' 9 | 通过一个文件名获得一个CppHeader结构数据 10 | filename 文件名称(含路径) 11 | ignoreSymbols 需要忽略的符号 12 | 返回 CppHeader结构 13 | ''' 14 | #TODO : 这里存在一个问题,如果使用了ignoreSymbols由于使用CppHeaderParser.ignoreSymbols.extend(ignoreSymbols) 15 | # 将影响到后续的调用,这个影响无法清楚,一直要等到重启解释器。 16 | 17 | if ignoreSymbols and type(ignoreSymbols) == list: 18 | CppHeaderParser.ignoreSymbols.extend(ignoreSymbols) 19 | return CppHeaderParser.CppHeader(filename) 20 | 21 | 22 | def getClass(header,className): 23 | ''' 24 | 通过一个类名称在一个CppHeader中查找一个Class结构(含struct) 25 | header CppHeader结构 26 | className 要查找的类名称 27 | 返回 Class结构 28 | ''' 29 | return header.classes[className] 30 | 31 | 32 | def getClassMethod(aclass,methodType,methodName): 33 | ''' 34 | 通过方法的类型和名称查找方法在一个class结构中查找一个结构 35 | acalss Class结构 36 | methodType 方法类型(指public,protected,private) 37 | methodName 方法名称 38 | 返回 Method结构 39 | ''' 40 | # TODO : 这里存在一个问题,就是在c++类中方法可以重载,方法名称不唯一 41 | # 在CTP接口函数中不存在这种情况,暂时忽略 42 | 43 | return {method['name']:method for method in aclass['methods'][methodType]}[methodName] 44 | 45 | def getStructFields(struct): 46 | ''' 47 | 获取一个结构体的字段列表 48 | struct 结构体数据 49 | 返回 字段列表 50 | ''' 51 | return [field for field in struct['properties']['public']] 52 | 53 | 54 | def getMethodParameters(method): 55 | ''' 56 | 给定一个method结构返回其所有参数 57 | method Method结构 58 | 返回 参数列表 59 | ''' 60 | return [parameter for parameter in method['parameters'] ] 61 | 62 | 63 | def getTypedefDict(filename): 64 | ''' 65 | 从一个C++头文件中获取所有的typedef数据 66 | filename 要读取的文件名称 67 | 返回 一个typedef信息字典,其格式为: 68 | ''' 69 | pattern = r'\s*typedef\s+(?P\w+(\s+\*\s+)*)\s+(?P\w+)\s*(\[\s*(?P\w+)\s*\]){0,1}\s*;' 70 | regex = re.compile(pattern) 71 | with open(filename) as f : 72 | header = f.read() 73 | typedefDict = {m.groupdict()['name']:m.groupdict() for m in regex.finditer(header)} 74 | return typedefDict 75 | 76 | def getEnumDict(filename): 77 | ''' 78 | 从一个c++头文件中获取所有的枚举类型的值 79 | filename 要读取的文件名称 80 | 返回 一个枚举数据字段, 81 | 其格式为{'类型名称':[{'name':'枚举名称1','value':'枚举对应值','remark':'说明'},...],...} 82 | 83 | ''' 84 | # TODO 注意该函数功能并不通用,其基于CTP头文件的特性结构 85 | # 先读取所有的typedef定义 86 | typedefs = getTypedefDict(filename) 87 | # 定义正则表达式 88 | definePattern = r'///(?P\S+)\s+#define\s+(?P\S+)\s+(?P\S+)\s+' 89 | defineRegex = re.compile(definePattern) 90 | 91 | # 读取对应的头文件信息 92 | with open(filename) as f: 93 | header = f.read() 94 | 95 | # 为每个类型尝试寻找其对应枚举值(如果存在的话) 96 | enumDict = {} 97 | for tdname in typedefs: 98 | enumPattern = r'T%s[\S\s]*?typedef\s*\S+\s+%s;' % (tdname[6:],tdname) 99 | enumDefineStringList = re.findall(enumPattern ,header) 100 | if enumDefineStringList : 101 | enumDefineString= enumDefineStringList [0] 102 | enumDict[tdname ] = [m.groupdict()for m in defineRegex .finditer(enumDefineString)] 103 | 104 | return enumDict 105 | 106 | 107 | 108 | #def getMethodReturn(method): 109 | # ''' 110 | # 给定一个method结构获取其返回值类型 111 | # method Method结构 112 | # 返回 返回值类型名称 113 | # ''' 114 | # return method['returns'] 115 | 116 | 117 | #def getParameterType(parameter): 118 | # ''' 119 | # 给定一个方法的参数结构,返回其类型 120 | # parameter 方法的参数结构 121 | # 返回 参数的类型名称 122 | # ''' 123 | # return parameter['type'] 124 | 125 | 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /api/ThostFtdcMdApi.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////// 2 | ///@system 新一代交易所系统 3 | ///@company 上海期货信息技术有限公司 4 | ///@file ThostFtdcMdApi.h 5 | ///@brief 定义了客户端接口 6 | ///@history 7 | ///20060106 赵鸿昊 创建该文件 8 | ///////////////////////////////////////////////////////////////////////// 9 | 10 | #if !defined(THOST_FTDCMDAPI_H) 11 | #define THOST_FTDCMDAPI_H 12 | 13 | #if _MSC_VER > 1000 14 | #pragma once 15 | #endif // _MSC_VER > 1000 16 | 17 | #include "ThostFtdcUserApiStruct.h" 18 | 19 | #if defined(ISLIB) && defined(WIN32) 20 | #ifdef LIB_MD_API_EXPORT 21 | #define MD_API_EXPORT __declspec(dllexport) 22 | #else 23 | #define MD_API_EXPORT __declspec(dllimport) 24 | #endif 25 | #else 26 | #define MD_API_EXPORT 27 | #endif 28 | 29 | class CThostFtdcMdSpi 30 | { 31 | public: 32 | ///当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。 33 | virtual void OnFrontConnected(){}; 34 | 35 | ///当客户端与交易后台通信连接断开时,该方法被调用。当发生这个情况后,API会自动重新连接,客户端可不做处理。 36 | ///@param nReason 错误原因 37 | /// 0x1001 网络读失败 38 | /// 0x1002 网络写失败 39 | /// 0x2001 接收心跳超时 40 | /// 0x2002 发送心跳失败 41 | /// 0x2003 收到错误报文 42 | virtual void OnFrontDisconnected(int nReason){}; 43 | 44 | ///心跳超时警告。当长时间未收到报文时,该方法被调用。 45 | ///@param nTimeLapse 距离上次接收报文的时间 46 | virtual void OnHeartBeatWarning(int nTimeLapse){}; 47 | 48 | 49 | ///登录请求响应 50 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 51 | 52 | ///登出请求响应 53 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 54 | 55 | ///错误应答 56 | virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 57 | 58 | ///订阅行情应答 59 | virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 60 | 61 | ///取消订阅行情应答 62 | virtual void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 63 | 64 | ///深度行情通知 65 | virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) {}; 66 | }; 67 | 68 | class MD_API_EXPORT CThostFtdcMdApi 69 | { 70 | public: 71 | ///创建MdApi 72 | ///@param pszFlowPath 存贮订阅信息文件的目录,默认为当前目录 73 | ///@return 创建出的UserApi 74 | ///modify for udp marketdata 75 | static CThostFtdcMdApi *CreateFtdcMdApi(const char *pszFlowPath = "", const bool bIsUsingUdp=false, const bool bIsMulticast=false); 76 | 77 | ///删除接口对象本身 78 | ///@remark 不再使用本接口对象时,调用该函数删除接口对象 79 | virtual void Release() = 0; 80 | 81 | ///初始化 82 | ///@remark 初始化运行环境,只有调用后,接口才开始工作 83 | virtual void Init() = 0; 84 | 85 | ///等待接口线程结束运行 86 | ///@return 线程退出代码 87 | virtual int Join() = 0; 88 | 89 | ///获取当前交易日 90 | ///@retrun 获取到的交易日 91 | ///@remark 只有登录成功后,才能得到正确的交易日 92 | virtual const char *GetTradingDay() = 0; 93 | 94 | ///注册前置机网络地址 95 | ///@param pszFrontAddress:前置机网络地址。 96 | ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 97 | ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。 98 | virtual void RegisterFront(char *pszFrontAddress) = 0; 99 | 100 | ///注册名字服务器网络地址 101 | ///@param pszNsAddress:名字服务器网络地址。 102 | ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:12001”。 103 | ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”12001”代表服务器端口号。 104 | ///@remark RegisterNameServer优先于RegisterFront 105 | virtual void RegisterNameServer(char *pszNsAddress) = 0; 106 | 107 | ///注册名字服务器用户信息 108 | ///@param pFensUserInfo:用户信息。 109 | virtual void RegisterFensUserInfo(CThostFtdcFensUserInfoField * pFensUserInfo) = 0; 110 | 111 | ///注册回调接口 112 | ///@param pSpi 派生自回调接口类的实例 113 | virtual void RegisterSpi(CThostFtdcMdSpi *pSpi) = 0; 114 | 115 | ///订阅行情。 116 | ///@param ppInstrumentID 合约ID 117 | ///@param nCount 要订阅/退订行情的合约个数 118 | ///@remark 119 | virtual int SubscribeMarketData(char *ppInstrumentID[], int nCount) = 0; 120 | 121 | ///退订行情。 122 | ///@param ppInstrumentID 合约ID 123 | ///@param nCount 要订阅/退订行情的合约个数 124 | ///@remark 125 | virtual int UnSubscribeMarketData(char *ppInstrumentID[], int nCount) = 0; 126 | 127 | ///用户登录请求 128 | virtual int ReqUserLogin(CThostFtdcReqUserLoginField *pReqUserLoginField, int nRequestID) = 0; 129 | 130 | 131 | ///登出请求 132 | virtual int ReqUserLogout(CThostFtdcUserLogoutField *pUserLogout, int nRequestID) = 0; 133 | protected: 134 | ~CThostFtdcMdApi(){}; 135 | }; 136 | 137 | #endif 138 | -------------------------------------------------------------------------------- /include/ThostFtdcMdApi.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////// 2 | ///@system 新一代交易所系统 3 | ///@company 上海期货信息技术有限公司 4 | ///@file ThostFtdcMdApi.h 5 | ///@brief 定义了客户端接口 6 | ///@history 7 | ///20060106 赵鸿昊 创建该文件 8 | ///////////////////////////////////////////////////////////////////////// 9 | 10 | #if !defined(THOST_FTDCMDAPI_H) 11 | #define THOST_FTDCMDAPI_H 12 | 13 | #if _MSC_VER > 1000 14 | #pragma once 15 | #endif // _MSC_VER > 1000 16 | 17 | #include "ThostFtdcUserApiStruct.h" 18 | 19 | #if defined(ISLIB) && defined(WIN32) 20 | #ifdef LIB_MD_API_EXPORT 21 | #define MD_API_EXPORT __declspec(dllexport) 22 | #else 23 | #define MD_API_EXPORT __declspec(dllimport) 24 | #endif 25 | #else 26 | #define MD_API_EXPORT 27 | #endif 28 | 29 | class CThostFtdcMdSpi 30 | { 31 | public: 32 | ///当客户端与交易后台建立起通信连接时(还未登录前),该方法被调用。 33 | virtual void OnFrontConnected(){}; 34 | 35 | ///当客户端与交易后台通信连接断开时,该方法被调用。当发生这个情况后,API会自动重新连接,客户端可不做处理。 36 | ///@param nReason 错误原因 37 | /// 0x1001 网络读失败 38 | /// 0x1002 网络写失败 39 | /// 0x2001 接收心跳超时 40 | /// 0x2002 发送心跳失败 41 | /// 0x2003 收到错误报文 42 | virtual void OnFrontDisconnected(int nReason){}; 43 | 44 | ///心跳超时警告。当长时间未收到报文时,该方法被调用。 45 | ///@param nTimeLapse 距离上次接收报文的时间 46 | virtual void OnHeartBeatWarning(int nTimeLapse){}; 47 | 48 | 49 | ///登录请求响应 50 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 51 | 52 | ///登出请求响应 53 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 54 | 55 | ///错误应答 56 | virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 57 | 58 | ///订阅行情应答 59 | virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 60 | 61 | ///取消订阅行情应答 62 | virtual void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField *pSpecificInstrument, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) {}; 63 | 64 | ///深度行情通知 65 | virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData) {}; 66 | }; 67 | 68 | class MD_API_EXPORT CThostFtdcMdApi 69 | { 70 | public: 71 | ///创建MdApi 72 | ///@param pszFlowPath 存贮订阅信息文件的目录,默认为当前目录 73 | ///@return 创建出的UserApi 74 | ///modify for udp marketdata 75 | static CThostFtdcMdApi *CreateFtdcMdApi(const char *pszFlowPath = "", const bool bIsUsingUdp=false, const bool bIsMulticast=false); 76 | 77 | ///删除接口对象本身 78 | ///@remark 不再使用本接口对象时,调用该函数删除接口对象 79 | virtual void Release() = 0; 80 | 81 | ///初始化 82 | ///@remark 初始化运行环境,只有调用后,接口才开始工作 83 | virtual void Init() = 0; 84 | 85 | ///等待接口线程结束运行 86 | ///@return 线程退出代码 87 | virtual int Join() = 0; 88 | 89 | ///获取当前交易日 90 | ///@retrun 获取到的交易日 91 | ///@remark 只有登录成功后,才能得到正确的交易日 92 | virtual const char *GetTradingDay() = 0; 93 | 94 | ///注册前置机网络地址 95 | ///@param pszFrontAddress:前置机网络地址。 96 | ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 97 | ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。 98 | virtual void RegisterFront(char *pszFrontAddress) = 0; 99 | 100 | ///注册名字服务器网络地址 101 | ///@param pszNsAddress:名字服务器网络地址。 102 | ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:12001”。 103 | ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”12001”代表服务器端口号。 104 | ///@remark RegisterNameServer优先于RegisterFront 105 | virtual void RegisterNameServer(char *pszNsAddress) = 0; 106 | 107 | ///注册名字服务器用户信息 108 | ///@param pFensUserInfo:用户信息。 109 | virtual void RegisterFensUserInfo(CThostFtdcFensUserInfoField * pFensUserInfo) = 0; 110 | 111 | ///注册回调接口 112 | ///@param pSpi 派生自回调接口类的实例 113 | virtual void RegisterSpi(CThostFtdcMdSpi *pSpi) = 0; 114 | 115 | ///订阅行情。 116 | ///@param ppInstrumentID 合约ID 117 | ///@param nCount 要订阅/退订行情的合约个数 118 | ///@remark 119 | virtual int SubscribeMarketData(char *ppInstrumentID[], int nCount) = 0; 120 | 121 | ///退订行情。 122 | ///@param ppInstrumentID 合约ID 123 | ///@param nCount 要订阅/退订行情的合约个数 124 | ///@remark 125 | virtual int UnSubscribeMarketData(char *ppInstrumentID[], int nCount) = 0; 126 | 127 | ///用户登录请求 128 | virtual int ReqUserLogin(CThostFtdcReqUserLoginField *pReqUserLoginField, int nRequestID) = 0; 129 | 130 | 131 | ///登出请求 132 | virtual int ReqUserLogout(CThostFtdcUserLogoutField *pUserLogout, int nRequestID) = 0; 133 | protected: 134 | ~CThostFtdcMdApi(){}; 135 | }; 136 | 137 | #endif 138 | -------------------------------------------------------------------------------- /tradeapitest/tradeapitest.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "tradeapitest.h" 3 | 4 | // tradeapitest.cpp : 5 | // 一个简单的例子,介绍CThostFtdcTraderApi和CThostFtdcTraderSpi接口的使用。 6 | // 本例将演示一个报单录入操作的过程 7 | 8 | 9 | 10 | // 报单录入操作是否完成的标志 11 | // Create a manual reset event with no signal 12 | // 这段需要注释掉,这是windows下的代码 13 | // TODO 映射功能到linux 14 | //HANDLE g_hEvent = CreateEvent(NULL, true, false, NULL); 15 | 16 | // 会员代码 17 | TThostFtdcBrokerIDType g_chBrokerID; 18 | // 交易用户代码 19 | TThostFtdcUserIDType g_chUserID; 20 | 21 | using namespace std; 22 | 23 | 24 | 25 | 26 | 27 | class CSimpleHandler : public CThostFtdcTraderSpi{ 28 | 29 | public: 30 | // 构造函数,需要一个有效的指向CThostFtdcMduserApi实例的指针 31 | CSimpleHandler(CThostFtdcTraderApi *pUserApi) : m_pUserApi(pUserApi) {} 32 | ~CSimpleHandler() {} 33 | 34 | // 当客户端与交易托管系统建立起通信连接,客户端需要进行登录 35 | //* 36 | virtual void OnFrontConnected() 37 | { 38 | CThostFtdcReqUserLoginField reqUserLogin; 39 | // get BrokerID 40 | printf("BrokerID:"); 41 | scanf("%s", (char*) &g_chBrokerID); 42 | strcpy(reqUserLogin.BrokerID, g_chBrokerID); 43 | // get userid 44 | printf("userid:"); 45 | scanf("%s", (char*) &g_chUserID); 46 | strcpy(reqUserLogin.UserID, g_chUserID); 47 | // get password 48 | printf("password:"); 49 | scanf("%s", (char*) &reqUserLogin.Password); 50 | // 发出登陆请求 51 | m_pUserApi->ReqUserLogin(&reqUserLogin, 0); 52 | } 53 | //*/ 54 | 55 | // 当客户端与交易托管系统通信连接断开时,该方法被调用 56 | //* 57 | virtual void OnFrontDisconnected(int nReason) 58 | { 59 | // 当发生这个情况后,API会自动重新连接,客户端可不做处理 60 | printf("OnFrontDisconnected.\n"); 61 | } 62 | //*/ 63 | 64 | // 当客户端发出登录请求之后,该方法会被调用,通知客户端登录是否成功 65 | //* 66 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 67 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) 68 | { 69 | printf("OnRspUserLogin:\n"); 70 | printf("ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, 71 | pRspInfo->ErrorMsg); 72 | printf("RequestID=[%d], Chain=[%d]\n", nRequestID, bIsLast); 73 | if (pRspInfo->ErrorID != 0) { 74 | // 端登失败,客户端需进行错误处理 75 | printf("Failed to login, errorcode=%d errormsg=%s requestid=%d chain=%d", 76 | pRspInfo->ErrorID, pRspInfo->ErrorMsg, nRequestID, bIsLast); 77 | exit(-1); 78 | }else{ 79 | printf("登录成功\n"); 80 | } 81 | 82 | // 端登成功,发出报单录入请求 83 | CThostFtdcInputOrderField ord; 84 | memset(&ord, 0, sizeof(ord)); 85 | //经纪公司代码 86 | strcpy(ord.BrokerID, g_chBrokerID); 87 | //投资者代码 88 | strcpy(ord.InvestorID, "12345"); 89 | // 合约代码 90 | strcpy(ord.InstrumentID, "cn0601"); 91 | ///报单引用 92 | strcpy(ord.OrderRef, "000000000001"); 93 | // 用户代码 94 | strcpy(ord.UserID, g_chUserID); 95 | // 报单价格条件 96 | ord.OrderPriceType = THOST_FTDC_OPT_LimitPrice; 97 | // 买卖方向 98 | ord.Direction = THOST_FTDC_D_Buy; 99 | // 组合开平标志 100 | strcpy(ord.CombOffsetFlag, "0"); 101 | // 组合投机套保标志 102 | strcpy(ord.CombHedgeFlag, "1"); 103 | // 价格 104 | ord.LimitPrice = 50000; 105 | // 数量 106 | ord.VolumeTotalOriginal = 10; 107 | // 有效期类型 108 | ord.TimeCondition = THOST_FTDC_TC_GFD; 109 | // GTD日期 110 | strcpy(ord.GTDDate, ""); 111 | // 成交量类型 112 | ord.VolumeCondition = THOST_FTDC_VC_AV; 113 | // 最小成交量 114 | ord.MinVolume = 0; 115 | // 触发条件 116 | ord.ContingentCondition = THOST_FTDC_CC_Immediately; 117 | // 止损价 118 | ord.StopPrice = 0; 119 | // 强平原因 120 | ord.ForceCloseReason = THOST_FTDC_FCC_NotForceClose; 121 | // 自动挂起标志 122 | ord.IsAutoSuspend = 0; 123 | m_pUserApi->ReqOrderInsert(&ord, 1); 124 | } 125 | //*/ 126 | 127 | // 报单录入应答 128 | //* 129 | virtual void OnRspOrderInsert(CThostFtdcInputOrderField *pInputOrder, 130 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) 131 | { 132 | // 输出报单录入结果 133 | printf("ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, 134 | pRspInfo->ErrorMsg); 135 | // 通知报单录入完成 136 | // TODO 映射功能到linux 137 | //SetEvent(g_hEvent); 138 | }; 139 | //*/ 140 | 141 | 142 | ///报单回报 143 | //* 144 | virtual void OnRtnOrder(CThostFtdcOrderField *pOrder) 145 | { 146 | printf("OnRtnOrder:\n"); 147 | printf("OrderSysID=[%s]\n", pOrder->OrderSysID); 148 | } 149 | //*/ 150 | 151 | // 针对用户请求的出错通知 152 | //* 153 | virtual void OnRspError(CThostFtdcRspInfoField *pRspInfo, int nRequestID, 154 | bool bIsLast) 155 | { 156 | printf("OnRspError:\n"); 157 | printf("ErrorCode=[%d], ErrorMsg=[%s]\n", pRspInfo->ErrorID, 158 | pRspInfo->ErrorMsg); 159 | printf("RequestID=[%d], Chain=[%d]\n", nRequestID, bIsLast); 160 | // 客户端需进行错误处理 161 | //{客户端的错误处理} 162 | } 163 | //*/ 164 | 165 | private: 166 | // 指向CThostFtdcMduserApi实例的指针 167 | CThostFtdcTraderApi *m_pUserApi; 168 | }; 169 | 170 | 171 | 172 | int main() 173 | { 174 | // 产生一个CThostFtdcTraderApi实例 175 | CThostFtdcTraderApi *pUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 176 | // 产生一个事件处理的实例 177 | CSimpleHandler sh(pUserApi); 178 | // 注册一事件处理的实例 179 | pUserApi->RegisterSpi(&sh); 180 | // 订阅私有流 181 | // TERT_RESTART:从本交易日开始重传 182 | // TERT_RESUME:从上次收到的续传 183 | // TERT_QUICK:只传送登录后私有流的内容 184 | pUserApi->SubscribePrivateTopic(THOST_TERT_RESUME); 185 | // 订阅公共流 186 | // TERT_RESTART:从本交易日开始重传 187 | // TERT_RESUME:从上次收到的续传 188 | // TERT_QUICK:只传送登录后公共流的内容 189 | pUserApi->SubscribePublicTopic(THOST_TERT_RESUME); 190 | // 设置交易托管系统服务的地址,可以注册多个地址备用 191 | //pUserApi->RegisterFront((char*) "tcp://172.16.0.31:57205"); 192 | //pUserApi->RegisterFront((char*) "tcp://ctpmn1-front1.citicsf.com:51213"); 193 | //pUserApi->RegisterFront((char*) "tcp://101.231.96.18:51213"); 194 | pUserApi->RegisterFront((char*) "tcp://101.231.96.18:51205"); 195 | 196 | // 使客户端开始与后台服务建立连接 197 | pUserApi->Init(); 198 | // 客户端等待报单操作完成 199 | // 这段必须注释掉,因为这是windows下的API 200 | // TODO:逻辑需要移植到linux下 201 | //WaitForSingleObject(g_hEvent, INFINITE); 202 | // 释放API实例 203 | pUserApi->Release(); 204 | 205 | return 0; 206 | } 207 | -------------------------------------------------------------------------------- /QryExchange.all/QryExchange.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询交易所响应 79 | virtual void OnRspQryExchange( 80 | CThostFtdcExchangeField * pExchange, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | //printf("OnRspQryExchange():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryExchange():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pExchange != NULL ) { 98 | // 读取返回信息,并做编码转化 99 | ///交易所代码 TThostFtdcExchangeIDType char[9] 100 | char ExchangeID[27]; 101 | gbk2utf8(pExchange->ExchangeID,ExchangeID,sizeof(ExchangeID)); 102 | ///交易所名称 TThostFtdcExchangeNameType char[61] 103 | char ExchangeName[183]; 104 | gbk2utf8(pExchange->ExchangeName,ExchangeName,sizeof(ExchangeName)); 105 | ///交易所属性 TThostFtdcExchangePropertyType char 106 | //// THOST_FTDC_EXP_Normal '0' 正常 107 | //// THOST_FTDC_EXP_GenOrderByTrade '1' 根据成交生成报单 108 | char ExchangeProperty = pExchange->ExchangeProperty; 109 | 110 | printf("ExchangeID=%s,ExchangeName=%s,ExchangeProperty=%c\n",ExchangeID,ExchangeName,ExchangeProperty); 111 | 112 | } 113 | 114 | // 如果响应函数已经返回最后一条信息 115 | if(bIsLast) { 116 | // 通知主过程,响应函数将结束 117 | sem_post(&sem); 118 | } 119 | } 120 | 121 | }; 122 | 123 | 124 | int main() { 125 | 126 | // 初始化线程同步变量 127 | sem_init(&sem,0,0); 128 | 129 | // 从环境变量中读取登录信息 130 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 131 | if ( CTP_FrontAddress == NULL ) { 132 | printf("环境变量CTP_FrontAddress没有设置\n"); 133 | return(0); 134 | } 135 | 136 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 137 | if ( CTP_BrokerId == NULL ) { 138 | printf("环境变量CTP_BrokerId没有设置\n"); 139 | return(0); 140 | } 141 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 142 | 143 | char * CTP_UserId = getenv("CTP_UserId"); 144 | if ( CTP_UserId == NULL ) { 145 | printf("环境变量CTP_UserId没有设置\n"); 146 | return(0); 147 | } 148 | strcpy(userLoginField.UserID,CTP_UserId); 149 | 150 | char * CTP_Password = getenv("CTP_Password"); 151 | if ( CTP_Password == NULL ) { 152 | printf("环境变量CTP_Password没有设置\n"); 153 | return(0); 154 | } 155 | strcpy(userLoginField.Password,CTP_Password); 156 | 157 | // 创建TraderAPI和回调响应控制器的实例 158 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 159 | CTraderHandler traderHandler = CTraderHandler(); 160 | CTraderHandler * pTraderHandler = &traderHandler; 161 | pTraderApi->RegisterSpi(pTraderHandler); 162 | 163 | // 设置服务器地址 164 | pTraderApi->RegisterFront(CTP_FrontAddress); 165 | // 链接交易系统 166 | pTraderApi->Init(); 167 | // 等待服务器发出登录消息 168 | sem_wait(&sem); 169 | // 发出登陆请求 170 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 171 | // 等待登录成功消息 172 | sem_wait(&sem); 173 | 174 | //////////////////////////////////////////////////////////////////////////////////////////////// 175 | ///请求查询交易所 176 | /////////////////////////////////////////////////////////////////////////////////////////////// 177 | // 定义调用API的数据结构 178 | CThostFtdcQryExchangeField requestData; 179 | // 确保没有初始化的数据不会被访问 180 | memset(&requestData,0,sizeof(requestData)); 181 | // 为调用结构题设置参数信息 182 | ///交易所代码 TThostFtdcExchangeIDType char[9] 183 | strcpy(requestData.ExchangeID,""); 184 | 185 | 186 | // 调用API,并等待响应函数返回 187 | int result = pTraderApi->ReqQryExchange(&requestData,requestID++); 188 | sem_wait(&sem); 189 | 190 | ///////////////////////////////////////////////////////////////////////////////////////////////// 191 | 192 | 193 | // 拷贝用户登录信息到登出信息 194 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 195 | strcpy(userLogoutField.UserID, userLoginField.UserID); 196 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 197 | 198 | // 等待登出成功 199 | sem_wait(&sem); 200 | 201 | printf("主线程执行完毕!\n"); 202 | return(0); 203 | 204 | } 205 | -------------------------------------------------------------------------------- /QryTransferBank.1/QryTransferBank.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询转帐银行响应 79 | virtual void OnRspQryTransferBank( 80 | CThostFtdcTransferBankField * pTransferBank, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | //printf("OnRspQryTransferBank():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryTransferBank():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pTransferBank != NULL ) { 98 | // 读取返回信息,并做编码转化 99 | ///银行代码 TThostFtdcBankIDType char[4] 100 | char BankID[12]; 101 | gbk2utf8(pTransferBank->BankID,BankID,sizeof(BankID)); 102 | ///银行分中心代码 TThostFtdcBankBrchIDType char[5] 103 | char BankBrchID[15]; 104 | gbk2utf8(pTransferBank->BankBrchID,BankBrchID,sizeof(BankBrchID)); 105 | ///银行名称 TThostFtdcBankNameType char[101] 106 | char BankName[303]; 107 | gbk2utf8(pTransferBank->BankName,BankName,sizeof(BankName)); 108 | ///是否活跃 TThostFtdcBoolType int 109 | int IsActive = pTransferBank->IsActive; 110 | 111 | 112 | printf("BankID=%s,",BankID); // 银行代码 113 | printf("BankBrchID=%s,",BankBrchID); //银行分中心代码 114 | printf("BankName=%s,",BankName); // 银行名称 115 | printf("IsActive=%d\n",IsActive); //是否活跃 116 | 117 | } 118 | 119 | // 如果响应函数已经返回最后一条信息 120 | if(bIsLast) { 121 | // 通知主过程,响应函数将结束 122 | sem_post(&sem); 123 | } 124 | } 125 | 126 | }; 127 | 128 | 129 | int main() { 130 | 131 | // 初始化线程同步变量 132 | sem_init(&sem,0,0); 133 | 134 | // 从环境变量中读取登录信息 135 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 136 | if ( CTP_FrontAddress == NULL ) { 137 | printf("环境变量CTP_FrontAddress没有设置\n"); 138 | return(0); 139 | } 140 | 141 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 142 | if ( CTP_BrokerId == NULL ) { 143 | printf("环境变量CTP_BrokerId没有设置\n"); 144 | return(0); 145 | } 146 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 147 | 148 | char * CTP_UserId = getenv("CTP_UserId"); 149 | if ( CTP_UserId == NULL ) { 150 | printf("环境变量CTP_UserId没有设置\n"); 151 | return(0); 152 | } 153 | strcpy(userLoginField.UserID,CTP_UserId); 154 | 155 | char * CTP_Password = getenv("CTP_Password"); 156 | if ( CTP_Password == NULL ) { 157 | printf("环境变量CTP_Password没有设置\n"); 158 | return(0); 159 | } 160 | strcpy(userLoginField.Password,CTP_Password); 161 | 162 | // 创建TraderAPI和回调响应控制器的实例 163 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 164 | CTraderHandler traderHandler = CTraderHandler(); 165 | CTraderHandler * pTraderHandler = &traderHandler; 166 | pTraderApi->RegisterSpi(pTraderHandler); 167 | 168 | // 设置服务器地址 169 | pTraderApi->RegisterFront(CTP_FrontAddress); 170 | // 链接交易系统 171 | pTraderApi->Init(); 172 | // 等待服务器发出登录消息 173 | sem_wait(&sem); 174 | // 发出登陆请求 175 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 176 | // 等待登录成功消息 177 | sem_wait(&sem); 178 | 179 | //////////////////////////////////////////////////////////////////////////////////////////////// 180 | ///请求查询转帐银行 181 | /////////////////////////////////////////////////////////////////////////////////////////////// 182 | // 定义调用API的数据结构 183 | CThostFtdcQryTransferBankField requestData; 184 | // 确保没有初始化的数据不会被访问 185 | memset(&requestData,0,sizeof(requestData)); 186 | // 为调用结构题设置参数信息 187 | ///银行代码 TThostFtdcBankIDType char[4] 188 | strcpy(requestData.BankID,""); 189 | ///银行分中心代码 TThostFtdcBankBrchIDType char[5] 190 | strcpy(requestData.BankBrchID,""); 191 | 192 | 193 | // 调用API,并等待响应函数返回 194 | int result = pTraderApi->ReqQryTransferBank(&requestData,requestID++); 195 | sem_wait(&sem); 196 | 197 | ///////////////////////////////////////////////////////////////////////////////////////////////// 198 | 199 | 200 | // 拷贝用户登录信息到登出信息 201 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 202 | strcpy(userLogoutField.UserID, userLoginField.UserID); 203 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 204 | 205 | // 等待登出成功 206 | sem_wait(&sem); 207 | 208 | printf("主线程执行完毕!\n"); 209 | return(0); 210 | 211 | } 212 | -------------------------------------------------------------------------------- /create_api_tester.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf8 -*- 2 | 3 | import sys 4 | import os 5 | import cpphelper 6 | from jinja2 import Environment, FileSystemLoader 7 | from os import path 8 | 9 | 10 | def addEnumInfo(fields,typedefDict,enumDict): 11 | ''' 12 | 对一个field列表增加枚举信息 13 | fields field的列表 14 | typedefDict 类型定义字典,通过调用cpphelper.getTypedefDict获得 15 | enumDict 枚举类型字典,通过cpphelper.getEnumDict获得 16 | 返回 无返回信息直接作用在fields中 17 | ''' 18 | for field in fields: 19 | typedef = typedefDict[ field['type'] ] 20 | field['original'] = typedef['type'] 21 | field['len'] = typedef['len'] 22 | field['enums'] = enumDict.get(field['type']) 23 | 24 | 25 | 26 | def main(): 27 | ''' 28 | 从命令行上获取一个API名称(不含Req和OnRsp前缀),生成相关的API组合的测试代码 29 | 命令行参数1 API名称(如:QryTradingAccount,对应的请求函数为ReqQryTradingAccount,对应的相应函数为OnRspQryTradingAccount) 30 | ''' 31 | if len(sys.argv) != 2 : 32 | print '命令使用格式不正确,请参照以下例子:' 33 | print '$ python create_api_tester.py API名称' 34 | print '其中API名称形如QryTradingAccount,对应的请求函数为ReqQryTradingAccount,对应的相应函数为OnRspQryTradingAccount。' 35 | return(0) 36 | 37 | apiName = sys.argv[1] 38 | print '开始生成%s的接口测试代码:...' % apiName 39 | 40 | # 检查目录是否存在 41 | #apiName = 'QryInstrument' 42 | if path.exists(apiName): 43 | print '同名(%s)目录(或文件)已经存在,请确认并修改其位置或重命名后再执行命令\n' 44 | return(0) 45 | 46 | # 创建目录 47 | os.makedirs(apiName) 48 | 49 | # 读取相关cpp头文件 50 | ThostFtdcTraderApi_h = cpphelper.getCppHeader('include/ThostFtdcTraderApi.h',['TRADER_API_EXPORT']) 51 | ThostFtdcUserApiStruct_h = cpphelper.getCppHeader('include/ThostFtdcUserApiStruct.h') 52 | typedefDict = cpphelper.getTypedefDict('include/ThostFtdcUserApiDataType.h') 53 | enumDict = cpphelper.getEnumDict('include/ThostFtdcUserApiDataType.h') 54 | 55 | # 将TThostFtdcOffsetFlagType类型数据映射到TThostFtdcCombOffsetFlagType 56 | # TThostFtdcCombOffsetFlagType表示多个TThostFtdcOffsetFlagType位的组合其实际字段含义在TThostFtdcOffsetFlagType中说明 57 | enumDict['TThostFtdcCombOffsetFlagType'] = enumDict['TThostFtdcOffsetFlagType'] 58 | # 情况同TThostFtdcCombOffsetFlagType 59 | enumDict['TThostFtdcCombHedgeFlagType'] = enumDict['TThostFtdcHedgeFlagType'] 60 | 61 | # 获取响应函数相关信息 62 | responseMethodName = 'OnRsp%s' % apiName 63 | CThostFtdcTraderSpi = cpphelper.getClass(ThostFtdcTraderApi_h,'CThostFtdcTraderSpi') 64 | responseMethod = cpphelper.getClassMethod(CThostFtdcTraderSpi,'public',responseMethodName) 65 | respParameters = cpphelper.getMethodParameters(responseMethod) 66 | 67 | # 获取调用函数相关信息 68 | requestMethodName = 'Req%s' % apiName 69 | CThostFtdcTraderApi = cpphelper.getClass(ThostFtdcTraderApi_h,'CThostFtdcTraderApi') 70 | requestMethod = cpphelper.getClassMethod(CThostFtdcTraderApi,'public',requestMethodName) 71 | reqParameters = cpphelper.getMethodParameters(requestMethod) 72 | 73 | # 读取报单通知回调函数信息 74 | onRtnOrderMethodName = 'OnRtnOrder' 75 | onRtnOrderMethod = cpphelper.getClassMethod(CThostFtdcTraderSpi,'public',onRtnOrderMethodName) 76 | onRtnOrderParameters =cpphelper.getMethodParameters(onRtnOrderMethod) 77 | 78 | # 读取成交通知回调函数信息 79 | onRtnTradeMethodName = 'OnRtnTrade' 80 | onRtnTradeMethod = cpphelper.getClassMethod(CThostFtdcTraderSpi,'public',onRtnTradeMethodName) 81 | onRtnTradeParameters =cpphelper.getMethodParameters(onRtnTradeMethod) 82 | 83 | # 读取入单错误回调函数信息 84 | onErrRtnOrderInsertMethodName = 'OnErrRtnOrderInsert' 85 | onErrRtnOrderInsertMethod = cpphelper.getClassMethod(CThostFtdcTraderSpi,'public',onErrRtnOrderInsertMethodName) 86 | onErrRtnOrderInsertParameters =cpphelper.getMethodParameters(onErrRtnOrderInsertMethod) 87 | 88 | # 检查请求函数的参数格式是否符合预期 89 | methedDeclare = '%s(%s)' % (requestMethod['name'], 90 | ','.join('%s %s' % (parameter['type'],parameter['name']) for parameter in reqParameters)) 91 | if len(reqParameters) != 2 : 92 | print '无法处理的请求API结构:%s' % methedDeclare 93 | return(0) 94 | if reqParameters[1]['name'] != 'nRequestID' : 95 | print '无法处理的请求API结构:%s' % methedDeclare 96 | return(0) 97 | if reqParameters[0]['pointer'] == False : 98 | print '无法处理的请求API结构:%s' % methedDeclare 99 | return(0) 100 | 101 | # 读取响应函数的返回的数据类型的所有字段 102 | responseDataStruct = cpphelper.getClass(ThostFtdcUserApiStruct_h,respParameters[0]['raw_type']) 103 | responseFields = cpphelper.getStructFields(responseDataStruct) 104 | addEnumInfo(responseFields,typedefDict,enumDict) 105 | 106 | # 读取请求类型的所有字段列表和原始类型 107 | requestDataStruct = cpphelper.getClass(ThostFtdcUserApiStruct_h,reqParameters[0]['raw_type']) 108 | requestFields = cpphelper.getStructFields(requestDataStruct) 109 | addEnumInfo(requestFields,typedefDict,enumDict) 110 | 111 | # 读取报单通知返回结构的所有字段列表和原始类型 112 | onRtnOrderDataStruct = cpphelper.getClass(ThostFtdcUserApiStruct_h,onRtnOrderParameters[0]['raw_type']) 113 | onRtnOrderFields = cpphelper.getStructFields(onRtnOrderDataStruct) 114 | addEnumInfo(onRtnOrderFields,typedefDict,enumDict) 115 | 116 | # 读取成交通知返回结构的所有字段列表和原始类型 117 | onRtnTradeDataStruct = cpphelper.getClass(ThostFtdcUserApiStruct_h,onRtnTradeParameters[0]['raw_type']) 118 | onRtnTradeFields = cpphelper.getStructFields(onRtnTradeDataStruct) 119 | addEnumInfo(onRtnTradeFields,typedefDict,enumDict) 120 | 121 | # 读取订单录入错误信息回报消息的字段列表和原始类型 122 | onErrRtnOrderInsertDataStruct = cpphelper.getClass(ThostFtdcUserApiStruct_h,onErrRtnOrderInsertParameters[0]['raw_type']) 123 | onErrRtnOrderInsertFields = cpphelper.getStructFields(onErrRtnOrderInsertDataStruct) 124 | addEnumInfo(onErrRtnOrderInsertFields,typedefDict,enumDict) 125 | 126 | 127 | # 生成模板所需的信息集 128 | data = { 129 | 'apiName' : apiName, 130 | # 响应函数 131 | 'responseMethod' : responseMethod, 132 | 'respParameters' : respParameters, 133 | 'responseFields' : responseFields, 134 | # 请求函数 135 | 'requestMethod' : requestMethod, 136 | 'reqParameters' : reqParameters, 137 | 'requestFields' : requestFields, 138 | # 报单通知函数 139 | 'onRtnOrderMethod' : onRtnOrderMethod, 140 | 'onRtnOrderParameters' : onRtnOrderParameters, 141 | 'onRtnOrderFields' : onRtnOrderFields, 142 | # 成交通知函数 143 | 'onRtnTradeMethod' : onRtnTradeMethod, 144 | 'onRtnTradeParameters' : onRtnTradeParameters, 145 | 'onRtnTradeFields' : onRtnTradeFields, 146 | # 订单错误回报函数 147 | 'onErrRtnOrderInsertMethod' : onErrRtnOrderInsertMethod, 148 | 'onErrRtnOrderInsertParameters' : onErrRtnOrderInsertParameters, 149 | 'onErrRtnOrderInsertFields' : onErrRtnOrderInsertFields 150 | } 151 | 152 | 153 | # 读取模板信息 154 | env = Environment(loader=FileSystemLoader('templates')) 155 | 156 | # 生成代码文件 157 | source = env.get_template('source.tpl.cpp') 158 | sourcePath = path.join(apiName,'%s.cpp' % apiName) 159 | with open(sourcePath, 'w') as f : 160 | # 间模板生成到文件 161 | f.write(source.render(**data).encode('utf-8')) 162 | # 使用astyle对文件进行格式化 163 | os.system('astyle %s' % sourcePath) 164 | 165 | # 生成make文件 166 | makefile = env.get_template('Makefile.tpl') 167 | makefilePath = path.join(apiName,'Makefile') 168 | with open(makefilePath, 'w') as f : 169 | f.write(makefile.render(**data).encode('utf-8')) 170 | 171 | 172 | 173 | if __name__ == '__main__': 174 | main() 175 | 176 | 177 | -------------------------------------------------------------------------------- /QryTradingCode.1/QryTradingCode.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询交易编码响应 79 | virtual void OnRspQryTradingCode( 80 | CThostFtdcTradingCodeField * pTradingCode, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | printf("OnRspQryTradingCode():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryTradingCode():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pTradingCode != NULL ) { 98 | printf("pTradingCode != NULL"); 99 | // 读取返回信息,并做编码转化 100 | ///投资者代码 TThostFtdcInvestorIDType char[13] 101 | char InvestorID[39]; 102 | gbk2utf8(pTradingCode->InvestorID,InvestorID,sizeof(InvestorID)); 103 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 104 | char BrokerID[33]; 105 | gbk2utf8(pTradingCode->BrokerID,BrokerID,sizeof(BrokerID)); 106 | ///交易所代码 TThostFtdcExchangeIDType char[9] 107 | char ExchangeID[27]; 108 | gbk2utf8(pTradingCode->ExchangeID,ExchangeID,sizeof(ExchangeID)); 109 | ///客户代码 TThostFtdcClientIDType char[11] 110 | char ClientID[33]; 111 | gbk2utf8(pTradingCode->ClientID,ClientID,sizeof(ClientID)); 112 | ///是否活跃 TThostFtdcBoolType int 113 | int IsActive = pTradingCode->IsActive; 114 | ///交易编码类型 TThostFtdcClientIDTypeType char 115 | //// THOST_FTDC_CIDT_Speculation '1' 投机 116 | //// THOST_FTDC_CIDT_Arbitrage '2' 套利 117 | //// THOST_FTDC_CIDT_Hedge '3' 套保 118 | char ClientIDType = pTradingCode->ClientIDType; 119 | 120 | printf("InvestorID=%s\n",InvestorID); // 投资者代码 121 | printf("BrokerID=%s\n",BrokerID); // 经纪公司代码 122 | printf("ExchangeID=%s\n",ExchangeID); // 交易所代码 123 | printf("ClientID=%s\n",ClientID); // 客户代码 124 | printf("IsActive=%d\n",IsActive); // 是否活跃 125 | printf("ClientIDType=%c\n",ClientIDType); //交易编码类型 126 | 127 | 128 | } 129 | 130 | // 如果响应函数已经返回最后一条信息 131 | if(bIsLast) { 132 | // 通知主过程,响应函数将结束 133 | sem_post(&sem); 134 | } 135 | } 136 | 137 | }; 138 | 139 | 140 | int main() { 141 | 142 | // 初始化线程同步变量 143 | sem_init(&sem,0,0); 144 | 145 | // 从环境变量中读取登录信息 146 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 147 | if ( CTP_FrontAddress == NULL ) { 148 | printf("环境变量CTP_FrontAddress没有设置\n"); 149 | return(0); 150 | } 151 | 152 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 153 | if ( CTP_BrokerId == NULL ) { 154 | printf("环境变量CTP_BrokerId没有设置\n"); 155 | return(0); 156 | } 157 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 158 | 159 | char * CTP_UserId = getenv("CTP_UserId"); 160 | if ( CTP_UserId == NULL ) { 161 | printf("环境变量CTP_UserId没有设置\n"); 162 | return(0); 163 | } 164 | strcpy(userLoginField.UserID,CTP_UserId); 165 | 166 | char * CTP_Password = getenv("CTP_Password"); 167 | if ( CTP_Password == NULL ) { 168 | printf("环境变量CTP_Password没有设置\n"); 169 | return(0); 170 | } 171 | strcpy(userLoginField.Password,CTP_Password); 172 | 173 | // 创建TraderAPI和回调响应控制器的实例 174 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 175 | CTraderHandler traderHandler = CTraderHandler(); 176 | CTraderHandler * pTraderHandler = &traderHandler; 177 | pTraderApi->RegisterSpi(pTraderHandler); 178 | 179 | // 设置服务器地址 180 | pTraderApi->RegisterFront(CTP_FrontAddress); 181 | // 链接交易系统 182 | pTraderApi->Init(); 183 | // 等待服务器发出登录消息 184 | sem_wait(&sem); 185 | // 发出登陆请求 186 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 187 | // 等待登录成功消息 188 | sem_wait(&sem); 189 | 190 | //////////////////////////////////////////////////////////////////////////////////////////////// 191 | ///请求查询交易编码 192 | /////////////////////////////////////////////////////////////////////////////////////////////// 193 | // 定义调用API的数据结构 194 | CThostFtdcQryTradingCodeField requestData; 195 | // 确保没有初始化的数据不会被访问 196 | memset(&requestData,0,sizeof(requestData)); 197 | // 为调用结构题设置参数信息 198 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 199 | strcpy(requestData.BrokerID,""); 200 | ///投资者代码 TThostFtdcInvestorIDType char[13] 201 | strcpy(requestData.InvestorID,""); 202 | ///交易所代码 TThostFtdcExchangeIDType char[9] 203 | strcpy(requestData.ExchangeID,""); 204 | ///客户代码 TThostFtdcClientIDType char[11] 205 | strcpy(requestData.ClientID,""); 206 | ///交易编码类型 TThostFtdcClientIDTypeType char 207 | //// THOST_FTDC_CIDT_Speculation '1' 投机 208 | //// THOST_FTDC_CIDT_Arbitrage '2' 套利 209 | //// THOST_FTDC_CIDT_Hedge '3' 套保 210 | requestData.ClientIDType = '1'; 211 | 212 | 213 | // 调用API,并等待响应函数返回 214 | int result = pTraderApi->ReqQryTradingCode(&requestData,requestID++); 215 | sem_wait(&sem); 216 | 217 | ///////////////////////////////////////////////////////////////////////////////////////////////// 218 | 219 | 220 | // 拷贝用户登录信息到登出信息 221 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 222 | strcpy(userLogoutField.UserID, userLoginField.UserID); 223 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 224 | 225 | // 等待登出成功 226 | sem_wait(&sem); 227 | 228 | printf("主线程执行完毕!\n"); 229 | return(0); 230 | 231 | } 232 | -------------------------------------------------------------------------------- /QrySettlementInfoConfirm.1/QrySettlementInfoConfirm.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 定时器相关 18 | #include 19 | #include 20 | 21 | 22 | // 字符串编码转化 23 | #include 24 | 25 | 26 | 27 | 28 | // 登录请求结构体 29 | CThostFtdcReqUserLoginField userLoginField; 30 | // 用户请求结构体 31 | CThostFtdcUserLogoutField userLogoutField; 32 | // 线程同步标志 33 | sem_t sem; 34 | // requestID 35 | int requestID = 0; 36 | 37 | 38 | class CTraderHandler : public CThostFtdcTraderSpi { 39 | 40 | public: 41 | 42 | CTraderHandler() { 43 | printf("CTraderHandler():被执行...\n"); 44 | } 45 | 46 | // 允许登录事件 47 | virtual void OnFrontConnected() { 48 | static int i = 0; 49 | printf("OnFrontConnected():被执行...\n"); 50 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 51 | if (i++==0) { 52 | sem_post(&sem); 53 | } 54 | } 55 | 56 | // 登录结果响应 57 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 58 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 59 | printf("OnRspUserLogin():被执行...\n"); 60 | if (pRspInfo->ErrorID == 0) { 61 | printf("登录成功!\n"); 62 | sem_post(&sem); 63 | } else { 64 | printf("登录失败!\n"); 65 | } 66 | } 67 | 68 | // 登出结果响应 69 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 70 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 71 | printf("OnReqUserLogout():被执行...\n"); 72 | if (pRspInfo->ErrorID == 0) { 73 | printf("登出成功!\n"); 74 | sem_post(&sem); 75 | } else { 76 | printf("登出失败!\n"); 77 | } 78 | } 79 | 80 | // 错误信息响应方法 81 | virtual void OnRspError 82 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 83 | printf("OnRspError():被执行...\n"); 84 | } 85 | 86 | ///请求查询结算信息确认响应 87 | virtual void OnRspQrySettlementInfoConfirm( 88 | CThostFtdcSettlementInfoConfirmField * pSettlementInfoConfirm, 89 | CThostFtdcRspInfoField * pRspInfo, 90 | int nRequestID, 91 | bool bIsLast 92 | ) { 93 | printf("OnRspQrySettlementInfoConfirm():被执行...\n"); 94 | 95 | // 进程返回结果检查 96 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 97 | // typedef int TThostFtdcErrorIDType; 98 | // typedef char TThostFtdcErrorMsgType[81]; 99 | char ErrorMsg[243]; 100 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 101 | printf("OnRspQrySettlementInfoConfirm():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 102 | } 103 | 104 | // 如果有返回结果读取返回信息 105 | if ( pSettlementInfoConfirm != NULL ) { 106 | // 读取返回信息,并做编码转化 107 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 108 | char BrokerID[33]; 109 | gbk2utf8(pSettlementInfoConfirm->BrokerID,BrokerID,sizeof(BrokerID)); 110 | ///投资者代码 TThostFtdcInvestorIDType char[13] 111 | char InvestorID[39]; 112 | gbk2utf8(pSettlementInfoConfirm->InvestorID,InvestorID,sizeof(InvestorID)); 113 | ///确认日期 TThostFtdcDateType char[9] 114 | char ConfirmDate[27]; 115 | gbk2utf8(pSettlementInfoConfirm->ConfirmDate,ConfirmDate,sizeof(ConfirmDate)); 116 | ///确认时间 TThostFtdcTimeType char[9] 117 | char ConfirmTime[27]; 118 | gbk2utf8(pSettlementInfoConfirm->ConfirmTime,ConfirmTime,sizeof(ConfirmTime)); 119 | 120 | printf("结算信息已确认:ConfirmDate=%s,ConfirmTime=%s\n",ConfirmDate,ConfirmTime); 121 | 122 | }else{ 123 | printf("结算信息未确认.\n"); 124 | } 125 | 126 | // 如果响应函数已经返回最后一条信息 127 | if(bIsLast) { 128 | // 通知主过程,响应函数将结束 129 | sem_post(&sem); 130 | } 131 | } 132 | 133 | }; 134 | 135 | 136 | // 定时器控制函数 137 | void timeout_handler(int signalno) { 138 | // 定时控制步骤 139 | } 140 | 141 | // 初始化定时器 142 | void init_timer(void) 143 | { 144 | // init sigaction 145 | struct sigaction act; 146 | act.sa_handler = timeout_handler; 147 | act.sa_flags = 0; 148 | sigemptyset(&act.sa_mask); 149 | sigaction(SIGPROF, &act, NULL); 150 | // set timer 151 | struct itimerval val; 152 | val.it_value.tv_sec = 1; 153 | val.it_value.tv_usec = 0; 154 | val.it_interval = val.it_value; 155 | setitimer(ITIMER_PROF, &val, NULL); 156 | } 157 | 158 | 159 | int main() { 160 | 161 | // 初始化线程同步变量 162 | sem_init(&sem,0,0); 163 | 164 | // 从环境变量中读取登录信息 165 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 166 | if ( CTP_FrontAddress == NULL ) { 167 | printf("环境变量CTP_FrontAddress没有设置\n"); 168 | return(0); 169 | } 170 | 171 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 172 | if ( CTP_BrokerId == NULL ) { 173 | printf("环境变量CTP_BrokerId没有设置\n"); 174 | return(0); 175 | } 176 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 177 | 178 | char * CTP_UserId = getenv("CTP_UserId"); 179 | if ( CTP_UserId == NULL ) { 180 | printf("环境变量CTP_UserId没有设置\n"); 181 | return(0); 182 | } 183 | strcpy(userLoginField.UserID,CTP_UserId); 184 | 185 | char * CTP_Password = getenv("CTP_Password"); 186 | if ( CTP_Password == NULL ) { 187 | printf("环境变量CTP_Password没有设置\n"); 188 | return(0); 189 | } 190 | strcpy(userLoginField.Password,CTP_Password); 191 | 192 | // 创建TraderAPI和回调响应控制器的实例 193 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 194 | CTraderHandler traderHandler = CTraderHandler(); 195 | CTraderHandler * pTraderHandler = &traderHandler; 196 | pTraderApi->RegisterSpi(pTraderHandler); 197 | 198 | // 订阅相关信息推送 199 | //// THOST_TERT_RESTART:从本交易日开始重传 200 | //// THOST_TERT_RESUME:从上次收到的续传 201 | //// THOST_TERT_QUICK:只传送登录后私有流的内容 202 | pTraderApi->SubscribePrivateTopic(THOST_TERT_RESUME); 203 | // 订阅公共流 204 | //// THOST_TERT_RESTART:从本交易日开始重传 205 | //// THOST_TERT_RESUME:从上次收到的续传 206 | //// THOST_TERT_QUICK:只传送登录后公共流的内容 207 | pTraderApi->SubscribePublicTopic(THOST_TERT_RESUME); 208 | 209 | // 设置服务器地址 210 | pTraderApi->RegisterFront(CTP_FrontAddress); 211 | // 链接交易系统 212 | pTraderApi->Init(); 213 | // 等待服务器发出登录消息 214 | sem_wait(&sem); 215 | // 发出登陆请求 216 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 217 | // 等待登录成功消息 218 | sem_wait(&sem); 219 | 220 | //////////////////////////////////////////////////////////////////////////////////////////////// 221 | ///请求查询结算信息确认 222 | /////////////////////////////////////////////////////////////////////////////////////////////// 223 | // 定义调用API的数据结构 224 | CThostFtdcQrySettlementInfoConfirmField requestData; 225 | // 确保没有初始化的数据不会被访问 226 | memset(&requestData,0,sizeof(requestData)); 227 | // 为调用结构题设置参数信息 228 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 229 | strcpy(requestData.BrokerID,CTP_BrokerId); 230 | ///投资者代码 TThostFtdcInvestorIDType char[13] 231 | strcpy(requestData.InvestorID,CTP_UserId); 232 | 233 | 234 | // 调用API,并等待响应函数返回 235 | int result = pTraderApi->ReqQrySettlementInfoConfirm(&requestData,requestID++); 236 | sem_wait(&sem); 237 | 238 | ///////////////////////////////////////////////////////////////////////////////////////////////// 239 | 240 | 241 | // 拷贝用户登录信息到登出信息 242 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 243 | strcpy(userLogoutField.UserID, userLoginField.UserID); 244 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 245 | 246 | // 等待登出成功 247 | sem_wait(&sem); 248 | 249 | printf("主线程执行完毕!\n"); 250 | return(0); 251 | 252 | } 253 | -------------------------------------------------------------------------------- /SettlementInfoConfirm.1/SettlementInfoConfirm.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 定时器相关 18 | #include 19 | #include 20 | 21 | 22 | // 字符串编码转化 23 | #include 24 | 25 | 26 | 27 | 28 | // 登录请求结构体 29 | CThostFtdcReqUserLoginField userLoginField; 30 | // 用户请求结构体 31 | CThostFtdcUserLogoutField userLogoutField; 32 | // 线程同步标志 33 | sem_t sem; 34 | // requestID 35 | int requestID = 0; 36 | 37 | 38 | class CTraderHandler : public CThostFtdcTraderSpi { 39 | 40 | public: 41 | 42 | CTraderHandler() { 43 | printf("CTraderHandler():被执行...\n"); 44 | } 45 | 46 | // 允许登录事件 47 | virtual void OnFrontConnected() { 48 | static int i = 0; 49 | printf("OnFrontConnected():被执行...\n"); 50 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 51 | if (i++==0) { 52 | sem_post(&sem); 53 | } 54 | } 55 | 56 | // 登录结果响应 57 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 58 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 59 | printf("OnRspUserLogin():被执行...\n"); 60 | if (pRspInfo->ErrorID == 0) { 61 | printf("登录成功!\n"); 62 | sem_post(&sem); 63 | } else { 64 | printf("登录失败!\n"); 65 | } 66 | } 67 | 68 | // 登出结果响应 69 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 70 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 71 | printf("OnReqUserLogout():被执行...\n"); 72 | if (pRspInfo->ErrorID == 0) { 73 | printf("登出成功!\n"); 74 | sem_post(&sem); 75 | } else { 76 | printf("登出失败!\n"); 77 | } 78 | } 79 | 80 | // 错误信息响应方法 81 | virtual void OnRspError 82 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 83 | printf("OnRspError():被执行...\n"); 84 | } 85 | 86 | ///投资者结算结果确认响应 87 | virtual void OnRspSettlementInfoConfirm( 88 | CThostFtdcSettlementInfoConfirmField * pSettlementInfoConfirm, 89 | CThostFtdcRspInfoField * pRspInfo, 90 | int nRequestID, 91 | bool bIsLast 92 | ) { 93 | printf("OnRspSettlementInfoConfirm():被执行...\n"); 94 | 95 | // 进程返回结果检查 96 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 97 | // typedef int TThostFtdcErrorIDType; 98 | // typedef char TThostFtdcErrorMsgType[81]; 99 | char ErrorMsg[243]; 100 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 101 | printf("OnRspSettlementInfoConfirm():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 102 | } 103 | 104 | // 如果有返回结果读取返回信息 105 | if ( pSettlementInfoConfirm != NULL ) { 106 | // 读取返回信息,并做编码转化 107 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 108 | char BrokerID[33]; 109 | gbk2utf8(pSettlementInfoConfirm->BrokerID,BrokerID,sizeof(BrokerID)); 110 | ///投资者代码 TThostFtdcInvestorIDType char[13] 111 | char InvestorID[39]; 112 | gbk2utf8(pSettlementInfoConfirm->InvestorID,InvestorID,sizeof(InvestorID)); 113 | ///确认日期 TThostFtdcDateType char[9] 114 | char ConfirmDate[27]; 115 | gbk2utf8(pSettlementInfoConfirm->ConfirmDate,ConfirmDate,sizeof(ConfirmDate)); 116 | ///确认时间 TThostFtdcTimeType char[9] 117 | char ConfirmTime[27]; 118 | gbk2utf8(pSettlementInfoConfirm->ConfirmTime,ConfirmTime,sizeof(ConfirmTime)); 119 | 120 | printf("结算信息操作确认:ConfirmDate=%s,ConfirmTime=%s\n",ConfirmDate,ConfirmTime); 121 | 122 | } 123 | 124 | // 如果响应函数已经返回最后一条信息 125 | if(bIsLast) { 126 | // 通知主过程,响应函数将结束 127 | sem_post(&sem); 128 | } 129 | } 130 | 131 | }; 132 | 133 | 134 | // 定时器控制函数 135 | void timeout_handler(int signalno) { 136 | // 定时控制步骤 137 | } 138 | 139 | // 初始化定时器 140 | void init_timer(void) 141 | { 142 | // init sigaction 143 | struct sigaction act; 144 | act.sa_handler = timeout_handler; 145 | act.sa_flags = 0; 146 | sigemptyset(&act.sa_mask); 147 | sigaction(SIGPROF, &act, NULL); 148 | // set timer 149 | struct itimerval val; 150 | val.it_value.tv_sec = 1; 151 | val.it_value.tv_usec = 0; 152 | val.it_interval = val.it_value; 153 | setitimer(ITIMER_PROF, &val, NULL); 154 | } 155 | 156 | 157 | int main() { 158 | 159 | // 初始化线程同步变量 160 | sem_init(&sem,0,0); 161 | 162 | // 从环境变量中读取登录信息 163 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 164 | if ( CTP_FrontAddress == NULL ) { 165 | printf("环境变量CTP_FrontAddress没有设置\n"); 166 | return(0); 167 | } 168 | 169 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 170 | if ( CTP_BrokerId == NULL ) { 171 | printf("环境变量CTP_BrokerId没有设置\n"); 172 | return(0); 173 | } 174 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 175 | 176 | char * CTP_UserId = getenv("CTP_UserId"); 177 | if ( CTP_UserId == NULL ) { 178 | printf("环境变量CTP_UserId没有设置\n"); 179 | return(0); 180 | } 181 | strcpy(userLoginField.UserID,CTP_UserId); 182 | 183 | char * CTP_Password = getenv("CTP_Password"); 184 | if ( CTP_Password == NULL ) { 185 | printf("环境变量CTP_Password没有设置\n"); 186 | return(0); 187 | } 188 | strcpy(userLoginField.Password,CTP_Password); 189 | 190 | // 创建TraderAPI和回调响应控制器的实例 191 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 192 | CTraderHandler traderHandler = CTraderHandler(); 193 | CTraderHandler * pTraderHandler = &traderHandler; 194 | pTraderApi->RegisterSpi(pTraderHandler); 195 | 196 | // 订阅相关信息推送 197 | //// THOST_TERT_RESTART:从本交易日开始重传 198 | //// THOST_TERT_RESUME:从上次收到的续传 199 | //// THOST_TERT_QUICK:只传送登录后私有流的内容 200 | pTraderApi->SubscribePrivateTopic(THOST_TERT_RESUME); 201 | // 订阅公共流 202 | //// THOST_TERT_RESTART:从本交易日开始重传 203 | //// THOST_TERT_RESUME:从上次收到的续传 204 | //// THOST_TERT_QUICK:只传送登录后公共流的内容 205 | pTraderApi->SubscribePublicTopic(THOST_TERT_RESUME); 206 | 207 | // 设置服务器地址 208 | pTraderApi->RegisterFront(CTP_FrontAddress); 209 | // 链接交易系统 210 | pTraderApi->Init(); 211 | // 等待服务器发出登录消息 212 | sem_wait(&sem); 213 | // 发出登陆请求 214 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 215 | // 等待登录成功消息 216 | sem_wait(&sem); 217 | 218 | //////////////////////////////////////////////////////////////////////////////////////////////// 219 | ///投资者结算结果确认 220 | /////////////////////////////////////////////////////////////////////////////////////////////// 221 | // 定义调用API的数据结构 222 | CThostFtdcSettlementInfoConfirmField requestData; 223 | // 确保没有初始化的数据不会被访问 224 | memset(&requestData,0,sizeof(requestData)); 225 | // 为调用结构题设置参数信息 226 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 227 | strcpy(requestData.BrokerID,CTP_BrokerId); 228 | ///投资者代码 TThostFtdcInvestorIDType char[13] 229 | strcpy(requestData.InvestorID,CTP_UserId); 230 | ///确认日期 TThostFtdcDateType char[9] 231 | strcpy(requestData.ConfirmDate,""); 232 | ///确认时间 TThostFtdcTimeType char[9] 233 | strcpy(requestData.ConfirmTime,""); 234 | 235 | 236 | // 调用API,并等待响应函数返回 237 | int result = pTraderApi->ReqSettlementInfoConfirm(&requestData,requestID++); 238 | sem_wait(&sem); 239 | 240 | ///////////////////////////////////////////////////////////////////////////////////////////////// 241 | 242 | 243 | // 拷贝用户登录信息到登出信息 244 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 245 | strcpy(userLogoutField.UserID, userLoginField.UserID); 246 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 247 | 248 | // 等待登出成功 249 | sem_wait(&sem); 250 | 251 | printf("主线程执行完毕!\n"); 252 | return(0); 253 | 254 | } 255 | -------------------------------------------------------------------------------- /QueryMaxOrderVolume.1/QueryMaxOrderVolume.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///查询最大报单数量响应 79 | virtual void OnRspQueryMaxOrderVolume( 80 | CThostFtdcQueryMaxOrderVolumeField * pQueryMaxOrderVolume, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | printf("OnRspQueryMaxOrderVolume():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQueryMaxOrderVolume():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pQueryMaxOrderVolume != NULL ) { 98 | printf("pQueryMaxOrderVolume != NULL\n"); 99 | // 读取返回信息,并做编码转化 100 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 101 | char BrokerID[33]; 102 | gbk2utf8(pQueryMaxOrderVolume->BrokerID,BrokerID,sizeof(BrokerID)); 103 | ///投资者代码 TThostFtdcInvestorIDType char[13] 104 | char InvestorID[39]; 105 | gbk2utf8(pQueryMaxOrderVolume->InvestorID,InvestorID,sizeof(InvestorID)); 106 | ///合约代码 TThostFtdcInstrumentIDType char[31] 107 | char InstrumentID[93]; 108 | gbk2utf8(pQueryMaxOrderVolume->InstrumentID,InstrumentID,sizeof(InstrumentID)); 109 | ///买卖方向 TThostFtdcDirectionType char 110 | //// THOST_FTDC_D_Buy '0' 买 111 | //// THOST_FTDC_D_Sell '1' 卖 112 | char Direction = pQueryMaxOrderVolume->Direction; 113 | ///开平标志 TThostFtdcOffsetFlagType char 114 | //// THOST_FTDC_OF_Open '0' 开仓 115 | //// THOST_FTDC_OF_Close '1' 平仓 116 | //// THOST_FTDC_OF_ForceClose '2' 强平 117 | //// THOST_FTDC_OF_CloseToday '3' 平今 118 | //// THOST_FTDC_OF_CloseYesterday '4' 平昨 119 | //// THOST_FTDC_OF_ForceOff '5' 强减 120 | //// THOST_FTDC_OF_LocalForceClose '6' 本地强平 121 | char OffsetFlag = pQueryMaxOrderVolume->OffsetFlag; 122 | ///投机套保标志 TThostFtdcHedgeFlagType char 123 | //// THOST_FTDC_HF_Speculation '1' 投机 124 | //// THOST_FTDC_HF_Arbitrage '2' 套利 125 | //// THOST_FTDC_HF_Hedge '3' 套保 126 | char HedgeFlag = pQueryMaxOrderVolume->HedgeFlag; 127 | ///最大允许报单数量 TThostFtdcVolumeType int 128 | int MaxVolume = pQueryMaxOrderVolume->MaxVolume; 129 | 130 | printf("BrokerID=%s,InvestorID=%s,InstrumentID=%s,Direction=%c,OffsetFlag=%c,HedgeFlag=%c,MaxVolume=%d\n", 131 | BrokerID,InvestorID,InstrumentID,Direction,OffsetFlag,HedgeFlag,MaxVolume); 132 | 133 | } 134 | 135 | // 如果响应函数已经返回最后一条信息 136 | if(bIsLast) { 137 | // 通知主过程,响应函数将结束 138 | sem_post(&sem); 139 | } 140 | } 141 | 142 | }; 143 | 144 | 145 | int main() { 146 | 147 | // 初始化线程同步变量 148 | sem_init(&sem,0,0); 149 | 150 | // 从环境变量中读取登录信息 151 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 152 | if ( CTP_FrontAddress == NULL ) { 153 | printf("环境变量CTP_FrontAddress没有设置\n"); 154 | return(0); 155 | } 156 | 157 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 158 | if ( CTP_BrokerId == NULL ) { 159 | printf("环境变量CTP_BrokerId没有设置\n"); 160 | return(0); 161 | } 162 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 163 | 164 | char * CTP_UserId = getenv("CTP_UserId"); 165 | if ( CTP_UserId == NULL ) { 166 | printf("环境变量CTP_UserId没有设置\n"); 167 | return(0); 168 | } 169 | strcpy(userLoginField.UserID,CTP_UserId); 170 | 171 | char * CTP_Password = getenv("CTP_Password"); 172 | if ( CTP_Password == NULL ) { 173 | printf("环境变量CTP_Password没有设置\n"); 174 | return(0); 175 | } 176 | strcpy(userLoginField.Password,CTP_Password); 177 | 178 | // 创建TraderAPI和回调响应控制器的实例 179 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 180 | CTraderHandler traderHandler = CTraderHandler(); 181 | CTraderHandler * pTraderHandler = &traderHandler; 182 | pTraderApi->RegisterSpi(pTraderHandler); 183 | 184 | // 设置服务器地址 185 | pTraderApi->RegisterFront(CTP_FrontAddress); 186 | // 链接交易系统 187 | pTraderApi->Init(); 188 | // 等待服务器发出登录消息 189 | sem_wait(&sem); 190 | // 发出登陆请求 191 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 192 | // 等待登录成功消息 193 | sem_wait(&sem); 194 | 195 | //////////////////////////////////////////////////////////////////////////////////////////////// 196 | ///查询最大报单数量请求 197 | /////////////////////////////////////////////////////////////////////////////////////////////// 198 | // 定义调用API的数据结构 199 | CThostFtdcQueryMaxOrderVolumeField requestData; 200 | // 确保没有初始化的数据不会被访问 201 | memset(&requestData,0,sizeof(requestData)); 202 | // 为调用结构题设置参数信息 203 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 204 | strcpy(requestData.BrokerID,"66666"); 205 | ///投资者代码 TThostFtdcInvestorIDType char[13] 206 | strcpy(requestData.InvestorID,"60003010"); 207 | ///合约代码 TThostFtdcInstrumentIDType char[31] 208 | strcpy(requestData.InstrumentID,"SR609C4900"); 209 | ///买卖方向 TThostFtdcDirectionType char 210 | //// THOST_FTDC_D_Buy '0' 买 211 | //// THOST_FTDC_D_Sell '1' 卖 212 | requestData.Direction = THOST_FTDC_D_Sell; 213 | ///开平标志 TThostFtdcOffsetFlagType char 214 | //// THOST_FTDC_OF_Open '0' 开仓 215 | //// THOST_FTDC_OF_Close '1' 平仓 216 | //// THOST_FTDC_OF_ForceClose '2' 强平 217 | //// THOST_FTDC_OF_CloseToday '3' 平今 218 | //// THOST_FTDC_OF_CloseYesterday '4' 平昨 219 | //// THOST_FTDC_OF_ForceOff '5' 强减 220 | //// THOST_FTDC_OF_LocalForceClose '6' 本地强平 221 | requestData.OffsetFlag = THOST_FTDC_OF_Open; 222 | ///投机套保标志 TThostFtdcHedgeFlagType char 223 | //// THOST_FTDC_HF_Speculation '1' 投机 224 | //// THOST_FTDC_HF_Arbitrage '2' 套利 225 | //// THOST_FTDC_HF_Hedge '3' 套保 226 | requestData.HedgeFlag = THOST_FTDC_HF_Arbitrage; 227 | ///最大允许报单数量 TThostFtdcVolumeType int 228 | requestData.MaxVolume = 0; 229 | 230 | 231 | // 调用API,并等待响应函数返回 232 | int result = pTraderApi->ReqQueryMaxOrderVolume(&requestData,requestID++); 233 | sem_wait(&sem); 234 | 235 | ///////////////////////////////////////////////////////////////////////////////////////////////// 236 | 237 | 238 | // 拷贝用户登录信息到登出信息 239 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 240 | strcpy(userLogoutField.UserID, userLoginField.UserID); 241 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 242 | 243 | // 等待登出成功 244 | sem_wait(&sem); 245 | 246 | printf("主线程执行完毕!\n"); 247 | return(0); 248 | 249 | } 250 | -------------------------------------------------------------------------------- /QryInvestor.1/QryInvestor.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询投资者响应 79 | virtual void OnRspQryInvestor( 80 | CThostFtdcInvestorField * pInvestor, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | printf("OnRspQryInvestor():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryInvestor():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pInvestor != NULL ) { 98 | printf("pInvestor != NULL\n"); 99 | // 读取返回信息,并做编码转化 100 | ///投资者代码 TThostFtdcInvestorIDType char[13] 101 | char InvestorID[39]; 102 | gbk2utf8(pInvestor->InvestorID,InvestorID,sizeof(InvestorID)); 103 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 104 | char BrokerID[33]; 105 | gbk2utf8(pInvestor->BrokerID,BrokerID,sizeof(BrokerID)); 106 | ///投资者分组代码 TThostFtdcInvestorIDType char[13] 107 | char InvestorGroupID[39]; 108 | gbk2utf8(pInvestor->InvestorGroupID,InvestorGroupID,sizeof(InvestorGroupID)); 109 | ///投资者名称 TThostFtdcPartyNameType char[81] 110 | char InvestorName[243]; 111 | gbk2utf8(pInvestor->InvestorName,InvestorName,sizeof(InvestorName)); 112 | ///证件类型 TThostFtdcIdCardTypeType char 113 | //// THOST_FTDC_ICT_EID '0' 组织机构代码 114 | //// THOST_FTDC_ICT_IDCard '1' 中国公民身份证 115 | //// THOST_FTDC_ICT_OfficerIDCard '2' 军官证 116 | //// THOST_FTDC_ICT_PoliceIDCard '3' 警官证 117 | //// THOST_FTDC_ICT_SoldierIDCard '4' 士兵证 118 | //// THOST_FTDC_ICT_HouseholdRegister '5' 户口簿 119 | //// THOST_FTDC_ICT_Passport '6' 护照 120 | //// THOST_FTDC_ICT_TaiwanCompatriotIDCard '7' 台胞证 121 | //// THOST_FTDC_ICT_HomeComingCard '8' 回乡证 122 | //// THOST_FTDC_ICT_LicenseNo '9' 营业执照号 123 | //// THOST_FTDC_ICT_TaxNo 'A' 税务登记号/当地纳税ID 124 | //// THOST_FTDC_ICT_HMMainlandTravelPermit 'B' 港澳居民来往内地通行证 125 | //// THOST_FTDC_ICT_TwMainlandTravelPermit 'C' 台湾居民来往大陆通行证 126 | //// THOST_FTDC_ICT_DrivingLicense 'D' 驾照 127 | //// THOST_FTDC_ICT_SocialID 'F' 当地社保ID 128 | //// THOST_FTDC_ICT_LocalID 'G' 当地身份证 129 | //// THOST_FTDC_ICT_BusinessRegistration 'H' 商业登记证 130 | //// THOST_FTDC_ICT_HKMCIDCard 'I' 港澳永久性居民身份证 131 | //// THOST_FTDC_ICT_OtherCard 'x' 其他证件 132 | char IdentifiedCardType = pInvestor->IdentifiedCardType; 133 | ///证件号码 TThostFtdcIdentifiedCardNoType char[51] 134 | char IdentifiedCardNo[153]; 135 | gbk2utf8(pInvestor->IdentifiedCardNo,IdentifiedCardNo,sizeof(IdentifiedCardNo)); 136 | ///是否活跃 TThostFtdcBoolType int 137 | int IsActive = pInvestor->IsActive; 138 | ///联系电话 TThostFtdcTelephoneType char[41] 139 | char Telephone[123]; 140 | gbk2utf8(pInvestor->Telephone,Telephone,sizeof(Telephone)); 141 | ///通讯地址 TThostFtdcAddressType char[101] 142 | char Address[303]; 143 | gbk2utf8(pInvestor->Address,Address,sizeof(Address)); 144 | ///开户日期 TThostFtdcDateType char[9] 145 | char OpenDate[27]; 146 | gbk2utf8(pInvestor->OpenDate,OpenDate,sizeof(OpenDate)); 147 | ///手机 TThostFtdcMobileType char[41] 148 | char Mobile[123]; 149 | gbk2utf8(pInvestor->Mobile,Mobile,sizeof(Mobile)); 150 | ///手续费率模板代码 TThostFtdcInvestorIDType char[13] 151 | char CommModelID[39]; 152 | gbk2utf8(pInvestor->CommModelID,CommModelID,sizeof(CommModelID)); 153 | ///保证金率模板代码 TThostFtdcInvestorIDType char[13] 154 | char MarginModelID[39]; 155 | gbk2utf8(pInvestor->MarginModelID,MarginModelID,sizeof(MarginModelID)); 156 | 157 | printf("InvestorID=%s\n",InvestorID); //投资者代码 158 | printf("BrokerID=%s\n",BrokerID); // 经纪公司代码 159 | printf("InvestorName=%s\n",InvestorName); // 投资者名称 160 | printf("IdentifiedCardType=%c\n",IdentifiedCardType); // 证件类型 161 | printf("IdentifiedCardNo=%s\n",IdentifiedCardNo); // 证件号码 162 | printf("IsActive=%d\n",IsActive); // 是否活跃 163 | printf("OpenDate=%s\n",OpenDate); // 开户日期 164 | 165 | } 166 | 167 | // 如果响应函数已经返回最后一条信息 168 | if(bIsLast) { 169 | // 通知主过程,响应函数将结束 170 | sem_post(&sem); 171 | } 172 | } 173 | 174 | }; 175 | 176 | 177 | int main() { 178 | 179 | // 初始化线程同步变量 180 | sem_init(&sem,0,0); 181 | 182 | // 从环境变量中读取登录信息 183 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 184 | if ( CTP_FrontAddress == NULL ) { 185 | printf("环境变量CTP_FrontAddress没有设置\n"); 186 | return(0); 187 | } 188 | 189 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 190 | if ( CTP_BrokerId == NULL ) { 191 | printf("环境变量CTP_BrokerId没有设置\n"); 192 | return(0); 193 | } 194 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 195 | 196 | char * CTP_UserId = getenv("CTP_UserId"); 197 | if ( CTP_UserId == NULL ) { 198 | printf("环境变量CTP_UserId没有设置\n"); 199 | return(0); 200 | } 201 | strcpy(userLoginField.UserID,CTP_UserId); 202 | 203 | char * CTP_Password = getenv("CTP_Password"); 204 | if ( CTP_Password == NULL ) { 205 | printf("环境变量CTP_Password没有设置\n"); 206 | return(0); 207 | } 208 | strcpy(userLoginField.Password,CTP_Password); 209 | 210 | // 创建TraderAPI和回调响应控制器的实例 211 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 212 | CTraderHandler traderHandler = CTraderHandler(); 213 | CTraderHandler * pTraderHandler = &traderHandler; 214 | pTraderApi->RegisterSpi(pTraderHandler); 215 | 216 | // 设置服务器地址 217 | pTraderApi->RegisterFront(CTP_FrontAddress); 218 | // 链接交易系统 219 | pTraderApi->Init(); 220 | // 等待服务器发出登录消息 221 | sem_wait(&sem); 222 | // 发出登陆请求 223 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 224 | // 等待登录成功消息 225 | sem_wait(&sem); 226 | 227 | //////////////////////////////////////////////////////////////////////////////////////////////// 228 | ///请求查询投资者 229 | /////////////////////////////////////////////////////////////////////////////////////////////// 230 | // 定义调用API的数据结构 231 | CThostFtdcQryInvestorField requestData; 232 | // 确保没有初始化的数据不会被访问 233 | memset(&requestData,0,sizeof(requestData)); 234 | // 为调用结构题设置参数信息 235 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 236 | strcpy(requestData.BrokerID,""); 237 | ///投资者代码 TThostFtdcInvestorIDType char[13] 238 | strcpy(requestData.InvestorID,""); 239 | 240 | 241 | // 调用API,并等待响应函数返回 242 | int result = pTraderApi->ReqQryInvestor(&requestData,requestID++); 243 | sem_wait(&sem); 244 | 245 | ///////////////////////////////////////////////////////////////////////////////////////////////// 246 | 247 | 248 | // 拷贝用户登录信息到登出信息 249 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 250 | strcpy(userLogoutField.UserID, userLoginField.UserID); 251 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 252 | 253 | // 等待登出成功 254 | sem_wait(&sem); 255 | 256 | printf("主线程执行完毕!\n"); 257 | return(0); 258 | 259 | } 260 | -------------------------------------------------------------------------------- /QryInstrument.all/QryInstrument.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询合约响应 79 | virtual void OnRspQryInstrument( 80 | CThostFtdcInstrumentField * pInstrument, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | //printf("OnRspQryInstrument():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryInstrument():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pInstrument != NULL ) { 98 | // 读取返回信息,并做编码转化 99 | ///合约代码 TThostFtdcInstrumentIDType char[31] 100 | char InstrumentID[93]; 101 | gbk2utf8(pInstrument->InstrumentID,InstrumentID,sizeof(InstrumentID)); 102 | ///交易所代码 TThostFtdcExchangeIDType char[9] 103 | char ExchangeID[27]; 104 | gbk2utf8(pInstrument->ExchangeID,ExchangeID,sizeof(ExchangeID)); 105 | ///合约名称 TThostFtdcInstrumentNameType char[21] 106 | char InstrumentName[63]; 107 | gbk2utf8(pInstrument->InstrumentName,InstrumentName,sizeof(InstrumentName)); 108 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 109 | char ExchangeInstID[93]; 110 | gbk2utf8(pInstrument->ExchangeInstID,ExchangeInstID,sizeof(ExchangeInstID)); 111 | ///产品代码 TThostFtdcInstrumentIDType char[31] 112 | char ProductID[93]; 113 | gbk2utf8(pInstrument->ProductID,ProductID,sizeof(ProductID)); 114 | ///产品类型 TThostFtdcProductClassType char 115 | //// THOST_FTDC_PC_Futures '1' 期货 116 | //// THOST_FTDC_PC_Options '2' 期权 117 | //// THOST_FTDC_PC_Combination '3' 组合 118 | //// THOST_FTDC_PC_Spot '4' 即期 119 | //// THOST_FTDC_PC_EFP '5' 期转现 120 | char ProductClass = pInstrument->ProductClass; 121 | ///交割年份 TThostFtdcYearType int 122 | int DeliveryYear = pInstrument->DeliveryYear; 123 | ///交割月 TThostFtdcMonthType int 124 | int DeliveryMonth = pInstrument->DeliveryMonth; 125 | ///市价单最大下单量 TThostFtdcVolumeType int 126 | int MaxMarketOrderVolume = pInstrument->MaxMarketOrderVolume; 127 | ///市价单最小下单量 TThostFtdcVolumeType int 128 | int MinMarketOrderVolume = pInstrument->MinMarketOrderVolume; 129 | ///限价单最大下单量 TThostFtdcVolumeType int 130 | int MaxLimitOrderVolume = pInstrument->MaxLimitOrderVolume; 131 | ///限价单最小下单量 TThostFtdcVolumeType int 132 | int MinLimitOrderVolume = pInstrument->MinLimitOrderVolume; 133 | ///合约数量乘数 TThostFtdcVolumeMultipleType int 134 | int VolumeMultiple = pInstrument->VolumeMultiple; 135 | ///最小变动价位 TThostFtdcPriceType double 136 | double PriceTick = pInstrument->PriceTick; 137 | ///创建日 TThostFtdcDateType char[9] 138 | char CreateDate[27]; 139 | gbk2utf8(pInstrument->CreateDate,CreateDate,sizeof(CreateDate)); 140 | ///上市日 TThostFtdcDateType char[9] 141 | char OpenDate[27]; 142 | gbk2utf8(pInstrument->OpenDate,OpenDate,sizeof(OpenDate)); 143 | ///到期日 TThostFtdcDateType char[9] 144 | char ExpireDate[27]; 145 | gbk2utf8(pInstrument->ExpireDate,ExpireDate,sizeof(ExpireDate)); 146 | ///开始交割日 TThostFtdcDateType char[9] 147 | char StartDelivDate[27]; 148 | gbk2utf8(pInstrument->StartDelivDate,StartDelivDate,sizeof(StartDelivDate)); 149 | ///结束交割日 TThostFtdcDateType char[9] 150 | char EndDelivDate[27]; 151 | gbk2utf8(pInstrument->EndDelivDate,EndDelivDate,sizeof(EndDelivDate)); 152 | ///合约生命周期状态 TThostFtdcInstLifePhaseType char 153 | //// THOST_FTDC_IP_NotStart '0' 未上市 154 | //// THOST_FTDC_IP_Started '1' 上市 155 | //// THOST_FTDC_IP_Pause '2' 停牌 156 | //// THOST_FTDC_IP_Expired '3' 到期 157 | char InstLifePhase = pInstrument->InstLifePhase; 158 | ///当前是否交易 TThostFtdcBoolType int 159 | int IsTrading = pInstrument->IsTrading; 160 | ///持仓类型 TThostFtdcPositionTypeType char 161 | //// THOST_FTDC_PT_Net '1' 净持仓 162 | //// THOST_FTDC_PT_Gross '2' 综合持仓 163 | char PositionType = pInstrument->PositionType; 164 | ///持仓日期类型 TThostFtdcPositionDateTypeType char 165 | //// THOST_FTDC_PDT_UseHistory '1' 使用历史持仓 166 | //// THOST_FTDC_PDT_NoUseHistory '2' 不使用历史持仓 167 | char PositionDateType = pInstrument->PositionDateType; 168 | ///多头保证金率 TThostFtdcRatioType double 169 | double LongMarginRatio = pInstrument->LongMarginRatio; 170 | ///空头保证金率 TThostFtdcRatioType double 171 | double ShortMarginRatio = pInstrument->ShortMarginRatio; 172 | ///是否使用大额单边保证金算法 TThostFtdcMaxMarginSideAlgorithmType char 173 | //// THOST_FTDC_MMSA_NO '0' 不使用大额单边保证金算法 174 | //// THOST_FTDC_MMSA_YES '1' 使用大额单边保证金算法 175 | char MaxMarginSideAlgorithm = pInstrument->MaxMarginSideAlgorithm; 176 | 177 | printf("InstrumentID=%s,ExchangeID=%s,InstrumentName=%s\n",InstrumentID,ExchangeID,InstrumentName); 178 | } 179 | 180 | // 如果响应函数已经返回最后一条信息 181 | if(bIsLast) { 182 | // 通知主过程,响应函数将结束 183 | sem_post(&sem); 184 | } 185 | } 186 | 187 | }; 188 | 189 | 190 | int main() { 191 | 192 | // 初始化线程同步变量 193 | sem_init(&sem,0,0); 194 | 195 | // 从环境变量中读取登录信息 196 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 197 | if ( CTP_FrontAddress == NULL ) { 198 | printf("环境变量CTP_FrontAddress没有设置\n"); 199 | return(0); 200 | } 201 | 202 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 203 | if ( CTP_BrokerId == NULL ) { 204 | printf("环境变量CTP_BrokerId没有设置\n"); 205 | return(0); 206 | } 207 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 208 | 209 | char * CTP_UserId = getenv("CTP_UserId"); 210 | if ( CTP_UserId == NULL ) { 211 | printf("环境变量CTP_UserId没有设置\n"); 212 | return(0); 213 | } 214 | strcpy(userLoginField.UserID,CTP_UserId); 215 | 216 | char * CTP_Password = getenv("CTP_Password"); 217 | if ( CTP_Password == NULL ) { 218 | printf("环境变量CTP_Password没有设置\n"); 219 | return(0); 220 | } 221 | strcpy(userLoginField.Password,CTP_Password); 222 | 223 | // 创建TraderAPI和回调响应控制器的实例 224 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 225 | CTraderHandler traderHandler = CTraderHandler(); 226 | CTraderHandler * pTraderHandler = &traderHandler; 227 | pTraderApi->RegisterSpi(pTraderHandler); 228 | 229 | // 设置服务器地址 230 | pTraderApi->RegisterFront(CTP_FrontAddress); 231 | // 链接交易系统 232 | pTraderApi->Init(); 233 | // 等待服务器发出登录消息 234 | sem_wait(&sem); 235 | // 发出登陆请求 236 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 237 | // 等待登录成功消息 238 | sem_wait(&sem); 239 | 240 | //////////////////////////////////////////////////////////////////////////////////////////////// 241 | ///请求查询合约 242 | /////////////////////////////////////////////////////////////////////////////////////////////// 243 | // 定义调用API的数据结构 244 | CThostFtdcQryInstrumentField requestData; 245 | // 确保没有初始化的数据不会被访问 246 | memset(&requestData,0,sizeof(requestData)); 247 | // 为调用结构题设置参数信息 248 | ///合约代码 TThostFtdcInstrumentIDType char[31] 249 | strcpy(requestData.InstrumentID,""); 250 | ///交易所代码 TThostFtdcExchangeIDType char[9] 251 | strcpy(requestData.ExchangeID,""); 252 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 253 | strcpy(requestData.ExchangeInstID,""); 254 | ///产品代码 TThostFtdcInstrumentIDType char[31] 255 | strcpy(requestData.ProductID,""); 256 | 257 | 258 | // 调用API,并等待响应函数返回 259 | int result = pTraderApi->ReqQryInstrument(&requestData,requestID++); 260 | sem_wait(&sem); 261 | 262 | ///////////////////////////////////////////////////////////////////////////////////////////////// 263 | 264 | 265 | // 拷贝用户登录信息到登出信息 266 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 267 | strcpy(userLogoutField.UserID, userLoginField.UserID); 268 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 269 | 270 | // 等待登出成功 271 | sem_wait(&sem); 272 | 273 | printf("主线程执行完毕!\n"); 274 | return(0); 275 | 276 | } 277 | -------------------------------------------------------------------------------- /QryInstrument.InstrumentID/QryInstrument.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询合约响应 79 | virtual void OnRspQryInstrument( 80 | CThostFtdcInstrumentField * pInstrument, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | printf("OnRspQryInstrument():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryInstrument():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pInstrument != NULL ) { 98 | // 读取返回信息,并做编码转化 99 | ///合约代码 TThostFtdcInstrumentIDType char[31] 100 | char InstrumentID[93]; 101 | gbk2utf8(pInstrument->InstrumentID,InstrumentID,sizeof(InstrumentID)); 102 | ///交易所代码 TThostFtdcExchangeIDType char[9] 103 | char ExchangeID[27]; 104 | gbk2utf8(pInstrument->ExchangeID,ExchangeID,sizeof(ExchangeID)); 105 | ///合约名称 TThostFtdcInstrumentNameType char[21] 106 | char InstrumentName[63]; 107 | gbk2utf8(pInstrument->InstrumentName,InstrumentName,sizeof(InstrumentName)); 108 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 109 | char ExchangeInstID[93]; 110 | gbk2utf8(pInstrument->ExchangeInstID,ExchangeInstID,sizeof(ExchangeInstID)); 111 | ///产品代码 TThostFtdcInstrumentIDType char[31] 112 | char ProductID[93]; 113 | gbk2utf8(pInstrument->ProductID,ProductID,sizeof(ProductID)); 114 | ///产品类型 TThostFtdcProductClassType char 115 | //// THOST_FTDC_PC_Futures '1' 期货 116 | //// THOST_FTDC_PC_Options '2' 期权 117 | //// THOST_FTDC_PC_Combination '3' 组合 118 | //// THOST_FTDC_PC_Spot '4' 即期 119 | //// THOST_FTDC_PC_EFP '5' 期转现 120 | char ProductClass = pInstrument->ProductClass; 121 | ///交割年份 TThostFtdcYearType int 122 | int DeliveryYear = pInstrument->DeliveryYear; 123 | ///交割月 TThostFtdcMonthType int 124 | int DeliveryMonth = pInstrument->DeliveryMonth; 125 | ///市价单最大下单量 TThostFtdcVolumeType int 126 | int MaxMarketOrderVolume = pInstrument->MaxMarketOrderVolume; 127 | ///市价单最小下单量 TThostFtdcVolumeType int 128 | int MinMarketOrderVolume = pInstrument->MinMarketOrderVolume; 129 | ///限价单最大下单量 TThostFtdcVolumeType int 130 | int MaxLimitOrderVolume = pInstrument->MaxLimitOrderVolume; 131 | ///限价单最小下单量 TThostFtdcVolumeType int 132 | int MinLimitOrderVolume = pInstrument->MinLimitOrderVolume; 133 | ///合约数量乘数 TThostFtdcVolumeMultipleType int 134 | int VolumeMultiple = pInstrument->VolumeMultiple; 135 | ///最小变动价位 TThostFtdcPriceType double 136 | double PriceTick = pInstrument->PriceTick; 137 | ///创建日 TThostFtdcDateType char[9] 138 | char CreateDate[27]; 139 | gbk2utf8(pInstrument->CreateDate,CreateDate,sizeof(CreateDate)); 140 | ///上市日 TThostFtdcDateType char[9] 141 | char OpenDate[27]; 142 | gbk2utf8(pInstrument->OpenDate,OpenDate,sizeof(OpenDate)); 143 | ///到期日 TThostFtdcDateType char[9] 144 | char ExpireDate[27]; 145 | gbk2utf8(pInstrument->ExpireDate,ExpireDate,sizeof(ExpireDate)); 146 | ///开始交割日 TThostFtdcDateType char[9] 147 | char StartDelivDate[27]; 148 | gbk2utf8(pInstrument->StartDelivDate,StartDelivDate,sizeof(StartDelivDate)); 149 | ///结束交割日 TThostFtdcDateType char[9] 150 | char EndDelivDate[27]; 151 | gbk2utf8(pInstrument->EndDelivDate,EndDelivDate,sizeof(EndDelivDate)); 152 | ///合约生命周期状态 TThostFtdcInstLifePhaseType char 153 | //// THOST_FTDC_IP_NotStart '0' 未上市 154 | //// THOST_FTDC_IP_Started '1' 上市 155 | //// THOST_FTDC_IP_Pause '2' 停牌 156 | //// THOST_FTDC_IP_Expired '3' 到期 157 | char InstLifePhase = pInstrument->InstLifePhase; 158 | ///当前是否交易 TThostFtdcBoolType int 159 | int IsTrading = pInstrument->IsTrading; 160 | ///持仓类型 TThostFtdcPositionTypeType char 161 | //// THOST_FTDC_PT_Net '1' 净持仓 162 | //// THOST_FTDC_PT_Gross '2' 综合持仓 163 | char PositionType = pInstrument->PositionType; 164 | ///持仓日期类型 TThostFtdcPositionDateTypeType char 165 | //// THOST_FTDC_PDT_UseHistory '1' 使用历史持仓 166 | //// THOST_FTDC_PDT_NoUseHistory '2' 不使用历史持仓 167 | char PositionDateType = pInstrument->PositionDateType; 168 | ///多头保证金率 TThostFtdcRatioType double 169 | double LongMarginRatio = pInstrument->LongMarginRatio; 170 | ///空头保证金率 TThostFtdcRatioType double 171 | double ShortMarginRatio = pInstrument->ShortMarginRatio; 172 | ///是否使用大额单边保证金算法 TThostFtdcMaxMarginSideAlgorithmType char 173 | //// THOST_FTDC_MMSA_NO '0' 不使用大额单边保证金算法 174 | //// THOST_FTDC_MMSA_YES '1' 使用大额单边保证金算法 175 | char MaxMarginSideAlgorithm = pInstrument->MaxMarginSideAlgorithm; 176 | 177 | printf("InstrumentID=%s,ExchangeID=%s,InstrumentName=%s\n",InstrumentID,ExchangeID,InstrumentName); 178 | 179 | } 180 | 181 | // 如果响应函数已经返回最后一条信息 182 | if(bIsLast) { 183 | // 通知主过程,响应函数将结束 184 | sem_post(&sem); 185 | } 186 | } 187 | 188 | }; 189 | 190 | 191 | int main() { 192 | 193 | // 初始化线程同步变量 194 | sem_init(&sem,0,0); 195 | 196 | // 从环境变量中读取登录信息 197 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 198 | if ( CTP_FrontAddress == NULL ) { 199 | printf("环境变量CTP_FrontAddress没有设置\n"); 200 | return(0); 201 | } 202 | 203 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 204 | if ( CTP_BrokerId == NULL ) { 205 | printf("环境变量CTP_BrokerId没有设置\n"); 206 | return(0); 207 | } 208 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 209 | 210 | char * CTP_UserId = getenv("CTP_UserId"); 211 | if ( CTP_UserId == NULL ) { 212 | printf("环境变量CTP_UserId没有设置\n"); 213 | return(0); 214 | } 215 | strcpy(userLoginField.UserID,CTP_UserId); 216 | 217 | char * CTP_Password = getenv("CTP_Password"); 218 | if ( CTP_Password == NULL ) { 219 | printf("环境变量CTP_Password没有设置\n"); 220 | return(0); 221 | } 222 | strcpy(userLoginField.Password,CTP_Password); 223 | 224 | // 创建TraderAPI和回调响应控制器的实例 225 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 226 | CTraderHandler traderHandler = CTraderHandler(); 227 | CTraderHandler * pTraderHandler = &traderHandler; 228 | pTraderApi->RegisterSpi(pTraderHandler); 229 | 230 | // 设置服务器地址 231 | pTraderApi->RegisterFront(CTP_FrontAddress); 232 | // 链接交易系统 233 | pTraderApi->Init(); 234 | // 等待服务器发出登录消息 235 | sem_wait(&sem); 236 | // 发出登陆请求 237 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 238 | // 等待登录成功消息 239 | sem_wait(&sem); 240 | 241 | //////////////////////////////////////////////////////////////////////////////////////////////// 242 | ///请求查询合约 243 | /////////////////////////////////////////////////////////////////////////////////////////////// 244 | // 定义调用API的数据结构 245 | CThostFtdcQryInstrumentField requestData; 246 | // 确保没有初始化的数据不会被访问 247 | memset(&requestData,0,sizeof(requestData)); 248 | // 为调用结构题设置参数信息 249 | ///合约代码 TThostFtdcInstrumentIDType char[31] 250 | strcpy(requestData.InstrumentID,"IF1504"); 251 | ///交易所代码 TThostFtdcExchangeIDType char[9] 252 | strcpy(requestData.ExchangeID,""); 253 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 254 | strcpy(requestData.ExchangeInstID,""); 255 | ///产品代码 TThostFtdcInstrumentIDType char[31] 256 | strcpy(requestData.ProductID,""); 257 | 258 | 259 | // 调用API,并等待响应函数返回 260 | int result = pTraderApi->ReqQryInstrument(&requestData,requestID++); 261 | sem_wait(&sem); 262 | 263 | ///////////////////////////////////////////////////////////////////////////////////////////////// 264 | 265 | 266 | // 拷贝用户登录信息到登出信息 267 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 268 | strcpy(userLogoutField.UserID, userLoginField.UserID); 269 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 270 | 271 | // 等待登出成功 272 | sem_wait(&sem); 273 | 274 | printf("主线程执行完毕!\n"); 275 | return(0); 276 | 277 | } 278 | -------------------------------------------------------------------------------- /QryInstrument.ExchangeID/QryInstrument.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | #include 20 | 21 | // 登录请求结构体 22 | CThostFtdcReqUserLoginField userLoginField; 23 | // 用户请求结构体 24 | CThostFtdcUserLogoutField userLogoutField; 25 | // 线程同步标志 26 | sem_t sem; 27 | // requestID 28 | int requestID = 0; 29 | 30 | 31 | class CTraderHandler : public CThostFtdcTraderSpi { 32 | 33 | public: 34 | 35 | CTraderHandler() { 36 | printf("CTraderHandler():被执行...\n"); 37 | } 38 | 39 | // 允许登录事件 40 | virtual void OnFrontConnected() { 41 | static int i = 0; 42 | printf("OnFrontConnected():被执行...\n"); 43 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 44 | if (i++==0) { 45 | sem_post(&sem); 46 | } 47 | } 48 | 49 | // 登录结果响应 50 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 51 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 52 | printf("OnRspUserLogin():被执行...\n"); 53 | if (pRspInfo->ErrorID == 0) { 54 | printf("登录成功!\n"); 55 | sem_post(&sem); 56 | } else { 57 | printf("登录失败!\n"); 58 | } 59 | } 60 | 61 | // 登出结果响应 62 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 63 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 64 | printf("OnReqUserLogout():被执行...\n"); 65 | if (pRspInfo->ErrorID == 0) { 66 | printf("登出成功!\n"); 67 | sem_post(&sem); 68 | } else { 69 | printf("登出失败!\n"); 70 | } 71 | printf("--------------------1------------------------\n"); 72 | } 73 | 74 | // 错误信息响应方法 75 | virtual void OnRspError 76 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 77 | printf("OnRspError():被执行...\n"); 78 | } 79 | 80 | ///请求查询合约响应 81 | virtual void OnRspQryInstrument( 82 | CThostFtdcInstrumentField * pInstrument, 83 | CThostFtdcRspInfoField * pRspInfo, 84 | int nRequestID, 85 | bool bIsLast 86 | ) { 87 | printf("OnRspQryInstrument():被执行...\n"); 88 | 89 | // 进程返回结果检查 90 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 91 | // typedef int TThostFtdcErrorIDType; 92 | // typedef char TThostFtdcErrorMsgType[81]; 93 | char ErrorMsg[243]; 94 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 95 | printf("OnRspQryInstrument():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 96 | } 97 | 98 | // 如果有返回结果读取返回信息 99 | if ( pInstrument != NULL ) { 100 | // 读取返回信息,并做编码转化 101 | ///合约代码 TThostFtdcInstrumentIDType char[31] 102 | char InstrumentID[93]; 103 | gbk2utf8(pInstrument->InstrumentID,InstrumentID,sizeof(InstrumentID)); 104 | ///交易所代码 TThostFtdcExchangeIDType char[9] 105 | char ExchangeID[27]; 106 | gbk2utf8(pInstrument->ExchangeID,ExchangeID,sizeof(ExchangeID)); 107 | ///合约名称 TThostFtdcInstrumentNameType char[21] 108 | char InstrumentName[63]; 109 | gbk2utf8(pInstrument->InstrumentName,InstrumentName,sizeof(InstrumentName)); 110 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 111 | char ExchangeInstID[93]; 112 | gbk2utf8(pInstrument->ExchangeInstID,ExchangeInstID,sizeof(ExchangeInstID)); 113 | ///产品代码 TThostFtdcInstrumentIDType char[31] 114 | char ProductID[93]; 115 | gbk2utf8(pInstrument->ProductID,ProductID,sizeof(ProductID)); 116 | ///产品类型 TThostFtdcProductClassType char 117 | //// THOST_FTDC_PC_Futures '1' 期货 118 | //// THOST_FTDC_PC_Options '2' 期权 119 | //// THOST_FTDC_PC_Combination '3' 组合 120 | //// THOST_FTDC_PC_Spot '4' 即期 121 | //// THOST_FTDC_PC_EFP '5' 期转现 122 | char ProductClass = pInstrument->ProductClass; 123 | ///交割年份 TThostFtdcYearType int 124 | int DeliveryYear = pInstrument->DeliveryYear; 125 | ///交割月 TThostFtdcMonthType int 126 | int DeliveryMonth = pInstrument->DeliveryMonth; 127 | ///市价单最大下单量 TThostFtdcVolumeType int 128 | int MaxMarketOrderVolume = pInstrument->MaxMarketOrderVolume; 129 | ///市价单最小下单量 TThostFtdcVolumeType int 130 | int MinMarketOrderVolume = pInstrument->MinMarketOrderVolume; 131 | ///限价单最大下单量 TThostFtdcVolumeType int 132 | int MaxLimitOrderVolume = pInstrument->MaxLimitOrderVolume; 133 | ///限价单最小下单量 TThostFtdcVolumeType int 134 | int MinLimitOrderVolume = pInstrument->MinLimitOrderVolume; 135 | ///合约数量乘数 TThostFtdcVolumeMultipleType int 136 | int VolumeMultiple = pInstrument->VolumeMultiple; 137 | ///最小变动价位 TThostFtdcPriceType double 138 | double PriceTick = pInstrument->PriceTick; 139 | ///创建日 TThostFtdcDateType char[9] 140 | char CreateDate[27]; 141 | gbk2utf8(pInstrument->CreateDate,CreateDate,sizeof(CreateDate)); 142 | ///上市日 TThostFtdcDateType char[9] 143 | char OpenDate[27]; 144 | gbk2utf8(pInstrument->OpenDate,OpenDate,sizeof(OpenDate)); 145 | ///到期日 TThostFtdcDateType char[9] 146 | char ExpireDate[27]; 147 | gbk2utf8(pInstrument->ExpireDate,ExpireDate,sizeof(ExpireDate)); 148 | ///开始交割日 TThostFtdcDateType char[9] 149 | char StartDelivDate[27]; 150 | gbk2utf8(pInstrument->StartDelivDate,StartDelivDate,sizeof(StartDelivDate)); 151 | ///结束交割日 TThostFtdcDateType char[9] 152 | char EndDelivDate[27]; 153 | gbk2utf8(pInstrument->EndDelivDate,EndDelivDate,sizeof(EndDelivDate)); 154 | ///合约生命周期状态 TThostFtdcInstLifePhaseType char 155 | //// THOST_FTDC_IP_NotStart '0' 未上市 156 | //// THOST_FTDC_IP_Started '1' 上市 157 | //// THOST_FTDC_IP_Pause '2' 停牌 158 | //// THOST_FTDC_IP_Expired '3' 到期 159 | char InstLifePhase = pInstrument->InstLifePhase; 160 | ///当前是否交易 TThostFtdcBoolType int 161 | int IsTrading = pInstrument->IsTrading; 162 | ///持仓类型 TThostFtdcPositionTypeType char 163 | //// THOST_FTDC_PT_Net '1' 净持仓 164 | //// THOST_FTDC_PT_Gross '2' 综合持仓 165 | char PositionType = pInstrument->PositionType; 166 | ///持仓日期类型 TThostFtdcPositionDateTypeType char 167 | //// THOST_FTDC_PDT_UseHistory '1' 使用历史持仓 168 | //// THOST_FTDC_PDT_NoUseHistory '2' 不使用历史持仓 169 | char PositionDateType = pInstrument->PositionDateType; 170 | ///多头保证金率 TThostFtdcRatioType double 171 | double LongMarginRatio = pInstrument->LongMarginRatio; 172 | ///空头保证金率 TThostFtdcRatioType double 173 | double ShortMarginRatio = pInstrument->ShortMarginRatio; 174 | ///是否使用大额单边保证金算法 TThostFtdcMaxMarginSideAlgorithmType char 175 | //// THOST_FTDC_MMSA_NO '0' 不使用大额单边保证金算法 176 | //// THOST_FTDC_MMSA_YES '1' 使用大额单边保证金算法 177 | char MaxMarginSideAlgorithm = pInstrument->MaxMarginSideAlgorithm; 178 | 179 | printf("InstrumentID=%s,ExchangeID=%s,InstrumentName=%s\n",InstrumentID,ExchangeID,InstrumentName); 180 | 181 | } 182 | 183 | // 如果响应函数已经返回最后一条信息 184 | if(bIsLast) { 185 | // 通知主过程,响应函数将结束 186 | sem_post(&sem); 187 | } 188 | } 189 | 190 | }; 191 | 192 | 193 | int main() { 194 | 195 | // 初始化线程同步变量 196 | sem_init(&sem,0,0); 197 | 198 | // 从环境变量中读取登录信息 199 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 200 | if ( CTP_FrontAddress == NULL ) { 201 | printf("环境变量CTP_FrontAddress没有设置\n"); 202 | return(0); 203 | } 204 | 205 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 206 | if ( CTP_BrokerId == NULL ) { 207 | printf("环境变量CTP_BrokerId没有设置\n"); 208 | return(0); 209 | } 210 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 211 | 212 | char * CTP_UserId = getenv("CTP_UserId"); 213 | if ( CTP_UserId == NULL ) { 214 | printf("环境变量CTP_UserId没有设置\n"); 215 | return(0); 216 | } 217 | strcpy(userLoginField.UserID,CTP_UserId); 218 | 219 | char * CTP_Password = getenv("CTP_Password"); 220 | if ( CTP_Password == NULL ) { 221 | printf("环境变量CTP_Password没有设置\n"); 222 | return(0); 223 | } 224 | strcpy(userLoginField.Password,CTP_Password); 225 | 226 | // 创建TraderAPI和回调响应控制器的实例 227 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 228 | CTraderHandler traderHandler = CTraderHandler(); 229 | CTraderHandler * pTraderHandler = &traderHandler; 230 | pTraderApi->RegisterSpi(pTraderHandler); 231 | 232 | // 设置服务器地址 233 | pTraderApi->RegisterFront(CTP_FrontAddress); 234 | // 链接交易系统 235 | pTraderApi->Init(); 236 | // 等待服务器发出登录消息 237 | sem_wait(&sem); 238 | // 发出登陆请求 239 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 240 | // 等待登录成功消息 241 | sem_wait(&sem); 242 | 243 | //////////////////////////////////////////////////////////////////////////////////////////////// 244 | ///请求查询合约 245 | /////////////////////////////////////////////////////////////////////////////////////////////// 246 | // 定义调用API的数据结构 247 | CThostFtdcQryInstrumentField requestData; 248 | // 确保没有初始化的数据不会被访问 249 | memset(&requestData,0,sizeof(requestData)); 250 | // 为调用结构题设置参数信息 251 | ///合约代码 TThostFtdcInstrumentIDType char[31] 252 | strcpy(requestData.InstrumentID,""); 253 | ///交易所代码 TThostFtdcExchangeIDType char[9] 254 | strcpy(requestData.ExchangeID,"CZCE"); 255 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 256 | strcpy(requestData.ExchangeInstID,""); 257 | ///产品代码 TThostFtdcInstrumentIDType char[31] 258 | strcpy(requestData.ProductID,""); 259 | 260 | 261 | // 调用API,并等待响应函数返回 262 | int result = pTraderApi->ReqQryInstrument(&requestData,requestID++); 263 | sem_wait(&sem); 264 | 265 | ///////////////////////////////////////////////////////////////////////////////////////////////// 266 | 267 | 268 | sleep(1); 269 | 270 | // 拷贝用户登录信息到登出信息 271 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 272 | strcpy(userLogoutField.UserID, userLoginField.UserID); 273 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 274 | 275 | 276 | // 等待登出成功 277 | sem_wait(&sem); 278 | printf("------------2------------------\n"); 279 | 280 | sleep(1); 281 | 282 | printf("主线程执行完毕!\n"); 283 | return(0); 284 | 285 | } 286 | -------------------------------------------------------------------------------- /api/error.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /include/error.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /QryDepthMarketData.all/QryDepthMarketData.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询行情响应 79 | virtual void OnRspQryDepthMarketData( 80 | CThostFtdcDepthMarketDataField * pDepthMarketData, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | //printf("OnRspQryDepthMarketData():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryDepthMarketData():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pDepthMarketData != NULL ) { 98 | // 读取返回信息,并做编码转化 99 | ///交易日 TThostFtdcDateType char[9] 100 | char TradingDay[27]; 101 | gbk2utf8(pDepthMarketData->TradingDay,TradingDay,sizeof(TradingDay)); 102 | ///合约代码 TThostFtdcInstrumentIDType char[31] 103 | char InstrumentID[93]; 104 | gbk2utf8(pDepthMarketData->InstrumentID,InstrumentID,sizeof(InstrumentID)); 105 | ///交易所代码 TThostFtdcExchangeIDType char[9] 106 | char ExchangeID[27]; 107 | gbk2utf8(pDepthMarketData->ExchangeID,ExchangeID,sizeof(ExchangeID)); 108 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 109 | char ExchangeInstID[93]; 110 | gbk2utf8(pDepthMarketData->ExchangeInstID,ExchangeInstID,sizeof(ExchangeInstID)); 111 | ///最新价 TThostFtdcPriceType double 112 | double LastPrice = pDepthMarketData->LastPrice; 113 | ///上次结算价 TThostFtdcPriceType double 114 | double PreSettlementPrice = pDepthMarketData->PreSettlementPrice; 115 | ///昨收盘 TThostFtdcPriceType double 116 | double PreClosePrice = pDepthMarketData->PreClosePrice; 117 | ///昨持仓量 TThostFtdcLargeVolumeType double 118 | double PreOpenInterest = pDepthMarketData->PreOpenInterest; 119 | ///今开盘 TThostFtdcPriceType double 120 | double OpenPrice = pDepthMarketData->OpenPrice; 121 | ///最高价 TThostFtdcPriceType double 122 | double HighestPrice = pDepthMarketData->HighestPrice; 123 | ///最低价 TThostFtdcPriceType double 124 | double LowestPrice = pDepthMarketData->LowestPrice; 125 | ///数量 TThostFtdcVolumeType int 126 | int Volume = pDepthMarketData->Volume; 127 | ///成交金额 TThostFtdcMoneyType double 128 | double Turnover = pDepthMarketData->Turnover; 129 | ///持仓量 TThostFtdcLargeVolumeType double 130 | double OpenInterest = pDepthMarketData->OpenInterest; 131 | ///今收盘 TThostFtdcPriceType double 132 | double ClosePrice = pDepthMarketData->ClosePrice; 133 | ///本次结算价 TThostFtdcPriceType double 134 | double SettlementPrice = pDepthMarketData->SettlementPrice; 135 | ///涨停板价 TThostFtdcPriceType double 136 | double UpperLimitPrice = pDepthMarketData->UpperLimitPrice; 137 | ///跌停板价 TThostFtdcPriceType double 138 | double LowerLimitPrice = pDepthMarketData->LowerLimitPrice; 139 | ///昨虚实度 TThostFtdcRatioType double 140 | double PreDelta = pDepthMarketData->PreDelta; 141 | ///今虚实度 TThostFtdcRatioType double 142 | double CurrDelta = pDepthMarketData->CurrDelta; 143 | ///最后修改时间 TThostFtdcTimeType char[9] 144 | char UpdateTime[27]; 145 | gbk2utf8(pDepthMarketData->UpdateTime,UpdateTime,sizeof(UpdateTime)); 146 | ///最后修改毫秒 TThostFtdcMillisecType int 147 | int UpdateMillisec = pDepthMarketData->UpdateMillisec; 148 | ///申买价一 TThostFtdcPriceType double 149 | double BidPrice1 = pDepthMarketData->BidPrice1; 150 | ///申买量一 TThostFtdcVolumeType int 151 | int BidVolume1 = pDepthMarketData->BidVolume1; 152 | ///申卖价一 TThostFtdcPriceType double 153 | double AskPrice1 = pDepthMarketData->AskPrice1; 154 | ///申卖量一 TThostFtdcVolumeType int 155 | int AskVolume1 = pDepthMarketData->AskVolume1; 156 | ///申买价二 TThostFtdcPriceType double 157 | double BidPrice2 = pDepthMarketData->BidPrice2; 158 | ///申买量二 TThostFtdcVolumeType int 159 | int BidVolume2 = pDepthMarketData->BidVolume2; 160 | ///申卖价二 TThostFtdcPriceType double 161 | double AskPrice2 = pDepthMarketData->AskPrice2; 162 | ///申卖量二 TThostFtdcVolumeType int 163 | int AskVolume2 = pDepthMarketData->AskVolume2; 164 | ///申买价三 TThostFtdcPriceType double 165 | double BidPrice3 = pDepthMarketData->BidPrice3; 166 | ///申买量三 TThostFtdcVolumeType int 167 | int BidVolume3 = pDepthMarketData->BidVolume3; 168 | ///申卖价三 TThostFtdcPriceType double 169 | double AskPrice3 = pDepthMarketData->AskPrice3; 170 | ///申卖量三 TThostFtdcVolumeType int 171 | int AskVolume3 = pDepthMarketData->AskVolume3; 172 | ///申买价四 TThostFtdcPriceType double 173 | double BidPrice4 = pDepthMarketData->BidPrice4; 174 | ///申买量四 TThostFtdcVolumeType int 175 | int BidVolume4 = pDepthMarketData->BidVolume4; 176 | ///申卖价四 TThostFtdcPriceType double 177 | double AskPrice4 = pDepthMarketData->AskPrice4; 178 | ///申卖量四 TThostFtdcVolumeType int 179 | int AskVolume4 = pDepthMarketData->AskVolume4; 180 | ///申买价五 TThostFtdcPriceType double 181 | double BidPrice5 = pDepthMarketData->BidPrice5; 182 | ///申买量五 TThostFtdcVolumeType int 183 | int BidVolume5 = pDepthMarketData->BidVolume5; 184 | ///申卖价五 TThostFtdcPriceType double 185 | double AskPrice5 = pDepthMarketData->AskPrice5; 186 | ///申卖量五 TThostFtdcVolumeType int 187 | int AskVolume5 = pDepthMarketData->AskVolume5; 188 | ///当日均价 TThostFtdcPriceType double 189 | double AveragePrice = pDepthMarketData->AveragePrice; 190 | ///业务日期 TThostFtdcDateType char[9] 191 | char ActionDay[27]; 192 | gbk2utf8(pDepthMarketData->ActionDay,ActionDay,sizeof(ActionDay)); 193 | 194 | printf("TradingDay=%s,",TradingDay); // 交易日 195 | printf("InstrumentID=%s,",InstrumentID); // 合约代码 196 | printf("LastPrice=%f\n",LastPrice); // 最新价 197 | 198 | } 199 | 200 | // 如果响应函数已经返回最后一条信息 201 | if(bIsLast) { 202 | // 通知主过程,响应函数将结束 203 | sem_post(&sem); 204 | } 205 | } 206 | 207 | }; 208 | 209 | 210 | int main() { 211 | 212 | // 初始化线程同步变量 213 | sem_init(&sem,0,0); 214 | 215 | // 从环境变量中读取登录信息 216 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 217 | if ( CTP_FrontAddress == NULL ) { 218 | printf("环境变量CTP_FrontAddress没有设置\n"); 219 | return(0); 220 | } 221 | 222 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 223 | if ( CTP_BrokerId == NULL ) { 224 | printf("环境变量CTP_BrokerId没有设置\n"); 225 | return(0); 226 | } 227 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 228 | 229 | char * CTP_UserId = getenv("CTP_UserId"); 230 | if ( CTP_UserId == NULL ) { 231 | printf("环境变量CTP_UserId没有设置\n"); 232 | return(0); 233 | } 234 | strcpy(userLoginField.UserID,CTP_UserId); 235 | 236 | char * CTP_Password = getenv("CTP_Password"); 237 | if ( CTP_Password == NULL ) { 238 | printf("环境变量CTP_Password没有设置\n"); 239 | return(0); 240 | } 241 | strcpy(userLoginField.Password,CTP_Password); 242 | 243 | // 创建TraderAPI和回调响应控制器的实例 244 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 245 | CTraderHandler traderHandler = CTraderHandler(); 246 | CTraderHandler * pTraderHandler = &traderHandler; 247 | pTraderApi->RegisterSpi(pTraderHandler); 248 | 249 | // 设置服务器地址 250 | pTraderApi->RegisterFront(CTP_FrontAddress); 251 | // 链接交易系统 252 | pTraderApi->Init(); 253 | // 等待服务器发出登录消息 254 | sem_wait(&sem); 255 | // 发出登陆请求 256 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 257 | // 等待登录成功消息 258 | sem_wait(&sem); 259 | 260 | //////////////////////////////////////////////////////////////////////////////////////////////// 261 | ///请求查询行情 262 | /////////////////////////////////////////////////////////////////////////////////////////////// 263 | // 定义调用API的数据结构 264 | CThostFtdcQryDepthMarketDataField requestData; 265 | // 确保没有初始化的数据不会被访问 266 | memset(&requestData,0,sizeof(requestData)); 267 | // 为调用结构题设置参数信息 268 | ///合约代码 TThostFtdcInstrumentIDType char[31] 269 | strcpy(requestData.InstrumentID,""); 270 | 271 | 272 | // 调用API,并等待响应函数返回 273 | int result = pTraderApi->ReqQryDepthMarketData(&requestData,requestID++); 274 | sem_wait(&sem); 275 | 276 | ///////////////////////////////////////////////////////////////////////////////////////////////// 277 | 278 | 279 | // 拷贝用户登录信息到登出信息 280 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 281 | strcpy(userLogoutField.UserID, userLoginField.UserID); 282 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 283 | 284 | // 等待登出成功 285 | sem_wait(&sem); 286 | 287 | printf("主线程执行完毕!\n"); 288 | return(0); 289 | 290 | } 291 | -------------------------------------------------------------------------------- /QryDepthMarketData.InstrumentID/QryDepthMarketData.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询行情响应 79 | virtual void OnRspQryDepthMarketData( 80 | CThostFtdcDepthMarketDataField * pDepthMarketData, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | //printf("OnRspQryDepthMarketData():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryDepthMarketData():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pDepthMarketData != NULL ) { 98 | // 读取返回信息,并做编码转化 99 | ///交易日 TThostFtdcDateType char[9] 100 | char TradingDay[27]; 101 | gbk2utf8(pDepthMarketData->TradingDay,TradingDay,sizeof(TradingDay)); 102 | ///合约代码 TThostFtdcInstrumentIDType char[31] 103 | char InstrumentID[93]; 104 | gbk2utf8(pDepthMarketData->InstrumentID,InstrumentID,sizeof(InstrumentID)); 105 | ///交易所代码 TThostFtdcExchangeIDType char[9] 106 | char ExchangeID[27]; 107 | gbk2utf8(pDepthMarketData->ExchangeID,ExchangeID,sizeof(ExchangeID)); 108 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 109 | char ExchangeInstID[93]; 110 | gbk2utf8(pDepthMarketData->ExchangeInstID,ExchangeInstID,sizeof(ExchangeInstID)); 111 | ///最新价 TThostFtdcPriceType double 112 | double LastPrice = pDepthMarketData->LastPrice; 113 | ///上次结算价 TThostFtdcPriceType double 114 | double PreSettlementPrice = pDepthMarketData->PreSettlementPrice; 115 | ///昨收盘 TThostFtdcPriceType double 116 | double PreClosePrice = pDepthMarketData->PreClosePrice; 117 | ///昨持仓量 TThostFtdcLargeVolumeType double 118 | double PreOpenInterest = pDepthMarketData->PreOpenInterest; 119 | ///今开盘 TThostFtdcPriceType double 120 | double OpenPrice = pDepthMarketData->OpenPrice; 121 | ///最高价 TThostFtdcPriceType double 122 | double HighestPrice = pDepthMarketData->HighestPrice; 123 | ///最低价 TThostFtdcPriceType double 124 | double LowestPrice = pDepthMarketData->LowestPrice; 125 | ///数量 TThostFtdcVolumeType int 126 | int Volume = pDepthMarketData->Volume; 127 | ///成交金额 TThostFtdcMoneyType double 128 | double Turnover = pDepthMarketData->Turnover; 129 | ///持仓量 TThostFtdcLargeVolumeType double 130 | double OpenInterest = pDepthMarketData->OpenInterest; 131 | ///今收盘 TThostFtdcPriceType double 132 | double ClosePrice = pDepthMarketData->ClosePrice; 133 | ///本次结算价 TThostFtdcPriceType double 134 | double SettlementPrice = pDepthMarketData->SettlementPrice; 135 | ///涨停板价 TThostFtdcPriceType double 136 | double UpperLimitPrice = pDepthMarketData->UpperLimitPrice; 137 | ///跌停板价 TThostFtdcPriceType double 138 | double LowerLimitPrice = pDepthMarketData->LowerLimitPrice; 139 | ///昨虚实度 TThostFtdcRatioType double 140 | double PreDelta = pDepthMarketData->PreDelta; 141 | ///今虚实度 TThostFtdcRatioType double 142 | double CurrDelta = pDepthMarketData->CurrDelta; 143 | ///最后修改时间 TThostFtdcTimeType char[9] 144 | char UpdateTime[27]; 145 | gbk2utf8(pDepthMarketData->UpdateTime,UpdateTime,sizeof(UpdateTime)); 146 | ///最后修改毫秒 TThostFtdcMillisecType int 147 | int UpdateMillisec = pDepthMarketData->UpdateMillisec; 148 | ///申买价一 TThostFtdcPriceType double 149 | double BidPrice1 = pDepthMarketData->BidPrice1; 150 | ///申买量一 TThostFtdcVolumeType int 151 | int BidVolume1 = pDepthMarketData->BidVolume1; 152 | ///申卖价一 TThostFtdcPriceType double 153 | double AskPrice1 = pDepthMarketData->AskPrice1; 154 | ///申卖量一 TThostFtdcVolumeType int 155 | int AskVolume1 = pDepthMarketData->AskVolume1; 156 | ///申买价二 TThostFtdcPriceType double 157 | double BidPrice2 = pDepthMarketData->BidPrice2; 158 | ///申买量二 TThostFtdcVolumeType int 159 | int BidVolume2 = pDepthMarketData->BidVolume2; 160 | ///申卖价二 TThostFtdcPriceType double 161 | double AskPrice2 = pDepthMarketData->AskPrice2; 162 | ///申卖量二 TThostFtdcVolumeType int 163 | int AskVolume2 = pDepthMarketData->AskVolume2; 164 | ///申买价三 TThostFtdcPriceType double 165 | double BidPrice3 = pDepthMarketData->BidPrice3; 166 | ///申买量三 TThostFtdcVolumeType int 167 | int BidVolume3 = pDepthMarketData->BidVolume3; 168 | ///申卖价三 TThostFtdcPriceType double 169 | double AskPrice3 = pDepthMarketData->AskPrice3; 170 | ///申卖量三 TThostFtdcVolumeType int 171 | int AskVolume3 = pDepthMarketData->AskVolume3; 172 | ///申买价四 TThostFtdcPriceType double 173 | double BidPrice4 = pDepthMarketData->BidPrice4; 174 | ///申买量四 TThostFtdcVolumeType int 175 | int BidVolume4 = pDepthMarketData->BidVolume4; 176 | ///申卖价四 TThostFtdcPriceType double 177 | double AskPrice4 = pDepthMarketData->AskPrice4; 178 | ///申卖量四 TThostFtdcVolumeType int 179 | int AskVolume4 = pDepthMarketData->AskVolume4; 180 | ///申买价五 TThostFtdcPriceType double 181 | double BidPrice5 = pDepthMarketData->BidPrice5; 182 | ///申买量五 TThostFtdcVolumeType int 183 | int BidVolume5 = pDepthMarketData->BidVolume5; 184 | ///申卖价五 TThostFtdcPriceType double 185 | double AskPrice5 = pDepthMarketData->AskPrice5; 186 | ///申卖量五 TThostFtdcVolumeType int 187 | int AskVolume5 = pDepthMarketData->AskVolume5; 188 | ///当日均价 TThostFtdcPriceType double 189 | double AveragePrice = pDepthMarketData->AveragePrice; 190 | ///业务日期 TThostFtdcDateType char[9] 191 | char ActionDay[27]; 192 | gbk2utf8(pDepthMarketData->ActionDay,ActionDay,sizeof(ActionDay)); 193 | 194 | printf("TradingDay=%s,",TradingDay); // 交易日 195 | printf("InstrumentID=%s,",InstrumentID); // 合约代码 196 | printf("LastPrice=%f\n",LastPrice); // 最新价 197 | 198 | } 199 | 200 | // 如果响应函数已经返回最后一条信息 201 | if(bIsLast) { 202 | // 通知主过程,响应函数将结束 203 | sem_post(&sem); 204 | } 205 | } 206 | 207 | }; 208 | 209 | 210 | int main() { 211 | 212 | // 初始化线程同步变量 213 | sem_init(&sem,0,0); 214 | 215 | // 从环境变量中读取登录信息 216 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 217 | if ( CTP_FrontAddress == NULL ) { 218 | printf("环境变量CTP_FrontAddress没有设置\n"); 219 | return(0); 220 | } 221 | 222 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 223 | if ( CTP_BrokerId == NULL ) { 224 | printf("环境变量CTP_BrokerId没有设置\n"); 225 | return(0); 226 | } 227 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 228 | 229 | char * CTP_UserId = getenv("CTP_UserId"); 230 | if ( CTP_UserId == NULL ) { 231 | printf("环境变量CTP_UserId没有设置\n"); 232 | return(0); 233 | } 234 | strcpy(userLoginField.UserID,CTP_UserId); 235 | 236 | char * CTP_Password = getenv("CTP_Password"); 237 | if ( CTP_Password == NULL ) { 238 | printf("环境变量CTP_Password没有设置\n"); 239 | return(0); 240 | } 241 | strcpy(userLoginField.Password,CTP_Password); 242 | 243 | // 创建TraderAPI和回调响应控制器的实例 244 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 245 | CTraderHandler traderHandler = CTraderHandler(); 246 | CTraderHandler * pTraderHandler = &traderHandler; 247 | pTraderApi->RegisterSpi(pTraderHandler); 248 | 249 | // 设置服务器地址 250 | pTraderApi->RegisterFront(CTP_FrontAddress); 251 | // 链接交易系统 252 | pTraderApi->Init(); 253 | // 等待服务器发出登录消息 254 | sem_wait(&sem); 255 | // 发出登陆请求 256 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 257 | // 等待登录成功消息 258 | sem_wait(&sem); 259 | 260 | //////////////////////////////////////////////////////////////////////////////////////////////// 261 | ///请求查询行情 262 | /////////////////////////////////////////////////////////////////////////////////////////////// 263 | // 定义调用API的数据结构 264 | CThostFtdcQryDepthMarketDataField requestData; 265 | // 确保没有初始化的数据不会被访问 266 | memset(&requestData,0,sizeof(requestData)); 267 | // 为调用结构题设置参数信息 268 | ///合约代码 TThostFtdcInstrumentIDType char[31] 269 | strcpy(requestData.InstrumentID,"SR609C4900"); 270 | 271 | 272 | // 调用API,并等待响应函数返回 273 | int result = pTraderApi->ReqQryDepthMarketData(&requestData,requestID++); 274 | sem_wait(&sem); 275 | 276 | ///////////////////////////////////////////////////////////////////////////////////////////////// 277 | 278 | 279 | // 拷贝用户登录信息到登出信息 280 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 281 | strcpy(userLogoutField.UserID, userLoginField.UserID); 282 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 283 | 284 | // 等待登出成功 285 | sem_wait(&sem); 286 | 287 | printf("主线程执行完毕!\n"); 288 | return(0); 289 | 290 | } 291 | -------------------------------------------------------------------------------- /QryTradingAccount.1/QryTradingAccount.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 字符串编码转化 18 | #include 19 | 20 | // 登录请求结构体 21 | CThostFtdcReqUserLoginField userLoginField; 22 | // 用户请求结构体 23 | CThostFtdcUserLogoutField userLogoutField; 24 | // 线程同步标志 25 | sem_t sem; 26 | // requestID 27 | int requestID = 0; 28 | 29 | 30 | class CTraderHandler : public CThostFtdcTraderSpi { 31 | 32 | public: 33 | 34 | CTraderHandler() { 35 | printf("CTraderHandler():被执行...\n"); 36 | } 37 | 38 | // 允许登录事件 39 | virtual void OnFrontConnected() { 40 | static int i = 0; 41 | printf("OnFrontConnected():被执行...\n"); 42 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 43 | if (i++==0) { 44 | sem_post(&sem); 45 | } 46 | } 47 | 48 | // 登录结果响应 49 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 50 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 51 | printf("OnRspUserLogin():被执行...\n"); 52 | if (pRspInfo->ErrorID == 0) { 53 | printf("登录成功!\n"); 54 | sem_post(&sem); 55 | } else { 56 | printf("登录失败!\n"); 57 | } 58 | } 59 | 60 | // 登出结果响应 61 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 62 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 63 | printf("OnReqUserLogout():被执行...\n"); 64 | if (pRspInfo->ErrorID == 0) { 65 | printf("登出成功!\n"); 66 | sem_post(&sem); 67 | } else { 68 | printf("登出失败!\n"); 69 | } 70 | } 71 | 72 | // 错误信息响应方法 73 | virtual void OnRspError 74 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 75 | printf("OnRspError():被执行...\n"); 76 | } 77 | 78 | ///请求查询资金账户响应 79 | virtual void OnRspQryTradingAccount( 80 | CThostFtdcTradingAccountField * pTradingAccount, 81 | CThostFtdcRspInfoField * pRspInfo, 82 | int nRequestID, 83 | bool bIsLast 84 | ) { 85 | printf("OnRspQryTradingAccount():被执行...\n"); 86 | 87 | // 进程返回结果检查 88 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 89 | // typedef int TThostFtdcErrorIDType; 90 | // typedef char TThostFtdcErrorMsgType[81]; 91 | char ErrorMsg[243]; 92 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 93 | printf("OnRspQryTradingAccount():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 94 | } 95 | 96 | // 如果有返回结果读取返回信息 97 | if ( pTradingAccount != NULL ) { 98 | // 读取返回信息,并做编码转化 99 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 100 | char BrokerID[33]; 101 | gbk2utf8(pTradingAccount->BrokerID,BrokerID,sizeof(BrokerID)); 102 | ///投资者帐号 TThostFtdcAccountIDType char[13] 103 | char AccountID[39]; 104 | gbk2utf8(pTradingAccount->AccountID,AccountID,sizeof(AccountID)); 105 | printf("len(pTradingAccount->AccountID)=%d\n",(int)strlen(pTradingAccount->AccountID)); 106 | printf("len(AccountID)=%d\n",(int)strlen(AccountID)); 107 | ///上次质押金额 TThostFtdcMoneyType double 108 | double PreMortgage = pTradingAccount->PreMortgage; 109 | ///上次信用额度 TThostFtdcMoneyType double 110 | double PreCredit = pTradingAccount->PreCredit; 111 | ///上次存款额 TThostFtdcMoneyType double 112 | double PreDeposit = pTradingAccount->PreDeposit; 113 | ///上次结算准备金 TThostFtdcMoneyType double 114 | double PreBalance = pTradingAccount->PreBalance; 115 | ///上次占用的保证金 TThostFtdcMoneyType double 116 | double PreMargin = pTradingAccount->PreMargin; 117 | ///利息基数 TThostFtdcMoneyType double 118 | double InterestBase = pTradingAccount->InterestBase; 119 | ///利息收入 TThostFtdcMoneyType double 120 | double Interest = pTradingAccount->Interest; 121 | ///入金金额 TThostFtdcMoneyType double 122 | double Deposit = pTradingAccount->Deposit; 123 | ///出金金额 TThostFtdcMoneyType double 124 | double Withdraw = pTradingAccount->Withdraw; 125 | ///冻结的保证金 TThostFtdcMoneyType double 126 | double FrozenMargin = pTradingAccount->FrozenMargin; 127 | ///冻结的资金 TThostFtdcMoneyType double 128 | double FrozenCash = pTradingAccount->FrozenCash; 129 | ///冻结的手续费 TThostFtdcMoneyType double 130 | double FrozenCommission = pTradingAccount->FrozenCommission; 131 | ///当前保证金总额 TThostFtdcMoneyType double 132 | double CurrMargin = pTradingAccount->CurrMargin; 133 | ///资金差额 TThostFtdcMoneyType double 134 | double CashIn = pTradingAccount->CashIn; 135 | ///手续费 TThostFtdcMoneyType double 136 | double Commission = pTradingAccount->Commission; 137 | ///平仓盈亏 TThostFtdcMoneyType double 138 | double CloseProfit = pTradingAccount->CloseProfit; 139 | ///持仓盈亏 TThostFtdcMoneyType double 140 | double PositionProfit = pTradingAccount->PositionProfit; 141 | ///期货结算准备金 TThostFtdcMoneyType double 142 | double Balance = pTradingAccount->Balance; 143 | ///可用资金 TThostFtdcMoneyType double 144 | double Available = pTradingAccount->Available; 145 | ///可取资金 TThostFtdcMoneyType double 146 | double WithdrawQuota = pTradingAccount->WithdrawQuota; 147 | ///基本准备金 TThostFtdcMoneyType double 148 | double Reserve = pTradingAccount->Reserve; 149 | ///交易日 TThostFtdcDateType char[9] 150 | char TradingDay[27]; 151 | gbk2utf8(pTradingAccount->TradingDay,TradingDay,sizeof(TradingDay)); 152 | ///结算编号 TThostFtdcSettlementIDType int 153 | int SettlementID = pTradingAccount->SettlementID; 154 | ///信用额度 TThostFtdcMoneyType double 155 | double Credit = pTradingAccount->Credit; 156 | ///质押金额 TThostFtdcMoneyType double 157 | double Mortgage = pTradingAccount->Mortgage; 158 | ///交易所保证金 TThostFtdcMoneyType double 159 | double ExchangeMargin = pTradingAccount->ExchangeMargin; 160 | ///投资者交割保证金 TThostFtdcMoneyType double 161 | double DeliveryMargin = pTradingAccount->DeliveryMargin; 162 | ///交易所交割保证金 TThostFtdcMoneyType double 163 | double ExchangeDeliveryMargin = pTradingAccount->ExchangeDeliveryMargin; 164 | ///保底期货结算准备金 TThostFtdcMoneyType double 165 | double ReserveBalance = pTradingAccount->ReserveBalance; 166 | ///币种代码 TThostFtdcCurrencyIDType char[4] 167 | char CurrencyID[12]; 168 | gbk2utf8(pTradingAccount->CurrencyID,CurrencyID,sizeof(pTradingAccount->CurrencyID)); 169 | ///上次货币质入金额 TThostFtdcMoneyType double 170 | double PreFundMortgageIn = pTradingAccount->PreFundMortgageIn; 171 | ///上次货币质出金额 TThostFtdcMoneyType double 172 | double PreFundMortgageOut = pTradingAccount->PreFundMortgageOut; 173 | ///货币质入金额 TThostFtdcMoneyType double 174 | double FundMortgageIn = pTradingAccount->FundMortgageIn; 175 | ///货币质出金额 TThostFtdcMoneyType double 176 | double FundMortgageOut = pTradingAccount->FundMortgageOut; 177 | ///货币质押余额 TThostFtdcMoneyType double 178 | double FundMortgageAvailable = pTradingAccount->FundMortgageAvailable; 179 | ///可质押货币金额 TThostFtdcMoneyType double 180 | double MortgageableFund = pTradingAccount->MortgageableFund; 181 | ///特殊产品占用保证金 TThostFtdcMoneyType double 182 | double SpecProductMargin = pTradingAccount->SpecProductMargin; 183 | ///特殊产品冻结保证金 TThostFtdcMoneyType double 184 | double SpecProductFrozenMargin = pTradingAccount->SpecProductFrozenMargin; 185 | ///特殊产品手续费 TThostFtdcMoneyType double 186 | double SpecProductCommission = pTradingAccount->SpecProductCommission; 187 | ///特殊产品冻结手续费 TThostFtdcMoneyType double 188 | double SpecProductFrozenCommission = pTradingAccount->SpecProductFrozenCommission; 189 | ///特殊产品持仓盈亏 TThostFtdcMoneyType double 190 | double SpecProductPositionProfit = pTradingAccount->SpecProductPositionProfit; 191 | ///特殊产品平仓盈亏 TThostFtdcMoneyType double 192 | double SpecProductCloseProfit = pTradingAccount->SpecProductCloseProfit; 193 | ///根据持仓盈亏算法计算的特殊产品持仓盈亏 TThostFtdcMoneyType double 194 | double SpecProductPositionProfitByAlg = pTradingAccount->SpecProductPositionProfitByAlg; 195 | ///特殊产品交易所保证金 TThostFtdcMoneyType double 196 | double SpecProductExchangeMargin = pTradingAccount->SpecProductExchangeMargin; 197 | 198 | // 打印出关键信息 199 | printf("经纪公司代码=%s,投资者帐号=%s,币种代码=%s,可用金额=%.2f\n",BrokerID,AccountID,CurrencyID,Available); 200 | 201 | } 202 | 203 | // 如果响应函数已经返回最后一条信息 204 | if(bIsLast) { 205 | // 通知主过程,响应函数将结束 206 | sem_post(&sem); 207 | } 208 | } 209 | 210 | }; 211 | 212 | 213 | int main() { 214 | 215 | // 初始化线程同步变量 216 | sem_init(&sem,0,0); 217 | 218 | // 从环境变量中读取登录信息 219 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 220 | if ( CTP_FrontAddress == NULL ) { 221 | printf("环境变量CTP_FrontAddress没有设置\n"); 222 | return(0); 223 | } 224 | 225 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 226 | if ( CTP_BrokerId == NULL ) { 227 | printf("环境变量CTP_BrokerId没有设置\n"); 228 | return(0); 229 | } 230 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 231 | 232 | char * CTP_UserId = getenv("CTP_UserId"); 233 | if ( CTP_UserId == NULL ) { 234 | printf("环境变量CTP_UserId没有设置\n"); 235 | return(0); 236 | } 237 | strcpy(userLoginField.UserID,CTP_UserId); 238 | 239 | char * CTP_Password = getenv("CTP_Password"); 240 | if ( CTP_Password == NULL ) { 241 | printf("环境变量CTP_Password没有设置\n"); 242 | return(0); 243 | } 244 | strcpy(userLoginField.Password,CTP_Password); 245 | 246 | // 创建TraderAPI和回调响应控制器的实例 247 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 248 | CTraderHandler traderHandler = CTraderHandler(); 249 | CTraderHandler * pTraderHandler = &traderHandler; 250 | pTraderApi->RegisterSpi(pTraderHandler); 251 | 252 | // 设置服务器地址 253 | pTraderApi->RegisterFront(CTP_FrontAddress); 254 | // 链接交易系统 255 | pTraderApi->Init(); 256 | // 等待服务器发出登录消息 257 | sem_wait(&sem); 258 | // 发出登陆请求 259 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 260 | // 等待登录成功消息 261 | sem_wait(&sem); 262 | 263 | //////////////////////////////////////////////////////////////////////////////////////////////// 264 | ///请求查询资金账户 265 | /////////////////////////////////////////////////////////////////////////////////////////////// 266 | // 定义调用API的数据结构 267 | CThostFtdcQryTradingAccountField requestData; 268 | // 确保没有初始化的数据不会被访问 269 | memset(&requestData,0,sizeof(requestData)); 270 | // 为调用结构题设置参数信息 271 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 272 | strcpy(requestData.BrokerID,""); 273 | ///投资者代码 TThostFtdcInvestorIDType char[13] 274 | strcpy(requestData.InvestorID,""); 275 | ///币种代码 TThostFtdcCurrencyIDType char[4] 276 | strcpy(requestData.CurrencyID,""); 277 | 278 | 279 | // 调用API,并等待响应函数返回 280 | int result = pTraderApi->ReqQryTradingAccount(&requestData,requestID++); 281 | sem_wait(&sem); 282 | 283 | ///////////////////////////////////////////////////////////////////////////////////////////////// 284 | 285 | 286 | // 拷贝用户登录信息到登出信息 287 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 288 | strcpy(userLogoutField.UserID, userLoginField.UserID); 289 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 290 | 291 | // 等待登出成功 292 | sem_wait(&sem); 293 | 294 | printf("主线程执行完毕!\n"); 295 | return(0); 296 | 297 | } 298 | -------------------------------------------------------------------------------- /QryInvestorPosition.1/QryInvestorPosition.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 定时器相关 18 | #include 19 | #include 20 | 21 | 22 | // 字符串编码转化 23 | #include 24 | 25 | 26 | 27 | 28 | // 登录请求结构体 29 | CThostFtdcReqUserLoginField userLoginField; 30 | // 用户请求结构体 31 | CThostFtdcUserLogoutField userLogoutField; 32 | // 线程同步标志 33 | sem_t sem; 34 | // requestID 35 | int requestID = 0; 36 | 37 | 38 | class CTraderHandler : public CThostFtdcTraderSpi { 39 | 40 | public: 41 | 42 | CTraderHandler() { 43 | printf("CTraderHandler():被执行...\n"); 44 | } 45 | 46 | // 允许登录事件 47 | virtual void OnFrontConnected() { 48 | static int i = 0; 49 | printf("OnFrontConnected():被执行...\n"); 50 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 51 | if (i++==0) { 52 | sem_post(&sem); 53 | } 54 | } 55 | 56 | // 登录结果响应 57 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 58 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 59 | printf("OnRspUserLogin():被执行...\n"); 60 | if (pRspInfo->ErrorID == 0) { 61 | printf("登录成功!\n"); 62 | sem_post(&sem); 63 | } else { 64 | printf("登录失败!\n"); 65 | } 66 | } 67 | 68 | // 登出结果响应 69 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 70 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 71 | printf("OnReqUserLogout():被执行...\n"); 72 | if (pRspInfo->ErrorID == 0) { 73 | printf("登出成功!\n"); 74 | sem_post(&sem); 75 | } else { 76 | printf("登出失败!\n"); 77 | } 78 | } 79 | 80 | // 错误信息响应方法 81 | virtual void OnRspError 82 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 83 | printf("OnRspError():被执行...\n"); 84 | } 85 | 86 | ///请求查询投资者持仓响应 87 | virtual void OnRspQryInvestorPosition( 88 | CThostFtdcInvestorPositionField * pInvestorPosition, 89 | CThostFtdcRspInfoField * pRspInfo, 90 | int nRequestID, 91 | bool bIsLast 92 | ) { 93 | printf("OnRspQryInvestorPosition():被执行...\n"); 94 | 95 | // 进程返回结果检查 96 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 97 | // typedef int TThostFtdcErrorIDType; 98 | // typedef char TThostFtdcErrorMsgType[81]; 99 | char ErrorMsg[243]; 100 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 101 | printf("OnRspQryInvestorPosition():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 102 | } 103 | 104 | // 如果有返回结果读取返回信息 105 | if ( pInvestorPosition != NULL ) { 106 | // 读取返回信息,并做编码转化 107 | ///合约代码 TThostFtdcInstrumentIDType char[31] 108 | char InstrumentID[93]; 109 | gbk2utf8(pInvestorPosition->InstrumentID,InstrumentID,sizeof(InstrumentID)); 110 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 111 | char BrokerID[33]; 112 | gbk2utf8(pInvestorPosition->BrokerID,BrokerID,sizeof(BrokerID)); 113 | ///投资者代码 TThostFtdcInvestorIDType char[13] 114 | char InvestorID[39]; 115 | gbk2utf8(pInvestorPosition->InvestorID,InvestorID,sizeof(InvestorID)); 116 | ///持仓多空方向 TThostFtdcPosiDirectionType char 117 | //// THOST_FTDC_PD_Net '1' 净 118 | //// THOST_FTDC_PD_Long '2' 多头 119 | //// THOST_FTDC_PD_Short '3' 空头 120 | char PosiDirection = pInvestorPosition->PosiDirection; 121 | ///投机套保标志 TThostFtdcHedgeFlagType char 122 | //// THOST_FTDC_HF_Speculation '1' 投机 123 | //// THOST_FTDC_HF_Arbitrage '2' 套利 124 | //// THOST_FTDC_HF_Hedge '3' 套保 125 | char HedgeFlag = pInvestorPosition->HedgeFlag; 126 | ///持仓日期 TThostFtdcPositionDateType char 127 | //// THOST_FTDC_PSD_Today '1' 今日持仓 128 | //// THOST_FTDC_PSD_History '2' 历史持仓 129 | char PositionDate = pInvestorPosition->PositionDate; 130 | ///上日持仓 TThostFtdcVolumeType int 131 | int YdPosition = pInvestorPosition->YdPosition; 132 | ///今日持仓 TThostFtdcVolumeType int 133 | int Position = pInvestorPosition->Position; 134 | ///多头冻结 TThostFtdcVolumeType int 135 | int LongFrozen = pInvestorPosition->LongFrozen; 136 | ///空头冻结 TThostFtdcVolumeType int 137 | int ShortFrozen = pInvestorPosition->ShortFrozen; 138 | ///开仓冻结金额 TThostFtdcMoneyType double 139 | double LongFrozenAmount = pInvestorPosition->LongFrozenAmount; 140 | ///开仓冻结金额 TThostFtdcMoneyType double 141 | double ShortFrozenAmount = pInvestorPosition->ShortFrozenAmount; 142 | ///开仓量 TThostFtdcVolumeType int 143 | int OpenVolume = pInvestorPosition->OpenVolume; 144 | ///平仓量 TThostFtdcVolumeType int 145 | int CloseVolume = pInvestorPosition->CloseVolume; 146 | ///开仓金额 TThostFtdcMoneyType double 147 | double OpenAmount = pInvestorPosition->OpenAmount; 148 | ///平仓金额 TThostFtdcMoneyType double 149 | double CloseAmount = pInvestorPosition->CloseAmount; 150 | ///持仓成本 TThostFtdcMoneyType double 151 | double PositionCost = pInvestorPosition->PositionCost; 152 | ///上次占用的保证金 TThostFtdcMoneyType double 153 | double PreMargin = pInvestorPosition->PreMargin; 154 | ///占用的保证金 TThostFtdcMoneyType double 155 | double UseMargin = pInvestorPosition->UseMargin; 156 | ///冻结的保证金 TThostFtdcMoneyType double 157 | double FrozenMargin = pInvestorPosition->FrozenMargin; 158 | ///冻结的资金 TThostFtdcMoneyType double 159 | double FrozenCash = pInvestorPosition->FrozenCash; 160 | ///冻结的手续费 TThostFtdcMoneyType double 161 | double FrozenCommission = pInvestorPosition->FrozenCommission; 162 | ///资金差额 TThostFtdcMoneyType double 163 | double CashIn = pInvestorPosition->CashIn; 164 | ///手续费 TThostFtdcMoneyType double 165 | double Commission = pInvestorPosition->Commission; 166 | ///平仓盈亏 TThostFtdcMoneyType double 167 | double CloseProfit = pInvestorPosition->CloseProfit; 168 | ///持仓盈亏 TThostFtdcMoneyType double 169 | double PositionProfit = pInvestorPosition->PositionProfit; 170 | ///上次结算价 TThostFtdcPriceType double 171 | double PreSettlementPrice = pInvestorPosition->PreSettlementPrice; 172 | ///本次结算价 TThostFtdcPriceType double 173 | double SettlementPrice = pInvestorPosition->SettlementPrice; 174 | ///交易日 TThostFtdcDateType char[9] 175 | char TradingDay[27]; 176 | gbk2utf8(pInvestorPosition->TradingDay,TradingDay,sizeof(TradingDay)); 177 | ///结算编号 TThostFtdcSettlementIDType int 178 | int SettlementID = pInvestorPosition->SettlementID; 179 | ///开仓成本 TThostFtdcMoneyType double 180 | double OpenCost = pInvestorPosition->OpenCost; 181 | ///交易所保证金 TThostFtdcMoneyType double 182 | double ExchangeMargin = pInvestorPosition->ExchangeMargin; 183 | ///组合成交形成的持仓 TThostFtdcVolumeType int 184 | int CombPosition = pInvestorPosition->CombPosition; 185 | ///组合多头冻结 TThostFtdcVolumeType int 186 | int CombLongFrozen = pInvestorPosition->CombLongFrozen; 187 | ///组合空头冻结 TThostFtdcVolumeType int 188 | int CombShortFrozen = pInvestorPosition->CombShortFrozen; 189 | ///逐日盯市平仓盈亏 TThostFtdcMoneyType double 190 | double CloseProfitByDate = pInvestorPosition->CloseProfitByDate; 191 | ///逐笔对冲平仓盈亏 TThostFtdcMoneyType double 192 | double CloseProfitByTrade = pInvestorPosition->CloseProfitByTrade; 193 | ///今日持仓 TThostFtdcVolumeType int 194 | int TodayPosition = pInvestorPosition->TodayPosition; 195 | ///保证金率 TThostFtdcRatioType double 196 | double MarginRateByMoney = pInvestorPosition->MarginRateByMoney; 197 | ///保证金率(按手数) TThostFtdcRatioType double 198 | double MarginRateByVolume = pInvestorPosition->MarginRateByVolume; 199 | 200 | printf("InstrumentID=%s,PosiDirection=%c,Position=%d,OpenVolume=%d,PositionCost=%.2f,CloseProfitByDate=%.2f,CloseVolume=%d\n",InstrumentID,PosiDirection,Position,OpenVolume,PositionCost,CloseProfitByDate,CloseVolume); 201 | 202 | } 203 | 204 | // 如果响应函数已经返回最后一条信息 205 | if(bIsLast) { 206 | // 通知主过程,响应函数将结束 207 | sem_post(&sem); 208 | } 209 | } 210 | 211 | }; 212 | 213 | 214 | // 定时器控制函数 215 | void timeout_handler(int signalno) { 216 | // 定时控制步骤 217 | } 218 | 219 | // 初始化定时器 220 | void init_timer(void) 221 | { 222 | // init sigaction 223 | struct sigaction act; 224 | act.sa_handler = timeout_handler; 225 | act.sa_flags = 0; 226 | sigemptyset(&act.sa_mask); 227 | sigaction(SIGPROF, &act, NULL); 228 | // set timer 229 | struct itimerval val; 230 | val.it_value.tv_sec = 1; 231 | val.it_value.tv_usec = 0; 232 | val.it_interval = val.it_value; 233 | setitimer(ITIMER_PROF, &val, NULL); 234 | } 235 | 236 | 237 | int main() { 238 | 239 | // 初始化线程同步变量 240 | sem_init(&sem,0,0); 241 | 242 | // 从环境变量中读取登录信息 243 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 244 | if ( CTP_FrontAddress == NULL ) { 245 | printf("环境变量CTP_FrontAddress没有设置\n"); 246 | return(0); 247 | } 248 | 249 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 250 | if ( CTP_BrokerId == NULL ) { 251 | printf("环境变量CTP_BrokerId没有设置\n"); 252 | return(0); 253 | } 254 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 255 | 256 | char * CTP_UserId = getenv("CTP_UserId"); 257 | if ( CTP_UserId == NULL ) { 258 | printf("环境变量CTP_UserId没有设置\n"); 259 | return(0); 260 | } 261 | strcpy(userLoginField.UserID,CTP_UserId); 262 | 263 | char * CTP_Password = getenv("CTP_Password"); 264 | if ( CTP_Password == NULL ) { 265 | printf("环境变量CTP_Password没有设置\n"); 266 | return(0); 267 | } 268 | strcpy(userLoginField.Password,CTP_Password); 269 | 270 | // 创建TraderAPI和回调响应控制器的实例 271 | CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 272 | CTraderHandler traderHandler = CTraderHandler(); 273 | CTraderHandler * pTraderHandler = &traderHandler; 274 | pTraderApi->RegisterSpi(pTraderHandler); 275 | 276 | // 订阅相关信息推送 277 | //// THOST_TERT_RESTART:从本交易日开始重传 278 | //// THOST_TERT_RESUME:从上次收到的续传 279 | //// THOST_TERT_QUICK:只传送登录后私有流的内容 280 | pTraderApi->SubscribePrivateTopic(THOST_TERT_RESUME); 281 | // 订阅公共流 282 | //// THOST_TERT_RESTART:从本交易日开始重传 283 | //// THOST_TERT_RESUME:从上次收到的续传 284 | //// THOST_TERT_QUICK:只传送登录后公共流的内容 285 | pTraderApi->SubscribePublicTopic(THOST_TERT_RESUME); 286 | 287 | // 设置服务器地址 288 | pTraderApi->RegisterFront(CTP_FrontAddress); 289 | // 链接交易系统 290 | pTraderApi->Init(); 291 | // 等待服务器发出登录消息 292 | sem_wait(&sem); 293 | // 发出登陆请求 294 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 295 | // 等待登录成功消息 296 | sem_wait(&sem); 297 | 298 | //////////////////////////////////////////////////////////////////////////////////////////////// 299 | ///请求查询投资者持仓 300 | /////////////////////////////////////////////////////////////////////////////////////////////// 301 | // 定义调用API的数据结构 302 | CThostFtdcQryInvestorPositionField requestData; 303 | // 确保没有初始化的数据不会被访问 304 | memset(&requestData,0,sizeof(requestData)); 305 | // 为调用结构题设置参数信息 306 | ///经纪公司代码 TThostFtdcBrokerIDType char[11] 307 | strcpy(requestData.BrokerID,CTP_BrokerId); 308 | ///投资者代码 TThostFtdcInvestorIDType char[13] 309 | strcpy(requestData.InvestorID,CTP_UserId); 310 | ///合约代码 TThostFtdcInstrumentIDType char[31] 311 | strcpy(requestData.InstrumentID,""); 312 | 313 | 314 | // 调用API,并等待响应函数返回 315 | int result = pTraderApi->ReqQryInvestorPosition(&requestData,requestID++); 316 | sem_wait(&sem); 317 | 318 | ///////////////////////////////////////////////////////////////////////////////////////////////// 319 | 320 | 321 | // 拷贝用户登录信息到登出信息 322 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 323 | strcpy(userLogoutField.UserID, userLoginField.UserID); 324 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 325 | 326 | // 等待登出成功 327 | sem_wait(&sem); 328 | 329 | printf("主线程执行完毕!\n"); 330 | return(0); 331 | 332 | } 333 | -------------------------------------------------------------------------------- /QryDepthMarketData.timer.all/QryDepthMarketData.cpp: -------------------------------------------------------------------------------- 1 | // 标准C库文件 2 | #include 3 | #include 4 | #include 5 | 6 | // CTP头文件 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | // 线程控制相关 13 | #include 14 | #include 15 | #include 16 | 17 | // 定时器相关 18 | #include 19 | #include 20 | 21 | 22 | // 字符串编码转化 23 | #include 24 | 25 | 26 | // 定义API信息 27 | CThostFtdcTraderApi *pTraderApi; 28 | 29 | // 登录请求结构体 30 | CThostFtdcReqUserLoginField userLoginField; 31 | // 用户请求结构体 32 | CThostFtdcUserLogoutField userLogoutField; 33 | // 线程同步标志 34 | sem_t sem; 35 | // requestID 36 | int requestID = 0; 37 | 38 | 39 | class CTraderHandler : public CThostFtdcTraderSpi { 40 | 41 | public: 42 | 43 | CTraderHandler() { 44 | printf("CTraderHandler():被执行...\n"); 45 | } 46 | 47 | // 允许登录事件 48 | virtual void OnFrontConnected() { 49 | static int i = 0; 50 | printf("OnFrontConnected():被执行...\n"); 51 | // 在登出后系统会重新调用OnFrontConnected,这里简单判断并忽略第1次之后的所有调用。 52 | if (i++==0) { 53 | sem_post(&sem); 54 | } 55 | } 56 | 57 | // 登录结果响应 58 | virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *pRspUserLogin, 59 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 60 | printf("OnRspUserLogin():被执行...\n"); 61 | if (pRspInfo->ErrorID == 0) { 62 | printf("登录成功!\n"); 63 | sem_post(&sem); 64 | } else { 65 | printf("登录失败!\n"); 66 | } 67 | } 68 | 69 | // 登出结果响应 70 | virtual void OnRspUserLogout(CThostFtdcUserLogoutField *pUserLogout, 71 | CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 72 | printf("OnReqUserLogout():被执行...\n"); 73 | if (pRspInfo->ErrorID == 0) { 74 | printf("登出成功!\n"); 75 | sem_post(&sem); 76 | } else { 77 | printf("登出失败!\n"); 78 | } 79 | } 80 | 81 | // 错误信息响应方法 82 | virtual void OnRspError 83 | (CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast) { 84 | printf("OnRspError():被执行...\n"); 85 | } 86 | 87 | ///请求查询行情响应 88 | virtual void OnRspQryDepthMarketData( 89 | CThostFtdcDepthMarketDataField * pDepthMarketData, 90 | CThostFtdcRspInfoField * pRspInfo, 91 | int nRequestID, 92 | bool bIsLast 93 | ) { 94 | //printf("OnRspQryDepthMarketData():被执行...\n"); 95 | 96 | // 进程返回结果检查 97 | if ( (pRspInfo) && (pRspInfo->ErrorID != 0) ) { 98 | // typedef int TThostFtdcErrorIDType; 99 | // typedef char TThostFtdcErrorMsgType[81]; 100 | char ErrorMsg[243]; 101 | gbk2utf8(pRspInfo->ErrorMsg,ErrorMsg,sizeof(ErrorMsg)); 102 | printf("OnRspQryDepthMarketData():出错:ErrorId=%d,ErrorMsg=%s\n",pRspInfo->ErrorID,ErrorMsg); 103 | } 104 | 105 | // 如果有返回结果读取返回信息 106 | if ( pDepthMarketData != NULL ) { 107 | // 读取返回信息,并做编码转化 108 | ///交易日 TThostFtdcDateType char[9] 109 | char TradingDay[27]; 110 | gbk2utf8(pDepthMarketData->TradingDay,TradingDay,sizeof(TradingDay)); 111 | ///合约代码 TThostFtdcInstrumentIDType char[31] 112 | char InstrumentID[93]; 113 | gbk2utf8(pDepthMarketData->InstrumentID,InstrumentID,sizeof(InstrumentID)); 114 | ///交易所代码 TThostFtdcExchangeIDType char[9] 115 | char ExchangeID[27]; 116 | gbk2utf8(pDepthMarketData->ExchangeID,ExchangeID,sizeof(ExchangeID)); 117 | ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] 118 | char ExchangeInstID[93]; 119 | gbk2utf8(pDepthMarketData->ExchangeInstID,ExchangeInstID,sizeof(ExchangeInstID)); 120 | ///最新价 TThostFtdcPriceType double 121 | double LastPrice = pDepthMarketData->LastPrice; 122 | ///上次结算价 TThostFtdcPriceType double 123 | double PreSettlementPrice = pDepthMarketData->PreSettlementPrice; 124 | ///昨收盘 TThostFtdcPriceType double 125 | double PreClosePrice = pDepthMarketData->PreClosePrice; 126 | ///昨持仓量 TThostFtdcLargeVolumeType double 127 | double PreOpenInterest = pDepthMarketData->PreOpenInterest; 128 | ///今开盘 TThostFtdcPriceType double 129 | double OpenPrice = pDepthMarketData->OpenPrice; 130 | ///最高价 TThostFtdcPriceType double 131 | double HighestPrice = pDepthMarketData->HighestPrice; 132 | ///最低价 TThostFtdcPriceType double 133 | double LowestPrice = pDepthMarketData->LowestPrice; 134 | ///数量 TThostFtdcVolumeType int 135 | int Volume = pDepthMarketData->Volume; 136 | ///成交金额 TThostFtdcMoneyType double 137 | double Turnover = pDepthMarketData->Turnover; 138 | ///持仓量 TThostFtdcLargeVolumeType double 139 | double OpenInterest = pDepthMarketData->OpenInterest; 140 | ///今收盘 TThostFtdcPriceType double 141 | double ClosePrice = pDepthMarketData->ClosePrice; 142 | ///本次结算价 TThostFtdcPriceType double 143 | double SettlementPrice = pDepthMarketData->SettlementPrice; 144 | ///涨停板价 TThostFtdcPriceType double 145 | double UpperLimitPrice = pDepthMarketData->UpperLimitPrice; 146 | ///跌停板价 TThostFtdcPriceType double 147 | double LowerLimitPrice = pDepthMarketData->LowerLimitPrice; 148 | ///昨虚实度 TThostFtdcRatioType double 149 | double PreDelta = pDepthMarketData->PreDelta; 150 | ///今虚实度 TThostFtdcRatioType double 151 | double CurrDelta = pDepthMarketData->CurrDelta; 152 | ///最后修改时间 TThostFtdcTimeType char[9] 153 | char UpdateTime[27]; 154 | gbk2utf8(pDepthMarketData->UpdateTime,UpdateTime,sizeof(UpdateTime)); 155 | ///最后修改毫秒 TThostFtdcMillisecType int 156 | int UpdateMillisec = pDepthMarketData->UpdateMillisec; 157 | ///申买价一 TThostFtdcPriceType double 158 | double BidPrice1 = pDepthMarketData->BidPrice1; 159 | ///申买量一 TThostFtdcVolumeType int 160 | int BidVolume1 = pDepthMarketData->BidVolume1; 161 | ///申卖价一 TThostFtdcPriceType double 162 | double AskPrice1 = pDepthMarketData->AskPrice1; 163 | ///申卖量一 TThostFtdcVolumeType int 164 | int AskVolume1 = pDepthMarketData->AskVolume1; 165 | ///申买价二 TThostFtdcPriceType double 166 | double BidPrice2 = pDepthMarketData->BidPrice2; 167 | ///申买量二 TThostFtdcVolumeType int 168 | int BidVolume2 = pDepthMarketData->BidVolume2; 169 | ///申卖价二 TThostFtdcPriceType double 170 | double AskPrice2 = pDepthMarketData->AskPrice2; 171 | ///申卖量二 TThostFtdcVolumeType int 172 | int AskVolume2 = pDepthMarketData->AskVolume2; 173 | ///申买价三 TThostFtdcPriceType double 174 | double BidPrice3 = pDepthMarketData->BidPrice3; 175 | ///申买量三 TThostFtdcVolumeType int 176 | int BidVolume3 = pDepthMarketData->BidVolume3; 177 | ///申卖价三 TThostFtdcPriceType double 178 | double AskPrice3 = pDepthMarketData->AskPrice3; 179 | ///申卖量三 TThostFtdcVolumeType int 180 | int AskVolume3 = pDepthMarketData->AskVolume3; 181 | ///申买价四 TThostFtdcPriceType double 182 | double BidPrice4 = pDepthMarketData->BidPrice4; 183 | ///申买量四 TThostFtdcVolumeType int 184 | int BidVolume4 = pDepthMarketData->BidVolume4; 185 | ///申卖价四 TThostFtdcPriceType double 186 | double AskPrice4 = pDepthMarketData->AskPrice4; 187 | ///申卖量四 TThostFtdcVolumeType int 188 | int AskVolume4 = pDepthMarketData->AskVolume4; 189 | ///申买价五 TThostFtdcPriceType double 190 | double BidPrice5 = pDepthMarketData->BidPrice5; 191 | ///申买量五 TThostFtdcVolumeType int 192 | int BidVolume5 = pDepthMarketData->BidVolume5; 193 | ///申卖价五 TThostFtdcPriceType double 194 | double AskPrice5 = pDepthMarketData->AskPrice5; 195 | ///申卖量五 TThostFtdcVolumeType int 196 | int AskVolume5 = pDepthMarketData->AskVolume5; 197 | ///当日均价 TThostFtdcPriceType double 198 | double AveragePrice = pDepthMarketData->AveragePrice; 199 | ///业务日期 TThostFtdcDateType char[9] 200 | char ActionDay[27]; 201 | gbk2utf8(pDepthMarketData->ActionDay,ActionDay,sizeof(ActionDay)); 202 | 203 | //printf("TradingDay=%s,",TradingDay); // 交易日 204 | //printf("InstrumentID=%s,",InstrumentID); // 合约代码 205 | //printf("LastPrice=%f\n",LastPrice); // 最新价 206 | 207 | } 208 | 209 | // 如果响应函数已经返回最后一条信息 210 | if(bIsLast) { 211 | // 通知主过程,响应函数将结束 212 | // sem_post(&sem); 213 | printf("一次全量行情读取完毕.\n"); 214 | } 215 | } 216 | 217 | }; 218 | 219 | 220 | // 定时器控制函数 221 | void timeout_handler(int signalno) { 222 | 223 | // 定时控制步骤 224 | 225 | //////////////////////////////////////////////////////////////////////////////////////////////// 226 | ///请求查询行情 227 | /////////////////////////////////////////////////////////////////////////////////////////////// 228 | // 定义调用API的数据结构 229 | CThostFtdcQryDepthMarketDataField requestData; 230 | // 确保没有初始化的数据不会被访问 231 | memset(&requestData,0,sizeof(requestData)); 232 | // 为调用结构题设置参数信息 233 | ///合约代码 TThostFtdcInstrumentIDType char[31] 234 | strcpy(requestData.InstrumentID,""); 235 | // 调用API 236 | int result = pTraderApi->ReqQryDepthMarketData(&requestData,requestID++); 237 | ///////////////////////////////////////////////////////////////////////////////////////////////// 238 | 239 | } 240 | 241 | // 初始化定时器 242 | void init_timer(void) 243 | { 244 | // init sigaction 245 | struct sigaction act; 246 | act.sa_handler = timeout_handler; 247 | act.sa_flags = 0; 248 | sigemptyset(&act.sa_mask); 249 | sigaction(SIGPROF, &act, NULL); 250 | // set timer 251 | struct itimerval val; 252 | val.it_value.tv_sec = 1; 253 | val.it_value.tv_usec = 0; 254 | val.it_interval = val.it_value; 255 | setitimer(ITIMER_PROF, &val, NULL); 256 | } 257 | 258 | 259 | int main() { 260 | 261 | // 初始化线程同步变量 262 | sem_init(&sem,0,0); 263 | 264 | // 从环境变量中读取登录信息 265 | char * CTP_FrontAddress = getenv("CTP_FrontAddress"); 266 | if ( CTP_FrontAddress == NULL ) { 267 | printf("环境变量CTP_FrontAddress没有设置\n"); 268 | return(0); 269 | } 270 | 271 | char * CTP_BrokerId = getenv("CTP_BrokerId"); 272 | if ( CTP_BrokerId == NULL ) { 273 | printf("环境变量CTP_BrokerId没有设置\n"); 274 | return(0); 275 | } 276 | strcpy(userLoginField.BrokerID,CTP_BrokerId); 277 | 278 | char * CTP_UserId = getenv("CTP_UserId"); 279 | if ( CTP_UserId == NULL ) { 280 | printf("环境变量CTP_UserId没有设置\n"); 281 | return(0); 282 | } 283 | strcpy(userLoginField.UserID,CTP_UserId); 284 | 285 | char * CTP_Password = getenv("CTP_Password"); 286 | if ( CTP_Password == NULL ) { 287 | printf("环境变量CTP_Password没有设置\n"); 288 | return(0); 289 | } 290 | strcpy(userLoginField.Password,CTP_Password); 291 | 292 | // 创建TraderAPI和回调响应控制器的实例 293 | pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); 294 | CTraderHandler traderHandler = CTraderHandler(); 295 | CTraderHandler * pTraderHandler = &traderHandler; 296 | pTraderApi->RegisterSpi(pTraderHandler); 297 | 298 | // 订阅相关信息推送 299 | //// THOST_TERT_RESTART:从本交易日开始重传 300 | //// THOST_TERT_RESUME:从上次收到的续传 301 | //// THOST_TERT_QUICK:只传送登录后私有流的内容 302 | pTraderApi->SubscribePrivateTopic(THOST_TERT_RESUME); 303 | // 订阅公共流 304 | //// THOST_TERT_RESTART:从本交易日开始重传 305 | //// THOST_TERT_RESUME:从上次收到的续传 306 | //// THOST_TERT_QUICK:只传送登录后公共流的内容 307 | pTraderApi->SubscribePublicTopic(THOST_TERT_RESUME); 308 | 309 | // 设置服务器地址 310 | pTraderApi->RegisterFront(CTP_FrontAddress); 311 | // 链接交易系统 312 | pTraderApi->Init(); 313 | // 等待服务器发出登录消息 314 | sem_wait(&sem); 315 | // 发出登陆请求 316 | pTraderApi->ReqUserLogin(&userLoginField, requestID++); 317 | // 等待登录成功消息 318 | sem_wait(&sem); 319 | 320 | //////////////////////////////////////////////////////////////////////////////////////////////// 321 | /// 启动定时器,通过定时器发出行情查询请求 322 | /////////////////////////////////////////////////////////////////////////////////////////////// 323 | init_timer(); 324 | while(1); 325 | ///////////////////////////////////////////////////////////////////////////////////////////////// 326 | 327 | 328 | // 拷贝用户登录信息到登出信息 329 | strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); 330 | strcpy(userLogoutField.UserID, userLoginField.UserID); 331 | pTraderApi->ReqUserLogout(&userLogoutField, requestID++); 332 | 333 | // 等待登出成功 334 | sem_wait(&sem); 335 | 336 | printf("主线程执行完毕!\n"); 337 | return(0); 338 | 339 | } 340 | --------------------------------------------------------------------------------