PE by MCap

Category : Uncategorized

PE by MCap

Please make sure you have the most updated mfd_dm, mfd_ta and mfd_tex packages for Stata installed for this post.

net from ""
net from ""
net from ""
* click on the blue mfd_dm package and then "click here to install" 

PE by market capitalization.

clear all
cd "/Users/mfd/Desktop"

* Get the components of S&P500 Index
mfd_dm_components_cnn sp500
drop if Symbol==""
sort Symbol
levelsof Symbol, local(tickers)

mfd_dm_EPS `tickers', item(eps) annual pe price
sort date
save data_eps.dta, replace

mfd_dm_EPS `tickers', item(revenue) annual price
sort date
save data_revenue.dta, replace

mfd_dm_yahooapi `tickers', field(s j2)
gen symbol=substr(v1,1,strpos(v1,",")-1)
gen shares=trim(substr(v1,strpos(v1,",")+1,.))
replace shares=subinstr(shares,",","",.)
destring shares, replace force
drop v1
sort symbol
save data_shares.dta, replace

use data_eps.dta, clear
merge date using data_revenue.dta
drop _merge
sort date
gen day=_n
tsset day
save data_sp500.dta, replace

* PE and revenues
clear all
cd "/Users/mfd/Desktop"
use data_sp500.dta, clear
keep date PE_*_annual revenue_*_annual adj*
keep if _n==_N
xpose, clear varname
drop if _n==1

gen PE_annual=v1 if (strpos(_varname,"PE")==1)
gen revenue_annual=v1 if (strpos(_varname,"revenue")==1)
gen price=v1 if (strpos(_varname,"adjclose")==1)

replace _varname=subinstr(_varname,"PE_","",.)
replace _varname=subinstr(_varname,"revenue_","",.)
replace _varname=subinstr(_varname,"_annual","",.)
replace _varname=subinstr(_varname,"adjclose_","",.)
drop v1
rename _varname symbol
sort symbol
replace PE_annual=PE_annual[_n-1] if (PE_annual==.) & (symbol==symbol[_n-1])
replace PE_annual=PE_annual[_n-2] if (PE_annual==.) & (symbol==symbol[_n-2])
replace PE_annual=PE_annual[_n+1] if (PE_annual==.) & (symbol==symbol[_n+1])
replace PE_annual=PE_annual[_n+2] if (PE_annual==.) & (symbol==symbol[_n+2])

replace revenue_annual=revenue_annual[_n-1] if (revenue_annual==.) & (symbol==symbol[_n-1])
replace revenue_annual=revenue_annual[_n-2] if (revenue_annual==.) & (symbol==symbol[_n-2])
replace revenue_annual=revenue_annual[_n+1] if (revenue_annual==.) & (symbol==symbol[_n+1])
replace revenue_annual=revenue_annual[_n+2] if (revenue_annual==.) & (symbol==symbol[_n+2])

replace price=price[_n-1] if (price==.) & (symbol==symbol[_n-1])
replace price=price[_n-2] if (price==.) & (symbol==symbol[_n-2])
replace price=price[_n+1] if (price==.) & (symbol==symbol[_n+1])
replace price=price[_n+2] if (price==.) & (symbol==symbol[_n+2])

duplicates drop

sort symbol
merge symbol using data_shares
drop _merge

drop if PE_annual==.
drop if revenue_annual==.

drop if PE_annual>100
drop if PE_annual<-100

replace revenue_annual=revenue_annual/1000000000
twoway (scatter PE_annual revenue_annual if symbol!="AAPL", mlabel(symbol)) (scatter PE_annual revenue_annual if symbol=="AAPL", mlabel(symbol)), ylabel(#30, angle(horizontal)) xlabel(#50, angle(vertical)) scale(.7) legend(off)

gen mcap=shares*price/1000000000
twoway (scatter PE_annual mcap if symbol!="AAPL", mlabel(symbol)) (scatter PE_annual mcap if symbol=="AAPL", mlabel(symbol)), ylabel(#30, angle(horizontal)) xlabel(#50, angle(vertical)) scale(.7) legend(off)