行情示例
# 行情示例
# 1.获取LV1行情数据
本示例用于说明如何通过函数获取行情数据
#coding:gbk
# get_market_data_ex(subscribe=True)有订阅股票数量限制
# 即stock_list参数的数量不能超过500
# get_market_data_ex(subscribe=False) 该模式下(非订阅模式),接口会从本地行情文件里获取数据,不会获取动态行情数,且不受订阅数限制,但需要提前下载数据
# 下载数据在 操作/数据管理/补充数据选项卡里,按照页面提示下载数据
# get_market_data_ex(subscribe=True) 该模式下(订阅模式),受订阅数量上限限制,可以取到动态行情
# 建议每天盘后增量补充对应周期的行情
import time
def init(C):
C.stock = C.stockcode + '.' + C.market
# 获取指定时间的k线
price = C.get_market_data_ex(['open','high','low','close'], [C.stock], start_time='', end_time='',period='1d', subscribe=False)
print(price[C.stock].head())
def handlebar(C):
bar_timetag = C.get_bar_timetag(C.barpos)
bar_date = timetag_to_datetime(bar_timetag, '%Y%m%d%H%M%S')
print('获取截至到%s为止前5根k线的开高低收等字段:'%(bar_date))
# 获取截至今天为止前30根k线
price = C.get_market_data_ex(
[], [C.stock],
end_time=bar_date,
period=C.period,
subscribe=True,
count=5,
)
print(price[C.stock].to_dict('dict'))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 2.获取LV2数据(需要数据源支持)
方法1:查询LV2数据,使用该函数后,会定期查询最新数据,并进行数据返回。
#coding:gbk
def init(C):
C.sub_nums = []
C.stock = C.stockcode+'.'+C.market
for field in ['l2transaction', 'l2order', 'l2transactioncount', 'l2quote']:
num = C.subscribe_quote(C.stock, period=field,
dividend_type='follow',
)
C.sub_nums.append(num)
def handlebar(C):
if not C.is_last_bar():
return
price = C.get_market_data_ex([],[C.stock],period='l2transaction',count=10)[C.stock]
price_dict = price.to_dict('index')
print(price_dict)
for pos, t in enumerate(price_dict):
print(f" 逐笔成交:{pos+1} 时间:{price_dict[t]['stime']}, 时间戳:{price_dict[t]['time']}, 成交价:{price_dict[t]['price']}, \
成交量:{price_dict[t]['volume']}, 成交额:{price_dict[t]['amount']} \
成交记录号:{price_dict[t]['tradeIndex']}, 买方委托号:{price_dict[t]['buyNo']},\
卖方委托号:{price_dict[t]['sellNo']}, 成交类型:{price_dict[t]['tradeType']}, \
成交标志:{price_dict[t]['tradeFlag']}, ")
price = C.get_market_data_ex([],[C.stock],period='l2quote',count=10)[C.stock]
price_dict = price.to_dict('index')
print(price_dict)
for pos, t in enumerate(price_dict):
print(f" 十档快照:{pos+1} 时间:{price_dict[t]['stime']}, 时间戳:{price_dict[t]['time']}, 最新价:{price_dict[t]['lastPrice']}, \
开盘价:{price_dict[t]['open']}, 最高价:{price_dict[t]['high']} 最低价:{price_dict[t]['low']}, 成交额:{price_dict[t]['amount']},\
成交总量:{price_dict[t]['volume']}, 原始成交总量:{price_dict[t]['pvolume']}, 证券状态:{price_dict[t]['stockStatus']}, 持仓量:{price_dict[t]['openInt']},\
成交笔数:{price_dict[t]['transactionNum']},前收盘价:{price_dict[t]['lastClose']},多档委卖价:{price_dict[t]['askPrice']},多档委卖量:{price_dict[t]['askVol']},\
多档委买价:{price_dict[t]['bidPrice']},多档委买量:{price_dict[t]['bidVol']}")
price = C.get_market_data_ex([],[C.stock],period='l2order',count=10)[C.stock]
price_dict = price.to_dict('index')
for pos, t in enumerate(price_dict):
print(f" 逐笔委托:{pos+1} 时间:{price_dict[t]['stime']}, 时间戳:{price_dict[t]['time']}, 委托价:{price_dict[t]['price']}, \
委托量:{price_dict[t]['volume']}, 委托号:{price_dict[t]['entrustNo']} \
委托类型:{price_dict[t]['entrustType']}, 委托方向:{price_dict[t]['entrustDirection']},\
")
# 委托类型: 0:未知 1: 买入,2: 卖出,3: 撤单
price = C.get_market_data_ex([],[C.stock],period='l2transactioncount',count=10)[C.stock]
price_dict = price.to_dict('index')
for pos, t in enumerate(price_dict):
print('大单统计:', price_dict[t])
def stop(C):
for num in C.sub_nums:
C.unsubscribe_quote(num)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
方法2:订阅LV2数据,此方法在发起订阅后,会自动收到所订阅数据,订阅方需要记录订阅函数返回的订阅号,并在不需要订阅时调用unsubscribe_quote反订阅数据,释放资源。
#coding:gbk
def l2_quote_callback(data):
for s in data:
print('逐笔快照:',s, data[s])
def l2transaction_callback(data):
for s in data:
print('逐笔成交',s, data[s])
def l2order_callback(data):
for s in data:
print('逐笔委托',s, data[s])
def l2quoteaux_callback(data):
for s in data:
print('行情快照补充',s, data[s])
def l2transactioncount_callback(data):
for s in data:
print('大单统计',s, data[s])
def l2orderqueue_callback(data):
for s in data:
print('委买委卖队列',s, data[s])
def init(C):
C.stock = C.stockcode + '.' + C.market
# Level2 逐笔快照
C.subscribe_quote(C.stock, 'l2quote', result_type='dict', callback=l2_quote_callback)
# Level2 行情快照补充
C.subscribe_quote(C.stock, 'l2quoteaux', result_type='dict', callback=l2quoteaux_callback)
# Level2 逐笔成交
C.subscribe_quote(C.stock, 'l2transaction', result_type='dict', callback=l2transaction_callback)
# Level2 逐笔委托
C.subscribe_quote(C.stock, 'l2order', result_type='dict', callback=l2order_callback)
# Level2大单统计
C.subscribe_quote(C.stock, 'l2transactioncount', result_type='dict', callback=l2transactioncount_callback)
# Level2委买委卖队列
C.subscribe_quote(C.stock, 'l2orderqueue', result_type='dict', callback=l2orderqueue_callback)
def handlebar(C):
return
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 3.使用level1全推数据计算全市场涨幅
#coding:gbk
import time
class a():
pass
A = a()
def init(C):
A.hsa = C.get_stock_list_in_sector('沪深A股')
A.vol_dict = {}
for stock in A.hsa:
A.vol_dict[stock] = C.get_last_volume(stock)
C.run_time("f","1nSecond","2019-10-14 13:20:00")
def f(C):
t0 = time.time()
full_tick = C.get_full_tick(A.hsa)
total_market_value = 0
total_ratio = 0
count = 0
for stock in A.hsa:
ratio = full_tick[stock]['lastPrice'] / full_tick[stock]['lastClose'] - 1
rise_price = round(full_tick[stock]['lastClose'] *1.2,2) if stock[0] == '3' or stock[:3] == '688' else round(full_tick[stock]['lastClose'] *1.1,2)
#如果要打印涨停品种
#if abs(full_tick[stock]['lastPrice'] - rise_price) <0.01:
# print(f"涨停品种 {stock} {C.get_stock_name(stock)}")
market_value = full_tick[stock]['lastPrice'] * A.vol_dict[stock]
total_ratio += ratio * market_value
total_market_value += market_value
count += 1
#print(count)
total_ratio /= total_market_value
total_ratio *= 100
print(f'A股加权涨幅 {round(total_ratio,2)}% 函数运行耗时{round(time.time()- t0,5)}秒')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 4.在行情回调函数里处理动态行情
ContextInfo.subscribe_quote函数说明
行情回调函数字段说明
#coding:gbk
sub_nums = []
def init(C):
global sub_nums
# def on_quote(data1, data2): # 错误写法
def on_quote(data):
for s in data:
q = data[s]
print(type(q), q)
stocks = [C.stockcode + '.' + C.market] # 获取到当前主图股票代码
for s in stocks:
num = C.subscribe_quote(s, period='1d',
dividend_type='none',
result_type='dict', # 回调函数的行情数据格式
callback=on_quote # 指定一个自定义的函数接收行情,自定义的函数只能有一个位置参数
)
sub_nums.append(num)
def stop(C):
# 反订阅
for num in sub_nums:
C.unsubscribe_quote(num)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
上次更新: 2023/10/12, 10:46:32