用聚宽获取板块涨幅前10名的板块股票,并且这些板块内个股数量超过10个,然后根据开盘换手率对板块内的个股排序,去除st股票、去除开盘小于10天的股票、去除股票代码包含688的股票,去除北京证交所的股票,并且开盘的涨幅要小于5%且大于9%,选择开盘换手率在所属板块排名第1,并且开盘换手率大于0.3的股票,并输出结果
时间: 2023-09-06 07:12:16 浏览: 121
以下是用聚宽获取满足条件的股票代码的代码:
```python
import jqdatasdk as jq
# 连接聚宽
auth('账号', '密码')
# 获取板块涨幅前10名的板块
q = query(valuation.code, valuation.turnover_ratio, valuation.market_cap, valuation.circulating_market_cap)\
.filter(valuation.market_cap > 0, valuation.circulating_market_cap > 0)\
.order_by(valuation.market_cap.desc()).limit(100)
df = get_fundamentals(q)['code']
bks = ['BK%s' % i for i in range(1, 11)]
# 获取所选板块内个股数量超过10个的股票
stocks = []
for bk in bks:
q = query(index_stock.code).filter(index_stock.index_code == bk)
df = get_fundamentals(q)
if len(df) > 10:
stocks.extend(df['code'].tolist())
# 获取开盘涨幅小于5%且大于9%的股票
stocks = [s for s in stocks if (
get_price(s, start_date='2022-01-10', end_date='2022-01-10', frequency='daily', fields='open')[
s][0] / get_price(s, start_date='2022-01-07', end_date='2022-01-07', frequency='daily', fields='close')[
s][0] - 1) * 100 > 9 and (
get_price(s, start_date='2022-01-10', end_date='2022-01-10', frequency='daily', fields='open')[
s][0] / get_price(s, start_date='2022-01-07', end_date='2022-01-07', frequency='daily',
fields='close')[s][0] - 1) * 100 < 5]
# 去除st股票、去除开盘小于10天的股票、去除股票代码包含688的股票、去除北京证交所的股票
stocks = [s for s in stocks if not s.startswith('ST') and (
get_security_info(s).start_date - datetime.date.today()).days >= -10 and '688' not in s and get_security_info(
s).exchange not in ['XSHE', 'XSHG']]
# 根据开盘换手率对板块内的个股排序,选择开盘换手率在所属板块排名第1,并且开盘换手率大于0.3的股票
res = {}
for bk in bks:
q = query(index_stock.code).filter(index_stock.index_code == bk)
df = get_fundamentals(q)
if len(df) <= 10:
continue
codes = df['code'].tolist()
codes = [s for s in codes if s in stocks]
if len(codes) <= 10:
continue
codes.sort(key=lambda x: get_price(x, start_date='2022-01-10', end_date='2022-01-10', frequency='daily',
fields='turnover_ratio')[x][0], reverse=True)
res[bk] = []
for code in codes:
if get_price(code, start_date='2022-01-10', end_date='2022-01-10', frequency='daily', fields='turnover_ratio')[
code][0] > 0.3 and codes.index(code) < 10:
res[bk].append(code)
# 输出结果
for bk in res:
print(bk)
for code in res[bk]:
print(code)
```
注意:在使用该代码前需要先安装聚宽的Python SDK,并且需要有聚宽账号和密码。
阅读全文