【Application】TQuant Lab Insider Transfer Strategy: A Solution to Timing Difficulties
Highlight
- Article Difficulty: ★★☆☆☆
- This article will introduce the approach of importing information obtained from
TEJToolAPI
intoPipeline
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.
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.
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:
- Calculate the RSI value using the built-in factors from
pipeline
. - Add the transfer methods and daily transfer data into
pipeline
. - Add liquidity slippage and transaction fees, and set the return of the TAIEX-Total Return Index as the benchmark.
- Set the restriction to only long positions that can be constructed in the strategy.
- Set the Insider Transfer Strategy and record the transaction details.
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
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
)
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.
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
- “Block Trades” as Warning Signals? Analyzing Insider Transfers and Changes in Stock Prices Using TEJ API
- TQuant Lab Ichimoku Kinko Hyo Strategy, A Self-contained Technical Analysis Indicator