【Introduction(1)】Building your own database through TEJ API

- how to grab data from TEJ API?

TEJ 台灣經濟新報
TEJ-API Financial Data Analysis
4 min readMar 15, 2021

--

* What is TEJ API?

Would you find it very difficult to find a high-quality and stable source of data before doing an analysis on it? Or is it time-consuming to maintain your own web-crawling program after spending a lot of time creating it? Now, TEJ (Taiwan Economic Journal) has launched the TEJ API tool for you to solve this problem! You are very welcome to apply for a trial.

TEJ API will be mainly used as the source of data, and we would like to share some financial data analysis and applications in Python with you in our Medium. For rookies, you could get a preliminary understanding of how to analyze data through programming, and for experts, you can access a stable and fast source of data!

* Highlights of this article

  • TEJ API Data Collecting
  • Saving/Reading the data into/from CSV, Xlsx, and SQLite.

* Links related to this article

* TEJ API Data Collecting-Using stock data as an example

It is very simple to grab the data from TEJ API.💪
First we have to install tejapi package in Python.

The next step is to import this package and set up your own api_key. If you don’t have one, you can apply for a trial on the TEJ API official website. But the trial version has limits on the range of data that could be obtained!

Now we can get the data through this code👀

Parameters:

  1. Name of the database-TWN/EWPRCD
    ❗❗if you are a trial account user, then the name of the database may diverse for the trial version.
  2. Stock/Company symbol-2330
  3. Range of the data-from 2020–01–01 to 2020–12–31
  4. Optional-only select OHLC columns
  5. paginate -data can be obtained in stages through this parameter

** What we used in this example is from the product package in TEJ E SHOP. You can use the E Shop website to understand more information about TEJ’s current products.👍👍 Then we can see the output below:

2020台積電(2330)股市日資料-開高低收
2020 TSMC(2330) Stock price data — OHLC

* Saving/Reading the data

📓Numpy, pandas, and datetime are very important packages in Python data analysis. We can do most operations, just like what we are going to do, through them.

  • Excel CSV
    We only need to use these lines of code to save or read our data into CSV file:
  • Excel Xlsx
    In addition to a CSV file, Excel has another storage file format called Xlsx
    . If there are many data tables you want to save in the same file, we can store them through Xlsx. The only thing you may have to pay attention to is that because the date format of the TEJ API is different from this to_excel() function. Therefore, we will have to process the date format before saving the data.
  • SQLite
    What if the amount of data is getting larger and larger and it will take you much time when opening the excel? Now you may need SQLite at this time! SQLite is very convenient to use in Python due to its characteristics. We will briefly introduce how to connect to the database and save/read the data. For detailed instructions, please refer to the SQLite official website.

** Using the database management software(we use DBeaver) to use whether we have successfully imported:

After importing into the SQLite

* Conclusion

What we share with you this time is the introduction to get the data through TEJ API and how to save/read it. Surely, we will write more articles related to data analysis and applications through Medium in the future!
Finally, if you have any questions or suggestions after reading this article, 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 questions and difficulties while reading or doing, do not hesitate to contact us: Contact Information

Source Code:

--

--

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

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