一.TuShare简介和环境安装
TuShare是一个著名的免费、开源的python财经数据接口包。其官网主页为:TuShare -财经数据接口包。该接口包如今提供了大量的金融数据,涵盖了股票、基本面、宏观、新闻的等诸多类别数据(具体请自行查看官网),并还在不断更新中。TuShare可以基本满足量化初学者的回测需求
环境安装:pip install tushare。如果是老版本升级,可以用升级命令pip install tushare --upgrade3,在python中导入包:import tushare as ts
二.Tushare的应用
我们主要还是应该掌握如何用tushare获取股票行情数据,使用的是ts.get_hist_data()函数或者ts.get_k_data()函数。
输入参数为:
code:股票代码,即6位数字代码,或者指数代码(sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板)
start:开始日期,格式YYYY-MM-DD
end:结束日期,格式YYYY-MM-DD
ktype:数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
retry_count:当网络异常后重试次数,默认为3
pause:重试时停顿秒数,默认为0
返回值说明:
date:日期
open:开盘价
high:最高价
close:收盘价
low:最低价
volume:成交量
price_change:价格变动
p_change:涨跌幅
ma5:5日均价
ma10:10日均价
ma20:20日均价
v_ma5:5日均量
v_ma10:10日均量
v_ma20:20日均量
turnover:换手率[注:指数无此项]
1:使用tushare包获取某股票的历史行情数据。
import tushare as ts
# 使用tushare包获取某股票的历史行情数据。
df = ts.get_k_data(code='600519',start='2000-01-01')
# 将从Tushare中获取的数据存储至本地
df.to_csv("600519.csv")
# 将原数据中的时间作为行索引,并将字符串类型的时间序列化成时间对象类型
# 将date这一列作为源数据的行索引且将数据类型转成时间类型
df = pd.read_csv('./600519.csv',index_col='date',parse_dates=['date'])
df.drop(labels='Unnamed: 0',axis=1,inplace=True)
# 多出来一行 Unnamed: 0 ,需要去掉它
# inplace默认值为false 将删除的操作映射到原数据
2:输出该股票所有收盘比开盘上涨3%以上的日期。
#指定条件
#输出该股票所有收盘比开盘上涨3%以上的日期。
#(收盘-开盘)/开盘 >= 0.03
df['close'] - df['open'] / df['open'] >= 0.03
# 打印结果:
date
2001-08-27 True
2001-08-28 True
2001-08-29 True
2001-08-30 True
2001-10-12 True
...
2019-08-02 True
2019-08-05 True
2019-08-06 True
2019-08-07 True
2019-08-08 True
2019-08-09 True
#将上述表达式返回的布尔值作为df的行索引:取出了所有符合需求的行数据
df.loc[(df['close']-df['open']) / df['open'] >= 0.03]
# 打印结果:
open close high low volume code
date
2001-08-27 5.392 5.554 5.902 5.132 406318.00 600519
2001-08-28 5.467 5.759 5.781 5.407 129647.79 600519
2001-09-10 5.531 5.734 5.757 5.470 18878.89 600519
... ... ... ... ... ... ...
2004-11-25 9.251 9.561 9.676 9.251 5924.14 600519
... ... ... ... ... ... ...
2017-11-16 676.406 709.043 709.881 676.406 60716.00 600519
... ... ... ... ... ... ...
2019-04-10 903.000 947.990 951.900 900.000 67814.00 600519
2019-04-16 904.900 939.900 939.900 901.220 46423.00 600519
2019-05-10 875.660 907.120 910.780 868.190 79907.00 600519
2019-05-15 890.240 927.000 933.000 890.240 63124.00 600519
2019-06-11 876.000 910.890 915.610 875.000 80106.00 600519
2019-06-20 932.500 975.000 975.500 932.200 67271.00 600519
df.loc[(df['close'] - df['open']) / df['open'] >= 0.03].index
# index 取到行索引
df.loc[(df['close'] - df['open']) / df['open'] >= 0.03].index
# 打印结果:
DatetimeIndex(['2001-08-27', '2001-08-28', '2001-09-10', '2001-12-21',
'2002-01-18', '2002-01-31', '2003-01-14', '2003-10-29',
'2004-01-05', '2004-01-14',
...
'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'],
dtype='datetime64[ns]', name='date', length=301, freq=None)
3:输出该股票所有开盘比前日收盘跌幅超过2%的日期。
#输出该股票所有开盘比前日收盘跌幅超过2%的日期。
#(开盘 - 前日收盘) / 前日收盘 < -0.02
# df['close'].shift(1)) 收盘数据往下移一位
(df['open'] - df['close'].shift(1)) / df['close'].shift(1) < -0.02
# 打印结果
DatetimeIndex(['2001-09-12', '2002-06-26', '2002-12-13', '2004-07-01',
'2004-10-29', '2006-08-21', '2006-08-23', '2007-01-25',
'2007-02-01', '2007-02-06', '2007-03-19', '2007-05-21',
'2007-05-30', '2007-06-05', '2007-07-27', '2007-09-05',
'2007-09-10', '2008-03-13', '2008-03-17', '2008-03-25',
'2008-03-27', '2008-04-22', '2008-04-23', '2008-04-29',
'2008-05-13', '2008-06-10', '2008-06-13', '2008-06-24',
'2008-06-27', '2008-08-11', '2008-08-19', '2008-09-23',
'2008-10-10', '2008-10-15', '2008-10-16', '2008-10-20',
'2008-10-23', '2008-10-27', '2008-11-06', '2008-11-12',
'2008-11-20', '2008-11-21', '2008-12-02', '2009-02-27',
'2009-03-25', '2009-08-13', '2010-04-26', '2010-04-30',
'2011-08-05', '2012-03-27', '2012-08-10', '2012-11-22',
'2012-12-04', '2012-12-24', '2013-01-16', '2013-01-25',
'2013-09-02', '2014-04-25', '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'],
dtype='datetime64[ns]', name='date', freq=None)
# 取出符合要求的行数据
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
# 执行结果为:
DatetimeIndex(['2001-09-12', '2002-06-26', '2002-12-13', '2004-07-01',
'2004-10-29', '2006-08-21', '2006-08-23', '2007-01-25',
'2007-02-01', '2007-02-06', '2007-03-19', '2007-05-21',
'2007-05-30', '2007-06-05', '2007-07-27', '2007-09-05',
'2007-09-10', '2008-03-13', '2008-03-17', '2008-03-25',
'2008-03-27', '2008-04-22', '2008-04-23', '2008-04-29',
'2008-05-13', '2008-06-10', '2008-06-13', '2008-06-24',
'2008-06-27', '2008-08-11', '2008-08-19', '2008-09-23',
'2008-10-10', '2008-10-15', '2008-10-16', '2008-10-20',
'2008-10-23', '2008-10-27', '2008-11-06', '2008-11-12',
'2008-11-20', '2008-11-21', '2008-12-02', '2009-02-27',
'2009-03-25', '2009-08-13', '2010-04-26', '2010-04-30',
'2011-08-05', '2012-03-27', '2012-08-10', '2012-11-22',
'2012-12-04', '2012-12-24', '2013-01-16', '2013-01-25',
'2013-09-02', '2014-04-25', '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'],
dtype='datetime64[ns]', name='date', freq=None)
4:假如我从2010年1月1日开始,每月第一个交易日买入1手股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
price_last = df['open'][-1]
df = df['2010-01':'2019-01'] #剔除首尾无用的数据
#Pandas提供了resample函数用便捷的方式对时间序列进行重采样,根据时间粒度的变大或者变小分为降采样和升采样:
df_monthly = df.resample("M").first()
df_yearly = df.resample("A").last()[:-1]
#去除最后一年
# [:-1] 把19年去掉,还没到19年底,19年只买了,还没卖
ost_money
cost_money = df_monthly['open'].sum()*100
# cost_money 3339687.1
df_yearly['open'].sum()*1200
# 12个月 一个月买100支 2948584.7999999993
recv_monry = df['open'][-1] * 800 + df_yearly['open'].sum()*1200
# df['open'][-1] * 800 为19年还剩的钱,今天是8月份 800支
recv_monry - cost_money
# 391697.69999999925
循环的方式实现
price_last = df['open'][-1]
df = df['2010-01':'2019-01'] #剔除首尾无用的数据
#Pandas提供了resample函数用便捷的方式对时间序列进行重采样,根据时间粒度的变大或者变小分为降采样和升采样:
df_monthly = df.resample("M").first()
df_yearly = df.resample("A").last()[:-1]
#去除最后一年
# [:-1] 把19年去掉,还没到19年底,19年只买了,还没卖
cost_money = 0
hold = 0 #每年持有的股票
for year in range(2010, 2019):
cost_money -= df_monthly.loc[str(year)]['open'].sum()*100
hold += len(df_monthly[str(year)]['open']) * 100
if year != 2019:
cost_money += df_yearly[str(year)]['open'][0] * hold
hold = 0 #每年持有的股票
cost_money += hold * price_last
print(cost_money)