【Quant】Implementation of Deviation Rate Trading Strategy using TEJAPI and LLM

TEJ 台灣經濟新報
5 min readNov 22, 2023

Utilizing TEJAPI data as the foundation for LLM market analysis.

photo by Mike Hindle on Unsplash


TEJAPI, Quantitative Trading, LLM


  • Article Difficulty:★☆☆☆☆
  • Using TEJAPI stock price database as the basis for LLM analysis.
  • Providing prompts to the model in a question-and-answer format for automated trading analysis.


In today’s complex financial landscape, investors and traders no longer rely on traditional research methods. Instead, they turn to highly intelligent data analysis tools to better understand stock market trends, identify investment opportunities, and make informed financial decisions. In this fast-paced financial environment, converting a vast amount of market data into actionable insights is crucial.

In this context, the combination of LLM (Large Language Model) and TEJAPI provides powerful capabilities for textual analysis. LLM is a robust model based on natural language processing, capable of understanding and generating natural language to explore market news, company reports, social media comments, and other forms of textual data for a deeper understanding of market sentiment and events. TEJAPI, on the other hand, offers rich financial market data for precise analysis by LLM.

This article explores how combining LLM and TEJAPI enhances the efficiency and accuracy of stock market analysis. It elaborates on how this integration aids in identifying market trends, analyzing stock performance, discovering key information in market news, and providing third-party investment insights. This combination not only benefits professional traders and investors but also provides ordinary investors with additional ways to grasp the market.

Trading Strategy

Using the deviation rate as a trading strategy prompt

  • If the closing price is higher than the highest price three days ago, and the closing deviation rate is >= 0, exit at the opening price on that day.
  • If the previous day’s closing price is lower than the lowest price three days ago, and the closing deviation rate is <= 0, enter at the opening price on that day.

Editing Environment and Module Requirements

This article uses the Mac operating system and Jupyter Notebook as the editor.



Data Import

Data period from May 20, 2023, to June 30, 2023, using TSMC (2330) as an example, fetching unadjusted opening and closing prices for analysis.

import tejapi
api_key = 'your key'
tejapi.ApiConfig.api_key = api_key
tejapi.ApiConfig.ignoretz = True
def get_stock_price(gte, lte, ticker):
stock = tejapi.get('TWN/APRCD',
mdate = {'gte':gte, 'lte':lte},
coid = ticker,
chinese_column_name = True
return stock

This time, the LLM used is OpenAI’s gpt-3.5-turbo-0301. To begin, you need to input your personal API key, which can be obtained by applying on the official OpenAI website.

import os
import openai
os.environ['OPENAI_API_KEY'] = 'your api key'
openai.api_key = os.environ['OPENAI_API_KEY']

Application of LangChain based on LLM

LangChain is an open-source Python library that provides the necessary tools to build AI applications based on Large Language Models (LLM). Developers can easily integrate with LLM to accomplish tasks such as text generation, question answering, translation, and dialogues. LLM itself lacks memory capabilities, but with LangChain technology, conversation records can be stored, offering a seamless conversational experience that resembles interacting with a human.

Import the LangChain module to leverage its functionality

from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferWindowMemory

Construct the gpt-3.5-turbo-0301 model

llm_model = 'gpt-3.5-turbo-0301'
llm = ChatOpenAI(temperature=0.0, model=llm_model)
memory = ConversationBufferWindowMemory(k=5)
conversation = ConversationChain(
memory = memory,

Import stock price data

gte = '2023-05-20'
lte = '2023-06-30'
stock = get_stock_price(gte, lte, '2330')
date_price = ''
for i in range(len(stock)):
date = stock['年月日'][i].strftime('%Y-%m-%d')
closes = stock['收盤價(元)'][i]
open_price = stock['開盤價(元)'][i]
date_price += f'date: {date} '
date_price += (f'open: {open_price} ' + f'close: {closes}\n')

prompt = f'幫我分析市場波動趨勢,以下為股價資料:\n'+ date_price + '幫我用繁體中文分析'

Interactive market analysis

Add a dialogue box


Set up the trading strategy


Generate a detailed result report



This presentation introduces the use of LLM for a deviation rate trading strategy. In situations where the market exhibits oversold conditions (deviation rate < 0) and the closing price is higher than the highest price over a certain period, it is judged that the stock price will gradually return to the moving average; hence, a long position is initiated. Conversely, when the stock price shows overbought conditions (deviation rate > 0) and the closing price is lower than the lowest price over a certain period, indicating an overvaluation with a downward trend, the existing long position is closed. The rapid analytical conclusions are facilitated by the language model’s computational and analytical capabilities.

However, it is crucial to reiterate that the targets mentioned in this article are for illustrative purposes only and do not represent recommendations or advice on any financial products. Additionally, the outcomes generated by the language model do not guarantee absolute correctness and require further confirmation. Therefore, readers interested in topics such as strategy construction, performance backtesting, and research evidence are encouraged to explore solutions available in TEJ E Shop, which provides comprehensive databases for various tests.

Source Code

Extended Reading

Related Links



TEJ 台灣經濟新報

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