【Application】TQuant Lab Insider Transfer Strategy: A Solution to Timing Difficulties

TEJ 台灣經濟新報
TEJ-API Financial Data Analysis
6 min readMay 24, 2024
Insider Transfer Strategy
Photo by Robert Bye on Unsplash

Highlight

  • Article Difficulty: ★★☆☆☆
  • This article will introduce the approach of importing information obtained from TEJToolAPI into Pipeline and use it in a strategy framework.
  • By identifying entry points through the timing of insider share transfers, we form an insider transfer strategy and backtest its performance using TQuant Lab.

Preface

In the dynamic landscape of today’s stock market, the activities of major investors often trigger significant price fluctuations. Among the various factors influencing these changes, the transfer of shares by directors and supervisors stands out. These insiders, armed with a profound understanding of the company’s operations, can wield substantial influence over its stock price. Consequently, when news of their share transfers surfaces, investors are quick to follow suit, aiming to capitalize on the resulting market swings.

As per the Taiwan Stock Exchange, the first quarter of 2024 witnessed a surge in market enthusiasm, with 4,879,506 individual investors engaging in transactions valued at less than NT$100 million, which is the highest in seven years. This trend, coupled with the stock market’s continuous ascent to new highs, underscores the importance of timing in market entry and the need to avoid purchasing at peak prices. To illustrate this, we will integrate the concept of insider share transfers with TQuant Lab, a powerful tool for backtesting strategies. This will allow us to thoroughly examine the performance of the Insider Transfer Strategy, shedding light on its potential benefits.

Insider Transfer Strategy

In this article, MSCI constituent stocks are used as the stock pool, and we select from the following insider transfer methods: general trading, block trading, and general trading combined with block trading. The insider transfer strategy is built and backtested using the following rules:

  • If an insider transfer occurs, buy or add NT$15,000 worth of shares. ( Note: The initial capital for this strategy is NT$1,000,000. )
  • If the RSI > 95, indicating a strong overbought sentiment in the market, sell half of the position.

Coding Environment and Module Requirements

This article is written using Windows 11 and Jupyter Lab as the editor.

import os
import numpy as np
import pandas as pd

# tej_key
tej_key = 'your key'
api_base = 'https://api.tej.com.tw'

os.environ['TEJAPI_KEY'] = tej_key
os.environ['TEJAPI_BASE'] = api_base

Selecting Stock Pool and Trading Data Import

We first obtain MSCI constituent stocks by get_universe function and TEJ Tool API.

Insider Transfer Strategy
Selecting MSCI constituent stocks from TEJ Tool API

With the backtest period set from May 2, 2019, to May 1, 2024, we then import the price and volume data of the 89 MSCI constituent stocks and the TAIEX-Total Return Index (IR0001) as the performance benchmark.

start = '2019-05-02'
end = '2024-05-01'

os.environ['mdate'] = start + ' ' + end
os.environ['ticker'] = ' '.join(MSCI_pool) + ' ' + 'IR0001'

!zipline ingest -b tquant

Obtain the Insider Transfer Data

Here, we use the TEJ Tool API to obtain the daily data of each stock's transfer start date, transfer end date, and transfer method. Furthermore, we added a transfer column to indicate whether there was an insider transfer of general trading, block trading, or general trading combined with block trading on that day.

Insider Transfer Strategy
The Insider Transfer Data from TEJ Tool API

Adding Transfer Information to Pipeline

CustomDataset allows us to import database content into Pipeline, facilitating subsequent backtesting. In this implementation, we use it to import the daily transfer information recorded in the transfer column into Pipeline. Part of the code is as follows:

from zipline.pipeline.data.dataset import Column, DataSet
from zipline.pipeline.domain import TW_EQUITIES

class CustomDataset(DataSet):

Transfer_Method_English = Column(object)
transfer = Column(bool)

domain = TW_EQUITIES

Construct the Insider Transfer Strategy

From the pipeline and zipline functions provided in TQuant Lab, we can:

  1. Calculate the RSI value using the built-in factors from pipeline.
  2. Add the transfer methods and daily transfer data into pipeline.
  3. Add liquidity slippage and transaction fees, and set the return of the TAIEX-Total Return Index as the benchmark.
  4. Set the restriction to only long positions that can be constructed in the strategy.
  5. Set the Insider Transfer Strategy and record the transaction details.
Insider Transfer Strategy
Pipeline Results to be Used in Our Strategy

Execute the Trading Strategy

We use run_algorithm() function to execute the configured Insider Transfer Strategy with the trading period from 2019-05-02 to 2024-05-01 and with the initial capital of 1,000,000 NTD. The output, or results, will represent the daily performance and detailed transaction records.

from zipline import run_algorithm

results = run_algorithm(
start = start_dt,
end = end_dt,
initialize = initialize,
bundle = 'tquant',
analyze = analyze,
capital_base = 1e6,
handle_data = handle_data,
custom_loader = custom_loader
)

results
Insider Transfer Strategy
Trading Details

Performance Evaluation Using Pyfolio

import pyfolio as pf

returns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)
benchmark_rets = results.benchmark_return

# Creating a Full Tear Sheet
pf.create_full_tear_sheet(returns=returns,
positions=positions,
transactions=transactions,
benchmark_rets=benchmark_rets
)
Insider Transfer Strategy
Backtesting Performance Compared to Benchmark

The table above shows that the insider transfer strategy achieved an annualized return of 19.348%. Additionally, the Sharpe and Sortino ratios are 1.22 and 1.75, respectively, as well as an alpha value of 0.06. This indicates that the insider transfer strategy can generate excess returns under the unit of risk taken and has a certain degree of capital protection capability during downside risks. Observing the chart, we can find that the strategy’s performance was slightly below the market index before April 2021. After that, it officially surpassed the market index performance. The reason is that initially, the strategy was still in the building phase, so its performance could have been better than the market. However, once the positions were established and combined with the RSI indicator for exits during overbought periods, it effectively avoided bear market risks and achieved performance that exceeded the market index.

Insider Transfer Strategy
Exposure Chart of Long and Short Positions

The exposure chart of long and short positions helps us understand the use of capital in long and short positions. From the above chart, we can see that the insider transfer strategy was building positions in the early phase. By the end of 2021, it sold part of the holdings with the help of the RSI indicator, avoiding the bear market trend in 2022 to a certain extent.

Conclusion

This strategy was inspired by insider share transfers, aiming to explore whether following insiders into the market in a constantly rising stock market has good timing capabilities. We constructed an insider transfer strategy by adding the RSI indicator to the timing of insider transfers, and we also briefly introduced how to import data obtained from TEJ Tool API into Pipeline for backtesting. Finally, the insider transfer strategy slightly outperformed the market index in the backtest results. However, it is important to remind investors that the MSCI stocks used in this implementation are large-cap stocks. If applied to small and mid-cap stocks, the backtest results may be different; additionally, the amount invested each time should be evaluated based on the current stock price trend and one's risk tolerance to obtain more stable returns.

Please note that the strategy and target discussed in this article are for reference only and do not constitute any recommendation for specific commodities or investments. In the future, we will also introduce using the TEJ database to construct various indicators and backtest their performance. Therefore, we welcome readers interested in various trading strategies to consider purchasing relevant solutions from TQuant Lab. With our high-quality databases, you can construct a trading strategy that suits your needs.

Source Code

Extended Reading

About

--

--

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

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