Direct CTP Data: Integrating Futures Market Data and Trading in Agents

Preface

When developing quantitative strategies or live trading assistants for domestic futures, a common approach is to maintain separate Python scripts or vnpy projects for market data, order logic, and conversational Agents. The CTP (Comprehensive Transaction Platform) is an interface commonly provided by futures companies. However, the front-end addresses and authentication fields for SimNow simulation and live penetrating environments are numerous, often leading to time wasted on configuration and integration debugging.

Below is an introduction to the community Skill “CTP Direct Connect Data” (Coordinates: @user_a459bc71/ctp-connector, current version 1.0.7) on SkillHub. It integrates capabilities such as market data subscription, trading queries and order placement, data persistence and streaming, and backtest data preparation into a standard SKILL.md and accompanying scripts. This facilitates documentation-driven development and troubleshooting within Agents like Cursor that support Skills.

What is this

“CTP Direct Connect Data” is a community / professional Skill published by SkillHub user user_a459bc71. Official page: SkillHub - CTP Direct Connect Data.

The Skill describes its positioning as: CTP direct connection data capabilities, supporting market data subscription, trading orders, position and capital queries, and compatible with both live penetrating environments and SimNow simulation environments. The SkillHub metadata marks requires_api_key=true; actual usage requires credentials such as accounts and authentication codes provided by SimNow or futures companies (environment variables are recommended; do not write them into the repository).

As of the SkillHub page and scraped data: Rating 100000, approximately 897 downloads (installation counts are subject to the store).

Core Features

The following capabilities are derived from the SKILL.md and module descriptions within the Skill package and can be used selectively.

  1. Market Data: Tick, depth quotes, and K-line (candlestick) subscriptions; CTPMarketData provides connection, login, subscription/unsubscription, blocking execution via run(), and callbacks such as on_tick.
  2. Trading: CTPTrader supports order placement, cancellation, and queries for accounts, positions, orders, and trades.
  3. Environment: config/ctp_config.yaml switches between SimNow and live penetrating configuration blocks via environment: simnow | production.
  4. Data Output: Local CSV/JSON/Parquet (configurable), optional SQLite/MySQL/MongoDB via the storage module; real-time streaming via optional ZeroMQ, WebSocket, or Redis via the streaming module.
  5. Backtest Preparation: The backtest module provides market data recording and CSV replay examples.
  6. Engineering: Documentation for v1.0.6+ mentions ConfigValidator and HealthChecker; v1.0.7 emphasizes log masking, pinned dependency versions, and tests/ unit tests.

Installation and Activation

1. Install the Skill via SkillHub

First, ensure the SkillHub CLI is installed on your machine (see SkillHub Installation Instructions). When installing the Skill, you must use --dir to point to the current Agent’s skills directory; otherwise, it defaults to ./skills/ which may not be recognized.

In Cursor, the common path is ~/.cursor/skills/:

skillhub install @user_a459bc71/ctp-connector --dir ~/.cursor/skills/

The official page also supports providing the following prompt to the Agent to execute the installation according to the documentation:

Please install `@user_a459bc71/ctp-connector` based on https://skillhub.cn/install/skillhub.md.

Restart or refresh the Agent after installation to load the new Skill.

2. Install Python Dependencies

The Skill directory contains hierarchical dependency files. Minimal core installation:

cd ~/.cursor/skills/@user_a459bc71/ctp-connector
pip install -r requirements-core.txt

requirements-core.txt includes pinned versions: openctp==0.1.0, pandas==2.2.3, numpy==2.2.6, pyyaml==6.0.2, python-dotenv==1.1.0. For full functionality, run pip install -r requirements.txt; optional capabilities like storage and streaming can be added manually (e.g., sqlalchemy, pymongo, pyzmq) as per the documentation.

SKILL.md also lists examples: pip install openctp and pandas sqlalchemy pymongo redis pyzmq websocket-client; when used in conjunction with requirements-*.txt, prioritizing the locked files inside the Skill package is better for reproducibility.

3. Credentials and Environment Variables

The Skill indicates that API Key credentials are required. It is recommended to copy .env.example to .env within the package, fill in variables related to SimNow or live trading (such as SIMNOW_USER_ID, SIMNOW_PASSWORD, SIMNOW_AUTH_CODE, etc.), and ensure .env is excluded from version control. Configuration files can reference these using ${ENV_VAR}, and environment variables take precedence over configuration items with the same name.

Typical Usage

Configuration File

Create config/ctp_config.yaml within your project. Below is an excerpt of the SimNow and output sections (refer to the Skill documentation for full fields):

environment: simnow

simnow:
  front_address: "tcp://180.168.146.187:10131"
  trade_front: "tcp://180.168.146.187:10130"
  broker_id: "9999"
  user_id: "${SIMNOW_USER_ID}"
  password: "${SIMNOW_PASSWORD}"
  auth_code: "${SIMNOW_AUTH_CODE}"
  app_id: "simnow_client_test"

output:
  file:
    enabled: true
    path: "./data"
    format: "csv"

subscriptions:
  - "cu2506"
  - "al2506"

Subscribe to Market Data

from ctp_connector import CTPMarketData

md = CTPMarketData(config_path="config/ctp_config.yaml")
md.connect()
md.login()
md.subscribe(["cu2506", "al2506", "ni2506"])
md.run()

Queries and Order Placement

from ctp_connector import CTPTrader

trader = CTPTrader(config_path="config/ctp_config.yaml")
trader.connect()
trader.login()

account = trader.query_account()
print(f"Available Funds: {account.available}")

order = trader.insert_order(
    instrument="cu2506",
    direction="buy",
    offset="open",
    price=70000,
    volume=1,
    order_type="limit",
)
trader.cancel_order(order.order_id)

Before running, ensure the Python path points to the ctp_connector package layout provided by the Skill (subject to your installed directory and scripts/__init__.py exports); within an Agent session, you can directly reference the examples in the Skill to allow the model to generate the complete project structure.

Applicable Scenarios and Notes

Who is it for

  • Futures developers who need to write or explain CTP market data/trading code within Agent conversations.
  • Those who want to verify subscriptions and order placement in SimNow first, then switch to production configuration to connect to futures company penetrating environments.
  • Those looking to persist Ticks to disk/databases or push them to downstream strategy services via ZeroMQ/WebSocket.

Limitations and Notes

  1. Compliance and Risk: The Skill disclaimer states it is for learning and research only and does not constitute investment advice; you must assess risks independently and complete futures company penetrating authentication before live trading.
  2. Network and Time: SimNow front-end addresses and trading hours are subject to the SimNow Official Website and the front-end table in the Skill documentation; there may be no market data during non-trading hours.
  3. Instrument Codes: Subscriptions are case-sensitive (e.g., CF509); if there is no data, check login status and trading hours according to the documentation.
  4. Dependencies and Platform: The core dependency openctp is related to the CTP native interface; the deployment environment must meet its runtime requirements; if optional modules fail, fall back to the minimal set in requirements-core.txt for troubleshooting.
  5. Sensitive Information: Do not hardcode authentication codes in SKILL.md or configuration files; v1.0.7 has emphasized removing hardcoded examples, but using only environment variables is still recommended.

Conclusion

“CTP Direct Connect Data” consolidates CTP market data, trading, storage/streaming, and backtest data preparation into a single installable Agent Skill, serving as a documented starting point for SimNow integration and live penetrating environment connectivity. Installation and version information are subject to SkillHub:

  • Store Page: https://www.skillhub.cn/skills/user_a459bc71/ctp-connector
  • API Index: https://api.skillhub.cn/user_a459bc71/ctp-connector
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye