数据采集20200618(市场平均PE序列)

#市场平均PE序列

BDate=”20200101″

EDate=”20200618″

calendar=DataAPI.TradeCalGet(exchangeCD=u”XSHG”,beginDate=BDate,endDate=EDate,field=u””,pandas=”1″)

calendar=calendar[calendar.isMonthEnd==1][calendar.isOpen==1]#[calendar.isQuarterEnd==1]#只取月末日期

factor_list=[]

for tdate in calendar.calendarDate.values:#拿到因子数据,遍历每一天

data=DataAPI.MktStockFactorsOneDayGet(secID=u””,tradeDate=tdate,field=u”ticker,tradeDate,PE,PB”,pandas=”1″)

data=data.set_index(‘ticker’)#将ticker作为索引

after_winsorizepe=winsorize(data[‘PE’].to_dict())#PE值dict化,并去极值

after_winsorizepb=winsorize(data[‘PB’].to_dict())

data[‘winsorized_PE’]=np.nan

data[‘winsorized_PB’]=np.nan

data.loc[after_winsorizepe.keys(),’winsorized_PE’]=after_winsorizepe.values()#新建winsorized PE列,赋值after_winsorize

data.loc[after_winsorizepb.keys(),’winsorized_PB’]=after_winsorizepb.values()

data[‘PEBack’]=1/data.PE

medPEBack={np.median(data.PEBack)}

table={‘tradeDate’:([tdate]),’APB’:([np.mean(data.PB)]),’APB+’:([np.mean(data.winsorized_PB)]),’APE(TTM)’:([np.mean(data.PE)]),

‘APE(TTM)+’:([np.mean(data.winsorized_PE)]),’MPE(TTM)’:([1/np.median(data.PEBack)])}

APE=pd.DataFrame(table)

factor_list.append(APE)

tAPE=pd.concat(factor_list)

tAPE.head(36)

APB APB+ APE(TTM) APE(TTM)+ MPE(TTM) tradeDate
0 3.587435 3.283817 87.702610 35.744915 42.725300 2020-01-23
0 3.679782 3.374192 82.559420 36.183157 43.056594 2020-02-28
0 3.348262 3.152043 84.612621 36.123545 40.023100 2020-03-31
0 3.662726 3.147461 -27.688440 37.730326 47.005800 2020-04-30
0 3.872089 3.308992 -33.765035 39.063655 47.691900 2020-05-29

Leave a Reply

邮箱地址不会被公开。 必填项已用*标注