[miniQMT real disk quantification 2] establish a connection with the client

foreword

The previous article introduced miniQMT and its advantages conceptually. This article starts with the first step in the actual operation: connect to the client, let you know what it is, and just why it is. Don’t say much, let’s do it!

login client

At the beginning of everything, we must first ensure the login of the QMT minimal version client. The principle is also very simple. It really provides the service of the market and transaction interface. In the client, the xtquant package only establishes communication with the client.

Be sure to ensure that the client successfully logs in! ! ! Don't make low-level mistakes! ! ! Then ask others why it doesn't work!

insert image description here

As shown above, be sure to check the [Minimalist Mode] option, and then enter the account password to log in.

It needs to be explained that the QMT client may not be able to log in during non-trading hours , especially on weekends . This is a normal phenomenon and there is no need to make a fuss. Therefore, some important tests are best performed during trading hours.

communication settings

As you can see, there is a [Communication Settings] button in the lower left corner of the login interface, let's click it to have a look.

insert image description here

There is no need for us to modify the default here, and the default is fine , but we can understand from here that there are two parts connecting QMT to the cloud: [Market Master Station] and [Trading Center]

  • Market master station: Provide market data, in miniQMT mode, xtdatacall through the module
  • Trading center: Provide trading interface, in miniQMT mode, xttradercall through the module

As for what is the difference between xtdataand xttrade, I will introduce it in detail later.

Download XtQuant

Download address: http://docs.thinktrader.net/vip/pages/633b48/

insert image description here

Ok, now we focus on xtquantthis thing, which is a Python package that can be downloaded and used.

It should be noted that Xuntou did not publish the package to PyPi, so we cannot use it pip installto install it, we can only download it manually, put it in the root directory of our Python project, and import it as a local package.

The concept of Python packages and modules is not the focus of this series of articles. It belongs to the basic part of Python. If you have a lack of knowledge in this area, it is recommended to find a channel to learn. The easiest and fastest way is to let ChatGPT teach you. Thieves 6 in this regard.

So, xtquantwhat do you do? It can be xtquantunderstood as a set of API methods for operating miniQMT. By calling the methods in xtquant, we can realize operations such as market acquisition and order placement to realize our strategy.

xtquantThe call does not need to be written inside the QMT software, it only needs to ensure that the QMT minimalist mode has been logged in, and it can be called in any environment that can execute Python xtquant.

However, what needs to be guaranteed is that xtquantthe calling program is in the same operating system as the QMT software, and the currently officially provided interface does not support RPC remote calling.

build local project

No matter what tool you use, the Python project should be built. In order to write this series of articles, I will build one on Github.

insert image description here
As you can see, put the downloaded one xtquantin the root folder of our project so that it can be referenced.

Note that because the description structure used as data is used in xtquant pandas, we need to install it here pandas.

poetry add pandas

or

pip install pandas

It depends on what package manager you use as a tool. If you don't understand this aspect, you will use it by default pip.

Next, we start the actual code, create a jupter notebook file, and write our program in it. As mentioned above, xtquantthe two most important modules in the game are xtdataand xttrader, the following two parts are actual combat.

xtdata

xtdataIt is the data acquisition interface, we can realize data acquisition, download, subscription and other functions.

The following is a simple example. After the successful login of our QMT client, the following interface can be adjusted.

from xtquant import xtdata

xtdata.download_history_data2(stock_list=['600519.SH'], period='1d')
res = xtdata.get_market_data(stock_list=['600519.SH'], period='1d')
print(res)

The returned data is a dictionary, and the value corresponding to each key is an pandas.DataFrameobject. We can process the data results according to our needs.
insert image description here

For xtdataa more detailed introduction, we will talk about it later. Here is a simple impression. It can be understood that all APIs related to data acquisition are in this module.

xttrader

xttraderIt is a transaction-related module, we can use it to place orders, cancel orders, etc.

The following is a simple example, an example of linking a client and placing an order

import random

from xtquant.xttype import StockAccount
from xtquant.xttrader import XtQuantTrader
from xtquant import xtconstant

# miniQMT安装路径
mini_qmt_path = r'D:\国金证券QMT交易端\userdata_mini'
# QMT账号
account = 'xxxx'
# 创建session_id
session_id = int(random.randint(100000, 999999))
# 创建交易对象
xt_trader = XtQuantTrader(mini_qmt_path, session_id)
# 启动交易对象
xt_trader.start()
# 连接客户端
connect_result = xt_trader.connect()

if connect_result == 0:
    print('连接成功')
# 创建账号对象
acc = StockAccount(account)
# 订阅账号
xt_trader.subscribe(acc)
# 下单
res = xt_trader.order_stock(acc, stock_code='600000.SH', order_type=xtconstant.STOCK_BUY, order_volume=100, price_type=xtconstant.FIX_PRICE, price=7.44)

res

Here are a few key objects

  • mini_qmt_path: This is the installation path of QMT, you need to select userdata_minithis folder
  • account: your QMT account
  • xt_trader: the created transaction object

After the connection is successful, you can place an order happily.

Note: During non-trading hours, it is very likely that the order will fail. Please check the information in the client in time, and the failure will be displayed in the message.

insert image description here

Summarize

In this article, we xtquantconnected the QMT client, xtdataobtained the stock transaction data, and xttraderconnected your fund account to place an order and execute the order operation.

In the following articles, xtquanteach important API will be introduced one by one, and source code analysis will be done when necessary.

The example code of this article can be obtained here: https://github.com/zsrl/miniqmt-demo

At present, QMT needs to find a brokerage company to open. For specific opening methods and requirements, please refer to "QMT Opening Rules Sharing"

Guess you like

Origin blog.csdn.net/u010214511/article/details/131734432