Jun 13 2023

Using PE ratio to assess current stock price

Table of Contents

- Difficulty: ★☆☆☆☆
- Via line chart to visualize the fluctuation of stock price and PE ratio.

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.

- Cross Section: “Buy low PE ratio targets and sell(short) high PE ratio targets” is the most stable and appropriate strategy because it could diversify the risk with the portfolio and gain an outstanding performance. However, For general investors, some issues need to be considered when constructing a portfolio, such as the adequacy of the finance or the limitation of shorting a target. These kinds of problems are the barriers to implementation.
- Time Series: “Buy shares when the price is at a relatively low point and sell at a relatively high price” is another strategy. Though it doesn’t diversify the risk, it genuinely is an easier way for general investors to fulfill the “buy low and sell high” strategy. The line chart of the PE ratio below draw lines of different PE ratio at the Candlestick Chart, so it can be clearly seen current price is close to which ratio’s line and whether it is higher or lower compared to the past period.

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
```

- TWN/EWIFINQ(Quarterly Financial Information Sheet)
- TWN/EWPRCD(Securities Transaction Data Sheet)

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 out EPS and closing price data to observe the data type and quantity.

```
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()
```

- As shown in the TSMC (2330) price-to-earnings ratio river graph above, After the P/E ratio of Q3 exceeded 40 times in 2018, TSMC maintained a P/E ratio of more than 40 times most of the time, and the data shows that the P/E ratio of more than 40 times has already exceeded 40 times. It is the recent market investor’s evaluation benchmark for TSMC’s reasonable stock price. After Q3, it rose to around 250 yuan and remained at more than 40 times. Based on this information, we can conclude that market investors have given a relatively high valuation to TSMC’s stock price, believing it has high-profit potential and investment value. It reflects the market’s confidence and optimism toward TSMC as a well-known chip manufacturer.
- The example of TSMC shows that the PE ratio river chart is a handy investment tool for judging the relative position of the current stock price and for “buy low, sell high.” It should be noted that calculating the price-to-earnings ratio has limitations. The stock price reflects investors’ expectations for the future. Still, the most commonly used price-to-earnings ratio is the company’s latest earnings (already occurred), which sometimes leads to a high price-to-earnings ratio. The valuation is not that the stock price deviates from a reasonable price but that the current price reflects the company’s future earnings growth expectations. Therefore, when investors use the price-to-earnings ratio river chart as a tool, they need to consider the company’s future expectations or use the expected earnings per share to calculate the price-to-earnings ratio to more reasonably evaluate whether the current stock price deviates from a reasonable price.

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()
```

Category