Bootstrap

qmt量化交易策略小白学习笔记第11期【qmt编程之获取股票订单流数据--原生Python】

qmt编程之获取股票订单流数据

qmt更加详细的教程方法,会持续慢慢梳理。

也可找寻博主的历史文章,搜索关键词查看解决方案 !

感谢关注,需免费开通量化回测与咨询实盘权限,可以和博主联系!

获取股票订单流数据

获取股票在某个价位的订单数量

提示

1.该数据通过get_market_dataget_market_data_ex接口获取,period参数选择orderflow1m 或者 orderflow1d
2.获取历史数据前需要先用download_history_data下载历史数据,订单流数据仅提供orderflow1m周期数据下载,其他周期的订单流数据都是通过1m周期合成的
3.订单流版 权限数据

原生pytrhon

python

from xtquant import xtdata
# 订单流数据仅提供1m周期数据下载,其他周期的订单流数据都是通过1m周期合成的
period = "orderflow1m"
# 下载000001.SZ的1m订单流数据
xtdata.download_history_data("000001.SZ",period=period)
# 获取000001.SZ的1m订单流数据
xtdata.get_market_data_ex([],["000001.SZ"],period=period)["000001.SZ"]

参数

名称类型描述
fieldlist数据字段,详情见下方field字段表
stock_listlist合约代码列表
periodstr订单流数据周期——orderflow1m, orderflow5m, orderflow15m, orderflow30m, orderflow1h, orderflow1d
start_timestr数据起始时间,格式为 %Y%m%d 或 %Y%m%d%H%M%S,填""为获取历史最早一天
end_timestr数据结束时间,格式为 %Y%m%d 或 %Y%m%d%H%M%S ,填""为截止到最新一天
countint数据个数
dividend_typestr除权方式
fill_databool是否填充数据
  • field字段可选:
field数据类型含义
timestr时间
pricestr价格段
buyNumstr各价格对应的买方订单量
sellNumstr各价格对应的卖方订单量
  • period字段可选:
period数据类型含义
orderflow1mstr1m周期订单流数据
orderflow5mstr5m周期订单流数据
orderflow15mstr15m周期订单流数据
orderflow30mstr30m周期订单流数据
orderflow1hstr1h周期订单流数据
orderflow1dstr1d周期订单流数据

返回值 返回一个 {stock_code:pd.DataFrame} 结构的dict对象,默认的列索引为取得的全部字段. 如果给定了 fields 参数, 则列索引与给定的 fields 对应.

示例

示例

# 下载000001.SZ的orderflow1m,以获取历史数据
# orderflow仅提供1m周期进行下载,其他周期皆在系统底层通过1m订单流数据进行合成给出
xtdata.download_history_data("000001.SZ",period="orderflow1m")


# 获取000001.SZ,1m订单流数据
period = "orderflow1m"
data1 = xtdata.get_market_data_ex([],["000001.SZ"],period=period)["000001.SZ"]

# 获取000001.SZ, 5m订单流数据
period = "orderflow5m"
data2 = xtdata.get_market_data_ex([],["000001.SZ"],period=period)["000001.SZ"]

# 获取000001.SZ 1d订单流数据
period = "orderflow1d"
data3 = xtdata.get_market_data_ex([],["000001.SZ"],period=period)["000001.SZ"]

# 订阅实时000001.SZ 1m订单流数据
period = "orderflow1m"

# 进行数据订阅
xtdata.subscribe_quote("000001.SZ", period = period)
# 获取订阅后的实时数据
data4 = xtdata.get_market_data_ex([],["000001.SZ"],period=period)["000001.SZ"]

print(data1)
print(data2)
print(data3)

print(data4)

 data1返回值

	time	price	buyNum	sellNum
