给定一些k线选股指标,如何挑选符合条件的公司,以python + windquant为例?
【申明:本例只用来作为python学习交流之用,切勿以此作为投资的选股条件】
0、用以下条件挑选公司:
仅作示例用:
【1】本周1 相对于上周五:close上跳,跳空
【2】周2,周3两天,跳空都没有补起来【没有回调——close周2,close周3 > high上周五】
【3】close周2, close周3 > ma20
【4】close周2 > close周1 or close周3 > close周1
1、连接服务器
from WindPy import w
w.start()
w.isconnected()
print(w.isconnected())
2、获取全部A股的股票代码
all_stocks = w.wset("sectorconstituent", "date=2022-05-18;sector=全部A股",usedf=True)
all_stocks_df = all_stocks[1]
all_stocks_df.head(10)
3、在已经过去的日期系列里面,找到离【今天】最近的【周一、周二、周三】日期组,并输出日期。
from datetime import date, timedelta
'''
给定一个指定的日期,从一个日期系列里面,找到此前最近的[周一,周二,周三]组成的系列,输出日期系列
测试代码:
today = date(2023,5,17)
monday, tuesday, wednesday = find_weekday_dates(today)
print(monday, tuesday, wednesday)
'''
def find_weekday_dates(today):
# 计算今天是星期几(0代表星期一,6代表星期日)
today_weekday = today.weekday()
print(today_weekday)
if(today_weekday<=4): #周3
today_weekday += 7
# 计算距离最近的周一、周二、周三的日期
monday = today - timedelta(days=today_weekday)
tuesday = monday + timedelta(days=1)
wednesday = monday + timedelta(days=2)
return monday, tuesday, wednesday
测试代码:
给定的【今天】是【2023-05-17】找到最近的【周一,周二,周三】
today = date(2023,5,17)
monday, tuesday, wednesday = find_weekday_dates(today)
print(monday, tuesday, wednesday)
2023-05-08 2023-05-09 2023-05-10
计算上个周五的日期
from datetime import datetime, timedelta
# 计算前3天的日期——上个周五的日期
last_friday = monday - timedelta(days=3)
last_friday
4、提取ohlc数据,计算指标,判断条件
计算起始日期和结束日期(today)
today = date.today()
last_month_today = today - timedelta(days = 40)
last_month_today
提取40个日历日的数据
df = w.wsd("000001.SZ", "close,open,low,high", last_month_today, today, "", usedf=True)[1]
df
5、计算ma20的值
# 计算 MA20 数据
ma20 = df["CLOSE"].rolling(window=20).mean()
# 将 MA20 数据添加到 DataFrame 中
df["MA20"] = ma20
df
6、条件判断
【1】本周1 相对于上周五:close上跳,跳空
【2】周2,周3两天,跳空都没有补起来【没有回调——close周2,close周3 > high上周五】
【3】close周2, close周3 > ma20
【4】close周2 > close周1 or close周3 > close周1
#monday, tuesday, wednesday last_friday
cond1 = df.loc[monday].CLOSE > df.loc[last_friday].HIGH
cond2 = (df.loc[tuesday].CLOSE > df.loc[last_friday].HIGH) and (df.loc[wednesday].CLOSE > df.loc[last_friday].HIGH)
cond3 = (df.loc[tuesday].CLOSE > df.loc[tuesday].MA20) and (df.loc[wednesday].CLOSE > df.loc[wednesday].MA20)
cond4 = (df.loc[tuesday].CLOSE > df.loc[monday].CLOSE) and (df.loc[wednesday].CLOSE > df.loc[monday].CLOSE)
print(cond1,cond2,cond3,cond4)
7、合并功能:【下载k线】【计算指标】【判断条件】
"""
功能:获取k线数据,并计算指标,然后用指标来判断选股条件,并返回计算结果
参数:
stockID:股票代码
last_month_today:获取k线时的起始日期
today:获取k线时的结束日期
返回:[stockID,cond1,cond2,cond3,cond4]
cond1:本周1 相对于上周五:close上跳,跳空
cond2:周2,周3两天,跳空都没有补起来【没有回调——close周2,close周3 > high上周五】
cond3:close周2, close周3 > ma20
cond4:close周2 > close周1 or close周3 > close周1
案例:
res = calculate_condition("000001.SZ",last_month_today,today)
res
"""
def calculate_condition(stockID,last_month_today,today):
df = w.wsd(stockID, "close,open,low,high", last_month_today, today, "", usedf=True)[1]
# 计算 MA20 数据
ma20 = df["CLOSE"].rolling(window=20).mean()
df["MA20"] = ma20
#计算选股条件是否满足
#monday, tuesday, wednesday last_friday
cond1 = df.loc[monday].CLOSE > df.loc[last_friday].HIGH
cond2 = (df.loc[tuesday].CLOSE > df.loc[last_friday].HIGH) and (df.loc[wednesday].CLOSE > df.loc[last_friday].HIGH)
cond3 = (df.loc[tuesday].CLOSE > df.loc[tuesday].MA20) and (df.loc[wednesday].CLOSE > df.loc[wednesday].MA20)
cond4 = (df.loc[tuesday].CLOSE > df.loc[monday].CLOSE) and (df.loc[wednesday].CLOSE > df.loc[monday].CLOSE)
#print(cond1,cond2,cond3,cond4)
return[stockID,cond1,cond2,cond3,cond4]
8、计算股池的股票,挑选公司
import pandas as pd
res_df = pd.DataFrame(columns = ['stockID','cond1','cond2','cond3','cond4'])
i= 0
for stockid in all_stock_code.values:
i+=1
print(i)
#计算指标和条件
rtn = calculate_condition(stockid,last_month_today,today)
new_row_data = {'stockID': rtn[0], 'cond1': rtn[1], 'cond2': rtn[2], 'cond3': rtn[3], 'cond4': rtn[4]}
res_df = res_df.append(new_row_data,ignore_index = True)
print("计算结束")