How to use large language models to help with financial research and algorithmic trading
Video Demo | GitHub Repo | Try it yourself on the cloud for free
There is a surprising lack of posts here about how large language models can help you perform financial research and develop algorithmic trading strategies.
And no, before you angrily type your comment, I’m not saying you can use LLMs to predict tomorrow’s stock price. Large language models can’t do that, and anybody that tells you otherwise (clearly) doesn’t understand how they work under the hood.
However, LLMs are able to generate structured data. What this means is that you can use it to streamline many workflows by building a natural language interface.
Here is an example: I open-sourced a tool for extracting and analyzing a company’s fundamentals using LLMs.
Requirements:
In order to run the AI Financial Analysis tool, you’ll need the following:
- An account with SimFin
- An OpenAI API key
- Python installed on your system
Setup:
After creating an OpenAI and SimFin account, you’ll need to setup the repository. To do this:
- Clone the repo on GitHub: https://github.com/austin-starks/AI-Financial-Analysis
- cd into the directory
- Create a file called .env and add your Simfin token and OpenAI tokens
- Create a virtual environment and install the dependencies in requirements.txt
Refer to the ReadMe for more detailed instructions on how to set up the repo.
Running the Script:
After installing the dependencies, you can run it as follows.
python3 chat.py
This command starts a chat conversation between the user and an AI.
During the conversation, the model will learn what ticker you want to analyze, what year to extract the earnings for, and what period (q1, q2, q3, q4, or the full year).
At the end of the conversation, the model will output a summary of the stock’s financials for that timeframe. For example:
Based on the provided financial information, here is a summary of the company's finances:1. Balance Sheet:
- Total Assets: $346,747,000,000
- Total Liabilities: $290,020,000,000
- Total Equity: $56,727,000,000 The company has a strong balance sheet with a significant amount of assets compared to its liabilities. This indicates a healthy financial position.2. Cash Flow:
- Net Cash from Operating Activities: $34,005,000,000
- Net Cash from Investing Activities: -$1,445,000,000
- Net Cash from Financing Activities: -$35,563,000,000
- Net Change in Cash: -$3,003,000,000
(etc) Based on the provided financial information, here is a summary of the company's finances:1. Balance Sheet:
- Total Assets: $346,747,000,000
- Total Liabilities: $290,020,000,000
- Total Equity: $56,727,000,000 The company has a strong balance sheet with a significant amount of assets compared to its liabilities. This indicates a healthy financial position.2. Cash Flow:
- Net Cash from Operating Activities: $34,005,000,000
- Net Cash from Investing Activities: -$1,445,000,000
- Net Cash from Financing Activities: -$35,563,000,000
- Net Change in Cash: -$3,003,000,000
(etc)
How it works
The goal is to generate a JSON object that interacts with financial APIs. You talk to the LLM and it will iteratively learn what stock you want to analyze and the timeframe you want to do it for.
After the conversation is over, the AI will generate a syntactically valid JSON object to call the API.
Then, after receiving a response from the model, you will take it and feed it back into the large language model to summarize the information.
Discussion
You can imagine how this is very extensible. For example, instead of generating JSONs, what if you generated a SQL query to the database? So then, in addition to getting earnings for a particular company, you could then execute commands like:
- Which AI stocks with a market cap above $50 billion had the highest volume last week?
- Which biotechnology stocks increased their revenue, net income, and free cash flow since last year?
- What electric vehicle stocks have the highest free cash flow in 2023?
In fact, you could go even deeper. For example, if you had a backtesting platform that works with JSON objects, you could generate and test new ideas in natural language!
For example:
Create a portfolio with two strategies. The buy strategy will buy 5% of my buying power in TQQQ whenever (QQQ’s price — QQQ’s 30 day SMA) / QQQ’s 5 day SD < -0.3. Sell 40% of my TQQQ positions if I have more than $2000 of TQQQ and my positions are up 13% and I haven’t sold TQQQ in 7 or more days
Then, for backtesting:
Backtest this portfolio from Jan 1st 2024 to today
That’s the idea behind NexusTrade. I’m building a comprehensive AI-Powered algorithmic trading platform to to help retail investors perform financial research and deploy algorithmic trading strategies.
While the GitHub repo shows a very simple use-case, NexusTrade has LLMs integrate to accomplish a wide range of functionality for retail investors.
The chat agent, Aurora, can do many things, including:
- Automate your investing: Create and deploy customizable algorithmic trading strategies
- Financial research: Find trading opportunities based on objective fundamental data
- Financial Comparison: Compare two or more companies to each other
- Simulations before deploying: Backtest your ideas on historical data, and then paper-trade them before risking your hard-earned money.
NexusTrade also has other advanced features, like genetic optimization, to help you create algorithmic trading strategies.
Try it now for free!
Conclusion
Large language models are useful for algorithmic trading, just not in the way people expect. It cannot predict tomorrow’s price, but it can help you expedite certain workflows that are useful when developing or deploying algorithmic trading strategies.
This article shows a concrete example of how to use LLMs for financial research. It then discusses ways LLMs can be used in other workflows across finance.
Thank you for reading!
Follow me: LinkedIn | X (Twitter) | TikTok | Instagram | Newsletter