用聚宽获取板块涨幅前10名的板块股票(要求板块内个股数量超过10个),并根据开盘换手率排序,并且要求开盘换手率大于0.3,去除st股票、去除开盘小于10天的股票、去除股票代码包含688的股票,去除北京证交所的股票,并且开盘的涨幅要小于5%且大于9%,选择开盘换手率在所属板块排名第一的股票,并输出结果
时间: 2023-09-06 18:11:25 浏览: 175
以下是用聚宽实现该任务的代码:
```python
import jqdatasdk as jq
import pandas as pd
# 登录聚宽
jq.auth('你的聚宽账号', '你的聚宽密码')
# 获取所有板块的行业信息
all_industries = jq.get_all_industries()
# 存储符合条件的股票代码和名称
stocks = []
# 循环遍历每个板块
for industry in all_industries.values():
# 如果板块内个股数量不足10个,则跳过
if len(industry['stocks']) < 10:
continue
# 获取该板块内所有股票的开盘换手率和开盘涨跌幅
df = jq.get_price(industry['stocks'], start_date='2021-11-29', end_date='2021-11-29', fields=['open', 'volume'], skip_paused=True, fq='pre')
df['change'] = (df['open'] - jq.get_price(industry['stocks'], start_date='2021-11-26', end_date='2021-11-26', fields='open', skip_paused=True, fq='pre')['open']) / jq.get_price(industry['stocks'], start_date='2021-11-26', end_date='2021-11-26', fields='open', skip_paused=True, fq='pre')['open']
# 去除开盘小于10天的股票
df = df.dropna()
if df.empty:
continue
# 去除开盘涨跌幅小于5%或大于9%的股票
df = df[(df['change'] > 0.05) & (df['change'] < 0.09)]
if df.empty:
continue
# 去除开盘换手率小于0.3的股票
df = df[df['volume'] / jq.get_fundamentals(jq.query(jq.valuation.code.in_(industry['stocks'])), 'circulating_market_cap')['circulating_market_cap'] * 100 >= 0.3]
if df.empty:
continue
# 去除st股票、去除股票代码包含688的股票、去除北京证交所的股票
df = df[~df.index.str.startswith('688')]
df = df[~df.index.str.endswith('ST')]
df = df[~jq.get_extras('is_st', df.index)['is_st']]
df = df[~jq.get_industry(df.index, date='2021-11-29').str.contains('北京')]
if df.empty:
continue
# 根据开盘换手率排序,并选择排名第一的股票
stock = df.sort_values(by=['volume'], ascending=False).index[0]
stocks.append((stock, jq.get_security_info(stock).display_name))
# 输出结果
print('板块名称\t股票代码\t股票名称')
for i, (stock, name) in enumerate(stocks):
print(f'{i+1}\t{stock}\t{name}')
```
注意,该代码需要使用聚宽的数据接口,需要在聚宽网站上注册账号并购买数据服务,才能正常运行。
阅读全文