【Data analysis (4)】Customize your own financial dashboard.

Building the financial dashboard through python. ( the top 5 buy list of the foreign & other investors, and combine the trading volume of foreign investors with the stock price.)

--

📚Preface

We have introduced some functions of the most-used python packages such as matplotlib, numpy, and pandas in previous articles. In this episode, we will further introduce the application of those packages.

As an investor, it’s your daily routine to spend time watching some statistical data or summary reports of the market after the market is closed. Therefore, each time before you start to observe these data, you will have to go to the TWSE’s website to download the most recent data, then open it on excel to plot and summarize. This kind of work sometimes annoys people because you do the same thing every day. To be honest, it’s totally a waste of time. Compared to execute the same action daily by ourselves, using python seems much more attractive and efficient.

The purpose of this article is to share how we process data, make a plot, and customize your financial dashboard through python🏄🏄~

✨ Highlights of this article ✨

  • 🌟 Matplotlib /construct dashboard

🚪Links related to this article🚪

✔️Using matplotlib to construct a dashboard ✅

📝Import packages

import tejapi
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime
tejapi.ApiConfig.api_key = ‘your key’

📝Data extraction

1️⃣ Get the stock code of the listed company

# 撈取上市所有普通股證券代碼
stk = tejapi.get('TWN/EWNPRCSTD'
,paginate = True
,chinese_column_name = True)code = stk['證券碼'][(stk['證券種類名稱']=='普通股')&(stk['上市別']=='TSE')].to_list()
code
the stock code of the listed company

2️⃣ Get foreign & other investors daily trading summary

# 撈取法人買賣超(日)
buyover = tejapi.get('TWN/EWTINST1'
             ,coid=code
             ,mdate = {'gte':'2021-01-01'}
             ,paginate = True
             ,chinese_column_name = True)
# 修改日期格式
buyover['日期'] = buyover['日期'].apply(lambda x: pd.to_datetime(x).date())
buyover['日期'] = buyover['日期'].astype('datetime64')
buyover[buyover['日期']>='2021-03-20']
foreign & other investors daily trading summary

3️⃣ Get stock data

# 撈取股價資料
stock_price = tejapi.get('TWN/EWPRCD',
coid = code,
mdate={'gte':'2021-03-20'},
opts={'columns':['coid','mdate','open_d', 'high_d','low_d','close_d','volume']}
,paginate = True,chinese_column_name = True)
# 調整日期格式
stock_price['日期'] = stock_price['日期'].apply(lambda x: pd.to_datetime(x).date())
stock_price['日期'] = stock_price['日期'].astype('datetime64')
stock_price
stock data

📝Data summarize

Calculating the monthly trading volume and sorting the trading volume of foreign investors, investment trust, and proprietary respectively.

1️⃣ The top 10 buy list in foreign investors are 6116, 2891, 2344, 2352, 2363, 2882, 3711, 2449, 2881, 1303. Most of them are from financial, display, and semiconductor-related industry.

2️⃣ The top 10 buy lists in investment trusts are 3481, 2409, 2002, 2610, 2303, 2603, 2344, 1440, 6116, 2606. The investing style of investment trusts is different from foreign investors. Besides the display and memory industry, the traditional industry(shipping, iron & textile)accounts for the most.

3️⃣ The top 10 buy list in proprietaries are 2002, 2303, 2888, 1314, 2409, 1101, 6116, 3481, 2027, 2883. Their attitude of investing is similar to investment trusts. Their capitals distribute on the display, traditional, and financial industry at the same time.

According to the data, the intersection among the three is the display industry. The domestic investors have the same view based on the trading volumes, and both of them put quite a large percentage of their capital in the traditional industry. However, foreign investors distribute more in the financial and tech sectors.

sorted_fi = buyover[buyover['日期']>'2021-03-20'].groupby(by='證券碼').sum().sort_values(by='外資買賣超(千股)',ascending = False)
sorted_fi[:10]
sorted_it = buyover[buyover['日期']>'2021-03-20'].groupby(by='證券碼').sum().sort_values(by='投信買賣超(千股)',ascending = False)
sorted_it[:10]
sorted_pro = buyover[buyover['日期']>'2021-03-20'].groupby(by='證券碼').sum().sort_values(by='自營買賣超(千股)',ascending = False)
sorted_pro[:10]
The top 10 buy list in investment trusts / The top 10 buy list in investment trusts / The top 10 buy list in proprietaries

Filter:All of the monthly trading volume of foreign & other investors are positive.

After filtering, we also sort the monthly trading volume of foreign investors, investment trusts, and proprietary respectively.

sorted_fi[(sorted_fi['外資買賣超(千股)']>0)&(sorted_fi['投信買賣超(千股)']>0)&(sorted_fi['自營買賣超(千股)']>0)][:10]sorted_it[(sorted_it['外資買賣超(千股)']>0)&(sorted_it['投信買賣超(千股)']>0)&(sorted_it['自營買賣超(千股)']>0)][:10]sorted_pro[(sorted_pro['外資買賣超(千股)']>0)&(sorted_pro['投信買賣超(千股)']>0)&(sorted_pro['自營買賣超(千股)']>0)][:10]
The top 10 buy list in investment trusts / The top 10 buy list in investment trusts / The top 10 buy list in proprietaries

Now we can start to build our dashboard😄~

📊Construct dashboard

📈 The top 5 buy/ sell list of foreign investors

  • 5-days the top 5 buy/ sell list of foreign investors.
  • 20-days the top 5 buy/ sell list of foreign investors.
  • 60-days the top 5 buy/ sell list of foreign investors.

buy_table : The data frame for storing top 5 buy list at different periods.
sell_table : The data frame for storing top 5 sell list at different periods.
xx : Calculate the monthly trading volume of the stocks under 5, 20, 60-days.
x.insert() : Insert the column which is named according to its stock code, and the value is xx.
x : The data frame for storing the monthly trading volume of the multi-stocks under the same period.
x.loc[len(x)-1] : Call the most recent data of x.
sort_values(ascending=False)[:5]: Sorting data and choose the top 5
reset_index(drop=True) : Reset the index of data frame.

top five buy/ sell list on 4/19

📈trading volumes/ stock price

plt.style.use : Set the style of plot.
plt.rcParams[‘font.sans-serif’]=[‘Microsoft YaHei’] : Set the font. There is no Chinese font in the python internals. You need to import additional fonts to display Chinese.
stock = input() : Input the stock code.
FI1.twinx() : Share the x-axis of FI1.

Conclusion

There are hundreds of ways to make charts and plots. We introduced how to implement the charts we often see through python. After all, one of the great benefits of learning python is that it can reduce tedious routines. If you like the topic such as integrating charts, you can leave a message below to tell us. In the future, we will continue to share the application of python in the investment field, please wait and see ❗️❗️

In addition, you can also explore more functions through the official website of these packages~

Finally, if you like this topic, please click 👏 below, giving us more support and encouragement. Additionally, if you have any questions or suggestions, please leave a message or email us, we will try our best to reply to you.👍👍

Links related to this article again!💪

If you have any question and difficulty, do not hesitate to contact us: Contact Information

Source Code🚪:

--

--

TEJ 台灣經濟新報
TEJ-API Financial Data Analysis

TEJ 為台灣本土第一大財經資訊公司,成立於 1990 年,提供金融市場基本分析所需資訊,以及信用風險、法遵科技、資產評價、量化分析及 ESG 等解決方案及顧問服務。鑒於財務金融領域日趨多元與複雜,TEJ 結合實務與學術界的精英人才,致力於開發機器學習、人工智慧 AI 及自然語言處理 NLP 等新技術,持續提供創新服務