栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

数据分析项目-合集DAY1

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据分析项目-合集DAY1

1. 股票数据处理
#读取股票数据
df = ts.get_k_data(code = '600519', start =  '2000-01-01')
#存储文件
df.to_csv('./maotai.csv')

#读取文件
df = pd.read_csv('./maotai.csv')
df.head()

#删除某一列
df.drop(labels = 'Unnamed: 0', axis =1, inplace = True)
df.head()

#查看数据类型
df.info()

#time转为时间序列
df['date'] = pd.to_datetime(df['date'])
df.head()

#将date列作为源数据的行索引
df.set_index('date', inplace = True)
df.head()
2. 股票上涨日期
#收盘比开盘上涨3%的日期
df.loc[(df['open']-df['close']) / df['open'] >0.03].index
3. 股票跌幅的日期
#开盘比前日跌幅超过2%的日期
df.loc[(df['open']-df['close'].shift(1)) / df['close'].shift(1) < -0.02].index
4. 股票买卖收益分析
#股票买卖收益分析
new_df = df['2010-01':'2020-02']
df_monthly = new_df.resample('M').first()
cost = df_monthly['open'].sum()*100
df_yearly = new_df.resample('A').last()[:-1]
resv = df_yearly['open'].sum()*1200
last_price = 200*new_df['close'][-1]
total = last_price + resv - cost
5. 双均线绘制
#5日均线和30日均线
ma5 = df['close'].rolling(5).mean()
ma30 = df['close'].rolling(30).mean()
plt.plot(ma5)
plt.plot(ma30)
6. 金叉死叉
#金叉死叉
ma5 = ma5[30:]
ma30 = ma30[30:]
df = df[30:]
s1 = ma5 < ma30
s2 = ma5 > ma30
death_ex = s1 & s2.shift(1)
dead_date = df.loc[death_ex].index
golden_ex = ~(s1 | s2.shift(1))
golden_date = df.loc[golden_ex].index

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589800.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号