股票分析

需求:股票分析

1.使用tushare包获取某股票的历史行情数据
2.输出该股票所有收盘比开盘上涨3%以上的日期
3.输出该股票所有开盘比前日收盘跌幅超2%的日期
4.假如从2017年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,迄今为止收益如何?

1
2
3
import tushare as ts
import pandas as pd
import numpy as np
1
2
3
4
df = ts.get_k_data(code='600519',start='2015-01-01')
df.to_csv('moutai.csv')
df = pd.read_csv('moutai.csv')
df.head()
本接口即将停止更新,请尽快使用Pro版接口:https://waditu.com/document/2

Unnamed: 0 date open close high low volume code
0 0 2015-01-05 161.056 172.013 173.474 160.266 94515.0 600519
1 1 2015-01-06 169.872 168.029 172.047 166.492 55020.0 600519
2 2 2015-01-07 166.509 163.876 169.448 161.370 54797.0 600519
3 3 2015-01-08 164.776 162.874 165.218 161.498 40525.0 600519
4 4 2015-01-09 161.719 161.642 166.280 161.472 53982.0 600519
1
2
df.drop(labels='Unnamed: 0',axis=1,inplace=True) # drop中axis与常规相反,1是垂直方向,0是水平方向
df.head()

date open close high low volume code
0 2015-01-05 161.056 172.013 173.474 160.266 94515.0 600519
1 2015-01-06 169.872 168.029 172.047 166.492 55020.0 600519
2 2015-01-07 166.509 163.876 169.448 161.370 54797.0 600519
3 2015-01-08 164.776 162.874 165.218 161.498 40525.0 600519
4 2015-01-09 161.719 161.642 166.280 161.472 53982.0 600519
1
2
print(df['date'].dtype)
df.info() # 查看每一列的数据类型
object
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1536 entries, 0 to 1535
Data columns (total 7 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   date    1536 non-null   object 
 1   open    1536 non-null   float64
 2   close   1536 non-null   float64
 3   high    1536 non-null   float64
 4   low     1536 non-null   float64
 5   volume  1536 non-null   float64
 6   code    1536 non-null   int64  
dtypes: float64(5), int64(1), object(1)
memory usage: 84.1+ KB
1
2
3
df['date'] = pd.to_datetime(df['date']) # 将日期时间转换为时间序列格式
df.info()
df.head()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1536 entries, 0 to 1535
Data columns (total 7 columns):
 #   Column  Non-Null Count  Dtype         
---  ------  --------------  -----         
 0   date    1536 non-null   datetime64[ns]
 1   open    1536 non-null   float64       
 2   close   1536 non-null   float64       
 3   high    1536 non-null   float64       
 4   low     1536 non-null   float64       
 5   volume  1536 non-null   float64       
 6   code    1536 non-null   int64         
dtypes: datetime64[ns](1), float64(5), int64(1)
memory usage: 84.1 KB

date open close high low volume code
0 2015-01-05 161.056 172.013 173.474 160.266 94515.0 600519
1 2015-01-06 169.872 168.029 172.047 166.492 55020.0 600519
2 2015-01-07 166.509 163.876 169.448 161.370 54797.0 600519
3 2015-01-08 164.776 162.874 165.218 161.498 40525.0 600519
4 2015-01-09 161.719 161.642 166.280 161.472 53982.0 600519
1
2
3
df.set_index('date',inplace=True)   # set_index重新设置索引,inplace表示是否作用于源数据
df.head()
print(df)
                open     close      high       low   volume    code
date                                                               
2015-01-05   161.056   172.013   173.474   160.266  94515.0  600519
2015-01-06   169.872   168.029   172.047   166.492  55020.0  600519
2015-01-07   166.509   163.876   169.448   161.370  54797.0  600519
2015-01-08   164.776   162.874   165.218   161.498  40525.0  600519
2015-01-09   161.719   161.642   166.280   161.472  53982.0  600519
...              ...       ...       ...       ...      ...     ...
2021-04-19  2055.000  2088.000  2098.360  2033.000  31754.0  600519
2021-04-20  2071.100  2094.800  2129.000  2070.000  28290.0  600519
2021-04-21  2076.000  2080.000  2097.800  2065.100  26150.0  600519
2021-04-22  2089.900  2055.500  2099.000  2051.500  26850.0  600519
2021-04-23  2055.970  2108.940  2119.880  2052.500  33463.0  600519

[1536 rows x 6 columns]

2.输出该股票所有收盘比开盘上涨3%以上的日期
(收盘-开盘)/开盘>0.03

1
2
3
(df['close']-df['open'])/df['open']>0.03    # 返回应为boolean类型
df.loc[(df['close']-df['open'])/df['open']>0.03] # 获取了True所对应的行数据
df.loc[(df['close']-df['open'])/df['open']>0.03].index # 获取对应行的索引
DatetimeIndex(['2015-01-05', '2015-02-09', '2015-03-09', '2015-04-16',
               '2015-04-21', '2015-05-08', '2015-05-19', '2015-05-22',
               '2015-05-25', '2015-06-08', '2015-06-23', '2015-06-24',
               '2015-06-30', '2015-07-08', '2015-07-09', '2015-07-10',
               '2015-08-25', '2015-08-26', '2015-08-27', '2015-08-31',
               '2015-09-14', '2015-11-30', '2015-12-02', '2015-12-21',
               '2016-01-14', '2016-01-19', '2016-03-04', '2016-03-15',
               '2016-03-24', '2016-04-06', '2016-05-03', '2016-05-31',
               '2016-06-03', '2016-06-27', '2016-07-06', '2016-07-26',
               '2016-12-06', '2017-01-04', '2017-02-20', '2017-04-25',
               '2017-08-14', '2017-10-19', '2017-10-27', '2017-11-10',
               '2017-11-16', '2017-11-28', '2017-12-11', '2017-12-28',
               '2018-01-09', '2018-01-31', '2018-04-19', '2018-05-07',
               '2018-05-28', '2018-06-04', '2018-06-20', '2018-08-09',
               '2018-08-21', '2018-08-27', '2018-09-18', '2018-09-26',
               '2018-10-19', '2018-10-31', '2018-11-13', '2018-12-28',
               '2019-01-15', '2019-02-11', '2019-03-01', '2019-03-18',
               '2019-04-10', '2019-04-16', '2019-05-10', '2019-05-15',
               '2019-06-11', '2019-06-20', '2019-09-12', '2019-09-18',
               '2020-02-11', '2020-03-02', '2020-03-05', '2020-03-10',
               '2020-04-02', '2020-04-22', '2020-05-06', '2020-05-18',
               '2020-07-02', '2020-07-06', '2020-07-07', '2020-07-13',
               '2020-12-30', '2021-01-05', '2021-01-12', '2021-01-25',
               '2021-02-04', '2021-02-09', '2021-02-10', '2021-03-03',
               '2021-03-05', '2021-03-11', '2021-04-02'],
              dtype='datetime64[ns]', name='date', freq=None)

3.输出该股票所有开盘比前日收盘跌幅超2%的日期
(开盘-前日收盘)/前日收盘<-0.02

1
df.head()

open close high low volume code
date
2015-01-05 161.056 172.013 173.474 160.266 94515.0 600519
2015-01-06 169.872 168.029 172.047 166.492 55020.0 600519
2015-01-07 166.509 163.876 169.448 161.370 54797.0 600519
2015-01-08 164.776 162.874 165.218 161.498 40525.0 600519
2015-01-09 161.719 161.642 166.280 161.472 53982.0 600519
1
2
df['close'].shift(1)    # shift(1)使close整体下移一位,-1为上移;移动后两列相减即可实现开盘-前日收盘
df.loc[(df['open']-df['close'].shift(1))/df['close'].shift(1)<-0.02].index
DatetimeIndex(['2015-01-19', '2015-05-25', '2015-07-03', '2015-07-08',
               '2015-07-13', '2015-08-24', '2015-09-02', '2015-09-15',
               '2017-11-17', '2018-02-06', '2018-02-09', '2018-03-23',
               '2018-03-28', '2018-07-11', '2018-10-11', '2018-10-24',
               '2018-10-25', '2018-10-29', '2018-10-30', '2019-05-06',
               '2019-05-08', '2019-10-16', '2020-01-02', '2020-02-03',
               '2020-03-13', '2020-03-23', '2020-10-26', '2021-02-26',
               '2021-03-04'],
              dtype='datetime64[ns]', name='date', freq=None)

4.假如从2017年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,迄今为止收益如何?
①时间节点拆分:2017-2021
②一手股票:100股
③买股票:
找出每个月第一个交易日–>每月第一行数据(每年共1200股)
卖股票:
每年最后一个交易日将所有股票全部卖出(特殊情况:该分析进行时,当年买入的股票由于没到该年最后一天所以无法卖出)
最后手中剩余的股票要估量其价值计算到总收益当中
买卖股票的单价:可以开盘价为准

1
2
3
4
5
6
new_df = df['2017-01':'2021-03']    # 只有在索引为时间序列时才可如此切分(pd.to_datetime())
print(new_df)
new_df.resample('M').first() # 根据月份重新取样;取每月的第一行数据;此处索引有问题但数据确为每月第一行
df_monthly = new_df.resample('M').first()
cost = df_monthly['open'].sum()*100 # 总花费
print(cost)
                open     close      high       low   volume    code
date                                                               
2017-01-03   324.689   324.961   327.331   323.261  20763.0  600519
2017-01-04   325.019   341.813   342.066   325.000  65257.0  600519
2017-01-05   339.958   336.792   341.366   335.529  41704.0  600519
2017-01-06   336.694   340.696   349.457   336.170  68095.0  600519
2017-01-09   337.821   338.511   342.755   336.597  35405.0  600519
...              ...       ...       ...       ...      ...     ...
2021-03-25  1970.010  1971.000  1988.880  1946.800  31575.0  600519
2021-03-26  1985.000  2013.000  2022.000  1958.000  50016.0  600519
2021-03-29  2043.200  2034.100  2096.350  2026.150  56992.0  600519
2021-03-30  2040.000  2056.050  2086.000  2035.080  32627.0  600519
2021-03-31  2045.100  2009.000  2046.020  2000.000  37154.0  600519

[1032 rows x 6 columns]
4817018.8
1
2
3
4
5
6
df_yearly = new_df.resample('A').last()[:-1]   # 最后2021年一行的数据需要忽略掉
print(df_yearly)
resv = df_yearly['open'].sum()*1200 # 截止2020年末收益
resv = 300*df['close'][-1] + resv # 加上2021年前三个月的收益,该收益以昨日收盘价计价
resv = resv-cost
print(resv)
                open     close      high       low   volume    code
date                                                               
2017-12-31   707.948   687.725   716.329   681.918  76038.0  600519
2018-12-31   563.300   590.010   596.400   560.000  63678.0  600519
2019-12-31  1183.000  1183.000  1188.000  1176.510  22588.0  600519
2020-12-31  1941.000  1998.000  1998.980  1939.000  38860.0  600519
1089960.7999999998
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2022 Woody
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信