获取qmt账号的资金账号后,我们进入下一步,如何获得当前账号的持仓情况
还是之前的步骤,打开qmt,选择独立交易,
之后使用pycharm,编写py文件。
from xtquant import xtdata
from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
from xtquant.xttype import StockAccount
import time, datetime, traceback, sys
import pandas as pd
连接miniqmt系统:
path = r'C:\国金QMT交易端模拟\userdata_mini'
# QMT账号
account = '55003243'
# 取账号信息
session_id = int(time.time())
xt_trader = XtQuantTrader(path, session_id)
acc = StockAccount(account, 'STOCK')
# 启动交易线程
xt_trader.start()
# 建立交易连接,返回0表示连接成功
connect_result = xt_trader.connect()
print('建立交易连接,返回0表示连接成功', connect_result)
# 对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功
subscribe_result = xt_trader.subscribe(acc)
print('对交易回调进行订阅,订阅后可以收到交易主推,返回0表示订阅成功', subscribe_result)
获取账号持仓:
# 查账号持仓
positions = xt_trader.query_stock_positions(acc)
data = pd.DataFrame()
if len(positions) != 0:for i in range(len(positions)):df = pd.DataFrame()df['账号类型'] = [positions[i].account_type]df['资金账号'] = [positions[i].account_id]df['证券代码'] = [positions[i].stock_code]df['证券代码'] = df['证券代码'].apply(lambda x: str(x)[:6])df['持仓数量'] = [positions[i].volume]df['可用数量'] = [positions[i].can_use_volume]df['平均建仓成本'] = [positions[i].open_price]df['市值'] = [positions[i].market_value]data = pd.concat([data, df], ignore_index=True)
print('持仓', data)