Table of Contents
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.
Using the deviation rate as a trading strategy prompt
This article uses the Mac operating system and Jupyter Notebook as the editor.
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_base="http://api.tej.com.tw"
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']
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.
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferWindowMemory
llm_model = 'gpt-3.5-turbo-0301'
llm = ChatOpenAI(temperature=0.0, model=llm_model)
memory = ConversationBufferWindowMemory(k=5) # 設定只保留五次對話以前的紀錄
# 建立對話鏈
conversation = ConversationChain(
llm=llm,
memory = memory,
verbose=False
)
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')
# opens = stock['開盤價(元)'][i]
closes = stock['收盤價(元)'][i]
open_price = stock['開盤價(元)'][i]
date_price += f'date: {date} '
# date_price += f'開盤價: {opens} '
date_price += (f'open: {open_price} ' + f'close: {closes}\n')
prompt = f'幫我分析市場波動趨勢,以下為股價資料:\n'+ date_price + '幫我用繁體中文分析'
print(prompt)
print(conversation.predict(input=prompt))
print(conversation.predict(input='用三天的移動平均,幫我計算從六月開始每次收盤當天的乖離率,產出表格'))
print(conversation.predict(input='幫我用本金十萬元在六月第一天進場,前一天收盤價大於四天前最高價,且收盤乖離率>=0,則當天開盤價出場。\
在前一天收盤價小於四天前最低價,且收盤乖離率<=0,則當天開盤價進場,生成表格'))
print(conversation.predict(input='幫我產出詳細表格報表,包含獲利金額大小、總獲利大小'))
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.
Subscribe to newsletter