Tongdaxin transaction interface: make transactions more efficient

Tongdaxin is a well-known stock trading software in China. Its trading interface allows developers to conduct automated trading through programming. This article will introduce some API parameters and code examples of the Tongdaxin trading interface, and share a quantitative strategy code based on the Tongdaxin trading interface.

login parameters

Login is the first step in using the Tongdaxin trading interface, and the following parameters need to be passed:

host = '127.0.0.1'  # 通达信交易接口服务器的IP地址
port = 7709  # 通达信交易接口服务器的端口号
version = '9.71'  # 通达信客户端的版本号
account = '123456'  # 登录账号
password = 'password'  # 登录密码

The parameters required by different types of interfaces are slightly different, according to the relevant documents.

Query stock information

The following API parameters can be used to query stock information:

code = '600000'  # 股票代码
type = 'stock'  # 股票类型
market = 'sh'  # 股票市场

buy stock

To buy stocks, you need to pass the following API parameters:

code = '600000'  # 股票代码
price = 12.34  # 买入股票的价格
amount = 100  # 买入股票的数量

sell stock

To sell stocks, you need to pass the following API parameters:

code = '600000'  # 股票代码
price = 12.34  # 卖出股票的价格
amount = 100  # 卖出股票的数量

The above are some API parameters and code examples of Tongdaxin trading interface. For more detailed API documentation, please refer to the official documentation.

Quantitative strategy code example

The following is a Python example of a simple moving average strategy implemented based on the real trading interface:

import tdxapi

# 登录实盘交易接口
tdx = tdxapi.TdxApi()
tdx.connect('127.0.0.1', 7709, '9.71')
tdx.login('123456', 'password')

# 查询股票信息
info = tdx.get_security_info('600000', 'stock', 'sh')

# 获取历史行情数据
data = tdx.get_history_data('600000', '2021-01-01', '2021-06-01')

# 计算5日均线和20日均线
data['ma5'] = data['close'].rolling(5).mean()
data['ma20'] = data['close'].rolling(20).mean()

# 判断买入和卖出信号
if data['ma5'].iloc[-1] > data['ma20'].iloc[-1] and data['ma5'].iloc[-2] < data['ma20'].iloc[-2]:
    tdx.buy('600000', data['close'].iloc[-1], 100)
elif data['ma5'].iloc[-1] < data['ma20'].iloc[-1] and data['ma5'].iloc[-2] > data['ma20'].iloc[-2]:
    tdx.sell('600000', data['close'].iloc[-1], 100)

# 登出通达信交易接口
tdx.logout()

The above code implements a simple moving average strategy, buy when the 5-day moving average crosses the 20-day moving average, and sell when the 5-day moving average crosses the 20-day moving average. This is just an example, and actual quantitative strategies need to be designed and optimized according to specific investor needs.

The use of the real trading interface can make the transaction more efficient and convenient. Developers can automate transactions by writing code, thereby reducing the time and error rate of manual operations. If you are interested in trading interface and quantitative trading, you might as well give it a try, maybe this will be the key to your more efficient trading.

Guess you like

Origin blog.csdn.net/LegendCode/article/details/129399066