数据分析项目-合集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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值