感谢,转载自:https://www.joinquant.com/community/post/detailMobile?postId=1058&page=&limit=20&replyId=&tag=
感谢cjhren在他的基础上增加了等权PB。目前等权PBPE都全了。
目前市场大部分还在高位,只有大蓝筹便宜。
import numpy as np
import pandas as pd import matplotlib.pyplot as plt import bisect #指定日期的指数PE(等权重) def get_index_pe_date(index_code,date): stocks = get_index_stocks(index_code, date) q = query(valuation).filter(valuation.code.in_(stocks)) df = get_fundamentals(q, date) if len(df)>0: pe = len(df)/sum([1/p if p>0 else 0 for p in df.pe_ratio]) return pe else: return float('NaN') #指定日期的指数PB(等权重) def get_index_pb_date(index_code,date): stocks = get_index_stocks(index_code, date) q = query(valuation).filter(valuation.code.in_(stocks)) df = get_fundamentals(q, date) if len(df)>0: pb = len(df)/sum([1/p if p>0 else 0 for p in df.pb_ratio]) return pb else: return float('NaN') #指数历史PEPB def get_index_pe_pb(index_code): start='2005-1-1' #start='2015-1-1' end = pd.datetime.today(); dates=[] pes=[] pbs=[] for d in pd.date_range(start,end,freq='M'): #频率为月 dates.append(d) pes.append(get_index_pe_date(index_code,d)) pbs.append(