Using the SimNow CTP API for commodity futures trading involves understanding several key concepts, technical components, and operational details. Below, I’ll provide a comprehensive overview of the knowledge points related to the SimNow platform and its CTP (Comprehensive Trading Platform) API, based on its purpose, functionality, and practical use for simulated futures trading.


1. Overview of SimNow and CTP API

  • What is SimNow?
    SimNow is a futures simulation trading platform developed by Shanghai Futures Information Technology Co., Ltd., a wholly-owned subsidiary of the Shanghai Futures Exchange (SHFE). It is designed to provide investors and developers with a realistic, risk-free environment to test trading strategies, learn market rules, and develop trading systems. It replicates the trading and settlement rules of major Chinese futures exchanges, including the SHFE, Dalian Commodity Exchange (DCE), Zhengzhou Commodity Exchange (CZCE), and China Financial Futures Exchange (CFFEX).

  • What is the CTP API?
    The CTP (Comprehensive Trading Platform) API is a robust interface provided by Shanghai Futures Information Technology Co., Ltd. for programmatic trading. It allows developers to connect their trading applications to the SimNow environment (or real futures exchanges) to access market data, submit orders, and manage trading activities. The API supports both market data (quotes) and trading functionalities, making it a critical tool for automated and algorithmic trading.

  • Purpose of SimNow CTP API

    • Education and Skill Development: Helps investors understand exchange rules, market dynamics, and risk management without financial risk.
    • Strategy Testing: Allows developers to test quantitative trading strategies (e.g., CTA strategies) in a simulated environment that mirrors real market conditions.
    • API Development: Provides a sandbox for developers to build and debug trading applications using the CTP interface.

2. Key Knowledge Points for Using SimNow CTP API

