三、示例:tushare股票分析

3.1 tushare模块

3.2 股票分析

  • demo

    import tushare as ts
    import pandas as pd
    
    # 调用接口
    df = ts.get_k_data(code='600519', start='2000-01-1')
    
    # 数据持久化到本地
    df.to_csv('./maotai.csv')
    
    # 读取本地文件数据
    df = pd.read_csv('./maotai.csv')
    """
    Unnamed: 0	date	open	close	high	low	volume	code
    0	0	2001-08-27	5.392	5.554	5.902	5.132	406318.00	600519
    1	1	2001-08-28	5.467	5.759	5.781	5.407	129647.79	600519
    2	2	2001-08-29	5.777	5.684	5.781	5.640	53252.75	600519
    3	3	2001-08-30	5.668	5.796	5.860	5.624	48013.06	600519
    4	4	2001-08-31	5.804	5.782	5.877	5.749	23231.48	600519
    """
    
    # 删除第一列无效信息列
    df.drop(labels='Unnamed: 0', axis=1, inplace=True)
    """
    date	open	close	high	low	volume	code
    0	2001-08-27	5.392	5.554	5.902	5.132	406318.00	600519
    1	2001-08-28	5.467	5.759	5.781	5.407	129647.79	600519
    2	2001-08-29	5.777	5.684	5.781	5.640	53252.75	600519
    3	2001-08-30	5.668	5.796	5.860	5.624	48013.06	600519
    4	2001-08-31	5.804	5.782	5.877	5.749	23231.48	600519
    """
    
    # 查看数据基本信息
    df.info()
    """
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 4705 entries, 0 to 4704
    Data columns (total 7 columns):
     #   Column  Non-Null Count  Dtype  
    ---  ------  --------------  -----  
     0   date    4705 non-null   object 
     1   open    4705 non-null   float64
     2   close   4705 non-null   float64
     3   high    4705 non-null   float64
     4   low     4705 non-null   float64
     5   volume  4705 non-null   float64
     6   code    4705 non-null   int64  
    dtypes: float64(5), int64(1), object(1)
    memory usage: 257.4+ KB
    """
    
    # 将date列转为时间格式
    df['date'] = pd.to_datetime(df['date'])
    
    # 将date列做为行索引
    df.set_index('date', inplace=True)
    
    
  • 输出该股票收盘比开盘高3%以上的日期

    # (收盘 - 开盘) / 开盘 > 0.03
    (df['close'] - df['open']) / df['open'] > 0.03
    """
    date
    2001-08-27    False
    2001-08-28    False
    2001-08-29    False
    2001-08-30    False
    2001-08-31    False
                  ...  
    2021-05-06    False
    2021-05-07     True
    2021-05-10    False
    2021-05-11    False
    2021-05-12    False
    Length: 4705, dtype: bool
    """
    
    # 将上面的bool列做为loc索引,可直接得到True对应的行
    df.loc[(df['open']-df['close'])/df['open']>0.03]
    """
    open	close	high	low	volume	code
    date						
    2001-10-10	5.827	5.640	5.848	5.629	17548.69	600519
    2001-11-07	5.468	5.288	5.468	5.281	9325.38	600519
    2001-11-16	5.656	5.326	5.656	5.310	9954.70	600519
    2001-12-20	5.609	5.431	5.609	5.413	11362.23	600519
    2002-01-04	6.049	5.866	6.062	5.851	29109.62	600519
    ...	...	...	...	...	...	...
    2021-02-24	2307.990	2189.000	2318.000	2160.500	82116.00	600519
    2021-03-02	2180.000	2058.000	2180.000	2033.000	70765.00	600519
    2021-03-08	2074.960	1960.000	2085.000	1960.000	63100.00	600519
    2021-03-15	2050.000	1975.450	2069.800	1951.150	62485.00	600519
    2021-05-07	1965.000	1903.000	1967.980	1901.000	52493.00	600519
    """
    
    # 取出行索引,即为对应日期
    df.loc[(df['open']-df['close'])/df['open']>0.03].index
    """
    DatetimeIndex(['2001-10-10', '2001-11-07', '2001-11-16', '2001-12-20',
                   '2002-01-04', '2002-01-17', '2002-01-28', '2002-04-17',
                   '2002-11-08', '2003-01-02',
                   ...
                   '2020-07-16', '2020-07-24', '2021-01-19', '2021-02-18',
                   '2021-02-22', '2021-02-24', '2021-03-02', '2021-03-08',
                   '2021-03-15', '2021-05-07'],
                  dtype='datetime64[ns]', name='date', length=177, freq=None)
    """
    
    
  • 输出该股票开盘比前日收盘跌幅超过2%的日期

    # (今日开盘 - 前日收盘) / 前日收盘 > 0.02
    # close列下移一位:df['close'].shift(1)
    (df['open'] - df['close'].shift(1)) / df['close'].shift(1) > 0.02
    
    # 将上面的bool列做为loc索引,可直接得到True对应的行
    df.loc[(df['open'] - df['close'].shift(1)) / df['close'].shift(1) > 0.02]
    
    # 取出行索引,即为对应日期
    df.loc[(df['open'] - df['close'].shift(1)) / df['close'].shift(1) > 0.02].index
    
    
  • 如果一个人从2010年1月1日开始,每月第一个交易日买入100支股票,每年最后一个交易日卖出所有股票,到今天为止的收益如何?

    """
    开盘买:1个完整年买1200支股票
    收盘卖:1个完整年卖1200支股票
    """
    
    # 对时间切片
    new_df = df['2010-01-01': ]
    
    # 每个月的第一个交易日:得到的行索引是最后一个,但是数据没问题
    # resample,重新取样
    df_monthly = new_df.resample('M').first()
    """
    						open	close	high	low	volume	code
    date						
    2010-01-31	109.760	108.446	109.760	108.044	44304.88	600519
    2010-02-28	107.769	107.776	108.216	106.576	29655.94	600519
    2010-03-31	106.219	106.085	106.857	105.925	21734.74	600519
    2010-04-30	101.324	102.141	102.422	101.311	23980.83	600519
    2010-05-31	81.676	82.091	82.678	80.974	23975.16	600519
    ...	...	...	...	...	...	...
    2021-01-31	1999.980	1997.000	2004.990	1983.810	43514.00	600519
    2021-02-28	2130.000	2109.320	2160.000	2095.000	29341.00	600519
    2021-03-31	2179.000	2158.000	2179.000	2120.000	44916.00	600519
    2021-04-30	2021.000	2044.500	2046.800	2001.220	26588.00	600519
    2021-05-31	2000.000	1959.000	2006.840	1953.000	53546.00	600519
    """
    
    # 买股票花费总金额
    cost = df_monthly['open'].sum() * 100
    
    # 每年最后一个交易日,今年除外
    df_yearly = new_df.resample('A').last()[:-1]
    
    # 卖出股票赚的钱:今年的股票不卖
    resv = df_yearly['open'].sum() * 1200
    
    # 剩余股票
    last_money = 500 * new_df['close'][-1]
    
    # 收益
    money = resv + last_money - cost
    
    
上一篇:01背包问题


下一篇:纪中DAY1 2021.07.12【NOIP提高A组】模拟