之前在如何計算ETF的PE ratio — 00878?中討論到如何使用計算00878的PE以進行對於此ETF的估值,我們發現關鍵在於如何有效撈取一檔ETF的成分和持有股數和其市值(或發行股數),此篇我們繼續探討如何撈取計算Invesco納斯達克100指數ETF-QQQ的本益比。關於QQQ的介紹,可以參考此連結。
You can check this for the English version!
以下為建議步驟:
步驟1: 安裝相關套件。
!pip install pandas
!pip install yfinance
步驟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(tickers):
try:
details = yf.Tickers(tickers)
forwardEps = details.tickers[tickers].info.get('forwardEps')
except:
# print(error)
pass
return forwardEps
for row in df.to_records(index=False):
ticker = str(row[2]).strip()
eps = get_info(ticker)
print(eps)
filt = df['Holding Ticker'] == str(row[2])
df.loc[filt, 'eps'] = eps
步驟4: 計算總盈餘和總市值,並利用此數字算出本益比。
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 and more to come! 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 :)