【Introduction(4)】TEJ Rest API Document.

Using the document to have a deep understanding of TEJ Rest API.

Preface

TEJ has official packages which are specialized for R, Python users to make data collection more convenient. (Python API document)

TEJ also cares about the programmers of other language users(C, C#, Java), so we have developed Rest API to extract data from the databases.

The methodology of Rest API is similar to website crawl, and we can get the information from the Uniform Resource Locator (URL). In this episode, we will show you how to use Rest API to collect the data.

✨ Highlights of this article ✨

  • TEJ API KEY Apply/Buy
  • User guide/Data collection
  • Optional Parameters Introduction

🚪Links related to this article🚪

* API KEY Apply/Buy

Before using our TEJ API, if you don’t have an API KEY, you can get it through the trial application in the link or directly buy the product package in the E-Shop, as the picture below:

Process of the trial API KEY application
Webpage of TEJ E-Shop

* User guide📗

1️⃣ Import packages

import requests
import pandas as pd
import json

2️⃣ Enter API key

# 輸入 api_key
api_key = 'your key'
url = 'https://api.tej.com.tw/api/datatables/TWN/APRCD.json?api_key='+api_key

3️⃣ Get the information of URL

rq = requests.get(url)
rq.content
Information

4️⃣ Data processing (json ➡️ Dataframe)

After transforming json to dataFrame, the data seems more tidy 😎😎~

data = json.loads(rq.content)['datatable']['data']
columns = pd.DataFrame(json.loads(rq.content)['datatable']['columns'])['cname'].to_list()
stock_price = pd.DataFrame(data,columns=columns)
stock_price
Dataframe

* Optional Parameters Introduction 📚

Analyzing the URL much further, we can find the combination of the URL has pattern! The former part of the URL is to connect to the TEJ databases, and the latter part (after the bold) is to select the database, the table, output format of the data, and some custom criteria.

TWN: Taiwan database.
APRCD: Stock data table.
json: Output format.
api_key: Key (like the password while sign in the FB or Google ).

https://api.tej.com.tw/api/datatables/{datatable_code}/{table_code}.{format}?<row_filter_criteria>https://api.tej.com.tw/api/datatables/TWN/APRCD.json?api_key=<YOURAPIKEY>

1️⃣ Get the stock data for the selected companies.

⚠️ When the parameters are more than two, you have to add & at the in front of the parameters, such as & coid and & api_key ⚠️

🔽 Individual Stock 🔽

Use Taiwan Weighted Index (code:Y9999) for example.

coid = 'Y9999'
url = 'https://api.tej.com.tw/api/datatables/TWN/APRCD.json?'+'&coid='+coid+'&api_key='+api_key
rq = requests.get(url)
data = json.loads(rq.content)['datatable']['data']
columns = pd.DataFrame(json.loads(rq.content)['datatable']['columns'])['cname'].to_list()
stock_price = pd.DataFrame(data,columns=columns)
stock_price
Data

🔽 Multi-stocks 🔽

Use Taiwan Weighted Index (code:Y9999), Taiwan semiconductor (code:2330), Mediatek (code:2454), YangMing Marine (code: 2609) for examples.

coid = 'Y9999,2330,2454,2609'
url = 'https://api.tej.com.tw/api/datatables/TWN/APRCD.json?'+'&coid='+coid+'&api_key='+api_key
rq = requests.get(url)
data = json.loads(rq.content)['datatable']['data']
columns = pd.DataFrame(json.loads(rq.content)['datatable']['columns'])['cname'].to_list()
stock_price = pd.DataFrame(data,columns=columns)
stock_price
Data

👺 Devil in details 👺

In the example of multi-stocks, we observed that the length of the data is 10,000. However, the data length of an individual stock is more than 5000, in theory, we must have more than 20,000 rows in the data. Why the differences?

For the stability of the host operation, TEJ limits the maximal amount of the output is 10,000 each time. When the data is more than 10,000, we should get the next_cursor_id, and add opts.cursor_id = next_cursor_id to get the rest of the data.

💻 next_cursor_id 💻

rq.json()['meta']['next_cursor_id']
next_cursor_id

According to the above logic, when there are 30,000 data, there will be 3 next_cursor_id, that is, there will be one next_cursor_id for every 10,000 data, and then it’s the turn to use the while loop ❗️

💻 Solution to next_cursor_id 💻

We have created a function for you, so users can use the function below to get the data easily 😎😎.

# multi-stocks
coid = 'Y9999,2330,2454,2609'
stock_price = tej_get_data(
db_code='TWN/APRCD',
api_key=api_key,
coid = coid)
stock_price
Data

2️⃣ Columns Selected

Use Taiwan Weighted Index (code:Y9999) for example.

🔽 Single Column 🔽

Add parameters &opts.columns=open_d, select column as open price.

# 單一欄位
coid = 'Y9999'
url = 'https://api.tej.com.tw/api/datatables/TWN/APRCD.json?&opts.columns=open_d'+'&api_key='+api_key+'&coid='+coid
rq = requests.get(url)
data = rq.json()['datatable']['data']
columns = pd.DataFrame(rq.json()['datatable']['columns'])['cname'].to_list()
stock_price = pd.DataFrame(data,columns=columns)
stock_price
data

🔽 Multi-columns 🔽

Columns:stock code, Date, open, high, low, close.

# 多欄位
coid = 'Y9999'
columns = 'coid,mdate,open_d,high_d,low_d,close_d'
url = 'https://api.tej.com.tw/api/datatables/TWN/APRCD.json?'+'&opts.columns='+columns+'&api_key='+api_key+'&coid='+coid
rq = requests.get(url)
data = rq.json()['datatable']['data']
columns = pd.DataFrame(rq.json()['datatable']['columns'])['cname'].to_list()
stock_price = pd.DataFrame(data,columns=columns)
stock_price
Data

3️⃣ Date Selected

Use Taiwan Weighted Index (code:Y9999) for example.

Start: 2020–01–01;End: 2020–12–31.

Parameter setting:

  • mdate.gte= start: date >= 2020–01–01
  • mdate.lte = end: date <= 2020–12–31
# 日期篩選
coid = 'Y9999'
start = '2020-01-01'
end = '2020-12-31'
url = 'https://api.tej.com.tw/api/datatables/TWN/APRCD.json?'+'&mdate.gte='+start+'&mdate.lte='+end+'&api_key='+api_key+'&coid='+coid
rq = requests.get(url)
data = rq.json()['datatable']['data']
columns = pd.DataFrame(rq.json()['datatable']['columns'])['cname'].to_list()
stock_price = pd.DataFrame(data,columns=columns)
stock_price
Data

4️⃣ Table Information

datatable_code = 'TWN/APRCD'
url = 'https://api.tej.com.tw/api/datatables/'+datatable_code+'/metadata?api_key='+api_key
rq = requests.get(url)
rq.json()
information

5️⃣ Search Table

Search EPS, it will appear many results. (For detail : TEJ API )

matchType:

  • TABLE_MATCH: Keyword in the table.
  • COLUMN_MATCH: Keyword in the column.
  • TABLE_MATCH & COLUMN_MATCH: Keyword in the table and the column.
key_word = '每股盈餘'
url = 'https://api.tej.com.tw/api/search/table/'+key_word+'?api_key='+api_key
rq = requests.get(url)
rq.json()
Data

6️⃣ API KEY information

api_key = 'your key'
url = 'https://api.tej.com.tw/api/apiKeyInfo/'+api_key
rq = requests.get(url)
rq.json()
Data

* Conclusion

The content today is for everyone to have a deeper knowledge and understanding of our TEJ Rest API. It is easier to understand, but through the codes, we can understand that these built-in functions could help users to get the data they want from the TEJ’s huge database more conveniently.

* 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 等新技術,持續提供創新服務