1 Star 2 Fork 2

crossin/TinyCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
stock_price.py 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
crossin 提交于 2020-12-04 15:09 . 股票价格区间分布
#!/usr/bin/env python
# coding: utf-8
# In[32]:
# 更多代码,请关注公众号:Crossin的编程教室
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tushare as ts
from datetime import *
#设置显示中文
plt.rcParams['font.sans-serif'] = 'SimHei' # windows可设为SimHei,mac可设为Hiragino Sans GB
ts.set_token('你的tushare token')
pro = ts.pro_api()
ts_code = '000001.SZ'
index_df = pro.daily(ts_code=ts_code, start_date='20130101', end_date='20201203')
# index_df = pro.index_dailybasic(ts_code = "000001.SH",start_date = "20130101",
# end_date = "20200311",fields="trade_date,pe_ttm")
index_df.sort_values(by="trade_date",inplace=True)
x = pd.to_datetime(index_df["trade_date"]).values
y = index_df["close"].values
# y = index_df["pe_ttm"].values
index_df
# print(x,y)
# In[33]:
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y)
# In[34]:
max = np.max(y)
pe_80 = np.percentile(y,80)
pe_20 = np.percentile(y,20)
min = np.min(y)
now = y[-1] #为了后面标注最新收盘价用
# In[35]:
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y,linewidth=3)
ax.fill_between(x,min-1,pe_20,facecolor = "#00ff80",alpha=0.2)
ax.fill_between(x,pe_20,pe_80,facecolor = "#ffff4d",alpha=0.2)
ax.fill_between(x,pe_80,max+1,facecolor = "#ff69b4",alpha=0.2)
# In[36]:
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y,linewidth=3)
ax.fill_between(x,min-1,pe_20,facecolor = "#00ff80",alpha=0.2)
ax.fill_between(x,pe_20,pe_80,facecolor = "#ffff4d",alpha=0.2)
ax.fill_between(x,pe_80,max+1,facecolor = "#ff69b4",alpha=0.2)
ax.tick_params(axis='both', which='both',length=0)
ax.tick_params(axis='both', which='major', labelsize=16)
ax.margins(0.01,0)
ax.text(0.75,0.9,"最新收盘价:{}".format(now),transform=ax.transAxes,fontdict={'size':18})
plt.xticks(rotation=45)
plt.box(False)
plt.title("{} 价格分布区间".format(ts_code), fontdict={'size':24})
# In[37]:
def pe_plot(ts_code = "",name="",period=5):
#准备数据
now = datetime.now()
end_date = str(now.date()).replace("-","")
start_date = str(now.year-period)+end_date[4:]
index_df = pro.daily(ts_code=ts_code, start_date='20130101', end_date='20201203')
index_df.sort_values(by="trade_date",inplace=True)
x = pd.to_datetime(index_df["trade_date"]).values
y = index_df["close"].values
#划定分割范围
max = np.max(y)
pe_80 = np.percentile(y,80)
pe_20 = np.percentile(y,20)
min = np.min(y)
now = y[-1]
#绘图
fig,ax = plt.subplots(figsize=(12,8))
ax.plot(x,y,linewidth=3)
ax.fill_between(x,min-1,pe_20,facecolor = "#00ff80",alpha=0.2)
ax.fill_between(x,pe_20,pe_80,facecolor = "#ffff4d",alpha=0.2)
ax.fill_between(x,pe_80,max+1,facecolor = "#ff69b4",alpha=0.2)
ax.tick_params(axis='both', which='both',length=0)
ax.tick_params(axis='both', which='major', labelsize=16)
ax.margins(0.01,0)
ax.text(0.75,0.9,"最新收盘价:{}".format(now),
transform=ax.transAxes,fontdict={'size':18})
plt.xticks(rotation=45)
plt.box(False)
plt.title("{} 价格分布区间".format(ts_code),fontdict={'size':24})
plt.show()
# In[38]:
pe_plot(ts_code="600519.SH",period=5)
# In[ ]:
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/crossin/tiny-code.git
git@gitee.com:crossin/tiny-code.git
crossin
tiny-code
TinyCode
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385