[Play Quantitative 17 from scratch] How to complete automated trading with python+QMT? (The most detailed introductory tutorial on the whole network)

1. What is QMT

This part is literacy content, and those who have a certain understanding can skip it.

concept

It is a quantitative trading client software, produced by a company called Xuntou, which can directly log in to your brokerage account for stock trading, but it is different from Flush/Communications in that it exposes a python-based trading API that can execute Programmatic transactions .

By the way, I checked the background of Xuntou, a company that hit the GEM in 21 years, and then gave up. Prospectus link

interface

Taking the China National Finance Edition as an example, its login interface is as follows, and the login interface of different brokerages is slightly different (there may not be a minimalist mode )

After entering the software, the interface is as follows. It is mainly divided into several modules: market quotation, transaction, model research, and model transaction. The QMT interface of each brokerage is similar.

insert image description here

2. Why use QMT

In the past, automated trading has always been a pain for retail investors. Most of the automated interfaces are only for institutions, and there are very few methods that retail investors can use. Although there are open source libraries like easyTrader that have solved similar problems, by simulating buttons, I I also tried to modify it, but it is not very stable.

QMT, on the other hand, directly calls the interface of the brokerage, which breaks through the last link of quantitative trading, exposes the python API, and can be called directly by the program, which greatly improves the stability of automated trading, and the programmability is also very high .

3. QMT vs miniQMT (minimalist mode)

miniQMT is the minimalist model of QMT. Some brokerages have this model, but some brokerages do not. How to tell if there is one is to see if there is an option of minimalist mode on the login interface . When the client starts in minimalist mode, the interface is as follows:
insert image description here

The biggest difference between miniQMT and QMT is:

QMT mode needs to write the program to run in the QMT client software;

In the miniQMT mode, you can directly use the python package xtquant , outside of the software, write a python program to connect with the QMT client, and place orders programmatically. Smart friends should understand it, so that the QMT client will completely become a trading terminal. Our quantification program can run independently of QMT, as long as the minimalist client is running. This greatly releases the transformation ability.

3. Code combat

It's useless to say more, let's go to the code, here is a demonstration in miniQMT (minimalist mode)

download xtquant

First of all, we need to download the python package xtquant. I recommend downloading it from the official website of Xuntou. The download link is as follows

xtquant download: http://docs.thinktrader.net/vip/pages/633b48/

insert image description here
Just download the latest version. After downloading, put this package in your own python project to ensure that it importcan be used.

The examples on the official website are still relatively complicated, so let’s start with the simplest example here.

Create a transaction object

import random
from xtquant.xttrader import XtQuantTrader

path = r'D:\国金证券QMT交易端\userdata_mini'
session_id = int(random.randint(100000, 999999))
xt_trader = XtQuantTrader(path, session_id)

Creating xt_traderan object requires two parameters: : path, which is the folder
pathunder the file where the QMT software is installed . : The session id needs to be different when multiple objects are created. Here we use 6 random numbers to generate./userdata_mini
session_id xt_trader

Connect to QMT client

xt_trader.start()

connect_result = xt_trader.connect()

print(connect_result)

if connect_result == 0:
    print('连接成功')

To execute xt_trader.connect(), you need to ensure that you have logged in to the QMT minimalist mode and keep the client running to connect successfully. Enter the client in non-minimalist mode, but the connection is unsuccessful. Pro-test.

subscription account

from xtquant.xttype import StockAccount

acc = StockAccount('xxxxx')
subscribe_result = xt_trader.subscribe(acc)
print(subscribe_result)

This step is used to subscribe to the fund account, xxxxxreplace it with the fund account number you are logging in, if the subscription is successful, subscribe_resultit will be 0, if it is not successful, it will be -1

place an order

from xtquant import xtconstant

stock_code = '000429.SZ'

order_id = xt_trader.order_stock(acc, stock_code, xtconstant.STOCK_BUY, 100, xtconstant.FIX_PRICE, 7.5)
print(order_id)

xtconstant.STOCK_BUYThe form order type is buy, xtconstant.FIX_PRICEwhich means that the quotation type is limit price. After the execution is successful, the order record can be directly seen in the miniQMT terminal, which can confirm that our order is successful. The method will return the order change, which is the order number in the figure below.

insert image description here

Cancellation

You can directly use the order number to cancel the order, execute the following method, note that the order number is in digital format

xt_trader.cancel_order_stock(acc, 1082130954)

In this way, the order just placed will be cancelled.
insert image description here

4. How to activate

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

V. Summary

This article introduces the basic concept and usage of QMT, and used the miniQMT mode for actual combat. It feels quite easy to use, and programmatic trading is no longer a bottleneck. The shared usage will continue to be used later.

Guess you like

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