Development of programmed transaction system based on CTP (III.)

Source: Internet
Author: User
This article discusses what data listener threads and order management threads do.
One, Data listener thread
Data listener thread, when the market processing thread receives the new market data, that is, whenever a tick arrives, sends a signal to the data listener thread, triggers the thread to start, and then sequentially:
1. The calculation of various indicators,
2. Then make a strategy calculation,
3. Finally, transactions are made when the strategy is met.
Index calculation refers to the calculation of some statistic values based on the new arrival data and historical data, such as common Ma,macd,rsi and so on, of course, it can construct a certain statistic value. The question to be mentioned here is the problem of the data cycle (which I explained before Boven). If the metrics calculation is based on the data cycle, then the data will be assembled for the market data, that is, each tick data (500 milliseconds) assembled into the data cycle you need (that is, assembled into a K-line), and then each cycle (k lines) in the Open,high,low, Close is calculated to calculate the statistics.
Strategy computing is a logical combination of your calculated metrics, your own trading ideas, and trading strategies, and then trading when you're satisfied with the logic of your strategy.
Here I suggest the architecture, instead of writing metrics and strategy calculations and transactions into a function, you write each metric as a function, then point to it with a function pointer, and then the policy calculation and transaction is written as a function, then the function pointer points to it and presses it into a stack, Thereafter, each index is added, the function pointer corresponding to the index is pressed into the stack;


Every time a data listener thread starts, it pops up each metric and evaluates it, and finally completes the policy function.
Second, order management thread
The order management thread is primarily used to handle two issues in order management:
1. Order queue management, generally in accordance with the declarations time sequence with the data structure such as array management, the principle is that each business request to accurately correspond to its return, each group of requests and returns to accurately correspond to the order of their attribution. The key is the business request number and the transaction serial number.
A business request number is a RequestID that is set when a request is sent to return a REQUESTID,TRADERAPI response when the request is returned. Because TRADERAPI is implemented asynchronously, a terminal program may issue multiple requests and query instructions in succession. RequestID can associate a request/query instruction with a related return.
and the trading serial number is a combination. From the declarations to the transaction process, there will be the following sets of transaction serial number:
Frontid + SessionID + orderref
The user uses this set of transaction serial numbers to uniquely mark any one of the commissions issued in their own way. Once the user has logged in successfully, they will receive the Frontid number, the session number SessionID and the maximum declarations reference maxorderref. The user sets the declarations reference orderref when declarations. Orderref can be incremented starting from Maxorderref. If the user does not set orderref, in the declarations response, Thost sets a orderref for the user. This sets the serial number of each declarations to remain unique.
Through this transaction serial number, can determine the entrusted return and the return of the transaction, can also use this set of transaction serial number to withdraw a single operation.
2. Order of the various state of management: In return, the order of the status of a variety of, please refer to ThostFtdcUserApiDataType.h, I suggest that according to its meaning summed up in these five states: declarations, has been sold, withdraw orders, has been withdrawn, error, because these five states for the completion of strategic logic is critical.
Here is a look at the mechanism and timing of order trading. First, give a sequence diagram:


Among them, the mechanism of declarations response and return is when the thost receives the declarations instruction and refuses to accept the declarations instruction if it fails to pass the parameter checksum. The user receives the Onrsporderinsert message, which contains the error encoding and error message. If Thost accepts the declarations directive, the user does not receive the Onrsporderinser and receives Onrtnorder to update the delegate status. After the exchange receives the declarations, pass the check. Users will receive Onrtnorder, Onrtntrade. If the exchange thinks declarations error, the user will receive Onerrrtnorder.
Withdrawal responses are similar to returns and declarations responses and returns. When Thost receives the withdrawal order, if it fails to pass the parameter check, it refuses to accept the withdrawal order. The user receives the Onrsporderaction message, which contains the error encoding and error message. If Thost accepts a withdraw order, the user will not receive onrsporderaction and receive Onrtnorder to update the delegate status. After the exchange received the withdrawal, through the check, the operation of withdrawal orders. Users will receive Onrtnorder. If the exchange thinks declarations error, the user will receive Onerrrtnorderaction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.