股票走势及K线绘制练习代码笔记(无注释)

股票走势及K线绘制练习代码笔记(无注释)

###所需要安装的新库:tushare;mpl_finance
#22.04.09
# In[走势图]
import tushare as ts
df=ts.get_k_data('000002',start='2000-01-01',end='2022-01-01')
df.head()

import tushare
print(tushare.__version__)

df.set_index('date',inplace=True)

df['close'].plot()

import matplotlib.pyplot as plt
from datetime import datetime
import tushare as ts
df=ts.get_k_data('000002',start='2000-01-01',end='2022-01-01')
df['date']=df['date'].apply(lambda x:datetime.strptime(x, '%Y-%m-%d'))
plt.figure(dpi=300,figsize=(12,6))
plt.plot(df['date'],df['close'])
plt.show()
type(df)

# In[K线图]

import tushare as ts
import matplotlib.pyplot as plt
import mpl_finance as mpf
import seaborn as sns

df=ts.get_k_data('000002','2019-06-01','2019-09-30')

from matplotlib.pylab import date2num
import datetime

def date_to_num(dates):
    num_time=[]
    for date in dates:
        date_time=datetime.datetime.strptime(date,'%Y-%m-%d')
        num_date=date2num(date_time)
        num_time.append(num_date)
    return num_time

df_arr=df.values
df_arr[:,0]=date_to_num(df_arr[:,0])

df_arr[:5]

fig,ax=plt.subplots(figsize=(15,6))
mpf.candlestick_ochl(ax,df_arr,width=0.6,colorup='r',colordown='g',alpha=1.0)
plt.grid(True)
ax.xaxis_date()

df['MA5']=df['close'].rolling(5).mean()
df['MA10']=df['close'].rolling(10).mean()

df.head(15)
    
plt.rcParams['font.sans-serif']='SimHei'

#fig,ax=plt.subplots(figsize=(15,6))


fig,axes=plt.subplots(2,1,sharex=True,figsize=(15,8))
ax1,ax2=axes.flatten()
mpf.candlestick_ochl(ax1,df_arr,width=0.6,colorup='r',colordown='g',alpha=1.0)
ax1.plot(df_arr[:,0],df['MA5'],label='5日均线')
ax1.plot(df_arr[:,0],df['MA10'],label='10日均线')
ax1.grid(True)
ax1.set_title('万科A')
ax1.set_xlabel('日期',x=1,y=-0.1)
ax1.set_ylabel('价格',x=-0.1,y=1)
ax1.legend()
ax.xaxis_date()

ax2.bar(df_arr[:,0],df_arr[:,5])
ax2.set_xlabel('日期',x=1,y=-0.1)
ax2.set_ylabel('成交量',x=-0.1,y=1)
ax2.grid()
ax2.xaxis_date()

结果1:

在这里插入图片描述

结果2:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值