CTP quantitative investment API workflow overview

MdApi workflow

MdApi is the market quotation interface of CTP, which mainly implements the functions of subscribing and receiving market quotations. The workflow of MdApi can be summarized as the following eight steps:

The first step is to use CreateFtdcMdApi to create a CThostFtdcMdApi instance, such as _api.
The second step is to create a CThostFtdcMdSpi instance, such as _spi, and use RegisterSpi to register _spi into _api.
The third step is to use RegisterFront to register the market prefix address into _api.
The fourth step is to initialize _api with Init(). The initialization process is the process of establishing a connection with the service.
Step 5 : When the connection is successfully established with the server, the callback function OnFrontConnect will receive a notification. At this time, use ReqUserLogin to log in to the account.
Step 6 : When the account login is successful, the callback function OnRspUserLogin will receive a notification. At this time, use SubscribeMarketData to subscribe to the market and SubscribeForQuoteRsp to subscribe to the inquiry. After this, MdApi can work normally.
Step 7 : When MdApi works normally, traders need to process or distribute data when the callback function OnRtnDepthMarketData/OnRtnForQuoteRsp receives the latest market notification.
Step 8 : MdApi is essentially a sub-process, so it needs to call the Join method to let the main process wait before it can run normally. If OnFrontDisconnected is triggered immediately after a successful connection and error code 8193 is encountered, the Join method may not be called.
Insert image description here
Under normal circumstances, the quantitative trading framework will encapsulate the operations of steps one to six and step eight, and traders only need to pass the relevant parameters. What traders really need to care about is step seven, which involves data processing and data distribution.

TraderApi workflow

TraderApi is the trading port of CTP, which mainly implements the functions of (buy, sell, close, close) orders and inquiries. The workflow of TraderApi can be summarized as the following ten steps:

The first step is to use CreateFtdcTraderApi to create a CThostFtdcTraderApi instance, such as _api.
The second step is to create a CThostFtdcTraderSpi instance, such as _spi, and use RegisterSpi to register _spi into _api.
The third step is to subscribe to the private stream using SubscribePrivateTopic and the public stream using SubscribePublicTopic.
The fourth step is to use RegisterFront to register the transaction front address into _api.
The fifth step is to initialize _api with Init(). The initialization process is the process of establishing a connection with the service.
Step 6 : When the connection is successfully established with the server, the callback function OnFrontConnect will receive a notification and use ReqUserAuthMethod to authenticate the client.
Step 7 : When the client authentication is successful, the callback function OnRspUserAuthMethod will receive a notification and use ReqUserLogin to log in to the account.
Step 8 : If the account is logged in successfully, the callback function OnRspUserLogin will be notified and the status will be marked at this time. After this, TraderApi can work normally.
Step 9 : When TraderApi is working normally, traders can use the request function starting with Req to place (buy, sell, open or close) orders and queries as needed, and the corresponding callback function starting with On will receive the result notification.
Step 10 : The essence of TraderApi is also a sub-process, so it also needs to call the Join method to let the main process wait before it can run normally.
Insert image description here
It should be noted that TraderApi needs to subscribe to public and private streams before initialization; after establishing a connection with the service, client authentication is required first, and only after the authentication is passed, the account can be logged in; TraderApi's query operation is flow controlled, every second It can only be queried once. Continuous queries within 1 second will not receive result notifications.

API function naming rules

In addition to the functions used to initialize the Api environment, other functional functions can be divided into the following categories based on prefixes:

  • Req, execute the request . For example, confirm the settlement order ReqSettlementInfoConfirm, enter the order ReqOrderInsert, enter the order cancellation ReqOrderAction, etc.
  • OnRsp, notification of request results . Requests other than (buy, sell, open and close) orders will be notified after successful execution. For example, the settlement order confirmation notification OnRspSettlementInfoConfirm indicates that the settlement order has been successfully confirmed. (Buy, buy, close, close) order request, you will be notified when the request passes the transaction front. This notification only means that the request is accepted by the trade front, and it will also need to enter the transaction queue, be executed and other processes. For example, the order entry notification OnRspOrderInsert indicates that the request has been accepted in advance.
  • OnRtn, status change notification of (buy, sell, close, close) order request, or public flow notification . Taking an order as an example, every time the order status changes, you will receive a notification in OnRtnOrder. When the order is completed, you will also receive a notification in OnRtnTrade. OnRtnInstrumentStatus is an example of a public stream notification.
  • OnErrRtn, notification of execution error results . For example, there are insufficient positions today/yesterday for the types on the previous exchange. Error results that have not reached the exchange are notified in OnRsp. Only errors that have reached the exchange and are executed are notified in OnErrRtn.
  • ReqQry, query request . For example, query order ReqQryOrder and query transaction ReqQryTrade.
  • OnRspQry, notification of query results . Query result notification is relatively simple. Generally, one query request corresponds to one query result.

If you still feel confused after reading this, it doesn't matter. The main purpose of this article is to give you an impression of some concepts. Later, we will also combine AlgoPlus to produce relevant tutorials to let everyone understand these processes. In addition, if you encounter problems in the process of learning and using CTP, you can ask questions in the quantitative research community at www.ctp.plus.

Guess you like

Origin blog.csdn.net/AlgoPlus/article/details/101995443