SuperMind首板选股
import datetime
import pandas as pd
from supermind.api import *
def 首板选股():
# 参数设置
历史天数 = 20
成交量倍数 = 2.0
当前日期 = datetime.datetime.now().strftime('%Y%m%d')
# 获取全A股列表(排除北交所)
stock_list = get_stock_list(exchange=['SH', 'SZ'], list_status='L')
# 存储结果的DataFrame
result_df = pd.DataFrame(columns=['代码','名称','当日涨停','成交量放大','历史无涨停','非ST','未停牌'])
for code in stock_list.code:
try:
# === 基础数据获取 ===
# 获取K线数据(前复权)
kline = get_kline(
code=code,
start_date=date_offset(当前日期, -历史天数*2), # 多取数据用于计算均线
end_date=当前日期,
fields=['close','high','low','volume','paused','is_st']
)
# === 停牌/ST过滤 ===
if kline.iloc[-1]['paused'] or kline.iloc[-1]['is_st']:
continue # 跳过停牌股和ST股
# === 涨停判断 ===
当日收盘价 = kline.iloc[-1]['close']
昨日收盘价 = kline.iloc[-2]['close']
# 动态计算涨停价(兼容不同板块)
if code.startswith('688') or code.startswith('300'):
涨停幅度 = 0.20 # 科创板和创业板
else:
涨停幅度 = 0.10 # 主板
涨停价 = round(昨日收盘价 * (1 + 涨停幅度), 2)
当日涨停 = (当日收盘价 >= 涨停价) # 包含涨停但未封死的情况
# === 历史涨停检查 ===
# 生成历史涨停序列(排除今日)
hist_zt = [1 if close >= round(kline.iloc[i-1]['close']*(1+涨停幅度),2) else 0
for i, close in enumerate(kline.close)][1:-1] # 排除今日
# 检查最近N天是否有涨停
历史涨停次数 = sum(hist_zt[-历史天数:])
符合历史条件 = (历史涨停次数 == 0) and 当日涨停
# === 成交量筛选 ===
volume_5ma = kline.volume.rolling(5).mean().iloc[-2] # 前5日均量
当日成交量 = kline.iloc[-1]['volume']
成交量达标 = (当日成交量 > volume_5ma * 成交量倍数)
# === 结果记录 ===
if 符合历史条件 and 成交量达标:
result_df = result_df.append({
'代码': code,
'名称': get_stock_name(code),
'当日涨停': 当日涨停,
'成交量放大': f"{当日成交量/volume_5ma:.1f}倍",
'历史无涨停': 历史涨停次数 == 0,
'非ST': not kline.iloc[-1]['is_st'],
'未停牌': not kline.iloc[-1]['paused']
}, ignore_index=True)
except Exception as e:
print(f"处理{code}时出错: {str(e)}")
continue
# 结果排序与保存
result_df = result_df.sort_values(by='成交量放大', ascending=False)
result_df.to_csv(f'首板选股_{当前日期}.csv', index=False)
return result_df
if __name__ == '__main__':
selected = 首板选股()
print(f"选股结果:\n{selected}")