如何选出符合一定条件的股票?

股票筛选
标签: #<Tag:0x00007f61dec123b0>

(njchenxin) #1

想做一个股票筛选器,筛选出满足连续3年每一年的最高价 小于 上一年开盘价的股票,求指导!


(小米) #2
克隆策略
In [1]:
# 获取15年到最近的数据
df=D.history_data(D.instruments(start_date='2015-01-01'), fields=['open', 'high', 'low', 'close'])
df.set_index('date', inplace=True)
In [6]:
# 将日K线转化成年K线的函数
def resample(df):
    # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
    # 周 W、月 M、季度 Q、10天 10D、2周 2W
    period = 'A' # 年k线

    year_df = df.resample(period, how='last')
    year_df['open'] = df['open'].resample(period, how='first')
    year_df['high'] = df['high'].resample(period, how='max')
    year_df['low'] = df['low'].resample(period, how='min')
    year_df['close'] = df['close'].resample(period, how='last')
  
    # 去除空的数据
    year_df = year_df[year_df.instrument.notnull()]
    year_df.reset_index(inplace=True)
    return year_df

year_df = df.groupby('instrument').apply(resample)
year_df.reset_index(drop=True, inplace=True)
In [27]:
# 筛选连续三年最高价小于上一年开盘价的股票
def seek_stock(df):
    df['pre_open'] = df['open'].shift()
    df['condi_0'] = df['high'] < df['pre_open']
    df['condi_1'] = df['condi_0'].shift(1)
    df['condi_2'] = df['condi_0'].shift(2)
    return df[(df['condi_0'] == True) & (df['condi_1'] == True) & (df['condi_2'] == True)]