20230324093000	1679621400000	[12.85]	[4230]	[0]
20230324093100	1679621460000	[12.790000000000001, 12.8, 12.81, 12.82, 12.83...	[888, 453, 769, 2536, 0, 1854, 1722]	[837, 3372, 1525, 6121, 575, 3324, 0]
20230324093200	1679621520000	[12.77, 12.780000000000001, 12.790000000000001...	[0, 3267, 5211, 318]	[1843, 1505, 3051, 197]
20230324093300	1679621580000	[12.780000000000001, 12.790000000000001, 12.8]	[0, 5552, 107]	[3990, 1539, 0]
20230324093400	1679621640000	[12.8, 12.81]	[889, 1728]	[852, 1611]
...	...	...	...	...
20231026134900	1698299340000	[10.36, 10.370000000000001, 10.38]	[0, 255, 353]	[15, 140, 0]
20231026135000	1698299400000	[10.370000000000001, 10.38]	[0, 596]	[3106, 0]
20231026135100	1698299460000	[10.370000000000001, 10.38]	[0, 608]	[175, 0]
20231026135200	1698299520000	[10.370000000000001, 10.38]	[0, 944]	[667, 0]
20231026135300	1698299580000	[10.370000000000001, 10.38]	[0, 160]	[106, 0]
34396 rows × 4 columns

data2返回值 

	time	price	buyNum	sellNum
20230324093500	1679621700000	[12.77, 12.780000000000001, 12.790000000000001...	[0, 3267, 11651, 1767, 4135, 3092, 0, 1854, 5952]	[1843, 5495, 5427, 4580, 4744, 6121, 575, 3324...
20230324094000	1679622000000	[12.81, 12.82, 12.83, 12.84, 12.85, 12.86]	[3515, 603, 4610, 5587, 3346, 158]	[3358, 2884, 4953, 1099, 61, 0]
20230324094500	1679622300000	[12.790000000000001, 12.8, 12.81, 12.82, 12.83...	[0, 322, 3573, 526, 604, 935, 1270]	[964, 11150, 2242, 4940, 1407, 517, 0]
20230324095000	1679622600000	[12.77, 12.780000000000001, 12.790000000000001...	[935, 11904, 119, 754, 2892]	[6065, 6067, 4771, 5898, 0]
20230324095500	1679622900000	[12.780000000000001, 12.790000000000001, 12.8,...	[300, 1229, 6217, 197]	[739, 4098, 858, 0]
...	...	...	...	...
20231026110500	1698289500000	[10.32, 10.33, 10.34]	[0, 1318, 264]	[3, 9260, 0]
20231026111000	1698289800000	[10.33, 10.34]	[0, 1880]	[4062, 0]
20231026111500	1698290100000	[10.33, 10.34]	[0, 1965]	[1729, 0]
20231026112000	1698290400000	[10.33, 10.34, 10.35, 10.36]	[0, 1414, 5373, 257]	[1309, 2367, 775, 0]
20231026112500	1698290700000	[10.33, 10.34, 10.35]	[0, 1077, 258]	[487, 499, 0]
6839 rows × 4 columns

data3返回值

	time	price	buyNum	sellNum
20230324000000	1679587200000	[12.77, 12.780000000000001, 12.790000000000001...	[935, 17170, 22882, 27895, 62600, 53273, 39324...	[8938, 27896, 31737, 80764, 68784, 68695, 2731...
20230327000000	1679846400000	[12.47, 12.48, 12.49, 12.5, 12.51, 12.52, 12.5...	[0, 8792, 4885, 4997, 50228, 57248, 31828, 348...	[915, 24135, 25945, 30326, 82575, 40025, 32308...
20230328000000	1679932800000	[12.55, 12.56, 12.57, 12.58, 12.59, 12.6, 12.6...	[0, 2411, 2096, 8403, 17269, 13652, 30554, 201...	[2002, 5320, 11049, 10937, 16325, 26177, 26658...
20230329000000	1680019200000	[12.52, 12.530000000000001, 12.540000000000001...	[0, 5689, 49134, 29969, 16598, 15290, 23969, 1...	[16122, 54360, 33434, 13624, 30877, 22648, 264...
20230330000000	1680105600000	[12.41, 12.42, 12.43, 12.44, 12.45000000000000...	[0, 19093, 24669, 16814, 9488, 7165, 9891, 109...	[7093, 37216, 34430, 13969, 12035, 11947, 1369...
...	...	...	...	...
20231020000000	1697731200000	[10.52, 10.53, 10.540000000000001, 10.55, 10.5...	[419, 13251, 17713, 12059, 6547, 14152, 17650,...	[5527, 2180, 5684, 4222, 8746, 20424, 22532, 4...
20231023000000	1697990400000	[10.43, 10.44, 10.450000000000001, 10.46, 10.4...	[0, 11496, 18358, 23063, 24492, 14307, 7609, 2...	[11067, 15592, 21853, 16322, 26661, 14717, 256...
20231024000000	1698076800000	[10.44, 10.450000000000001, 10.46, 10.47, 10.4...	[0, 7838, 11767, 11598, 10783, 8160, 7532, 223...	[6030, 15551, 17457, 7944, 12948, 3154, 17360,...
20231025000000	1698163200000	[10.36, 10.370000000000001, 10.38, 10.39, 10.4...	[0, 30043, 48101, 93420, 77355, 58783, 34336, ...	[15876, 59255, 135796, 82676, 96175, 51600, 32...
20231026000000	1698249600000	[10.31, 10.32, 10.33, 10.34, 10.35, 10.36, 10....	[2314, 3430, 13070, 30194, 45518, 29091, 40124...	[16564, 3579, 42438, 42624, 26508, 26492, 1297...
143 rows × 4 columns

data4返回值

	time	price	buyNum	sellNum
20230324093000	1679621400000	[12.85]	[4230]	[0]
20230324093100	1679621460000	[12.790000000000001, 12.8, 12.81, 12.82, 12.83...	[888, 453, 769, 2536, 0, 1854, 1722]	[837, 3372, 1525, 6121, 575, 3324, 0]
20230324093200	1679621520000	[12.77, 12.780000000000001, 12.790000000000001...	[0, 3267, 5211, 318]	[1843, 1505, 3051, 197]
20230324093300	1679621580000	[12.780000000000001, 12.790000000000001, 12.8]	[0, 5552, 107]	[3990, 1539, 0]
20230324093400	1679621640000	[12.8, 12.81]	[889, 1728]	[852, 1611]
...	...	...	...	...
20231026134100	1698298860000	[10.36, 10.370000000000001]	[0, 11]	[44, 0]
20231026134200	1698298920000	[10.36, 10.370000000000001]	[0, 206]	[86, 0]
20231026134300	1698298980000	[10.36, 10.370000000000001]	[0, 0]	[78, 0]
20231026134400	1698299040000	[10.36, 10.370000000000001]	[0, 33]	[291, 0]
20231026134500	1698299100000	[10.36]	[0]	[14]
;