开始获取股票数据课程的学习:
获取qmt账号的持仓情况后,我们进入下一步,如何获得当前账号的委托状况
还是之前的步骤,打开qmt,选择独立交易,
之后使用pycharm,编写py文件
导入包:
from xtquant import xtdata
import time
获取股票清单:
from xtquant import xtdata
# 获取沪深A股全部股票的代码
xtdata.get_stock_list_in_sector("沪深A股")
print(xtdata.get_stock_list_in_sector("沪深A股"))
修改股票代码,进行分类:
def ret_code(stock_code): # 将收到的股票代码进行转换 含基金
if stock_code[:2] in ["60", "68", "90", "50", "51"]:
return stock_code[:6] + ".SH"
elif stock_code[:2] in ["00", "30", "15"] or "XSHG" in stock_code: #
#elif stock_code[:2] in ["00", "30", "15"] : #
return stock_code[:6] + ".SZ"
elif stock_code[:2] in ["43", "83", "87"]:
return stock_code[:6] + ".BJ"
elif "XSHG" in stock_code:
return stock_code[:6] + ".SH"
elif "XSHE" in stock_code:
return stock_code[:6] + ".SZ"
else:
return stock_code
for i in xtdata.get_stock_list_in_sector("沪深A股"):
print(ret_code(i))
获取股票历史价格行情
############ 仅获取历史行情 #####################
start_date = '20250101' # 格式"YYYYMMDD",开始下载的日期,date = ""时全量下载
end_date = ""
period = "1d"
# period = "d", "w", "mon", "q", "y",
code_list = xtdata.get_stock_list_in_sector("沪深A股")[:1]
data1 = xtdata.get_market_data_ex([], code_list, period=period, start_time=start_date, end_time=end_date)
print(data1[code_list[0]])
仅获取最新行情
def do_subscribe_quote(stock_list: list, period: str):
for i in stock_list:
xtdata.subscribe_quote(i, period=period)
time.sleep(1) # 等待订阅完成
############ 仅获取最新行情 #####################
do_subscribe_quote(code_list, period) # 设置订阅参数,使gmd_ex取到最新行情
count = 1 # 设置count参数,使gmd_ex仅返回最新行情数据
data2 = xtdata.get_market_data_ex([], code_list, period=period, start_time=start_date, end_time=end_date,
count=1) # count 设置为1,使返回值只包含最新行情
print(data2[code_list[0]])
获取历史行情+最新行情
############ 获取历史行情+最新行情 #####################
do_subscribe_quote(code_list, period) # 设置订阅参数,使gmd_ex取到最新行情
count = -1 # 设置count参数,使gmd_ex返回全部数据
data3 = xtdata.get_market_data_ex([], code_list, period=period, start_time=start_date, end_time=end_date,
count=-1) # count 设置为1,使返回值只包含最新行情
print(data3[code_list[0]])
获得量价数据,
['time', 'open', 'high', 'low', 'close', 'volume', 'amount', 'settelementPrice', 'openInterest', 'preClose', 'suspendFlag'],
下一步获取财务数据