目录
3.假如我从2010年1月1日开始,每月第一个交易日买入1手(100支)股票,每年最后一个交易日卖出所有股票,到今天为止,我的收益如何?
将金叉日期和死叉日期分别存储到两个Series中,将日期作为Series的索引,将0和1作为Series的value值
tushare财经数据接口包
- pip install tushare
- 作用:提供相关指定的财经数据
- 相关文档:Tushare -财经数据接口包
需求:股票分析
使用tushare包获取某股票的历史行情数据。
- tushare财经数据接口包,基于该模块可以获取任意股票的历史交易数据
- pip install tushare
数据处理
使用tushare包获取某股票的历史行情数据
df = ts.get_k_data(code='600519',start='2010-01-10')
df的持久化存储 df.to_xxx()
#将df的数据存储到本地
df.to_csv('./maotai.csv')
加载外部数据 pd.read_xxx()
#加载外部数据到df中:read_xxx()
df=pd.read_csv('./maotai.csv')
print(df.head())
输出结果
Unnamed: 0 date open close high low volume code
0 5 2010-01-11 104.400 102.926 105.230 102.422 24461.03 600519
1 6 2010-01-12 103.028 105.708 106.040 102.492 31063.40 600519
2 7 2010-01-13 104.649 103.022 105.389 102.741 37924.44 600519
3 8 2010-01-14 103.379 107.552 107.974 103.379 46454.64 600519
4 9 2010-01-15 107.533 108.401 110.641 107.533 45938.50 600519
将Unnamed: 0列进行删除
在drop系列的函数中
- axis=0表示的行,1表示的是列
- inplace=True将删除操作直接作用原始数据
#将Unnamed: 0列进行删除
df.drop(labels="Unnamed: 0",axis=1,inplace=True) #inplace=True代表原列删掉 不保存
print(df.head())
输出结果:Unnamed: 0已经没有了
date open close high low volume code
0 2010-01-11 104.400 102.926 105.230 102.422 24461.03 600519
1 2010-01-12 103.028 105.708 106.040 102.492 31063.40 600519
2 2010-01-13 104.649 103.022 105.389 102.741 37924.44 600519
3 2010-01-14 103.379 107.552 107.974 103.379 46454.64 600519
4 2010-01-15 107.533 108.401 110.641 107.533 45938.50 600519
将date列的字符串类型的时间转换成时间序列类型
#将date列的字符串类型的时间转换成时间序列类型
df["date"]=pd.to_datetime(df["date"])
print(df["date"].dtype)
输出结果:date列已经变成了一个时间类型
datetime64[ns]
将date列作为源数据的行索引
#将date列作为源数据的行索引
df.set_index("date",inplace=True)
print(df.head())
输出结果:时间列作为了行索引
open close high low volume code
date
2010-01-11 104.400 102.926 105.230 102.422 24461.03 600519
2010-01-12 103.028 105.708 106.040 102.492 31063.40 600519
2010-01-13 104.649 103.022 105.389 102.741 37924.44 600519
2010-01-14 103.379 107.552 107.974 103.379 46454.64 600519
2010-01-15 107.533 108.401 110.641 107.533 45938.50 600519
数据分析
1.输出该股票所有收盘比开盘上涨3%以上的日期。
(收盘-开盘)/开盘 > 0.03
print((df['close']-df['open'])/df['open']>0.03)
输出结果
date
2010-01-11 False
2010-01-12 False
2010-01-13 False
2010-01-14 True
2010-01-15 False
...
2021-11-16 False
经验:在df的相关操作中如果一旦返回了布尔值,下一步马上将布尔值作为原始数据的行索引
将满足需求的行数据获取(收盘比开盘上涨3%以上)
# 发现布尔值可以作为df的行索引,可以直接取出true对应的行数据
df2=df.loc[(df['close'] - df['open']) / df['open'] > 0.03]
print(df2.head())
输出结果:只保留了true的行
date
2010-01-14 103.379 107.552 107.974 103.379 46454.64 600519
2010-01-28 104.566 107.974 108.708 104.336 30267.52 600519
2010-04-21 91.573 94.458 94.579 90.431 63365.87 600519
2010-05-07 80.406 82.965 83.195 80.087 31254.76 600519
2010-05-21 82.576 85.645 85.894 81.982 27341.38 600519
Process finished with exit code 0
获取满足要求的日期
#获取满足要求的日期
print(df2.index)
输出结果
DatetimeIndex(['2010-01-14', '2010-01-28', '2010-04-21', '2010-05-07',
'2010-05-21', '2010-08-02', '2010-08-30', '2010-10-20',
'2010-11-10', '2010-11-23',
...
'2021-05-25', '2021-07-28', '2021-08-02', '2021-08-10',
'2021-08-24', '2021-09-01', '2021-09-17', '2021-09-24',
'2021-09-27', '2021-10-13'],
dtype='datetime64[ns]', name='date', length=172, freq=None)
Process finished with exit code 0
2.输出该股票所有开盘比前日收盘跌幅超过2%的日期。
- (开盘 - 前日收盘) / 前日收盘 < -0.02
- shift(n) 可以使该列整体移动n行,正数表示下移,负数表示上移
- df['close'].shift(1)代表前日收盘的数