如何計算ETF的PE ratio — QQQ? (2)

閱讀時間約 5 分鐘
  • 文內如有投資理財相關經驗、知識、資訊等內容,皆為創作者個人分享行為。
  • 有價證券、指數與衍生性商品之數據資料,僅供輔助說明之用,不代表創作者投資決策之推介及建議。
  • 閱讀同時,請審慎思考自身條件及自我決策,並應有為決策負責之事前認知。
  • 方格子希望您能從這些分享內容汲取投資養份,養成獨立思考的能力、判斷、行動,成就最適合您的投資理財模式。


在上一篇如何計算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!

raw-image



以下為建議步驟:

步驟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
raw-image

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 :)

𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲


6會員
17內容數
Informula 作為上班族的臨時急救包,介紹一些簡單的程式工具、資料處理、數據分析、網路爬蟲應用等。
留言0
查看全部
發表第一個留言支持創作者!