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