Method for realizing direct connection to futures transaction counter based on Electron framework
Through the Electron framework and node.js counter plug-in, combined with IPC mechanism and Web technology, the problem of difficult development of existing futures trading counters and insufficient resources is solved, and direct connection to futures trading counters and multiple counters is achieved, reducing development costs and difficulty.
Patent Information
- Application Number
- CN202411571470.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-06
- Publication Date
- 2025-02-25
AI Technical Summary
The development of existing futures trading counters is difficult, slow, not rich in ecological resources, and difficult to expand and reuse.
Using the Electron framework, we create a communication thread in the main process of Electron to call the node.js counter plug-in, combine IPC mechanism and web technology to create a cross-platform native desktop application, and support direct connection to the CTP futures counter for transactions.
It realizes direct connection to the futures trading counter, reduces development difficulty and cost, supports multi-counter access and unified data flow control, filtering and exception handling, and meets the needs of rich functions and rapid iteration of the client interface.
Smart Images

Figure HDA0005120566330000011 
Figure HDA0005120566330000012
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of financial technology, and specifically relates to a method for realizing direct access to a futures trading counter based on an Electron framework. Background Art
[0002] Currently, futures trading counter developers generally only provide API access libraries based on Windows and Linux systems, which are developed based on C / C++. In order to directly connect to futures trading counters, terminal developers generally choose to develop PC clients based on C++ frameworks such as MFC and QT. C++ frameworks such as MFC and QT have the disadvantages of high development difficulty, long cycle, imperfect ecological resources, and poor reusability.
[0003] The Electron framework is created based on Chromium and Node.js counter plug-in. The Node.js counter plug-in is used in the publicly available open source project https: / / github.com / dreamyzhang / nodectp. The framework supports the use of Web technologies such as JavaScript, HTML and CSS to create cross-platform native desktop applications. It is compatible with operating systems such as Mac, Window and Linux, and supports direct connection to CTP futures counters for trading. It has the characteristics of low development difficulty, fast speed, rich ecological resources, easy expansion and reuse. Summary of the invention
[0004] The purpose of the present invention is to solve the problems of the prior art that the development is difficult, the speed is slow, the ecological resources are not abundant, and the expansion and reuse are not easy.
[0005] In order to solve the above problems, the present invention provides a method for directly connecting to a futures trading counter based on the Electron framework, comprising the following steps:
[0006] Step S1: call the node.js counter plug-in;
[0007] In the main process of Electron, worker_threads is used to create the communication thread trader_worker responsible for accessing the futures trading counter. The trade_worker thread calls the Node.js counter plug-in.
[0008] Step S2: Create a client interface;
[0009] The vue-based front-end interface is loaded in the window of the Electron framework. The rendering process and the main process communicate through the IPC mechanism of the Electron framework. The communication data is in json format, supporting query and subscription modes, and supporting communication between multiple windows.
[0010] Step S3: PC client packaging and installation;
[0011] Use the electron-builder plug-in to package the code into an executable program that can be run directly, and generate an installation package for the Windows system by configuring nsis;
[0012] Step S4: PC client data transmission method;
[0013] Step S4-1: data request;
[0014] Step S4-1-1: The front-end window of the Electron framework sends data to the main process through the IPC pipeline through the ipcRenderer.send function;
[0015] Step S4-1-2: The main process receives the request data of the corresponding IPC pipe through the ipcMain.on function, and forwards it to the corresponding woker thread through the worker.postMessage function;
[0016] Step S4-1-3: The worker thread receives the request data forwarded by the main process through parentPort.on, and calls the corresponding interface function to send it to the node.js counter plug-in. The interface function is registered through the persistent function;
[0017] Step S4-1-4: the node.js counter plug-in sends the request data to the futures trading counter through the API interface packaged by the counter developer;
[0018] Step S4-2: Response data;
[0019] Step S4-2-1: After receiving the data sent by the futures trading counter, the node.js counter plug-in calls the corresponding callback function to send the data to the worker thread. The callback function is called through the blockingCall function of the node-addon-api library;
[0020] Step S4-2-2: The worker thread sends the response data to the main process through the parentPort.postMessage function;
[0021] Step S4-2-3: The main process finds the corresponding window according to the winId in the response data, and forwards the data to the corresponding window through the webContents.fromId(winId).send function;
[0022] Step S4-2-4: The front-end window receives the response data through ipcRenderer.on in the IPC mechanism and performs business processing.
[0023] Preferably, node-gyp is a cross-platform command-line tool written in Node.js for compiling native add-ons for Node.js.
[0024] Preferably, Node.js is the core of the Electron framework, and Electron uses related plug-ins of Node.js.
[0025] In a preferred embodiment, the communication thread trader_worker is responsible for calling the node.js counter plug-in compiled in step S1 to realize communication with different futures trading counters, realize filtering, flow control and exception handling functions during the communication process, and realize interface conversion of different futures trading counters.
[0026] In the preferred mode, the front-end interface is completely developed based on Web technologies such as JavaScript, HTML, CSS, etc., which can make full use of the rich ecological resources of Web technology to develop professional charts such as time-sharing charts and K-line charts, greatly reducing the difficulty of development.
[0027] In the preferred mode, the client interface supports functions including fund inquiry, three-key order, standard order, bank-to-futures transfer, position information, contract list, market information, transaction visibility, time-sharing chart, K-line chart and various technical indicator charts.
[0028] electron-builder plugin: is an open source tool for packaging Electron applications. It can package Electron applications into executable files for distribution and installation on different operating systems. Download address: https: / / www.electron.build /
[0029] IPC mechanism: provided by the Electron framework, it is a key part of building feature-rich desktop applications in Electron. Since the main process and renderer process have different responsibilities in Electron's process model, IPC is the only way to perform many common tasks, such as calling native APIs from the UI or triggering changes to web content from native menus. Introduction address:
[0030] https: / / www.electronjs.org / en / docs / latest / tutorial / ipc
[0031] The beneficial effects of the present invention are as follows: a transaction data communication thread is created in the main process of the Electron framework to call the node.js plug-in, which solves the heterogeneous development problem between JavaScript and C++ and realizes direct access to the futures trading counter. The PC client implemented based on the method of the present invention has a clear structure, supports multi-counter access, supports unified data flow control, filtering and exception handling, and makes full use of the rich ecological resources of the Electron framework to meet the needs of rich interface functions and rapid iteration in client development. The futures trading PC client is based on the Electron framework, which can make full use of the rich Web front-end components, reduce the difficulty and cost of development, and meet the current needs of rich client interface functions and rapid iteration. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 This is a PC client architecture diagram of the present invention;
[0033] Figure 2 This is the PC client data flow diagram of the present invention. DETAILED DESCRIPTION
[0034] Example 1: Combination Figure 1 , describing the specific process of the present invention:
[0035] Step S1: Generate a node.js counter plug-in;
[0036] Obtain the access API library released by the futures trading counter developer, and use node-gyp to compile the C++ API interface into a node.js plug-in that supports Electron framework calls based on the node-addon-api library;
[0037] Step S2: Call the node.js counter plug-in;
[0038] In the main process of Electron, use the worker_threads method of Node.js to create a communication thread trader_worker responsible for accessing the futures trading counter. The main process and the communication thread trader_worker communicate through a single channel. One thread can only access one futures trading counter at a time.
[0039] Step S3: Create a client interface;
[0040] The vue-based front-end interface is loaded in the window of the Electron framework. The rendering process and the main process communicate through the IPC mechanism of the Electron framework. The communication data is in json format, supporting query and subscription modes, and supporting communication between multiple windows.
[0041] The communication layer is divided into market worker threads and transaction worker threads, and the threads are created through the worker_threads module of the Electron framework. The market worker thread supports access to the market center of futures market publishers and supports forwarding market information from six domestic futures exchanges. The transaction worker thread is the core of the present invention, which supports access to multiple futures trading counters. Currently, direct access to four futures trading counters, including CTP, Hang Seng, Jinshida, and X_One, has been realized. The transaction worker thread is responsible for communicating with the main process through the channel pipeline, communicating with the futures trading counter by calling the node.js counter plug-in generated by the counter API encapsulation, supporting unified encapsulation and forwarding of data from different counters, supporting filtering, flow control and exception handling during the communication process, and supporting direct access to multiple counters, which greatly reduces the development difficulty of counter access.
[0042] The forwarding layer is located in the main process of the Electron framework. It is responsible for forwarding the data sent by the Electron window (rendering process) through the IPC mechanism to the corresponding worker thread through the channel pipeline, and forwarding the data sent by the worker thread through the channel pipeline to the corresponding Electron window through the IPC mechanism.
[0043] The view layer is located in the Electron window and is developed based on the Vue front-end framework. It supports the creation of multiple windows and communicates with the forwarding layer through the IPC mechanism of the Electron framework. The view layer makes full use of the rich ecological resources of Web front-end technology for interface development, such as introducing the vxe-table component to develop market lists, introducing the KLineChart component to develop time-sharing charts and K-line charts, and introducing the better-sqlite3 component to develop local database storage. The introduction of open source components greatly reduces the difficulty of interface development and improves development efficiency.
[0044] Step S4: PC client packaging and installation;
[0045] The code is packaged into an executable program that can be run directly through the electron-builder plug-in, and the installation package of the Windows system is generated by configuring nsis to facilitate the installation and release of the client;
[0046] Combination Figure 2 , a specific example of the PC client data flow of the present invention is described as follows:
[0047] Step S5: PC client data transmission method;
[0048] Step S5-1: data request;
[0049] Step S5-1-1: The front-end window of the Electron framework sends data through the ipcRenderer.send function to the main process via the IPC pipeline;
[0050] Step S5-1-2: The main process receives the request data of the corresponding IPC pipeline through the ipcMain.on function and forwards it to the corresponding worker thread through the worker.postMessage function;
[0051] Step S5-1-3: The worker thread receives the request data forwarded by the main process through parentPort.on and calls the corresponding interface function to send it to the node.js counter plugin. The interface function is registered through the persistent function of the node-addon-api library;
[0052] Step S5-1-4: The node.js counter plugin sends the request data to the futures trading counter through the API interface encapsulated by the counter developer;
[0053] Step S5-2: Response data;
[0054] Step S5-2-1: After receiving the data sent by the futures trading counter, the node.js counter plugin calls the corresponding callback function to send the data to the worker thread. The callback function is called through the blockingCall function of the node-addon-api library;
[0055] Step S5-2-2: The worker thread sends the response data to the main process through the parentPort.postMessage function;
[0056] Step S5-2-3: The main process finds the corresponding window according to the winId in the response data and forwards the data to the corresponding window through the webContents.fromId(winId).send function;
[0057] Step S5-2-4: The front-end window receives the response data through ipcRenderer.on in the IPC mechanism and performs business processing.
[0058] Step S5-2-5: Data format;
[0059] The window and the main process, and the main process and the thread communicate through a JSON format data protocol. The specific protocol is:
[0060] opcode / / / <number type, identifying the message type;
[0061] winId / / / <type: number, identifying the window ID;
[0062] comId_set / / / <set of type: number, set of component IDs, unique within the window;
[0063] json_str / / / <type: string, custom object string;
[0064] The opcode identifies the message type. Based on this field, without deserializing the custom object string,
[0065] judge the message type and forwarding direction, saving the consumption of deserialization and improving the transmission efficiency;
[0066] winId identifies the window ID in the rendering process, determining which window the message comes from and which window it should be sent to, for supporting multi-window data forwarding;
[0067] comId_set identifies the set of components that request or receive this message. Components can judge whether the message needs to be processed based on this field without deserializing the custom object string json_str;
[0068] pc_str is a custom object string, obtained from the JSON.stringify operation on the custom object. Performing the JSON.parse deserialization operation on it gets the custom object;
[0069] The API library is obtained through the official website of the futures trading counter developer or technical support personnel. For example, the CTP counter API library is obtained through the official website
[0070] of Shanghai Futures Information Technology Co., Ltd.
[0071] node-addon-api is a wrapper class for C++. It simplifies the use of the C-based N-API provided by Node.js and can provide a C++ object model and exception semantic processing with relatively low overhead.
[0072] Node-gyp is a Node.js tool that provides a cross-platform build tool for compiling C / C++ code into plugins loaded by Node.js.
[0073] worker_threads is a multi-threaded module in Electron, used to create multi-threads.
[0074] Trader_worker is an encapsulated thread class, which is mainly responsible for communicating with the futures trading counter and has functions such as exception handling, flow control, and network disconnection and reconnection.
[0075] Vue is a JavaScript front-end framework for building user interfaces.
[0076] ElectronBuilder is a tool for packaging Electron applications. It can package Electron applications into executable files for distribution and installation on different operating systems.
[0077] NSIS (Nullsoft Scriptable Install System) is an open source Windows installation program creation program that provides installation, uninstallation, system settings, file decompression and other functions.
[0078] ipcRenderer.send is the message sending function in Electron.
[0079] ipcMain.on is the message receiving function in Electron.
[0080] worker.postMessage is the sending function that the parent thread sends data to the child thread.
[0081] parentPort.on is the receiving function of the child thread receiving the parent thread data.
[0082] blockingCall is the callback function for communication between C++ and JavaScript.
[0083] parentPort.postMessage is the sending function of the child thread to send data to the parent thread.
[0084] winId is the window ID of Electron, which is unique and determines the corresponding window.
[0085] ipcRenderer.on is the receiving function of the front-end window receiving the Electron main process message.
[0086] The above functions are all provided by the Electron framework. Instructions for use: https: / / www.electronjs.org / zh / docs / latest / api / app.
[0087] Embodiment 2:
[0088] Step S1: call the node.js counter plug-in;
[0089] In the main process of Electron, worker_threads is used to create the communication thread trader_worker responsible for accessing the futures trading counter. The trade_worker thread calls the Node.js counter plug-in.
[0090] Step S2: Create a client interface;
[0091] The vue-based front-end interface is loaded in the window of the Electron framework. The rendering process and the main process communicate through the IPC mechanism of the Electron framework. The communication data is in json format, supporting query and subscription modes, and supporting communication between multiple windows.
[0092] Step S3: PC client packaging and installation;
[0093] Use the electron-builder plug-in to package the code into an executable program that can be run directly, and generate an installation package for the Windows system by configuring nsis;
[0094] Step S4: PC client data transmission method;
[0095] Step S4-1: data request;
[0096] Step S4-1-1: The front-end window of the Electron framework sends data to the main process through the IPC pipeline through the ipcRenderer.send function;
[0097] Step S4-1-2: The main process receives the request data of the corresponding IPC pipe through the ipcMain.on function, and forwards it to the corresponding woker thread through the worker.postMessage function;
[0098] Step S4-1-3: The worker thread receives the request data forwarded by the main process through parentPort.on, and calls the corresponding interface function to send it to the node.js counter plug-in. The interface function is registered through the persistent function;
[0099] Step S4-1-4: the node.js counter plug-in sends the request data to the futures trading counter through the API interface packaged by the counter developer;
[0100] Step S4-2: Response data;
[0101] Step S4-2-1: After receiving the data sent by the futures trading counter, the node.js counter plug-in calls the corresponding callback function to send the data to the worker thread. The callback function is called through the blockingCall function of the node-addon-api library;
[0102] Step S4-2-2: The worker thread sends the response data to the main process through the parentPort.postMessage function;
[0103] Step S4-2-3: The main process finds the corresponding window according to the winId in the response data, and forwards the data to the corresponding window through the webContents.fromId(winId).send function;
[0104] Step S4-2-4: The front-end window receives the response data through ipcRenderer.on in the IPC mechanism and performs business processing.
[0105] The above shows and describes the basic principles and main features of the present invention and the advantages of the present invention. It should be understood by those skilled in the art that the present invention is not limited to the above embodiments. The above embodiments and descriptions are only for explaining the principles of the present invention. Without departing from the spirit and scope of the present invention, the present invention may have various changes and improvements, which fall within the scope of the present invention to be protected. The scope of protection of the present invention is defined by the attached claims and their equivalents.
Claims
1. A method for directly connecting to a futures trading counter based on the Electron framework, characterized in that: The following steps are involved: Step S1: Call the node.js counter plug-in; In the main process of Electron, worker_threads is used to create the communication thread trader_worker responsible for accessing the futures trading counter. The communication thread trade_worker calls the Node.js counter plug-in. Step S2: Create a client interface; The vue-based front-end interface is loaded in the window of the Electron framework. The rendering process and the main process communicate through the IPC mechanism of the Electron framework. The communication data is in json format, supporting query and subscription modes, and supporting communication between multiple windows. Step S3: PC client packaging and installation; Use the electron-builder plug-in to package the code into an executable program that can be run directly, and generate an installation package for the Windows system by configuring nsis; Step S4: PC client data transmission method; Step S4-1: data request; Step S4-1-1: The front-end window of the Electron framework sends data to the main process through the IPC pipeline through the ipcRenderer.send function; Step S4-1-2: The main process receives the request data of the corresponding IPC pipe through the ipcMain.on function, and forwards it to the corresponding woker thread through the worker.postMessage function; Step S4-1-3: The worker thread receives the request data forwarded by the main process through parentPort.on, and calls the corresponding interface function to send it to the node.js counter plug-in. The interface function is registered through the persistent function; Step S4-1-4: the node.js counter plug-in sends the request data to the futures trading counter through the API interface packaged by the counter developer; Step S4-2: Response data; Step S4-2-1: After receiving the data sent by the futures trading counter, the node.js counter plug-in calls the corresponding callback function to send the data to the worker thread. The callback function is called through the blockingCall function of the node-addon-api library; Step S4-2-2: The worker thread sends the response data to the main process through the parentPort.postMessage function; Step S4-2-3: The main process finds the corresponding window according to the winId in the response data, and forwards the data to the corresponding window through the webContents.fromId(winId).send function; Step S4-2-4: The front-end window receives the response data through ipcRenderer.on in the IPC mechanism and performs business processing.
2. The method for implementing direct access to a futures trading counter based on the Electron framework according to claim 1, characterized in that: Node.js is the core of the Electron framework, and Electron uses related plug-ins of Node.js.
3. The method for implementing direct access to a futures trading counter based on the Electron framework according to claim 1, characterized in that: The communication thread trader_worker is responsible for calling the node.js counter plug-in compiled in step S1 to realize communication with different futures trading counters, realize filtering, flow control and exception handling functions in the communication process, and realize interface conversion of different futures trading counters.
4. The method for implementing direct access to a futures trading counter based on the Electron framework according to claim 1, characterized in that: The functions supported by the client interface include fund inquiry, three-key order, standard order, silver-futures transfer, position information, contract list, market information, transaction visibility, time-sharing chart, K-line chart and various technical indicator charts.
Citation Information
Cited By
High-performance interactive firmware updating method
CN121143838A
Realization method of futures transaction gateway
CN121441682A