Using PE ratio to assess current stock price
Table of Contents
The PE ratio is a typical indicator that evaluates the reasonability of the stock’s price. The formula is PE ratio = (Price Per Share) / (Earning Per Share). The higher PE ratio indicates the overestimated price of the stock, which means the company’s current profit can not support such a high stock price. It will probably drop in the near future. On the other hand, the lower PE ratio conversely implies the underestimated price of the stock, meaning that current stock price doesn’t comprehensively reflect the company’s profit and the probability of the price’s rising is high.
In the 1970s and 1980s, Ball(1978) and Basu(1983) proved that the PE ratio could effectively explain the return of the stock’s price in the US market. Moreover, Chen’s (2011) Multi-factor Performance Analysis of the Taiwan Stock Market also showed a better performance of the PE ratio investment strategy. As a result of theory and implementation, the PE ratio is suitable as a reference for investment.
In the world of the investment, “buy low and sell high” is the permanent law, there are two aspects to realize it – cross section and time series.
Based on the info mentioned above, the PE ratio line chart is a convenient investment tool. Today’s article will specifically focus on how to use TEJ API and visualization tools to draw the graph.
This article uses Mac OS as a system and VScode as an editor.
import tejapi
import matplotlib.pyplot as plt
Use tejapi to set the securities code to be queried, the starting (gte), and ending date (lte) parameters, and the column for earnings per share. Optional fields include security code (coid), financial data date (mdate), and earnings per share (ac_3990).
company = '2330'
price_data = tejapi.get('TWN/EWIFINQ', # tej 財務資料庫
coid = company,
mdate={
# 起始日期
'gte':'2022-01-01',
# 結束日期
'lte':'2022-12-31'},
opts={'columns': ['coid','mdate','ac_3990']},
paginate=True
)
Use the tejapi function to substitute 3 parameters: set the security code to be queried and the starting date (gte), ending date (lte), and closing price (close_d) parameters.
company_data = tejapi.get(
'TWN/EWPRCD', # 資料庫
coid=company, # 股票代碼
mdate={
# 起始日期
'gte':'2018-01-01',
# 結束日期
'lte':'2018-12-31'},
paginate=True,
opts={'columns': ['mdate', 'close_d']},
)
To calculate the price-to-earnings ratio, use the daily closing stock price to divide the quarterly earnings per share to get the current day’s price-to-earnings ratio. In the previous section, the tejapi.get() function was used to obtain financial-related data (df). In this section, the plot function in matplotlib.pyplot was used to draw the price-earnings ratio river graph, and further empirical analysis was carried out. The following are the operation steps:
print(price_data['ac_3990'])
print(company_data['close_d'])
We can see the EPS data for the four quarters of the year (four transactions) and the closing data for the entire year (247 transactions).
2. Store the opening days of each quarter into the “quarter” parameter, and divide the closing price of that day by the corresponding EPS in the loop.
close = company_data['close_d']
Price_to_Earnings_Ratio = []
quarter = [60,63,63,61]
quarter_count = 0
season_days = 0
init_day = 0
while quarter_count<4:
season_days = quarter[quarter_count] # 每季有幾天
print(init_day+season_days)
for i in range(init_day, int(init_day+season_days)):
Price_to_Earnings_Ratio.append(round(company_data['close_d'][i]/price_data['ac_3990'][quarter_count]))
init_day = init_day + season_days
quarter_count += 1
3. Set the multiple to draw the price-to-earnings ratio.
x = [x for x in range(0, len(close))]
Price_to_Earnings_Ratio_30 = [ratio * 30 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_35 = [ratio * 35 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_40 = [ratio * 40 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_45 = [ratio * 35 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_50 = [ratio * 50 for ratio in Price_to_Earnings_Ratio]
4. Draw the price-to-earnings ratio graph.
plt.plot(x, Price_to_Earnings_Ratio_30, label='Ratio=30', color='blue')
plt.plot(x, Price_to_Earnings_Ratio_35, label='Ratio=35', color='blue')
plt.plot(x, Price_to_Earnings_Ratio_40, label='Ratio=40', color='yellow')
plt.plot(x, Price_to_Earnings_Ratio_45, label='Ratio=45', color='brown')
plt.plot(x, Price_to_Earnings_Ratio_50, label='Ratio=50', color='purple')
plt.plot(x, close.tolist(), label='K line', color='red') # 畫出收盤價曲線
plt.title('Price_to_Earnings_Ratio') # 設定圖形標題
plt.xlabel('Days') # 設定X軸標籤
plt.ylabel('Ratio') # 設定Y軸標籤
plt.legend() # 添加圖例
plt.show()
This article introduces how to use the TEJ API to present the price-to-earnings ratio river graph with a visualization kit. Investors can judge the current relative position of the stock by observing the price-to-earnings ratio river graph of individual stocks and carrying out the “buy low, sell high” operation. Taking TSMC’s price-to-earnings ratio river chart as an example in this article, users can easily observe the reasonable price-to-earnings ratio given to TSMC by market investors and judge whether the current stock price of TSMC is reasonable or not. Suppose you want to use the P/E ratio river graph to accurately evaluate a reasonable stock price. In that case, you must consider the company’s future expectations or use the expected earnings per share to calculate the P/E ratio.
Lastly, please note that the “Stocks this article mentions are just for the discussion. Please do not consider it to be any recommendations or suggestions for investment or products.” Hence, if you are interested in Creating Trading Strategy, Performance Backtesting, and Evidence-based research, please purchase the plans offered in TEJ E-Shop and use the well-complete database to find the potential event.
Source Code
import tejapi
import matplotlib.pyplot as plt
tejapi.ApiConfig.api_key = "your_api_key"
company = '2330'
price_data = tejapi.get('TWN/EWIFINQ', # tej 財務資料庫
coid = company,
mdate={
# 起始日期
'gte':'2022-01-01',
# 結束日期
'lte':'2022-12-31'},
opts={'columns': ['coid','mdate','ac_3990']},
paginate=True
)
company_data = tejapi.get(
'TWN/EWPRCD', # 資料庫
coid=company, # 股票代碼
mdate={
# 起始日期
'gte':'2018-01-01',
# 結束日期
'lte':'2018-12-31'},
paginate=True,
opts={'columns': ['mdate', 'close_d']},
)
print(price_data['ac_3990'])
print(company_data['close_d'])
close = company_data['close_d']
Price_to_Earnings_Ratio = []
quarter = [60,63,63,61]
quarter_count = 0
season_days = 0
init_day = 0
while quarter_count<4:
season_days = quarter[quarter_count] # 每季有幾天
print(init_day+season_days)
for i in range(init_day, int(init_day+season_days)):
Price_to_Earnings_Ratio.append(round(company_data['close_d'][i]/price_data['ac_3990'][quarter_count]))
init_day = init_day + season_days
quarter_count += 1
# print(Price_to_Earnings_Ratio)
# 設定畫出本益比倍數
x = [x for x in range(0, len(close))]
Price_to_Earnings_Ratio_30 = [ratio * 30 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_35 = [ratio * 35 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_40 = [ratio * 40 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_45 = [ratio * 35 for ratio in Price_to_Earnings_Ratio]
Price_to_Earnings_Ratio_50 = [ratio * 50 for ratio in Price_to_Earnings_Ratio]
# 畫出本益比
plt.plot(x, Price_to_Earnings_Ratio_30, label='Ratio=30', color='blue')
plt.plot(x, Price_to_Earnings_Ratio_35, label='Ratio=35', color='blue')
plt.plot(x, Price_to_Earnings_Ratio_40, label='Ratio=40', color='yellow')
plt.plot(x, Price_to_Earnings_Ratio_45, label='Ratio=45', color='brown')
plt.plot(x, Price_to_Earnings_Ratio_50, label='Ratio=50', color='purple')
plt.plot(x, close.tolist(), label='K line', color='red') # 畫出收盤價曲線
plt.title('Price_to_Earnings_Ratio') # 設定圖形標題
plt.xlabel('Days') # 設定X軸標籤
plt.ylabel('Ratio') # 設定Y軸標籤
plt.legend() # 添加圖例
plt.show()
Subscribe to newsletter