2.1. Account Setup and Access

  • Registration:
    To use the SimNow CTP API, you need to register an account on the SimNow website (http://www.simnow.com.cn/).
    • Use a valid Chinese mobile number (China Mobile or China Unicom recommended) or email for registration, as verification codes are sent to these.
    • Each phone number or email can only register one account.
    • After registration, you’ll receive an Investor ID (not the phone number) and a password. The Investor ID is used for API logins, and the password must be changed after the first login for API access.
    • New accounts may take up to the third trading day to access certain environments (e.g., the second CTP API testing environment).
  • Broker ID:
    For SimNow, the Broker ID is standardized as 9999 for all users. This is used in API configurations to identify the simulated brokerage environment.

  • Password Management:
    • Change the default password via the SimNow website (“Forget Password” option) or through the SimNow Fast Client software.
    • Ensure the password for API trading is distinct from real trading environments to avoid security risks.

2.2. Environment and Server Configurations

SimNow provides multiple server environments for different use cases. These environments differ in availability and functionality:

  • Trading and Market Data Servers:
    SimNow offers multiple server groups for trading and market data access. The connection details are found in the SimNow Fast Client download package (e.g., in the ctp_sim.xml file) or on the website. Common server addresses include:
    • First Group (Telecom):
      • Trade Front: 180.168.146.187:10201
      • Market Front: 180.168.146.187:10211
    • Second Group (Telecom):
      • Trade Front: 180.168.146.187:10202
      • Market Front: 180.168.146.187:10212
    • Third Group (Mobile):
      • Trade Front: 218.202.237.33:10203
      • Market Front: 218.202.237.33:10213
    • 7x24 Environment:
      • Trade Front: 180.168.146.187:10130
      • Market Front: 180.168.146.187:10131
    • The first two groups are available during trading hours (Monday to Friday, including night sessions), while the 7x24 environment is for non-trading hours with playback of recent market data.
  • Environment Differences:
    • First and Second Groups: Mirror real market conditions with live quotes and consistent trading rules.
    • 7x24 Environment: Provides market data playback for testing during non-trading hours but does not support settlement services.
    • Second Environment (API Testing): Exclusively for CTP API developers, offering only API testing without settlement or other services.
  • Trading Hours:
    SimNow follows the real trading hours of Chinese futures exchanges, including night sessions for certain contracts. The platform is unavailable on weekends and holidays.

2.3. CTP API Components

The CTP API consists of several key components, which are defined in header files and libraries available for download from the SimNow website (http://www.simnow.com.cn/static/apiDownload.action). These include:

  • Header Files:
    • ThostFtdcUserApiDataType.h: Defines data types used by the API (e.g., price, volume).
    • ThostFtdcUserApiStruct.h: Defines data structures for trading and market data.
    • FtdcTraderApi.h: Trading interface for order placement, cancellation, and account queries.
    • ThostFtdcMdApi.h: Market data interface for subscribing to quotes and receiving market updates.
  • Dynamic Link Libraries (DLLs):
    • thosttraderapi.dll: For trading operations.
    • thostmduserapi.dll: For market data operations.
    • Corresponding .lib files (thosttraderapi.lib, thostmduserapi.lib) are used for linking in C++ applications.
  • Communication Modes:
    The CTP API supports three communication modes:
    • Dialogue Mode: Client-initiated requests (e.g., login, order placement, queries) with responses from the server.
    • Private Mode: Server pushes order updates, trade confirmations, or errors to the client.
    • Broadcast Mode: Server pushes market quotes (e.g., price updates) to all subscribed clients.

2.4. Key API Functionalities

  • Market Data (MdApi):
    • Subscribe to real-time market quotes for specific contracts (e.g., futures on commodities like copper, soybeans, or crude oil).
    • Receive tick-level data (2 ticks per second, lower than some cryptocurrency APIs).
    • Note: SimNow does not provide historical market data or tick-by-tick transaction data; historical data must be sourced from third-party providers.
  • Trading (TraderApi):
    • Order Types: Supports limit orders, Fill-and-Kill (FAK), and Fill-or-Kill (FOK) orders. Market orders are not supported in SimNow.
    • Order Management: Place, modify, or cancel orders. Orders can be monitored via the API’s private communication mode.
    • Position and Account Queries: Retrieve account balances, positions, and order statuses.
  • Authentication:
    • Default APPID: simnow_client_test
    • Default Authentication Code: 0000000000000000 (16 zeros).
    • Terminal authentication can be disabled for programmatic access.

2.5. Trading Rules and Limitations

  • Contract Rules:
    SimNow contracts follow the trading rules of the SHFE, including margin requirements, price limits, and settlement procedures. Ensure your trading strategy complies with these rules.

  • Order Restrictions:
    • CTP imposes strict limits on request frequency (e.g., one request every 2 seconds is considered safe).
    • Excessive order submissions or cancellations may trigger restrictions.
  • No Tick-by-Tick Transaction Data:
    Unlike some cryptocurrency APIs, SimNow’s CTP API does not provide detailed transaction-level data. Transaction details must be inferred from position changes.

2.6. Development and Integration

  • Supported Languages:
    The CTP API is primarily designed for C++ but can be wrapped for other languages like Python or Rust using libraries like pybind11 or cc-rs. For example, Python developers can refer to tutorials on wrapping CTP APIs (e.g., https://zhuanlan.zhihu.com/p/20031660).

  • Development Steps:
    1. Download API Files: Obtain the necessary DLLs and header files from http://www.simnow.com.cn/static/apiDownload.action.
    2. Set Up Environment: Configure the trading and market data server addresses in your application.
    3. Implement Callbacks: Use callback functions to handle market data updates, order confirmations, and errors.
    4. Test Connectivity: Use tools like telnet to verify server connectivity before running your application.
    5. Debug and Log: Monitor logs for initialization and connection status (e.g., TradeAPI and MdAPI login success).
  • Libraries for Integration:
    • For Rust: Use libloading or libc for FFI bindings.
    • For Python: Use pybind11 or similar to interface with C++ DLLs.
    • Ensure memory safety when unloading libraries to avoid dangling pointers.

2.7. Common Pitfalls and Best Practices

  • Connectivity Issues:
    • SimNow servers may be unavailable on weekends or holidays. Use telnet to test connectivity if your program fails to respond.
    • Ensure the correct server group is selected based on trading hours.
  • Data Limitations:
    • SimNow does not support market price orders or tick-by-tick transaction data. For transaction-level testing, use a futures company’s simulation environment or real trading account.
    • Historical data requires third-party providers.
  • Password and Security:
    • Always use a unique password for SimNow to avoid confusion with real trading accounts.
    • Ensure secure handling of API credentials in your code.
  • Testing Strategies:
    • Use SimNow to validate strategy logic before deploying to a real environment.
    • Test during trading hours to ensure real-time data accuracy.
    • Simulate various market conditions to assess strategy robustness.

3. Practical Workflow for Commodity Futures Trading with SimNow CTP API

  1. Register and Set Up: Create a SimNow account, obtain Investor ID, and change the password.
  2. Download API: Get the CTP API files from the SimNow website.
  3. Choose a Programming Language: Develop your trading application in C++, Python, or another supported language.
  4. Configure Servers: Input the appropriate trading and market data server addresses.
  5. Implement Trading Logic: Write code to subscribe to market data, place orders, and handle callbacks for order updates.
  6. Test and Debug: Run your application in the SimNow environment, monitor logs, and verify connectivity.
  7. Iterate and Deploy: Refine your strategy based on test results and prepare for deployment in a real trading environment (after modifying server and login details).

4. Comparison with Other APIs

  • CTP vs. Cryptocurrency APIs:
    • CTP uses the FTD protocol (TCP-based), while cryptocurrency APIs typically use REST or WebSocket.
    • CTP has lower tick frequency (2 ticks/second) compared to cryptocurrency APIs (up to 10 ticks/second).
    • CTP lacks historical data and transaction-level quotes, unlike many cryptocurrency APIs.
  • CTP vs. Other Futures APIs:
    • Compared to APIs like CME Group’s or TradeStation’s, CTP is tailored for Chinese futures markets, with specific support for SHFE, DCE, CZCE, and CFFEX contracts.
    • CTP’s stability is high, with fewer network errors compared to some global APIs.

5. Additional Resources

  • SimNow Website: http://www.simnow.com.cn/ for account registration, API downloads, and FAQs.
  • API Documentation: Available at http://www.sfit.com.cn/5_2_DocumentDown.htm.
  • Community Support: Check forums like CSDN or GitHub for CTP API tutorials and sample code (e.g., Python or Rust implementations).
  • Client Software: Download the SimNow Fast Client (e.g., TradeNow or Quick 3) for manual trading and testing alongside API development.

6. Important Notes

  • Risk Warning: SimNow is a simulated environment, but real trading involves significant financial risks. Ensure strategies are thoroughly tested before live deployment.
  • Availability: SimNow is not accessible on weekends or holidays, and new accounts may face delays in activation. Plan your testing accordingly.
  • API Limitations: The second environment is for API testing only and does not support settlement or advanced features. Use the first environment for full simulation.

Comments