在上一篇如何計算ETF的PE ratio — QQQ?我們討論到如何計算出ETF QQQ的PE,有些朋友詢問為什麼跟有些平台的數字還要低很多,其主要理由是有些平台給出的是跟去過去12個月的EPS,而我們之前計算的是所謂的Forward PE,是根據預測未來的Forward EPS,因此會有所落差,我們這篇也會討論如何計算trailing 12-months PE。
You can check this for the English version!
以下為建議步驟:
步驟1: 安裝相關套件。
!pip install pandas
!pip install yahooquery
步驟2: 根據此官方連結獲取QQQ的基本資訊,包含持倉、每檔持有股數、市值等。
import pandas as pd
df = pd.read_csv('https://www.invesco.com/us/financial-products/etfs/holdings/main/holdings/0?audienceType=Investor&action=download&ticker=QQQ')
步驟3: 定義撈取過去四季的EPS的函式。
def get_info_yahooquery(tickers):
try:
# data = yq.search(query)
details = Ticker(tickers)
earnings = details.earnings
trailing_eps_1 = list(earnings.values())[0].get('earningsChart').get('quarterly')[0].get('actual')
trailing_eps_2 = list(earnings.values())[0].get('earningsChart').get('quarterly')[1].get('actual')
trailing_eps_3 = list(earnings.values())[0].get('earningsChart').get('quarterly')[2].get('actual')
trailing_eps_4 = list(earnings.values())[0].get('earningsChart').get('quarterly')[3].get('actual')
except:
print(tickers)
return
return trailing_eps_1 + trailing_eps_2 + trailing_eps_3 + trailing_eps_4
步驟4: 將持倉代碼(Ticker)丟進函式撈取過去四季的EPS。
from yahooquery import Ticker
import pandas as pd
for row in df.to_records(index=False):
ticker = str(row[2]).strip()
eps = get_info_yahooquery(ticker)
print(eps)
filt = df['Holding Ticker'] == str(row[2])
df.loc[filt, 'eps'] = eps
步驟5: 計算總盈餘和總市值,並利用此數字算出本益比。
df['Earning'] = df['Shares/Par Value'].str.replace(',', '', regex=True).astype(float) * df['eps'].astype(float)
Earning_QQQ = df['Earning'].sum()
MarketValues_QQQ = df['MarketValue'].str.replace(',', '', regex=True).astype(float).sum()
MarketValues_QQQ / Earning_QQQ
Thank you! We will talk about this topic for more ETFs, approaches, matrices, etc. Enjoy it :)
If you want to support Informula, you can buy us a coffee here :)