JNIEXPORT void JNICALL Java_nativeinterfaces_DefaultNativeInterface_sendTradeRequest (JNIEnv *, jobject, jobjectArray){ // create a CThostFtdcMdApi instance CThostFtdcMdApi *pUserApi = CThostFtdcMdApi::CreateFtdcMdApi(); CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); // create an event handler instance quotationeventhandler sh(pUserApi); TraderEventHandler th(pTraderApi); // register an event handler instance pTraderApi->RegisterSpi(&th); // register the CTP front address and port pTraderApi ->RegisterFront("tcp://218.1.96.8:41205"); pTraderApi->SubscribePrivateTopic(THOST_TERT_RESUME); pTraderApi->SubscribePublicTopic(THOST_TERT_RESUME); // start the connection between client and CTP server pTraderApi->Init(); // waiting for the quotation data WaitForSingleObject(g_hEvent, INFINITE); // release API instance pTraderApi -> Release(); //pUserApi->RegisterSpi(&sh); //pUserApi->RegisterFront("tcp://218.1.96.8:41213"); //pUserApi->Init(); //WaitForSingleObject(g_hEvent, INFINITE); //pUserApi->Release(); }
int main(int argc, char* argv[]) { // create a CThostFtdcTraderApi instance CThostFtdcTraderApi *pUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); // create an event handler instance CSimpleHandler pSpi(pUserApi); // register an event handler instance pUserApi->RegisterSpi(&pSpi); pUserApi->SubscribePrivateTopic(THOST_TERT_QUICK); pUserApi->SubscribePublicTopic(THOST_TERT_QUICK); pUserApi->RegisterFront("tcp://10.253.117.107:13153"); pUserApi->Init(); printf ("\npress return to release...\n"); getchar(); // release the API instance pUserApi->Release(); printf ("\npress return to quit...\n"); getchar(); return 0; }
//连接 TRADEAPI_API CThostFtdcTraderApi* Connect(char *frontAddr, char *pszFlowPath, CTraderSpi* pUserSpi) { // 初始化UserApi CThostFtdcTraderApi* pUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi(pszFlowPath); // 创建UserApi //pUserSpi = new CTraderSpi(); pUserApi->RegisterSpi((CThostFtdcTraderSpi*)pUserSpi); // 注册事件类 pUserApi->SubscribePublicTopic(THOST_TERT_QUICK/*THOST_TERT_RESTART*/); // 注册公有流 pUserApi->SubscribePrivateTopic(THOST_TERT_QUICK/*THOST_TERT_RESTART*/); // 注册私有流 pUserApi->RegisterFront(frontAddr); // connect pUserApi->Init(); //pUserApi->Join(); return pUserApi; }
void test_order(void) { //初始化UserApi CThostFtdcTraderApi* pUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); CtpTraderSpi* pUserSpi = new CtpTraderSpi(pUserApi); pUserApi->RegisterSpi((CThostFtdcTraderSpi*)pUserSpi); // 注册事件类 pUserApi->SubscribePublicTopic(THOST_TERT_RESTART); // 注册公有流 pUserApi->SubscribePrivateTopic(THOST_TERT_RESTART); // 注册私有流 pUserApi->RegisterFront(tradeFront); // 注册交易前置地址 pUserApi->Init(); ShowTraderCommand(pUserSpi,true); pUserApi->Join(); //pUserApi->Release(); }
void searchCodelist(){ cout <<"(线程用来查询对应市场的所有合约号)searchCodelist!!!!!!!: " << endl; CThostFtdcTraderApi *pUserApi = CThostFtdcTraderApi::CreateFtdcTraderApi("./outfile3/"); MyCTPTraderSpi sh(pUserApi); pUserApi->RegisterSpi(&sh); pUserApi->RegisterFront(front_ip_trader); pUserApi->Init(); sleep(2); sh.ReqUserLogin(broker_id,user_id,p_w); sleep(2); sh.qryCode(exchangeID); sleep(2); }
int main() { // 初始化线程同步变量 sem_init(&sem,0,0); // 从环境变量中读取登录信息 char * CTP_FrontAddress = getenv("CTP_FrontAddress"); if ( CTP_FrontAddress == NULL ) { printf("环境变量CTP_FrontAddress没有设置\n"); return(0); } char * CTP_BrokerId = getenv("CTP_BrokerId"); if ( CTP_BrokerId == NULL ) { printf("环境变量CTP_BrokerId没有设置\n"); return(0); } strcpy(userLoginField.BrokerID,CTP_BrokerId); char * CTP_UserId = getenv("CTP_UserId"); if ( CTP_UserId == NULL ) { printf("环境变量CTP_UserId没有设置\n"); return(0); } strcpy(userLoginField.UserID,CTP_UserId); char * CTP_Password = getenv("CTP_Password"); if ( CTP_Password == NULL ) { printf("环境变量CTP_Password没有设置\n"); return(0); } strcpy(userLoginField.Password,CTP_Password); // 创建TraderAPI和回调响应控制器的实例 CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); CTraderHandler traderHandler = CTraderHandler(); CTraderHandler * pTraderHandler = &traderHandler; pTraderApi->RegisterSpi(pTraderHandler); // 订阅相关信息推送 //// THOST_TERT_RESTART:从本交易日开始重传 //// THOST_TERT_RESUME:从上次收到的续传 //// THOST_TERT_QUICK:只传送登录后私有流的内容 pTraderApi->SubscribePrivateTopic(THOST_TERT_RESUME); // 订阅公共流 //// THOST_TERT_RESTART:从本交易日开始重传 //// THOST_TERT_RESUME:从上次收到的续传 //// THOST_TERT_QUICK:只传送登录后公共流的内容 pTraderApi->SubscribePublicTopic(THOST_TERT_RESUME); // 设置服务器地址 pTraderApi->RegisterFront(CTP_FrontAddress); // 链接交易系统 pTraderApi->Init(); // 等待服务器发出登录消息 sem_wait(&sem); // 发出登陆请求 pTraderApi->ReqUserLogin(&userLoginField, requestID++); // 等待登录成功消息 sem_wait(&sem); //////////////////////////////////////////////////////////////////////////////////////////////// ///报单录入请求 /////////////////////////////////////////////////////////////////////////////////////////////// // 定义调用API的数据结构 CThostFtdcInputOrderField requestData; // 确保没有初始化的数据不会被访问 memset(&requestData,0,sizeof(requestData)); // 为调用结构题设置参数信息 ///经纪公司代码 TThostFtdcBrokerIDType char[11] strcpy(requestData.BrokerID,CTP_BrokerId); ////////////////////////////////////////////////////////// ///投资者代码 TThostFtdcInvestorIDType char[13] strcpy(requestData.InvestorID,CTP_UserId); ////////////////////////////////////////////////////////// ///合约代码 TThostFtdcInstrumentIDType char[31] strcpy(requestData.InstrumentID,"IF1504"); ////////////////////////////////////////////////////////// ///报单引用 TThostFtdcOrderRefType char[13] strcpy(requestData.OrderRef,"000000000001"); ////////////////////////////////////////////////////////// ///用户代码 TThostFtdcUserIDType char[16] strcpy(requestData.UserID,CTP_UserId); ////////////////////////////////////////////////////////// ///报单价格条件 TThostFtdcOrderPriceTypeType char //// THOST_FTDC_OPT_AnyPrice '1' 任意价 //// THOST_FTDC_OPT_LimitPrice '2' 限价 //// THOST_FTDC_OPT_BestPrice '3' 最优价 //// THOST_FTDC_OPT_LastPrice '4' 最新价 //// THOST_FTDC_OPT_LastPricePlusOneTicks '5' 最新价浮动上浮1个ticks //// THOST_FTDC_OPT_LastPricePlusTwoTicks '6' 最新价浮动上浮2个ticks //// THOST_FTDC_OPT_LastPricePlusThreeTicks '7' 最新价浮动上浮3个ticks //// THOST_FTDC_OPT_AskPrice1 '8' 卖一价 //// THOST_FTDC_OPT_AskPrice1PlusOneTicks '9' 卖一价浮动上浮1个ticks //// THOST_FTDC_OPT_AskPrice1PlusTwoTicks 'A' 卖一价浮动上浮2个ticks //// THOST_FTDC_OPT_AskPrice1PlusThreeTicks 'B' 卖一价浮动上浮3个ticks //// THOST_FTDC_OPT_BidPrice1 'C' 买一价 //// THOST_FTDC_OPT_BidPrice1PlusOneTicks 'D' 买一价浮动上浮1个ticks //// THOST_FTDC_OPT_BidPrice1PlusTwoTicks 'E' 买一价浮动上浮2个ticks //// THOST_FTDC_OPT_BidPrice1PlusThreeTicks 'F' 买一价浮动上浮3个ticks requestData.OrderPriceType = '1'; ////////////////////////////////////////////////////////// ///买卖方向 TThostFtdcDirectionType char //// THOST_FTDC_D_Buy '0' 买 //// THOST_FTDC_D_Sell '1' 卖 requestData.Direction = '0'; ////////////////////////////////////////////////////////// ///组合开平标志 TThostFtdcCombOffsetFlagType char[5] //// THOST_FTDC_OF_Open '0' 开仓 //// THOST_FTDC_OF_Close '1' 平仓 //// THOST_FTDC_OF_ForceClose '2' 强平 //// THOST_FTDC_OF_CloseToday '3' 平今 //// THOST_FTDC_OF_CloseYesterday '4' 平昨 //// THOST_FTDC_OF_ForceOff '5' 强减 //// THOST_FTDC_OF_LocalForceClose '6' 本地强平 strcpy(requestData.CombOffsetFlag,"0"); ////////////////////////////////////////////////////////// ///组合投机套保标志 TThostFtdcCombHedgeFlagType char[5] //// THOST_FTDC_HF_Speculation '1' 投机 //// THOST_FTDC_HF_Arbitrage '2' 套利 //// THOST_FTDC_HF_Hedge '3' 套保 strcpy(requestData.CombHedgeFlag,"1"); ////////////////////////////////////////////////////////// ///价格 TThostFtdcPriceType double requestData.LimitPrice = 0; ////////////////////////////////////////////////////////// ///数量 TThostFtdcVolumeType int requestData.VolumeTotalOriginal = 1; ////////////////////////////////////////////////////////// ///有效期类型 TThostFtdcTimeConditionType char //// THOST_FTDC_TC_IOC '1' 立即完成,否则撤销 //// THOST_FTDC_TC_GFS '2' 本节有效 //// THOST_FTDC_TC_GFD '3' 当日有效 //// THOST_FTDC_TC_GTD '4' 指定日期前有效 //// THOST_FTDC_TC_GTC '5' 撤销前有效 //// THOST_FTDC_TC_GFA '6' 集合竞价有效 requestData.TimeCondition = '1'; ////////////////////////////////////////////////////////// ///GTD日期 TThostFtdcDateType char[9] strcpy(requestData.GTDDate,""); ////////////////////////////////////////////////////////// ///成交量类型 TThostFtdcVolumeConditionType char //// THOST_FTDC_VC_AV '1' 任何数量 //// THOST_FTDC_VC_MV '2' 最小数量 //// THOST_FTDC_VC_CV '3' 全部数量 requestData.VolumeCondition = '1'; ////////////////////////////////////////////////////////// ///最小成交量 TThostFtdcVolumeType int requestData.MinVolume = 1; ////////////////////////////////////////////////////////// ///触发条件 TThostFtdcContingentConditionType char //// THOST_FTDC_CC_Immediately '1' 立即 //// THOST_FTDC_CC_Touch '2' 止损 //// THOST_FTDC_CC_TouchProfit '3' 止赢 //// THOST_FTDC_CC_ParkedOrder '4' 预埋单 //// THOST_FTDC_CC_LastPriceGreaterThanStopPrice '5' 最新价大于条件价 //// THOST_FTDC_CC_LastPriceGreaterEqualStopPrice '6' 最新价大于等于条件价 //// THOST_FTDC_CC_LastPriceLesserThanStopPrice '7' 最新价小于条件价 //// THOST_FTDC_CC_LastPriceLesserEqualStopPrice '8' 最新价小于等于条件价 //// THOST_FTDC_CC_AskPriceGreaterThanStopPrice '9' 卖一价大于条件价 //// THOST_FTDC_CC_AskPriceGreaterEqualStopPrice 'A' 卖一价大于等于条件价 //// THOST_FTDC_CC_AskPriceLesserThanStopPrice 'B' 卖一价小于条件价 //// THOST_FTDC_CC_AskPriceLesserEqualStopPrice 'C' 卖一价小于等于条件价 //// THOST_FTDC_CC_BidPriceGreaterThanStopPrice 'D' 买一价大于条件价 //// THOST_FTDC_CC_BidPriceGreaterEqualStopPrice 'E' 买一价大于等于条件价 //// THOST_FTDC_CC_BidPriceLesserThanStopPrice 'F' 买一价小于条件价 //// THOST_FTDC_CC_BidPriceLesserEqualStopPrice 'H' 买一价小于等于条件价 requestData.ContingentCondition = '1'; ////////////////////////////////////////////////////////// ///止损价 TThostFtdcPriceType double requestData.StopPrice = 0; ////////////////////////////////////////////////////////// ///强平原因 TThostFtdcForceCloseReasonType char //// THOST_FTDC_FCC_NotForceClose '0' 非强平 //// THOST_FTDC_FCC_LackDeposit '1' 资金不足 //// THOST_FTDC_FCC_ClientOverPositionLimit '2' 客户超仓 //// THOST_FTDC_FCC_MemberOverPositionLimit '3' 会员超仓 //// THOST_FTDC_FCC_NotMultiple '4' 持仓非整数倍 //// THOST_FTDC_FCC_Violation '5' 违规 //// THOST_FTDC_FCC_Other '6' 其它 //// THOST_FTDC_FCC_PersonDeliv '7' 自然人临近交割 requestData.ForceCloseReason = '0'; ////////////////////////////////////////////////////////// ///自动挂起标志 TThostFtdcBoolType int requestData.IsAutoSuspend = 0; ////////////////////////////////////////////////////////// ///业务单元 TThostFtdcBusinessUnitType char[21] strcpy(requestData.BusinessUnit,""); ////////////////////////////////////////////////////////// ///请求编号 TThostFtdcRequestIDType int requestData.RequestID = ++requestID; ////////////////////////////////////////////////////////// ///用户强评标志 TThostFtdcBoolType int requestData.UserForceClose = 0; ////////////////////////////////////////////////////////// ///互换单标志 TThostFtdcBoolType int requestData.IsSwapOrder = 0; ////////////////////////////////////////////////////////// // 调用API,并等待响应函数返回 int result = pTraderApi->ReqOrderInsert(&requestData,requestID); sem_wait(&sem); ///////////////////////////////////////////////////////////////////////////////////////////////// // 拷贝用户登录信息到登出信息 strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); strcpy(userLogoutField.UserID, userLoginField.UserID); pTraderApi->ReqUserLogout(&userLogoutField, requestID++); // 等待登出成功 sem_wait(&sem); printf("主线程执行完毕!\n"); return(0); }
int main() { // 初始化线程同步变量 sem_init(&sem,0,0); // 从环境变量中读取登录信息 char * CTP_FrontAddress = getenv("CTP_FrontAddress"); if ( CTP_FrontAddress == NULL ) { printf("环境变量CTP_FrontAddress没有设置\n"); return(0); } char * CTP_BrokerId = getenv("CTP_BrokerId"); if ( CTP_BrokerId == NULL ) { printf("环境变量CTP_BrokerId没有设置\n"); return(0); } strcpy(userLoginField.BrokerID,CTP_BrokerId); char * CTP_UserId = getenv("CTP_UserId"); if ( CTP_UserId == NULL ) { printf("环境变量CTP_UserId没有设置\n"); return(0); } strcpy(userLoginField.UserID,CTP_UserId); char * CTP_Password = getenv("CTP_Password"); if ( CTP_Password == NULL ) { printf("环境变量CTP_Password没有设置\n"); return(0); } strcpy(userLoginField.Password,CTP_Password); // 创建TraderAPI和回调响应控制器的实例 CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); CTraderHandler traderHandler = CTraderHandler(); CTraderHandler * pTraderHandler = &traderHandler; pTraderApi->RegisterSpi(pTraderHandler); // 设置服务器地址 pTraderApi->RegisterFront(CTP_FrontAddress); // 链接交易系统 pTraderApi->Init(); // 等待服务器发出登录消息 sem_wait(&sem); // 发出登陆请求 pTraderApi->ReqUserLogin(&userLoginField, requestID++); // 等待登录成功消息 sem_wait(&sem); //////////////////////////////////////////////////////////////////////////////////////////////// ///查询最大报单数量请求 /////////////////////////////////////////////////////////////////////////////////////////////// // 定义调用API的数据结构 CThostFtdcQueryMaxOrderVolumeField requestData; // 确保没有初始化的数据不会被访问 memset(&requestData,0,sizeof(requestData)); // 为调用结构题设置参数信息 ///经纪公司代码 TThostFtdcBrokerIDType char[11] strcpy(requestData.BrokerID,"66666"); ///投资者代码 TThostFtdcInvestorIDType char[13] strcpy(requestData.InvestorID,"60003010"); ///合约代码 TThostFtdcInstrumentIDType char[31] strcpy(requestData.InstrumentID,"SR609C4900"); ///买卖方向 TThostFtdcDirectionType char //// THOST_FTDC_D_Buy '0' 买 //// THOST_FTDC_D_Sell '1' 卖 requestData.Direction = THOST_FTDC_D_Sell; ///开平标志 TThostFtdcOffsetFlagType char //// THOST_FTDC_OF_Open '0' 开仓 //// THOST_FTDC_OF_Close '1' 平仓 //// THOST_FTDC_OF_ForceClose '2' 强平 //// THOST_FTDC_OF_CloseToday '3' 平今 //// THOST_FTDC_OF_CloseYesterday '4' 平昨 //// THOST_FTDC_OF_ForceOff '5' 强减 //// THOST_FTDC_OF_LocalForceClose '6' 本地强平 requestData.OffsetFlag = THOST_FTDC_OF_Open; ///投机套保标志 TThostFtdcHedgeFlagType char //// THOST_FTDC_HF_Speculation '1' 投机 //// THOST_FTDC_HF_Arbitrage '2' 套利 //// THOST_FTDC_HF_Hedge '3' 套保 requestData.HedgeFlag = THOST_FTDC_HF_Arbitrage; ///最大允许报单数量 TThostFtdcVolumeType int requestData.MaxVolume = 0; // 调用API,并等待响应函数返回 int result = pTraderApi->ReqQueryMaxOrderVolume(&requestData,requestID++); sem_wait(&sem); ///////////////////////////////////////////////////////////////////////////////////////////////// // 拷贝用户登录信息到登出信息 strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); strcpy(userLogoutField.UserID, userLoginField.UserID); pTraderApi->ReqUserLogout(&userLogoutField, requestID++); // 等待登出成功 sem_wait(&sem); printf("主线程执行完毕!\n"); return(0); }
int main() { // 初始化线程同步变量 sem_init(&sem,0,0); // 从环境变量中读取登录信息 char * CTP_FrontAddress = getenv("CTP_FrontAddress"); if ( CTP_FrontAddress == NULL ) { printf("环境变量CTP_FrontAddress没有设置\n"); return(0); } char * CTP_BrokerId = getenv("CTP_BrokerId"); if ( CTP_BrokerId == NULL ) { printf("环境变量CTP_BrokerId没有设置\n"); return(0); } strcpy(userLoginField.BrokerID,CTP_BrokerId); char * CTP_UserId = getenv("CTP_UserId"); if ( CTP_UserId == NULL ) { printf("环境变量CTP_UserId没有设置\n"); return(0); } strcpy(userLoginField.UserID,CTP_UserId); char * CTP_Password = getenv("CTP_Password"); if ( CTP_Password == NULL ) { printf("环境变量CTP_Password没有设置\n"); return(0); } strcpy(userLoginField.Password,CTP_Password); // 创建TraderAPI和回调响应控制器的实例 CThostFtdcTraderApi *pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi(); CTraderHandler traderHandler = CTraderHandler(); CTraderHandler * pTraderHandler = &traderHandler; pTraderApi->RegisterSpi(pTraderHandler); // 设置服务器地址 pTraderApi->RegisterFront(CTP_FrontAddress); // 链接交易系统 pTraderApi->Init(); // 等待服务器发出登录消息 sem_wait(&sem); // 发出登陆请求 pTraderApi->ReqUserLogin(&userLoginField, requestID++); // 等待登录成功消息 sem_wait(&sem); //////////////////////////////////////////////////////////////////////////////////////////////// ///请求查询合约 /////////////////////////////////////////////////////////////////////////////////////////////// // 定义调用API的数据结构 CThostFtdcQryInstrumentField requestData; // 确保没有初始化的数据不会被访问 memset(&requestData,0,sizeof(requestData)); // 为调用结构题设置参数信息 ///合约代码 TThostFtdcInstrumentIDType char[31] strcpy(requestData.InstrumentID,"IF1504"); ///交易所代码 TThostFtdcExchangeIDType char[9] strcpy(requestData.ExchangeID,""); ///合约在交易所的代码 TThostFtdcExchangeInstIDType char[31] strcpy(requestData.ExchangeInstID,""); ///产品代码 TThostFtdcInstrumentIDType char[31] strcpy(requestData.ProductID,""); // 调用API,并等待响应函数返回 int result = pTraderApi->ReqQryInstrument(&requestData,requestID++); sem_wait(&sem); ///////////////////////////////////////////////////////////////////////////////////////////////// // 拷贝用户登录信息到登出信息 strcpy(userLogoutField.BrokerID,userLoginField.BrokerID); strcpy(userLogoutField.UserID, userLoginField.UserID); pTraderApi->ReqUserLogout(&userLogoutField, requestID++); // 等待登出成功 sem_wait(&sem); printf("主线程执行完毕!\n"); return(0); }