Exemple #1
0
JNIEXPORT void JNICALL Java_nativeinterfaces_DefaultNativeInterface_sendQuoteRequest
  (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;
}
Exemple #3
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;
}
Exemple #4
0
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();
}
Exemple #5
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);

    // 订阅相关信息推送
    //// 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);

}
  CTPWRAPPER_API int Process(void *instance,CTP_REQUEST_TYPE type,int p1,char *p2)
  {

    printf("Process %s","Process");

    switch(type)
    {

    case TraderApiCreate:
      {
        //C#回调函数
        CTPResponseCallback callback = (CTPResponseCallback)instance;

        //创建交易接口
        CThostFtdcTraderApi* pTrader = CThostFtdcTraderApi::CreateFtdcTraderApi(p2);
        //创建交易回调
        CTPTraderSpi *spi = new CTPTraderSpi(callback);

        //注册回调
        pTrader->RegisterSpi((CThostFtdcTraderSpi*)spi);

        //订阅流
        pTrader->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        pTrader->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);

        return (int)pTrader;
      }


      ///删除接口对象本身
      ///@remark 不再使用本接口对象时,调用该函数删除接口对象
    case TraderApiRelease:
      {

        CThostFtdcTraderApi* pTrader = (CThostFtdcTraderApi *)instance;

        pTrader->RegisterSpi(NULL);
        pTrader->Release();

        //delete pTrader;
        instance = NULL;

        return 0;
      }

      ///初始化
      ///@remark 初始化运行环境,只有调用后,接口才开始工作
    case  TraderApiInit:
      {
        ((CThostFtdcTraderApi *)instance)->Init();
        return 0;
      }

      ///等待接口线程结束运行
      ///@return 线程退出代码
    case TraderApiJoin:
      {
        return ((CThostFtdcTraderApi *)instance)->Join();
      }

      ///获取当前交易日
      ///@retrun 获取到的交易日
      ///@remark 只有登录成功后,才能得到正确的交易日
    case TraderApiGetTradingDay:
      {
        const char* date = ((CThostFtdcTraderApi *)instance)->GetTradingDay();
        memcpy(p2,date,sizeof(date));
        return 0;
      }

      ///注册前置机网络地址
      ///@param pszFrontAddress:前置机网络地址。
      ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 
      ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。
    case TraderApiRegisterFront:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterFront(p2);
        return 0;
      }

      ///注册前置机网络地址
      ///@param pszFrontAddress:前置机网络地址。
      ///@remark 网络地址的格式为:“protocol://ipaddress:port”,如:”tcp://127.0.0.1:17001”。 
      ///@remark “tcp”代表传输协议,“127.0.0.1”代表服务器地址。”17001”代表服务器端口号。
    case TraderApiRegisterNameServer:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterNameServer(p2);
        return 0;
      }

      ///注册回调接口
      ///@param pSpi 派生自回调接口类的实例
    case TraderApiRegisterSpi:
      {
        ((CThostFtdcTraderApi *)instance)->RegisterSpi((CThostFtdcTraderSpi*)p1);
        return 0;
      }

      ///订阅私有流。
      ///@param nResumeType 私有流重传方式  
      ///        THOST_TERT_RESTART:从本交易日开始重传
      ///        THOST_TERT_RESUME:从上次收到的续传
      ///        THOST_TERT_QUICK:只传送登录后私有流的内容
      ///@remark 该方法要在Init方法前调用。若不调用则不会收到私有流的数据。
    case TraderApiSubscribePrivateTopic:
      {
        ((CThostFtdcTraderApi *)instance)->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);
        return 0;
      }

      ///订阅公共流。
      ///@param nResumeType 公共流重传方式  
      ///        THOST_TERT_RESTART:从本交易日开始重传
      ///        THOST_TERT_RESUME:从上次收到的续传
      ///        THOST_TERT_QUICK:只传送登录后公共流的内容
      ///@remark 该方法要在Init方法前调用。若不调用则不会收到公共流的数据。
    case TraderApiSubscribePublicTopic:
      {
        ((CThostFtdcTraderApi *)instance)->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        return 0;
      }


    case MarketDataCreate:
      {
        //C#回调函数
        CTPResponseCallback callback = (CTPResponseCallback)instance;

        //创建行情接口
        CThostFtdcMdApi* pMdApi = CThostFtdcMdApi::CreateFtdcMdApi(p2);
        //创建交易回调
        CTPMarketDataSpi *spi = new CTPMarketDataSpi(callback);

        //注册回调
        pMdApi->RegisterSpi(spi);
        
        //订阅流
        //pMdApi->SubscribePublicTopic((THOST_TE_RESUME_TYPE)p1);
        //pMdApi->SubscribePrivateTopic((THOST_TE_RESUME_TYPE)p1);

        return (int)pMdApi;
      }

    case MarketDataRelease:
      {
        CThostFtdcMdApi* pMarketData = (CThostFtdcMdApi *)instance;

        pMarketData->RegisterSpi(NULL);
        pMarketData->Release();

        //delete instance;
        instance = NULL;

        return 0;
      }

    case MarketDataInit:
      {
        ((CThostFtdcMdApi *)instance)->Init();
        return 0;
      }

    case MarketDataRegisterFront:
      {
        ((CThostFtdcMdApi *)instance)->RegisterFront(p2);
        return 0;
      }

    case MarketDataRegisterNameServer:
      {
        ((CThostFtdcMdApi *)instance)->RegisterNameServer(p2);
        return 0;
      }
      
       ///订阅行情
    case MarketDataSubscribeMarketData:
      {
        char** p = (char**)calloc(p1, sizeof(char*));

        p = (char**)p2;

        p[0] = *p;

        for(int i=1;i<p1-1;i++)
        {
          p[i] = p[i-1] + strlen(p[i-1]) + 1;
        }

        return ((CThostFtdcMdApi*)instance)->SubscribeMarketData(p, p1);
      }

       ///退订行情
    case MarketDataUnSubscribeMarketData:
      {
        char** p = (char**)calloc(p1, sizeof(char*));

        

        //p = (char**)p2;
        OutputDebugStringA(p2);
        p[0] = p2;

        for(int i=1;i<p1-1;i++)
        {
          p[i] = p[i-1] + strlen(p[i-1]) + 1;
        }

        return ((CThostFtdcMdApi*)instance)->UnSubscribeMarketData(p, p1);
      }
    }
  }