zoukankan      html  css  js  c++  java
  • [Python数据分析]新股破板买入,赚钱几率如何?

    这是本人一直比较好奇的问题,网上没搜到,最近在看python数据分析,正好自己动手做一下试试。作者对于python是零基础,需要从头学起。

    在写本文时,作者也没有完成这个小分析目标,边学边做吧。

    ================================================================

    Python基础:

    中国大学Mooc,南京大学,张莉老师 -《用Python玩转数据》

    了解基本的语法和常用函数就行了,其他的用的时候再搜。

    财经数据源:

    TuShare - http://tushare.org/index.html

    免费,开源Python财经数据接口包。

    GitHub: https://github.com/waditu/tushare

    基本的资料目前就看了这些,其他的都是网上即时搜的,以前的没保存,接下来会慢慢加。

    ================================================================

    既然是新股发行后破板的表现,要设一个时间段,否则没意义,所以先以半年为时间段,统计破板后的数据,时间太久了也没用,次新股一年为限,但我们可以以一个月为限,本别分析,破板买入,30个交易日内每天的盈利概率。

    要做的事情可以分为以下几步:

    1. 获取近半年来的新股数据

    2. 拿到每支股票上市后的数据

    3. 筛选出破板后三十天的数据,并汇总

    4. 计算整体上每天盈利的概率

    -----

    第一步:如何获取近半年的新股数据。

    TuShare新股数据接口:

    输入参数:retry_count, pause

    返回数据较多,是Pandas DataFrame格式的数据,下面折叠的部分是测试输出结果,其中很多对于现在要做的没有用,需要筛选一下。

    关心的返回值:code - 股票代码,name - 股票名称, issue_date - 上市日期

    import tushare as ts
    ts.new_stocks()
    Last login: Fri Oct 28 23:50:59 on ttys001
    localhost:~ shengtianhe$ python
    Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:52:12) 
    [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tushare as ts
    >>> ts.new_stocks()
    [Getting data:]########       code  name    ipo_date  issue_date   amount  markets  price     pe  
    0    603319   湘油泵  2016-11-21         NaN   2023.0    723.0  10.46  22.98   
    1    603323  吴江银行  2016-11-16         NaN  11150.0   3345.0   6.83  12.62   
    2    601229  上海银行  2016-11-02         NaN  60045.0  18000.0  17.77   8.26   
    3    603977  国泰集团  2016-11-01         NaN   5528.0   2028.0   0.00   0.00   
    4    603556  海兴电力  2016-10-31         NaN   9334.0   3734.0  23.63  21.32   
    5    603060  国检集团  2016-10-28         NaN   5500.0   2200.0  10.04  22.97   
    6    002819  东方中科  2016-10-28         NaN   2834.0   1124.0   4.96  22.95   
    7    603203  快克股份  2016-10-27         NaN   2300.0   2070.0  16.50  21.04   
    8    300558  贝达药业  2016-10-27         NaN   4100.0   3690.0  17.57  22.99   
    9    300556  丝路视觉  2016-10-26         NaN   2780.0   2502.0   5.54  22.99   
    10   002818   富森美  2016-10-25         NaN   4400.0   3960.0  23.49  22.99   
    11   601882  海天精工  2016-10-25         NaN   5220.0   4698.0   1.50  22.98   
    12   300557  理工光科  2016-10-21         NaN   1400.0   1400.0  13.91  22.99   
    13   300560   中富通  2016-10-20         NaN   1753.0   1753.0  10.26  22.99   
    14   603716   塞力斯  2016-10-19  2016-10-31   1274.0   1274.0  26.91  22.98   
    15   300559  佳发安泰  2016-10-19         NaN   1800.0   1800.0  17.56  22.99   
    16   603888   新华网  2016-10-18  2016-10-28   5190.0   4672.0  27.69  22.99   
    17   603258  电魂网络  2016-10-17  2016-10-26   6000.0   5400.0  15.62  18.16   
    18   600926  杭州银行  2016-10-14  2016-10-27  26175.0  23558.0  14.39  10.23   
    19   002817  黄山胶囊  2016-10-13  2016-10-25   2167.0   1950.0  13.88  22.98   
    20   603667  五洲新春  2016-10-12  2016-10-25   5060.0   4554.0   8.80  22.96   
    21   002816   和科达  2016-10-12  2016-10-25   2500.0   2250.0   8.29  22.98   
    22   603859  能科股份  2016-10-11  2016-10-21   2839.0   2555.0   7.54  22.97   
    23   300552  万集科技  2016-10-11  2016-10-21   2670.0   2403.0  12.25  20.91   
    24   300553  集智股份  2016-10-10  2016-10-21   1200.0   1200.0  14.08  22.99   
    25   300550  和仁科技  2016-09-30  2016-10-18   2000.0   2000.0  12.53  22.99   
    26   300555  路通视信  2016-09-29  2016-10-18   2000.0   2000.0  15.40  22.99   
    27   603160  汇顶科技  2016-09-28  2016-10-17   4500.0   4050.0  19.42  22.99   
    28   300551  古鳌科技  2016-09-28  2016-10-18   1836.0   1836.0  12.48  22.88   
    29   603816  顾家家居  2016-09-27  2016-10-14   8250.0   7425.0  24.66  22.99   
    ..      ...   ...         ...         ...      ...      ...    ...    ...   
    270  002755  东方新星  2015-05-06  2015-05-15   2534.0   2281.0   7.49  20.49   
    271  603718  海利生物  2015-05-06  2015-05-15   7000.0   6300.0   6.81  21.28   
    272  603227  雪峰科技  2015-05-06  2015-05-15   8235.0   7412.0   4.98  22.95   
    273  300451  创业软件  2015-05-05  2015-05-14   1700.0   1530.0  14.02  22.98   
    274  002756  永兴特钢  2015-05-05  2015-05-15   5000.0   4500.0  21.74  17.97   
    275  603355  莱克电气  2015-05-05  2015-05-13   4100.0   3690.0  19.08  22.99   
    276  300460  惠伦晶体  2015-05-05  2015-05-15   4208.0   3787.0   6.43  22.96   
    277  300457  赢合科技  2015-05-05  2015-05-14   1950.0   1755.0  12.41  20.34   
    278  300452  山河药辅  2015-05-05  2015-05-15   1160.0   1044.0  14.96  22.99   
    279  300414  中光防雷  2015-05-05  2015-05-13   2107.0   1896.0  14.74  13.24   
    280  300452  山河药辅  2015-05-05  2015-05-15   1160.0   1044.0  14.96  22.99   
    281  300414  中光防雷  2015-05-05  2015-05-13   2107.0   1896.0  14.74  13.24   
    282  300442   普丽盛  2015-04-16  2015-04-24   2500.0   2250.0  19.17  22.55   
    283  300434  金石东方  2015-04-16  2015-04-24   1700.0   1530.0  10.57  22.98   
    284  300438  鹏辉能源  2015-04-16  2015-04-24   2100.0   1890.0  14.87  22.99   
    285  002751  易尚展示  2015-04-16  2015-04-24   1756.0   1580.0  10.48  22.98   
    286  300404  博济医药  2015-04-16  2015-04-24   1667.0   1500.0  12.87  22.98   
    287  300446  乐凯新材  2015-04-15  2015-04-23   1540.0   1386.0   8.85   7.11   
    288  300444  双杰电气  2015-04-15  2015-04-23   3449.0   3104.0  12.13  22.89   
    289  603021  山东华鹏  2015-04-15  2015-04-23   2640.0   2376.0   8.73  22.97   
    290  300441  鲍斯股份  2015-04-15  2015-04-23   2112.0   1901.0   9.81  22.81   
    291  603315  福鞍股份  2015-04-15  2015-04-24   2500.0   2250.0  10.77  22.91   
    292  600959  江苏有线  2015-04-15  2015-04-28  59700.0  53730.0   5.47  22.98   
    293  300448  浩云科技  2015-04-15  2015-04-24   2000.0   1800.0  15.79  22.88   
    294  300440  运达科技  2015-04-15  2015-04-23   2800.0   2520.0  21.70  22.97   
    295  300437   清水源  2015-04-15  2015-04-23   1670.0   1503.0  10.53  19.30   
    296  603567   珍宝岛  2015-04-14  2015-04-24   6458.0   5812.0  23.60  22.91   
    297  603025  大豪科技  2015-04-14  2015-04-22   5100.0   4590.0  11.17  22.98   
    298  300436   广生堂  2015-04-14  2015-04-22   1750.0   1260.0  21.47  18.67   
    299  002752   NaN         NaN         NaN      NaN      NaN    NaN    NaN   
    
         limit    funds  ballot  
    0     0.70    2.116    0.00  
    1     3.30    7.615    0.00  
    2    18.00  106.700    0.00  
    3     0.00    3.566    0.00  
    4     3.70   22.065    0.00  
    5     2.20    5.522    0.00  
    6     1.10    1.406    0.00  
    7     0.90    3.795    0.03  
    8     1.20    7.204    0.04  
    9     1.10    1.540    0.03  
    10    1.30   10.336    0.04  
    11    1.50    4.305    0.05  
    12    1.40    1.947    0.01  
    13    1.75    1.799    0.02  
    14    1.20    3.428    0.02  
    15    1.80    3.161    0.02  
    16    2.00   14.372    0.04  
    17    2.40    9.372    0.05  
    18    7.80   37.666    0.13  
    19    0.85    3.008    0.02  
    20    2.00    4.453    0.04  
    21    1.00    2.072    0.03  
    22    1.10    2.141    0.03  
    23    1.05    3.271    0.03  
    24    1.20    1.690    0.01  
    25    2.00    2.506    0.02  
    26    2.00    3.080    0.02  
    27    1.30    8.739    0.05  
    28    1.80    2.291    0.02  
    29    2.40   20.345    0.06  
    ..     ...      ...     ...  
    270   1.00    1.898    0.32  
    271   2.80    4.774    0.31  
    272   3.20    3.875    0.30  
    273   0.65    2.384    0.20  
    274   2.00   10.872    0.48  
    275   1.20    7.823    0.40  
    276   1.55    2.706    0.34  
    277   0.75    2.421    0.33  
    278   0.45    1.736    0.30  
    279   0.80    3.106    0.30  
    280   0.45    1.736    0.30  
    281   0.80    3.106    0.30  
    282   1.00    4.212    0.62  
    283   0.65    1.475    0.44  
    284   0.80    2.782    0.45  
    285   0.65    1.550    0.34  
    286   0.65    1.884    0.30  
    287   0.60    1.115    0.46  
    288   1.30    3.698    0.77  
    289   1.00    1.934    0.47  
    290   0.80    1.732    0.68  
    291   1.00    2.399    0.47  
    292  17.70   31.220    1.24  
    293   0.80    2.841    0.46  
    294   1.10    5.578    0.61  
    295   0.60    1.523    0.50  
    296   1.80   14.275    0.83  
    297   1.50    5.183    0.62  
    298   0.70    2.588    0.30  
    299    NaN      NaN     NaN  
    
    [300 rows x 11 columns]
    View Code

    筛选规则:

    issue_date 在半年之内。

    Pandas API:

    http://pandas.pydata.org/pandas-docs/stable/api.html

    其中pandas.DataFrame.Sort可以带参数过滤DataFrame:

    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort.html

    但觉得这样还得进一步过滤,搜了一下,发现可以直接操作。

    import tushare as ts
    df=ts.new_stocks()
    df=df[df['issue_date']>'2016-06-01']

    这样我们就直接取到了所有六月一日后上市的新股数据:

    >>> import tushare as ts
    >>> df=ts.new_stocks()
    [Getting data:]########>>> df=df[df['issue_date']>'2016-06-01']
    >>> df
           code  name    ipo_date  issue_date    amount   markets  price     pe  
    14   603716   塞力斯  2016-10-19  2016-10-31    1274.0    1274.0  26.91  22.98   
    16   603888   新华网  2016-10-18  2016-10-28    5190.0    4672.0  27.69  22.99   
    17   603258  电魂网络  2016-10-17  2016-10-26    6000.0    5400.0  15.62  18.16   
    18   600926  杭州银行  2016-10-14  2016-10-27   26175.0   23558.0  14.39  10.23   
    19   002817  黄山胶囊  2016-10-13  2016-10-25    2167.0    1950.0  13.88  22.98   
    20   603667  五洲新春  2016-10-12  2016-10-25    5060.0    4554.0   8.80  22.96   
    21   002816   和科达  2016-10-12  2016-10-25    2500.0    2250.0   8.29  22.98   
    22   603859  能科股份  2016-10-11  2016-10-21    2839.0    2555.0   7.54  22.97   
    23   300552  万集科技  2016-10-11  2016-10-21    2670.0    2403.0  12.25  20.91   
    24   300553  集智股份  2016-10-10  2016-10-21    1200.0    1200.0  14.08  22.99   
    25   300550  和仁科技  2016-09-30  2016-10-18    2000.0    2000.0  12.53  22.99   
    26   300555  路通视信  2016-09-29  2016-10-18    2000.0    2000.0  15.40  22.99   
    27   603160  汇顶科技  2016-09-28  2016-10-17    4500.0    4050.0  19.42  22.99   
    28   300551  古鳌科技  2016-09-28  2016-10-18    1836.0    1836.0  12.48  22.88   
    29   603816  顾家家居  2016-09-27  2016-10-14    8250.0    7425.0  24.66  22.99   
    30   300548  博创科技  2016-09-26  2016-10-12    2067.0    1860.0  11.75  22.97   
    31   603313  恒康家居  2016-09-26  2016-10-13    6000.0    5400.0  15.41  22.66   
    32   002813  路畅科技  2016-09-23  2016-10-12    3000.0    2700.0   6.89  22.97   
    33   603777   来伊份  2016-09-23  2016-10-12    6000.0    5400.0  11.67  22.98   
    34   603421  鼎信通讯  2016-09-22  2016-10-11    4340.0    3906.0  14.02  22.99   
    35   002815  崇达技术  2016-09-22  2016-10-12    5000.0    4500.0  16.31  22.98   
    36   603887  城地股份  2016-09-21  2016-10-10    2460.0    2214.0  12.13  22.97   
    37   300547  川环科技  2016-09-21  2016-09-30    1495.0    1495.0  22.07  22.99   
    38   300549  优德精密  2016-09-20  2016-09-30    1667.0    1667.0  15.03  22.99   
    39   601128  常熟银行  2016-09-20  2016-09-30   22227.0   20005.0   4.28   9.89   
    40   300546  雄帝科技  2016-09-19  2016-09-28    1334.0    1334.0  20.43  22.99   
    41   603738  泰晶科技  2016-09-14  2016-09-28    1668.0    1668.0  16.14  22.99   
    42   300545  联得装备  2016-09-14  2016-09-28    1783.0    1783.0  13.50  22.98   
    43   600908  无锡银行  2016-09-08  2016-09-23   18481.0   16633.0   4.47   9.91   
    44   300536  农尚环境  2016-09-06  2016-09-20    2328.0    2095.0   9.06  18.12   
    ..      ...   ...         ...         ...       ...       ...    ...    ...   
    85   300529  健帆生物  2016-07-22  2016-08-02    4200.0    3780.0  10.80  22.98   
    86   300526  中潜股份  2016-07-21  2016-08-02    2125.0    1913.0  10.50  22.99   
    87   600919  江苏银行  2016-07-20  2016-08-02  115445.0  103900.0   6.27   7.64   
    88   603663  三祥新材  2016-07-19  2016-08-01    3355.0    3020.0   5.28  22.98   
    89   603322  超讯通信  2016-07-18  2016-07-28    2000.0    2000.0  11.99  22.98   
    90   300525  博思软件  2016-07-15  2016-07-26    1710.0    1710.0  11.68  22.99   
    91   002806  华锋股份  2016-07-14  2016-07-26    2000.0    2000.0   6.20  22.96   
    92   300523  辰安科技  2016-07-13  2016-07-26    2000.0    2000.0  21.92  22.99   
    93   300517  海波重科  2016-07-07  2016-07-19    2560.0    2304.0  10.04  22.97   
    94   002803  吉宏股份  2016-06-30  2016-07-12    2900.0    2610.0   6.37  22.84   
    95   603069  海汽集团  2016-06-29  2016-07-12    7900.0    7110.0   3.82  18.20   
    96   300520  科大国创  2016-06-28  2016-07-08    2300.0    2070.0  10.05  22.84   
    97   002805  丰元股份  2016-06-27  2016-07-07    2423.0    2181.0   5.80  22.97   
    98   300521   爱司凯  2016-06-24  2016-07-05    2000.0    2000.0  11.26  22.98   
    99   300522  世名科技  2016-06-23  2016-07-05    1667.0    1667.0  18.55  22.98   
    100  601966  玲珑轮胎  2016-06-22  2016-07-06   20000.0   18000.0  12.98  22.97   
    101  603016   新宏泰  2016-06-21  2016-07-01    3705.0    3335.0   8.49  19.30   
    102  002802  洪汇新材  2016-06-20  2016-06-29    2700.0    2430.0   9.52  22.98   
    103  603958  哈森股份  2016-06-17  2016-06-29    5436.0    4892.0   9.15  22.98   
    104  603909  合诚股份  2016-06-16  2016-06-28    2500.0    2250.0  10.55  21.00   
    105  300518   盛讯达  2016-06-15  2016-06-24    2334.0    2101.0  22.22  22.68   
    106  002801  微光股份  2016-06-13  2016-06-22    1472.0    1472.0  19.51  15.24   
    107  300519  新光药业  2016-06-08  2016-06-24    2000.0    2000.0  12.20   8.26   
    108  601127  小康股份  2016-05-31  2016-06-15   14250.0   12825.0   5.81  18.19   
    109  300515  三德科技  2016-05-30  2016-06-08    2500.0    2250.0   8.57  22.98   
    110  002799  环球印务  2016-05-27  2016-06-08    2500.0    2250.0   7.98  22.99   
    111  603131  上海沪工  2016-05-26  2016-06-07    2500.0    2250.0  10.09  22.98   
    112  601611  中国核建  2016-05-25  2016-06-06   52500.0   47250.0   3.47  15.60   
    113  300516   久之洋  2016-05-24  2016-06-02    3000.0    2700.0  22.50  22.98   
    114  603737   三棵树  2016-05-23  2016-06-03    2500.0    2250.0  15.94  18.09   
    
         limit   funds  ballot  
    14    1.20   3.428    0.02  
    16    2.00  14.372    0.04  
    17    2.40   9.372    0.05  
    18    7.80  37.666    0.13  
    19    0.85   3.008    0.02  
    20    2.00   4.453    0.04  
    21    1.00   2.072    0.03  
    22    1.10   2.141    0.03  
    23    1.05   3.271    0.03  
    24    1.20   1.690    0.01  
    25    2.00   2.506    0.02  
    26    2.00   3.080    0.02  
    27    1.30   8.739    0.05  
    28    1.80   2.291    0.02  
    29    2.40  20.345    0.06  
    30    0.80   2.429    0.03  
    31    2.40   9.246    0.05  
    32    1.20   2.067    0.03  
    33    2.40   7.002    0.05  
    34    1.30   6.085    0.05  
    35    1.50   8.155    0.04  
    36    0.90   3.647    0.03  
    37    1.45   3.299    0.02  
    38    1.65   2.506    0.02  
    39    6.60   9.513    0.12  
    40    1.30   2.725    0.01  
    41    1.60   2.692    0.02  
    42    1.75   2.407    0.02  
    43    5.50   8.261    0.10  
    44    0.90   2.110    0.03  
    ..     ...     ...     ...  
    85    1.20   4.536    0.05  
    86    0.70   2.231    0.04  
    87   34.60  72.384    0.47  
    88    1.30   1.771    0.04  
    89    2.00   2.398    0.02  
    90    1.70   1.997    0.02  
    91    2.00   1.240    0.02  
    92    2.00   4.384    0.02  
    93    1.00   2.570    0.04  
    94    1.15   3.004    0.03  
    95    2.30   3.018    0.08  
    96    0.90   2.312    0.04  
    97    0.95   1.405    0.03  
    98    2.00   2.252    0.02  
    99    1.65   3.092    0.02  
    100   6.00  25.960    0.14  
    101   1.40   3.148    0.05  
    102   1.05   2.570    0.03  
    103   2.10   4.974    0.06  
    104   1.00   2.638    0.04  
    105   0.90   5.188    0.04  
    106   1.45   2.872    0.02  
    107   2.00   2.440    0.02  
    108   4.20   8.550    0.11  
    109   1.00   2.143    0.04  
    110   1.00   1.995    0.03  
    111   1.00   2.522    0.04  
    112  15.70  20.212    0.28  
    113   1.20   6.750    0.04  
    114   1.00   3.987    0.04  
    
    [100 rows x 11 columns]
    View Code

    但我们只需要code, name 和 issue_date的信息,所以再加一行:

    import tushare as ts
    df=ts.new_stocks()
    df=df[df['issue_date']>'2016-06-01']
    df=df[['code','name','issue_date']]

    这是运行结果:

    >>> df=df[['code','name','issue_date']]
    >>> df
           code  name  issue_date
    14   603716   塞力斯  2016-10-31
    16   603888   新华网  2016-10-28
    17   603258  电魂网络  2016-10-26
    18   600926  杭州银行  2016-10-27
    19   002817  黄山胶囊  2016-10-25
    20   603667  五洲新春  2016-10-25
    21   002816   和科达  2016-10-25
    22   603859  能科股份  2016-10-21
    23   300552  万集科技  2016-10-21
    24   300553  集智股份  2016-10-21
    25   300550  和仁科技  2016-10-18
    26   300555  路通视信  2016-10-18
    27   603160  汇顶科技  2016-10-17
    28   300551  古鳌科技  2016-10-18
    29   603816  顾家家居  2016-10-14
    30   300548  博创科技  2016-10-12
    31   603313  恒康家居  2016-10-13
    32   002813  路畅科技  2016-10-12
    33   603777   来伊份  2016-10-12
    34   603421  鼎信通讯  2016-10-11
    35   002815  崇达技术  2016-10-12
    36   603887  城地股份  2016-10-10
    37   300547  川环科技  2016-09-30
    38   300549  优德精密  2016-09-30
    39   601128  常熟银行  2016-09-30
    40   300546  雄帝科技  2016-09-28
    41   603738  泰晶科技  2016-09-28
    42   300545  联得装备  2016-09-28
    43   600908  无锡银行  2016-09-23
    44   300536  农尚环境  2016-09-20
    ..      ...   ...         ...
    85   300529  健帆生物  2016-08-02
    86   300526  中潜股份  2016-08-02
    87   600919  江苏银行  2016-08-02
    88   603663  三祥新材  2016-08-01
    89   603322  超讯通信  2016-07-28
    90   300525  博思软件  2016-07-26
    91   002806  华锋股份  2016-07-26
    92   300523  辰安科技  2016-07-26
    93   300517  海波重科  2016-07-19
    94   002803  吉宏股份  2016-07-12
    95   603069  海汽集团  2016-07-12
    96   300520  科大国创  2016-07-08
    97   002805  丰元股份  2016-07-07
    98   300521   爱司凯  2016-07-05
    99   300522  世名科技  2016-07-05
    100  601966  玲珑轮胎  2016-07-06
    101  603016   新宏泰  2016-07-01
    102  002802  洪汇新材  2016-06-29
    103  603958  哈森股份  2016-06-29
    104  603909  合诚股份  2016-06-28
    105  300518   盛讯达  2016-06-24
    106  002801  微光股份  2016-06-22
    107  300519  新光药业  2016-06-24
    108  601127  小康股份  2016-06-15
    109  300515  三德科技  2016-06-08
    110  002799  环球印务  2016-06-08
    111  603131  上海沪工  2016-06-07
    112  601611  中国核建  2016-06-06
    113  300516   久之洋  2016-06-02
    114  603737   三棵树  2016-06-03
    
    [100 rows x 3 columns]
    View Code

    现在我们已经取到过去半年上市的新股的数据,包括股票代码,名称和上市日期。

    -----

    第二步:如何拿到每支股票上市后的数据

    TuShare历史交易数据接口:

    输入参数:code, start, end, ktype(数据类型,默认是日K线), retry_count, pause

    返回数据较多,同样也是Pandas DataFrame格式的数据,下面折叠的部分是测试输出结果,其中很多对于现在要做的没有用,需要筛选一下。

    关心的返回值:date - 日期,open - 开盘价, close - 收盘价, p_change - 涨跌幅

    我们以六月三日上市的新股三棵树(603737)为例:

    import tushare as ts
    df=ts.get_hist_data('603737')

    测试运行如下:

    >>> df=ts.get_hist_data('603737')
    >>> df
                  open    high   close     low     volume  price_change  p_change  
    date                                                                            
    2016-10-28   82.50   82.70   81.53   81.10    8990.79         -0.57     -0.69   
    2016-10-27   82.30   82.35   82.19   81.34    6959.08          0.24      0.29   
    2016-10-26   82.04   84.18   81.99   81.51   11004.83         -0.11     -0.13   
    2016-10-25   82.68   83.32   82.09   82.03   14014.53         -0.89     -1.07   
    2016-10-24   78.98   83.79   83.00   78.31   26251.55          3.93      4.97   
    2016-10-21   79.19   79.34   79.08   77.32   12459.65         -0.17     -0.21   
    2016-10-20   78.50   79.50   79.25   78.05   10224.54          0.75      0.95   
    2016-10-19   80.60   80.60   78.49   78.44   17851.50         -1.27     -1.59   
    2016-10-18   77.72   79.80   79.77   77.72   12765.18          1.76      2.26   
    2016-10-17   78.60   79.70   78.01   77.80   15855.89         -1.07     -1.35   
    2016-10-14   79.42   80.10   79.00   78.21   11452.91         -0.14     -0.18   
    2016-10-13   78.85   79.88   79.15   78.20   11277.68          0.18      0.23   
    2016-10-12   77.17   79.47   78.95   76.60   18557.77          0.90      1.15   
    2016-10-11   77.95   79.30   78.07   76.63   21518.05          0.05      0.06   
    2016-10-10   72.93   79.95   78.03   72.93   25128.50          5.11      7.01   
    2016-09-30   73.08   73.29   72.90   71.90    7940.73         -0.15     -0.20   
    2016-09-29   73.18   74.16   73.46   73.08    9711.11          0.05      0.07   
    2016-09-28   73.25   74.10   73.37   72.30    8694.66          0.10      0.14   
    2016-09-27   72.02   73.31   73.30   71.88   10588.05          0.78      1.08   
    2016-09-26   76.24   76.24   72.51   72.30   18983.21         -3.77     -4.94   
    2016-09-23   78.18   78.18   76.31   76.12   13200.18         -1.59     -2.04   
    2016-09-22   79.10   79.80   77.90   77.61   15900.57         -0.78     -0.99   
    2016-09-21   79.10   79.80   78.67   77.80   13804.02         -0.96     -1.21   
    2016-09-20   81.60   81.60   79.64   78.80   15751.28         -1.07     -1.33   
    2016-09-19   80.56   81.39   80.71   80.48    8901.68          0.12      0.15   
    2016-09-14   81.80   82.80   80.57   80.28   23667.66         -3.47     -4.13   
    2016-09-13   86.20   88.16   83.99   83.62   38293.43         -2.19     -2.54   
    2016-09-12   82.50   86.22   86.19   81.30   30861.92          1.55      1.83   
    2016-09-09   83.78   85.96   84.66   83.55   23143.33          0.95      1.14   
    2016-09-08   82.50   83.87   83.71   82.50   13567.35          0.90      1.09   
    ...            ...     ...     ...     ...        ...           ...       ...   
    2016-07-18  100.00  100.98   97.17   96.66   25811.42         -3.71     -3.68   
    2016-07-15  100.50  103.20  100.90   99.01   35970.92          1.18      1.18   
    2016-07-14   98.00  100.84   99.73   97.50   29597.28          0.87      0.88   
    2016-07-13   99.00  101.66   98.87   96.50   38939.97         -1.65     -1.64   
    2016-07-12   96.96  101.58  100.51   92.06   54520.75          1.13      1.14   
    2016-07-11  110.00  110.80   99.38   99.38   63645.73        -11.04    -10.00   
    2016-07-08  111.51  113.40  110.47  110.00   42732.83         -3.25     -2.86   
    2016-07-07  111.12  118.50  113.71  111.02   64406.13          0.96      0.85   
    2016-07-06  114.00  115.60  112.75  110.00   57436.00         -2.93     -2.53   
    2016-07-05  110.11  117.99  115.63  109.10   96591.87          5.25      4.76   
    2016-07-04  111.89  112.00  110.46  108.33   56773.07         -1.35     -1.21   
    2016-07-01  111.00  116.00  111.82  108.50   87725.18         -4.26     -3.67   
    2016-06-30  111.00  116.08  116.08  106.00  179909.75         10.55     10.00   
    2016-06-29  105.53  105.53  105.53  105.53    1748.55          9.59     10.00   
    2016-06-28   95.94   95.94   95.94   95.94    2631.18          8.72     10.00   
    2016-06-27   87.22   87.22   87.22   87.22   10737.11          7.93     10.00   
    2016-06-24   79.29   79.29   79.29   79.29    2013.69          7.21     10.00   
    2016-06-23   72.08   72.08   72.08   72.08    2413.01          6.55      9.99   
    2016-06-22   65.53   65.53   65.53   65.53    1152.33          5.96     10.01   
    2016-06-21   59.57   59.57   59.57   59.57     998.79          5.42     10.01   
    2016-06-20   54.15   54.15   54.15   54.15    1332.01          4.92      9.99   
    2016-06-17   49.23   49.23   49.23   49.23    1086.13          4.48     10.01   
    2016-06-16   44.75   44.75   44.75   44.75     257.15          4.07     10.01   
    2016-06-15   40.68   40.68   40.68   40.68     286.50          3.70     10.01   
    2016-06-14   36.98   36.98   36.98   36.98     627.34          3.36      9.99   
    2016-06-13   33.62   33.62   33.62   33.62     346.27          3.06     10.01   
    2016-06-08   30.56   30.56   30.56   30.56     158.75          2.78     10.01   
    2016-06-07   27.78   27.78   27.78   27.78      35.00          2.53     10.02   
    2016-06-06   25.25   25.25   25.25   25.25      22.00          2.30     10.02   
    2016-06-03   22.95   22.95   22.95   22.95     155.00          7.01     43.98   
    
                    ma5     ma10    ma20     v_ma5    v_ma10    v_ma20  turnover  
    date                                                                          
    2016-10-28   82.160   80.540  78.207  13444.16  13637.75  14011.51      3.60  
    2016-10-27   81.670   80.287  77.946  14137.93  13883.97  14221.98      2.78  
    2016-10-26   81.082   79.983  77.732  14791.02  14315.83  14669.05      4.40  
    2016-10-25   80.382   79.679  77.566  16160.35  15071.12  14809.01      5.61  
    2016-10-24   79.918   79.277  77.443  15910.48  15821.47  14895.85     10.50  
    2016-10-21   78.920   78.780  77.329  13831.35  15709.17  14028.36      4.98  
    2016-10-20   78.904   78.162  77.403  13630.00  15257.28  14588.76      4.09  
    2016-10-19   78.884   77.583  77.640  13840.63  15205.93  15992.20      7.14  
    2016-10-18   78.976   77.071  78.025  13981.89  14290.25  16642.72      5.11  
    2016-10-17   78.636   76.424  78.270  15732.46  14072.54  17161.63      6.34  
    2016-10-14   78.640   75.874  78.555  17586.98  14385.27  17047.20      4.58  
    2016-10-13   77.420   75.605  78.744  16884.55  14559.99  16960.55      4.51  
    2016-10-12   76.282   75.480  78.975  16571.23  15022.28  17103.11      7.42  
    2016-10-11   75.166   75.452  79.172  14598.61  14546.91  16525.29      8.61  
    2016-10-10   74.212   75.609  79.420  12412.61  13970.23  15914.86     10.05  
    2016-09-30   73.108   75.877  79.673  11183.55  12347.55  15266.72      3.18  
    2016-09-29   73.790   76.644  80.274  12235.44  13920.24  15776.66      3.88  
    2016-09-28   74.678   77.697  80.795  13473.33  16778.47  15717.01      3.48  
    2016-09-27   75.738   78.979  81.333  14495.21  18995.20  15837.12      4.24  
    2016-09-26   77.006   80.115  81.848  15527.85  20250.73  16159.17      7.59  
    2016-09-23   78.646   81.235  82.447  13511.55  19709.14  16344.07      5.28  
    2016-09-22   79.498   81.883  82.944  15605.04  19361.10  16273.08      6.36  
    2016-09-21   80.716   82.470  83.386  20083.61  19183.94  16362.07      5.52  
    2016-09-20   82.220   82.891  83.768  23495.19  18503.67  17271.29      6.30  
    2016-09-19   83.224   83.231  84.417  24973.60  17859.48  17452.37      3.56  
    2016-09-14   83.824   83.469  85.042  25906.74  18185.89  18336.10      9.47  
    2016-09-13   84.268   83.905  85.753  23117.16  17633.07  18567.30     15.32  
    2016-09-12   84.224   83.894  86.355  18284.26  14655.54  18866.01     12.34  
    2016-09-09   83.562   83.686  86.659  13512.14  12679.05  19115.24      9.26  
    2016-09-08   83.238   83.580  86.929  10745.36  12067.61  19107.11      5.43  
    ...             ...      ...     ...       ...       ...       ...       ...  
    2016-07-18   99.436  104.912  97.632  36968.07  50965.29  42787.78     10.32  
    2016-07-15   99.878  106.241  95.481  44534.93  54061.45  41563.81     14.39  
    2016-07-14  101.792  107.333  92.898  45887.31  59236.88  39819.57     11.84  
    2016-07-13  104.588  108.968  90.149  52849.08  74268.13  38352.56     15.58  
    2016-07-12  107.364  109.634  87.239  56548.29  70548.99  36419.89     21.81  
    2016-07-11  110.388  109.177  84.063  64962.51  65360.03  33725.22     25.46  
    2016-07-08  112.604  107.961  80.774  63587.98  60069.17  30560.24     17.09  
    2016-07-07  112.874  104.843  76.779  72586.45  55997.25  28431.54     25.76  
    2016-07-06  113.348  100.680  72.483  95687.17  49797.94  25212.98     22.97  
    2016-07-05  111.904   95.958  68.108  84549.68  44169.57  22342.28     38.64  
    2016-07-04  107.966   90.352  63.474  65757.55  34610.27  17520.44     22.71  
    2016-07-01  103.318   84.721  61.001  56550.35  29066.16  15454.51     35.09  
    2016-06-30   96.812   78.462  58.177  39408.06  20402.25  11439.48     71.96  
    2016-06-29   88.012   71.329  54.771   3908.71   2437.00   1529.46      0.70  
    2016-06-28   80.012   64.844  51.599   3789.46   2290.79   1515.77      1.05  
    2016-06-27   72.738   58.948  48.643   3462.99   2090.41   1441.41      4.29  
    2016-06-24   66.124   53.588  45.887   1581.97   1051.32    777.43      0.81  
    2016-06-23   60.112   48.715  43.318   1396.45    865.83    682.33      0.97  
    2016-06-22   54.646   44.285  40.921    965.28    628.03    538.11      0.46  
    2016-06-21   49.676   40.257  38.684    792.12    514.99    482.27      0.40  
    2016-06-20   45.158   36.595  36.595    717.83    430.62    430.62      0.53  
    2016-06-17   41.052   34.644  34.644    520.68    330.46    330.46      0.43  
    2016-06-16   37.318   32.821  32.821    335.20    236.00    236.00      0.10  
    2016-06-15   33.924   31.117  31.117    290.77    232.98    232.98      0.11  
    2016-06-14   30.838   29.523  29.523    237.87    224.06    224.06      0.25  
    2016-06-13   28.032   28.032  28.032    143.40    143.40    143.40      0.14  
    2016-06-08   26.635   26.635  26.635     92.69     92.69     92.69      0.06  
    2016-06-07   25.327   25.327  25.327     70.67     70.67     70.67      0.01  
    2016-06-06   24.100   24.100  24.100     88.50     88.50     88.50      0.01  
    2016-06-03   22.950   22.950  22.950    155.00    155.00    155.00      0.06  
    
    [97 rows x 14 columns]
    View Code

    同样,按照第一步的思路对数据稍加处理:

    df=df[['date','open','close','p_change']]

    此时却出了问题,报错如下:

    >>> df=df[['date','open','close','p_change']]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/shengtianhe/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 1991, in __getitem__
        return self._getitem_array(key)
      File "/Users/shengtianhe/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 2035, in _getitem_array
        indexer = self.ix._convert_to_indexer(key, axis=1)
      File "/Users/shengtianhe/anaconda/lib/python3.5/site-packages/pandas/core/indexing.py", line 1214, in _convert_to_indexer
        raise KeyError('%s not in index' % objarr[mask])
    KeyError: "['date'] not in index"

    检查打出来的数据名称没问题,就考虑,第一列会不会是其他的东西,搜到几个有用的命令:

    from pandas import DataFrame #从pandas库中引用DataFrame
    
    df_obj = DataFrame() #创建DataFrame对象
    df_obj.dtypes #查看各行的数据格式
    df_obj.head() #查看前几行的数据,默认前5行
    df_obj.tail() #查看后几行的数据,默认后5行
    df_obj.index #查看索引
    df_obj.columns #查看列名
    df_obj.values #查看数据值
    df_obj.describe #描述性统计
    df_obj.T #转置
    df_obj.sort(columns = ‘’)#按列名进行排序
    df_obj.sort_index(by=[‘’,’’])#多列排序,使用时报该函数已过时,请用sort_values
    df_obj.sort_values(by=['',''])同上

    运行index和columns,果然date是index:

    >>> df.columns
    Index(['open', 'high', 'close', 'low', 'volume', 'price_change', 'p_change',
           'ma5', 'ma10', 'ma20', 'v_ma5', 'v_ma10', 'v_ma20', 'turnover'],
          dtype='object')
    >>> df.index
    Index(['2016-10-28', '2016-10-27', '2016-10-26', '2016-10-25', '2016-10-24',
           '2016-10-21', '2016-10-20', '2016-10-19', '2016-10-18', '2016-10-17',
           '2016-10-14', '2016-10-13', '2016-10-12', '2016-10-11', '2016-10-10',
           '2016-09-30', '2016-09-29', '2016-09-28', '2016-09-27', '2016-09-26',
           '2016-09-23', '2016-09-22', '2016-09-21', '2016-09-20', '2016-09-19',
           '2016-09-14', '2016-09-13', '2016-09-12', '2016-09-09', '2016-09-08',
           '2016-09-07', '2016-09-06', '2016-09-05', '2016-09-02', '2016-09-01',
           '2016-08-31', '2016-08-30', '2016-08-29', '2016-08-26', '2016-08-25',
           '2016-08-24', '2016-08-23', '2016-08-22', '2016-08-19', '2016-08-18',
           '2016-08-17', '2016-08-16', '2016-08-15', '2016-08-12', '2016-08-11',
           '2016-08-10', '2016-08-09', '2016-08-08', '2016-08-05', '2016-08-04',
           '2016-08-03', '2016-08-02', '2016-08-01', '2016-07-29', '2016-07-28',
           '2016-07-27', '2016-07-26', '2016-07-25', '2016-07-22', '2016-07-21',
           '2016-07-20', '2016-07-19', '2016-07-18', '2016-07-15', '2016-07-14',
           '2016-07-13', '2016-07-12', '2016-07-11', '2016-07-08', '2016-07-07',
           '2016-07-06', '2016-07-05', '2016-07-04', '2016-07-01', '2016-06-30',
           '2016-06-29', '2016-06-28', '2016-06-27', '2016-06-24', '2016-06-23',
           '2016-06-22', '2016-06-21', '2016-06-20', '2016-06-17', '2016-06-16',
           '2016-06-15', '2016-06-14', '2016-06-13', '2016-06-08', '2016-06-07',
           '2016-06-06', '2016-06-03'],
          dtype='object', name='date')
    View Code

    所以选取列的语句应该是:

    df=df[['open','close','p_change']]

    结果如下:

    >>> df=df[['open','close','p_change']]
    >>> df
                  open   close  p_change
    date                                
    2016-10-28   82.50   81.53     -0.69
    2016-10-27   82.30   82.19      0.29
    2016-10-26   82.04   81.99     -0.13
    2016-10-25   82.68   82.09     -1.07
    2016-10-24   78.98   83.00      4.97
    2016-10-21   79.19   79.08     -0.21
    2016-10-20   78.50   79.25      0.95
    2016-10-19   80.60   78.49     -1.59
    2016-10-18   77.72   79.77      2.26
    2016-10-17   78.60   78.01     -1.35
    2016-10-14   79.42   79.00     -0.18
    2016-10-13   78.85   79.15      0.23
    2016-10-12   77.17   78.95      1.15
    2016-10-11   77.95   78.07      0.06
    2016-10-10   72.93   78.03      7.01
    2016-09-30   73.08   72.90     -0.20
    2016-09-29   73.18   73.46      0.07
    2016-09-28   73.25   73.37      0.14
    2016-09-27   72.02   73.30      1.08
    2016-09-26   76.24   72.51     -4.94
    2016-09-23   78.18   76.31     -2.04
    2016-09-22   79.10   77.90     -0.99
    2016-09-21   79.10   78.67     -1.21
    2016-09-20   81.60   79.64     -1.33
    2016-09-19   80.56   80.71      0.15
    2016-09-14   81.80   80.57     -4.13
    2016-09-13   86.20   83.99     -2.54
    2016-09-12   82.50   86.19      1.83
    2016-09-09   83.78   84.66      1.14
    2016-09-08   82.50   83.71      1.09
    ...            ...     ...       ...
    2016-07-18  100.00   97.17     -3.68
    2016-07-15  100.50  100.90      1.18
    2016-07-14   98.00   99.73      0.88
    2016-07-13   99.00   98.87     -1.64
    2016-07-12   96.96  100.51      1.14
    2016-07-11  110.00   99.38    -10.00
    2016-07-08  111.51  110.47     -2.86
    2016-07-07  111.12  113.71      0.85
    2016-07-06  114.00  112.75     -2.53
    2016-07-05  110.11  115.63      4.76
    2016-07-04  111.89  110.46     -1.21
    2016-07-01  111.00  111.82     -3.67
    2016-06-30  111.00  116.08     10.00
    2016-06-29  105.53  105.53     10.00
    2016-06-28   95.94   95.94     10.00
    2016-06-27   87.22   87.22     10.00
    2016-06-24   79.29   79.29     10.00
    2016-06-23   72.08   72.08      9.99
    2016-06-22   65.53   65.53     10.01
    2016-06-21   59.57   59.57     10.01
    2016-06-20   54.15   54.15      9.99
    2016-06-17   49.23   49.23     10.01
    2016-06-16   44.75   44.75     10.01
    2016-06-15   40.68   40.68     10.01
    2016-06-14   36.98   36.98      9.99
    2016-06-13   33.62   33.62     10.01
    2016-06-08   30.56   30.56     10.01
    2016-06-07   27.78   27.78     10.02
    2016-06-06   25.25   25.25     10.02
    2016-06-03   22.95   22.95     43.98
    
    [97 rows x 3 columns]
    View Code

    现在我们已经取得了过去半年新上市的股票和他们上市后的数据。

    -----

    第三步:如何筛选出破板后三十天的数据,并汇总

    ……此处省略长达二十分钟的思考与百度……想到这样一个办法取得第一次破板的日期:

    import tushare as ts 
    df=ts.get_hist_data('603737')
    df=df[['open','close','p_change']]
    start_date=df[df['p_change']<9.8].tail(1).index[0]

    释一下:df是一个DataFrame, 可以提取某一列中值小于9.8的所有行,提取结果仍然是DataFrame,DF有个方法叫tail()取最后几条数据,我们已经知道日期是index,所以取日期用index[0],由于本人python没基础…取日期的方法是试出来的……

    接下来,用这个日期去过滤,我们只关心破板后的数据:

    df=df[df.index>=start_date]

    这里[]中的还是我试出来的……觉得应该这么写……

    >>> import tushare as ts 
    >>> df=ts.get_hist_data('603737')
    >>> 
    >>> df=df[['open','close','p_change']]
    >>> start_date=df[df['p_change']<9.8].tail(1).index[0]
    >>> df=df[df.index>start_date]
    >>> df
                  open   close  p_change
    date                                
    2016-10-28   82.50   81.53     -0.69
    2016-10-27   82.30   82.19      0.29
    2016-10-26   82.04   81.99     -0.13
    2016-10-25   82.68   82.09     -1.07
    2016-10-24   78.98   83.00      4.97
    2016-10-21   79.19   79.08     -0.21
    2016-10-20   78.50   79.25      0.95
    2016-10-19   80.60   78.49     -1.59
    2016-10-18   77.72   79.77      2.26
    2016-10-17   78.60   78.01     -1.35
    2016-10-14   79.42   79.00     -0.18
    2016-10-13   78.85   79.15      0.23
    2016-10-12   77.17   78.95      1.15
    2016-10-11   77.95   78.07      0.06
    2016-10-10   72.93   78.03      7.01
    2016-09-30   73.08   72.90     -0.20
    2016-09-29   73.18   73.46      0.07
    2016-09-28   73.25   73.37      0.14
    2016-09-27   72.02   73.30      1.08
    2016-09-26   76.24   72.51     -4.94
    2016-09-23   78.18   76.31     -2.04
    2016-09-22   79.10   77.90     -0.99
    2016-09-21   79.10   78.67     -1.21
    2016-09-20   81.60   79.64     -1.33
    2016-09-19   80.56   80.71      0.15
    2016-09-14   81.80   80.57     -4.13
    2016-09-13   86.20   83.99     -2.54
    2016-09-12   82.50   86.19      1.83
    2016-09-09   83.78   84.66      1.14
    2016-09-08   82.50   83.71      1.09
    ...            ...     ...       ...
    2016-08-12   88.29   90.08      0.11
    2016-08-11   89.00   90.16     -0.50
    2016-08-10   87.18   90.58      3.70
    2016-08-09   86.61   87.34      1.17
    2016-08-08   85.00   86.35      1.41
    2016-08-05   87.52   85.12     -3.47
    2016-08-04   88.01   88.20     -0.68
    2016-08-03   85.67   88.78      2.98
    2016-08-02   84.93   86.21      1.51
    2016-08-01   87.35   84.93     -4.30
    2016-07-29   85.75   88.77      3.75
    2016-07-28   86.10   85.53     -0.48
    2016-07-27   91.50   85.95     -6.56
    2016-07-26   87.90   91.97      4.54
    2016-07-25   88.33   87.90     -0.66
    2016-07-22   92.59   88.45     -4.69
    2016-07-21   94.69   92.81     -1.91
    2016-07-20   94.98   94.60      0.06
    2016-07-19   97.00   94.56     -2.71
    2016-07-18  100.00   97.17     -3.68
    2016-07-15  100.50  100.90      1.18
    2016-07-14   98.00   99.73      0.88
    2016-07-13   99.00   98.87     -1.64
    2016-07-12   96.96  100.51      1.14
    2016-07-11  110.00   99.38    -10.00
    2016-07-08  111.51  110.47     -2.86
    2016-07-07  111.12  113.71      0.85
    2016-07-06  114.00  112.75     -2.53
    2016-07-05  110.11  115.63      4.76
    2016-07-04  111.89  110.46     -1.21
    
    [78 rows x 3 columns]
    View Code

    接下来,假设破板买入后最长持股10天,这样我们只取妹纸股票之后10天的数据,这样数据比较整齐,也便于后续处理。如果数据不满10天,则跳过。

    df=df.tail(11)['close']
    >>> import tushare as ts 
    >>> df=ts.get_hist_data('603737')
    >>> 
    >>> df=df[['open','close','p_change']]
    >>> start_date=df[df['p_change']<9.8].tail(1).index[0]
    >>> df=df[df.index>=start_date]
    >>> df=df.tail(11)['close']
    >>> df
    date
    2016-07-15    100.90
    2016-07-14     99.73
    2016-07-13     98.87
    2016-07-12    100.51
    2016-07-11     99.38
    2016-07-08    110.47
    2016-07-07    113.71
    2016-07-06    112.75
    2016-07-05    115.63
    2016-07-04    110.46
    2016-07-01    111.82
    Name: close, dtype: float64
    View Code

    通过上一步,我们取到包含破板当日以及之后十天的收盘价,将数据转换为numpy.array数据。

    close_array=df.values
    >>> close_array=df.values
    >>> close_array
    array([ 100.9 ,   99.73,   98.87,  100.51,   99.38,  110.47,  113.71,
            112.75,  115.63,  110.46,  111.82])
    View Code

    进一步进行处理,如果破板后某个交易日收盘价格高于破板当天收盘价,则将array中的值置为1,否则为0。

    import tushare as ts
    import numpy as np
    
    df=ts.get_hist_data('603737')
    
    df=df[['open','close','p_change']]
    start_date=df[df['p_change']<9.8].tail(1).index[0]
    df=df[df.index>=start_date]
    df=df.tail(11)['close']
    
    close_array=df.values
    for i in range(1,11):
      if close_array[i]>close_array[0]:
        close_array[i]=1
      else:
        close_array[i]=0
    
    close_array[0]=0
    
    print (close_array)

    输出结果:

    localhost:~ shengtianhe$ python find.py
    [ 0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  1.]

    一只股票的数据组织好了,接下来把所有新股的数据取出来,这里需要用到合并array的方法numpy.concatenate,另外代码里还有一些小地方的处理,搜了一圈才知道怎么做,比如如何返回值判断非空:

    import tushare as ts
    import numpy as np
    import pandas as pd
    
    df=ts.new_stocks()
    df=df[df['issue_date']>'2016-06-01']
    df=df[['code','name','issue_date']]
    
    df=df[['code']]
    stock_code=df.values
    
    # print(stock_code)
    
    i=0
    df_matrix = None
    
    for stock in stock_code:
      detail=ts.get_hist_data(stock_code[i][0])
      if detail is None:
        i=i+1
        continue  
    
      detail=detail[['open','close','p_change']]
      hasbreak=detail[detail['p_change']<9.8]
    
      if hasbreak.size==0:
        i=i+1
        continue
    
      start_date=hasbreak.tail(1).index[0]
      hasbreak=hasbreak[hasbreak.index>=start_date]
      hasbreak=hasbreak.tail(11)['close']
    
      if hasbreak.size <11:
        i=i+1
        continue
    
      close_array=hasbreak.values
      for day in range(1,hasbreak.size):
        if close_array[day]>close_array[0]:
          close_array[day]=1
        else:
          close_array[day]=0
      close_array[0]=0
    
      df_matrix_thisRound=pd.DataFrame(close_array)
      df_matrix_thisRound=df_matrix_thisRound.T
    
      if df_matrix is None:
        df_matrix=df_matrix_thisRound
      else:
        df_matrix=np.concatenate((df_matrix,df_matrix_thisRound.values))
    
      i=i+1
    
    print(df_matrix)  

    运行结果如下:

    localhost:~ shengtianhe$ python proData.py 
    [Getting data:]########[[ 0.  1.  1.  0.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  0.  1.  0.  0.  1.  1.  1.  0.  0.]
     [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  0.  0.  0.]
     [ 0.  1.  1.  1.  1.  0.  1.  0.  1.  1.  1.]
     [ 0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  0.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  0.  0.  1.  1.  1.  1.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  1.  1.]
     [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  0.  0.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  0.  0.  0.  0.  1.  1.  1.  1.  1.]
     [ 0.  1.  0.  0.  0.  0.  0.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  0.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  0.  1.  0.  0.  1.  1.  1.  1.  0.]
     [ 0.  1.  1.  1.  1.  1.  1.  0.  0.  0.  0.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  0.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  0.  1.  1.]
     [ 0.  1.  1.  1.  1.  0.  0.  0.  0.  0.  1.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  1.  1.  1.  1.  1.  0.  0.  0.  0.]
     [ 0.  1.  1.  0.  0.  1.  1.  0.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  0.  0.  1.  1.  1.  1.  0.  1.]
     [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  0.  0.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  0.  0.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  1.  0.  0.  0.  0.  0.  0.  0.]
     [ 0.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  0.  0.  0.  1.  1.  1.  1.  1.  0.]
     [ 0.  1.  0.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  1.  1.  0.  1.  1.  1.  1.  1.  1.  1.]
     [ 0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  1.]]
    View Code

    -----

    第四步:如何计算整体上每天盈利的概率

    这一步就相对简单了,直接求每一列的均值即可,数值上就代表当天盈利的概率了。

    http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.mean.html

    加入代码如下:

    mean=df_matrix.mean(0)
    print(mean)

    运行结果如下:

    [ 0.          0.62686567  0.65671642  0.64179104  0.65671642  0.71641791
      0.74626866  0.73134328  0.71641791  0.70149254  0.70149254]
    View Code

    结论:

    就目前时间段来看,破板后买入十个交易日内赚钱的概率还是蛮高的。

    这段程序只是第一次写,当然还有很多可以改进的地方,但想要的结果已经有了个大概了。下一步感兴趣可以把结果用可视化的输出表示出来,先去睡觉啦!

    总体来说还是比较满足的!

    哈哈哈

  • 相关阅读:
    【04】Vue 之 事件处理
    【03】Vue 之列表渲染及条件渲染
    【02】 Vue 之 数据绑定
    传递参数的模式最适合向函数传入大量可先参数的情形
    ie6 PNG图片透明
    实现表单的输入框当光标为当前时,去掉默认值
    SSIS ->> Environment Variables
    SQL Server ->> FileTable
    SQL Server ->> 间接实现COUNT(DISTINCT XXX) OVER(PARTITION BY YYY)
    SQL Server ->> EXECUTE AS LOGIN/USER和Revert表达式
  • 原文地址:https://www.cnblogs.com/sthv/p/6012838.html
Copyright © 2011-2022 走看看