In [29]:
stock_seeked = year_df.groupby('instrument').apply(seek_stock)
stock_seeked.reset_index(drop=True, inplace=True)
In [32]:
stock_seeked
Out[32]:
date instrument open high low close pre_open condi_0 condi_1 condi_2
0 2012-12-31 000638.SZA 13.327325 17.902376 11.603393 16.421562 19.007462 True True True
1 2014-12-31 000655.SZA 42.468651 57.274460 35.597065 51.158325 70.953659 True True True
2 2013-12-31 000717.SZA 10.469631 10.649367 7.414116 8.177996 12.222059 True True True
3 2014-12-31 000780.SZA 19.943371 28.495905 15.814127 26.998438 42.444012 True True True
4 2014-12-31 000792.SZA 81.469856 119.867195 69.066986 106.038246 129.878174 True True True
5 2013-12-31 000898.SZA 8.511791 8.653655 5.066543 6.505441 9.302172 True True True
6 2013-12-31 000932.SZA 3.367370 3.497440 2.196739 2.803733 4.003268 True True True
7 2014-12-31 000933.SZA 57.544319 78.373924 40.028397 72.003036 90.797974 True True True
8 2013-12-31 000987.SZA 27.621641 29.314201 19.808920 23.529251 36.384460 True True True
9 2013-12-31 002054.SZA 15.300266 19.089584 13.303122 16.653179 22.057257 True True True
10 2014-12-31 002109.SZA 12.400472 17.606215 11.197258 14.659568 18.263977 True True True
11 2013-12-31 002110.SZA 5.355849 6.483951 4.670553 4.860328 7.516285 True True True
12 2014-12-31 002154.SZA 33.285629 51.137436 29.778950 45.299572 54.007309 True True True
13 2013-12-31 002274.SZA 8.714908 10.216104 7.479410 9.326014 11.078819 True True True
14 2013-12-31 002295.SZA 9.528625 10.412882 7.622900 9.040759 11.891724 True True True
15 2012-12-31 002314.SZA 10.324490 12.162250 6.124815 7.389837 17.485935 True True True
16 2013-12-31 002314.SZA 7.432004 8.855154 6.757326 7.200084 10.324490 True True True
17 2012-12-31 002316.SZA 16.704624 24.339167 14.852630 19.974140 25.803013 True True True
18 2012-12-31 002319.SZA 9.841306 13.198796 7.888819 12.132739 22.077015 True True True
19 2013-12-31 002336.SZA 9.761864 11.750992 8.324070 10.075367 15.856431 True True True
20 2013-12-31 002343.SZA 17.515585 20.403233 13.823819 17.466286 27.019287 True True True
21 2017-12-31 002353.SZA 162.601120 193.997192 133.051880 136.424347 203.630432 True True True
22 2013-12-31 002365.SZA 19.582220 23.457001 15.563728 19.034163 23.587708 True True True
23 2013-12-31 002387.SZA 21.313618 25.326147 15.705708 18.690502 27.319319 True True True
24 2013-12-31 002399.SZA 39.579422 49.368702 33.943180 43.955467 51.161850 True True True
25 2013-12-31 002472.SZA 18.702982 21.405865 14.769220 17.080713 21.929464 True True True
26 2013-12-31 002504.SZA 9.391103 11.708255 7.665998 10.864033 13.290639 True True True
27 2014-12-31 002506.SZA 7.553264 8.562535 6.218420 6.218420 16.636713 True True True
28 2013-12-31 300138.SZA 19.001459 22.951656 15.162008 18.358324 23.946815 True True True
29 2013-12-31 300139.SZA 52.513149 60.186165 41.382645 54.341854 61.354103 True True True
... ... ... ... ... ... ... ... ... ... ...
38 2013-12-31 600320.SHA 36.485737 47.827347 28.675009 37.876690 50.502254 True True True
39 2014-12-31 600467.SHA 24.653645 30.190546 22.183868 28.320335 32.004143 True True True
40 2014-12-31 600508.SHA 30.108395 37.129601 23.209831 34.523476 48.600182 True True True
41 2014-12-31 600546.SHA 39.248051 50.884918 26.698267 45.942276 80.109467 True True True
42 2013-12-31 600550.SHA 50.547791 59.786888 39.018009 39.705215 85.595276 True True True
43 2013-12-31 600569.SHA 5.696411 5.771364 3.797607 4.322277 6.695782 True True True
44 2013-12-31 600581.SHA 19.806049 20.913626 12.776519 13.634890 23.438372 True True True
45 2013-12-31 600808.SHA 4.065070 4.175434 2.759097 3.090189 4.616889 True True True
46 2012-12-31 600854.SHA 21.205729 32.934036 19.606415 23.101213 39.568233 True True True
47 2013-12-31 600858.SHA 62.386604 70.332687 43.897453 50.130623 71.599403 True True True
48 2011-12-31 600900.SHA 16.535328 18.341160 13.528242 14.339936 18.870081 True True True
49 2012-12-31 600900.SHA 14.339936 16.244909 13.911542 16.081055 16.535328 True True True
50 2013-12-31 600966.SHA 16.003487 17.961720 14.214067 16.543688 18.130533 True True True
51 2012-12-31 600999.SHA 13.798978 18.790962 11.028613 14.471625 19.447906 True True True
52 2013-12-31 601001.SHA 20.078804 24.009123 11.662795 12.364703 25.722437 True True True
53 2013-12-31 601003.SHA 12.198891 12.820490 7.692294 8.080793 13.480940 True True True
54 2012-12-31 601186.SHA 3.979717 6.546099 3.833096 6.278693 7.037933 True True True
55 2014-12-31 601268.SHA 2.550691 2.641070 2.149007 2.359891 5.121466 True True True
56 2014-12-31 601558.SHA 17.092915 19.546642 11.603219 19.360552 22.000370 True True True
57 2013-12-31 601600.SHA 5.311465 5.474581 3.068620 3.466215 6.596003 True True True
58 2012-12-31 601618.SHA 2.679521 2.933263 1.999491 2.293832 3.930000 True True True
59 2013-12-31 601618.SHA 2.364880 2.395329 1.542754 1.776198 2.679521 True True True
60 2013-12-31 601666.SHA 20.900076 23.262484 11.924231 12.958978 25.352551 True True True
61 2014-12-31 601666.SHA 12.909704 16.086918 9.189541 15.156894 20.900076 True True True
62 2014-12-31 601717.SHA 13.377225 18.981543 9.931252 16.879856 21.362225 True True True
63 2013-12-31 601718.SHA 3.064660 3.418664 2.326310 2.835993 3.426757 True True True
64 2013-12-31 601727.SHA 4.300395 4.688480 3.282985 3.951404 5.303279 True True True
65 2013-12-31 601898.SHA 8.507450 9.109748 5.160347 5.293517 9.531440 True True True
66 2014-12-31 601898.SHA 5.315712 8.351348 4.401998 7.830803 8.507450 True True True
67 2013-12-31 601919.SHA 4.692969 4.859830 2.794924 3.441511 4.901546 True True True

68 rows × 10 columns