【Introduction】Tutorial on Running TQuant Lab in Google Colab and Common Errors

TEJ 台灣經濟新報
5 min readJul 16, 2024

--

Running TQuant Lab in Google Colab
Photo by Jametlene Reskp on Unsplash

Key Points

  • Article Difficulty: ★☆☆☆☆
  • Tutorial on Using TQuant Lab in Google Colab
  • Simple Test: Select the top five companies by revenue (from any industry) and backtest their returns.
  • Install fonts to ensure the Pyfolio package can plot graphs smoothly.

Introduction

For students who want to use TQuant Lab, in addition to the original GitHub installation tutorial, we now offer a faster and simpler way to use it directly on Google Colab. The tutorial on running TQuant Lab in Google Colab eliminates the need to set up a virtual environment, significantly lowering the barrier to entry.

Running TQuant Lab in Google Colab and Installing some Commonly used Package

You only need one simple lines of code to complete the environment setup.

!pip install zipline-tej

If you encounter a pop-up window showing an error, simply click “Restart session” to resolve it.

Running TQuant Lab in Google Colab

In the end, you may see the following error message, but you can ignore it. The system is actually ready for use.

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
cudf-cu12 24.4.1 requires pandas<2.2.2dev0,>=2.0, but you have pandas 1.5.3 which is incompatible.
google-colab 1.0.0 requires pandas==2.0.3, but you have pandas 1.5.3 which is incompatible.

TQuant Lab Tutorial: Selecting the Top Five Semiconductor Companies by Revenue for Backtesting

Load Commonly Used Packages

import os 
os.environ['TEJAPI_BASE'] = 'https://api.tej.com.tw'
os.environ['TEJAPI_KEY'] = 'Your_Key'

import datetime
import tejapi
import pandas as pd
import numpy as np

First, identify all companies in the semiconductor industry

from zipline.sources.TEJ_Api_Data import get_universe

pool = get_universe(start = '2023-07-02',
end = '2024-07-02',
mkt_bd_e = 'TSE',
stktp_e = 'Common Stock',
sub_ind_e=['M2324 Semiconductor', 'M2325 Computer and Peripheral Equipment'])

Check their revenue, select the top five highest, and then save their stock symbols.

import TejToolAPI

start_time = pd.Timestamp('2023-07-02')
end_time = pd.Timestamp('2024-07-02')
data = TejToolAPI.get_history_data(start = start_time,
end = end_time,
ticker = pool,
fin_type = 'Q',
columns = ['主產業別_中文', '常續ROE', '營業毛利率', '營運產生現金流量', '投資產生現金流量', '負債比率', 'per_tej', '營業總收入'],
transfer_to_chinese = True)
data = data.drop_duplicates(subset=['股票代碼'], keep='last').reset_index(drop=True)
data = data.nlargest(5, '營業總收入_Q')
data
Running TQuant Lab in Google Colab
tickers = data['股票代碼'].unique()

start = '2023-01-01'
end = '2024-07-02'
os.environ['mdate'] = start + ' ' + end
os.environ['ticker'] = ' '.join(tickers) + ' ' + 'IR0001'
!zipline ingest -b tquant

Load the data onto your local computer

start = '2023-01-01'
end = '2024-07-02'

os.environ['mdate'] = start + ' ' + end
os.environ['ticker'] = ' '.join(tickers) + ' ' + 'IR0001'
!zipline ingest -b tquant
from zipline.data import bundles
bundle_data = bundles.load('tquant')
from zipline.api import *
from zipline.finance import commission, slippage
def initialize(context):
context.day = 0
context.tickers = tickers
set_slippage(slippage.VolumeShareSlippage(volume_limit = 0.025, price_impact = 0.1))
set_commission(commission.Custom_TW_Commission(min_trade_cost = 20, discount = 1.0, tax = 0.003))
set_benchmark(symbol('IR0001'))
set_liquidity_risk_management_rule(['全額交割股票(Full-Cash Delivery Securities)', '漲停股票(Limit Up)', '跌停股票(Limit Down)', '開盤即鎖死(Limited Whole Day)'])
def handle_data(context, data):
if context.day == 0:
for ticker in context.tickers:
order_percent(symbol(ticker), 1 / len(tickers))
context.day += 1
import matplotlib.pyplot as plt
capital_base = 1e6
def analyze(context, results):
fig = plt.figure()
ax1 = fig.add_subplot(111)
results['benchmark_cum'] = results.benchmark_return.add(1).cumprod() * capital_base
results[['portfolio_value', 'benchmark_cum']].plot(ax = ax1, label = 'Portfolio Value($)')
ax1.set_ylabel('Portfolio value (TWD)')
plt.legend(loc = 'upper left')
plt.gcf().set_size_inches(18, 8)
plt.grid()
plt.show()
from zipline import run_algorithm
start_date = pd.Timestamp('20230101', tz = 'utc')
end_date = pd.Timestamp('20240702', tz = 'utc')
results = run_algorithm(
start = start_date,
end = end_date,
initialize = initialize,
handle_data = handle_data,
analyze = analyze,
bundle = 'tquant',
capital_base = capital_base,
)
results
Running TQuant Lab in Google Colab

Font Issues Encountered with Pyfolio and How to Resolve Them

from pyfolio.utils import extract_rets_pos_txn_from_zipline
import pyfolio as pf

returns, positions, transactions = extract_rets_pos_txn_from_zipline(results)
from pyfolio.plotting import plot_rolling_sharpe
plot_rolling_sharpe(returns, factor_returns=benchmark_rets)
WARNING:matplotlib.font_manager:findfont: Generic family 'sans-serif' not found because none of the following families were found: Microsoft JhengHei
WARNING:matplotlib.font_manager:findfont: Generic family 'sans-serif' not found because none of the following families were found: Microsoft JhengHei
WARNING:matplotlib.font_manager:findfont: Generic family 'sans-serif' not found because none of the following families were found: Microsoft JhengHei
WARNING:matplotlib.font_manager:findfont: Generic family 'sans-serif' not found because none of the following families were found: Microsoft JhengHei
WARNING:matplotlib.font_manager:findfont: Generic family 'sans-serif' not found because none of the following families were found: Microsoft JhengHei

Even though the charts have been successfully plotted, you may still encounter warnings about missing fonts. To resolve this issue, you can manually load the fonts.

import matplotlib
import matplotlib.font_manager as fm
!wget -O MicrosoftJhengHei.ttf https://github.com/a7532ariel/ms-web/raw/master/Microsoft-JhengHei.ttf
!wget -O ArialUnicodeMS.ttf https://github.com/texttechnologylab/DHd2019BoA/raw/master/fonts/Arial%20Unicode%20MS.TTF

fm.fontManager.addfont('MicrosoftJhengHei.ttf')
matplotlib.rc('font', family='Microsoft Jheng Hei')

fm.fontManager.addfont('ArialUnicodeMS.ttf')
matplotlib.rc('font', family='Arial Unicode MS')

After trying again, the warning messages should no longer appear.

plot_rolling_sharpe(returns, factor_returns=benchmark_rets)
Running TQuant Lab in Google Colab

Conclusion

This introduction’s testing section mostly references the code from the article “TQuant Lab Warren E. Buffett’s Business Investment Rules.“ You can refer to the original extended reading content if you have any questions.

Additionally, this introduction allows everyone to use TQuant without needing extra virtual environment installations and extensive settings. It’s much more convenient to get started right away and follow this recommended approach.

Source Code

Extended Reading

Relevant Links

--

--

TEJ 台灣經濟新報
TEJ 台灣經濟新報

Written by TEJ 台灣經濟新報

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

No responses yet