import pandas as pd
import numpy as np
from jqdata import *
def _get_stock_valuation_date(stock, date):
q = query(valuation).filter(valuation.code.in_(stock))
df = get_fundamentals(q, date)
return df['pe_ratio'][0]
def get_stock_valuation(code, start_date='2005-1-1', end_date=pd.datetime.today()):
''' 返回指定日期内的全市场PE/PB估值,默认按照月返回数据 '''
pes=[]
pbs=[]
time_list = pd.date_range(start_date, end_date,freq='M') # 频率为月
for i, d in enumerate(time_list):
pes.append(_get_stock_valuation_date([code], d))
df = pd.DataFrame({'PE' : pd.Series(pes, index=time_list),
})
df.columns.name = code
return df
def draw_stock_valuation(df_data, code="000001.XSHG", draw_va=False, draw_analysis=False):
name = get_security_info(code).display_name
price = get_price(code, start_date='2005-01-01', end_date=datetime.datetime.today())
plt.rcParams['figure.figsize'] = (16.0, 8.0)
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
fig, ax1 = plt.subplots()
ax1.plot_date(price.index, price['close'], fmt='b', label=name)
ax1.set_ylabel(r"point", fontsize=18, color="b")
for label in ax1.get_yticklabels():
label.set_color("b")
ax1.legend(loc='upper left')
ax2 = ax1.twinx()
ax2.plot_date(df_data.index, df_data['PE'], fmt='r.-.', label=name + ' PE估值')
ax2.legend(loc='upper right')
ax1.set_title(name + '估值图', fontsize=14)
plt.grid()
plt.show()
stock = "600704.XSHG"
df = get_stock_valuation(stock)
draw_stock_valuation(df, stock)