CTP开发(1)准备工作

下载API接口及说明文档:上期技术官网

申请模拟账号并接入测试:上期技术SIMNOW官网

API文件如下

  • ThostFtdcTraderApi.h:C++头文件,包含交易相关的指令,如报单。
  • ThostFtdcMdApi.h:C++头文件,包含获取行情相关的指令。
  • ThostFtdcUserApiStruct.h:包含了所有用到的数据结构。
  • ThostFtdcUserApiDataType.h:包含了所有用到的数据类型。
  • thosttraderapi.lib、thosttraderapi.dll:交易部分的动态链接库和静态链接库。
  • thostmduserapi.lib、thostmduserapi.dll:行情部分的动态链接库和静态链接库。
  • error.dtd、error.xml:包含所有可能的错误信息。

我下载的是 v6.7.0_traderapi_20230209,api文件在目录 20230209_traderapi64_se_windows下,修改目录名称为CTP-API,放到项目下。

配置 Visual Studio 2022

添加静态库所在目录
在配置属性->链接器->常规->附加库目录中添加库所在路径,我这里是D:\source\ctp_c\CTP_API

添加附加依赖项
右键项目->属性->配置属性->链接器->输入->附加依赖项->编辑,添加依赖的lib文件名,一行一个

  • thostmduserapi_se.lib
  • thosttraderapi_se.lib

最后
dll文件要放在exe文件的同目录下,调试阶段exe在Debug目录下。
等写完代码,点击本地Windows调试器,应该没有错误了。

配置 Visual Studio Code

windows要链接CTP的lib库,用g++编译器无法编译通过,只能用cl编译器。配置方法见 VSCode编译C++连接lib文件

为了方便调试,我用code runner插件来运行,配置如下

 "code-runner.executorMap": {
        "cpp": "cl /std:c++20 $fileName /Fobin/$fileNameWithoutExt.obj /Febin/$fileNameWithoutExt.exe /link /LIBPATH:d:/work_vscode/CTP/api thostmduserapi_se.lib thosttraderapi_se.lib >nul 2>&1 && start cmd /k $dir/bin/$fileNameWithoutExt.exe"
    },

Linux下的配置

Linux下的库文件是2个so文件,比如我把项目放在/home/admin/test目录下,所有的头文件和库文件放在test/CTP_API目录下

先用cd进入到test目录

cd /home/admin/test

在编译命令中指定库的路径即可,

g++ -o main.out main.cpp ./CTP_API/thostmduserapi_se.so

库文件也可以在代码中链接

#pragma comment (lib, "thostmduserapi.lib")
#pragma comment (lib, "thosttraderapi.lib")

#pragma comment(lib, "thostmduserapi.lib") 是一种编译器指令,用于告诉编译器在链接阶段将指定的库文件添加到链接器的输入文件列表中。

在这个特定的例子中,thostmduserapi.lib 是与 C++ 代码一起使用的某个库文件。通过添加这个指令,开发者可以避免手动在编译命令行或 IDE 设置中指定需要链接的库文件,而是直接在源代码中通过这个指令告诉编译器。

这样做的好处是使得项目的构建更加简单,不需要在每个使用该库的源文件中都手动添加链接库的命令。注意,这种方式是非标准的,具体效果和可移植性可能会因编译器和平台而异。

行情连接

#include "./CTP_API/ThostFtdcMdApi.h"
#include <Windows.h>
#include <stdio.h>
//#include <iostream>
class CMduserHandler : public CThostFtdcMdSpi
{
private:
	CThostFtdcMdApi* m_mdApi;
public:
	void connect()
	{
		char MdFrontAddr[] = "tcp://180.168.146.187:10211";
		m_mdApi = CThostFtdcMdApi::CreateFtdcMdApi("./flow_md/"); //必须先建立好文件夹,否则会出错
		m_mdApi->RegisterSpi(this);
		m_mdApi->RegisterFront(MdFrontAddr);
		m_mdApi->Init();
	}
	void OnFrontConnected()
	{
		puts("=====建立交易连接成功=====");
		// 发送登录请求
		CThostFtdcReqUserLoginField t = { 0 };
		while (m_mdApi->ReqUserLogin(&t, 1) != 0) Sleep(1000);
	}
	void OnRspUserLogin(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pRspInfo->ErrorID) {
			printf("返回错误--->>> ErrorID=%d,ErrorMsg=%s", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
			return;
		}
		puts("=====账户登录成功=====");
		//订阅行情
		const char* strings[] = { "rb2401", "sc2311" };
		char** ppInstrument = const_cast<char**>(strings);
		while (m_mdApi->SubscribeMarketData(ppInstrument, sizeof(strings) / sizeof(strings[0])) != 0) Sleep(1000);
	}
	void OnRspSubMarketData(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast)
	{
		if (pRspInfo->ErrorID) {
			printf("返回错误--->>> ErrorID=%d,ErrorMsg=%s", pRspInfo->ErrorID, pRspInfo->ErrorMsg);
			return;
		}
		printf("=====订阅 %s 成功\n", pSpecificInstrument->InstrumentID);
	}
	void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField* pDepthMarketData)

	{
		printf("InstrumentID:%s => LastPrice:%g\n", pDepthMarketData->InstrumentID, pDepthMarketData->LastPrice);

	}

};
int main()
{
	CMduserHandler* mduser = new CMduserHandler;
	mduser->connect();
	Sleep(INFINITE);
}


参考文献:

相关的学习资料:

踏莎行大神写的C++版本的demo https://github.com/junyuzr/CTPtest

景色大神编译了原生Python版CTP API https://github.com/nicai0609/Python-CTPAPI

景色大神的教程在csdn和知乎上都有更新,微信公众号上也有。

CSDN:https://blog.csdn.net/pjjing/article/details/90381795
知乎:https://zhuanlan.zhihu.com/p/80709410
微信:https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg5MjEwNDEwMQ==&action=getalbum&album_id=1545992091560968194

实战应用可以看csdn的付费教程,有参考价值

python从0到1:期货量化交易系统 https://edu.csdn.net/course/detail/37142

posted @   C羽言  阅读(544)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示