← Back


Agents for Stock Prediction

Example of a stock prediction

Published: 2025-09-02


Understanding Agents

Recently I've been playing around with agents and tool calling from LLMs. Check out my blog post about Spectre to see more about how I've been building my own coding agent. I've been working on my rails app, and have it setup to connect to OpenAI and Vector Databases. I really wanted to see how agents could work in a rails app. It's not something I had done previously, and it turns out to be pretty straightforward.

The rails app I have it setup in has nothing to do with stocks. It's just quicker to throw this stock prediction into this app, instead of spinning up a whole new server. I like to move quick when I have an idea, and setting up servers and databases is mind numbing work to me.

Processing Stocks

The idea I had was to build a very aggressive agent that ingests economic news and then presents a couple "plays" to do in the stock market. Aggressive is the key here, I want to see if it can make >100% gains in a short amount of time.

Finding a data source for news was difficult, lots of places require you to pay lots of money and don't allow filtering. What I ended up finding was Alpha Vantage, they work closely with YCombinator. They are only focused on economic news, and allow you to filter based on a handful of economic categories.

Now Alpha Vantage is great, but they only allow 25 requests per day, not great for building a database of all stocks, I wanted to allow the agent to search stocks based on a news article. Their base plan is something like $50/mo, so I subscribed to that. This allows 75 requests per minute, still not amazing for pulling in stocks, but workable. The idea is to pull in all stocks, their names, and descriptions of the company, then vectorize the entire thing, and have the agent search this database to find stocks.

Why not use elastic search you may ask. Well, I had vectors already set up in my database since I was allowing recruiters to search developers to hire. I didn't need to go through the process of setting up elastic search. Again, we're going for speed of implementation, not the most greatest solution in the world. The search functionality is not the most important thing since articles often include stock symbols that the agent can act upon.

Building the Agent

The agent needs data to work off of. We want it to read news articles and process them into a play on the stock market. To do this I setup a simple cron that calls a rake task to pull the most recent 3 articles from a subset of the categories available. This cron runs every hour. It could probably be optimized to only run during trading hours, but again, moving fast and not focusing on small things. Only doing 3 allows me to not spend too much on OpenAI. Plus, since it's every hour, not many articles are written in that time, so it doesn't really miss any articles.

The steps for the agent:

  1. Send article to LLM, this includes a snippet of the article, and the URL.
    • The agent has a tool to pull in the full article. To accomplish pulling the full article, it uses a basic request and uses a gem called readability to pull just the main content of the article.
  2. Search for stock symbols, using the vector database we have setup. The LLM sends a search query and the top 5 results are returned. This allows for the agent to find stocks that may be related to the article, but not explicitly mentioned.
  3. Tell the agent to pull more articles for every stock symbol present, this allows the agent to fully understand what is going on in the news for the stock. This was necessary for the agent to not over-inflate the effect of a single article.
  4. Then the agent pulls the most recent stock price for every stock it found, this is done via Alpha Vantage.
  5. There's a final tool call to forecast the stocks that it found. This is not a very well made forecast and uses a very simple forecasting model. Again, moving fast, and I'm not a data scientist. This is just to get a rough idea of where the stock may go. The forecasting model is prophet, the script is the first result I found when I searched for "stock forecasting with python".

Results

This is still fairly early days, but I've seen some decent results from it. Some plays die, but there's been a couple that have had >100%. The plays still require my own research, but it sets up a good base for me to work off of. What I've found is small stocks do not result in good plays.


I'm currently looking for work, if you have a role that you think I'd be a good fit for feel free to reach out to me on LinkedIn.