- Avanzai Blog
- Posts
- how to build your own universe screening tool with Avanzai
how to build your own universe screening tool with Avanzai
OpenAI’s SDK Agent tool is all you need

It's no surprise that the last few months have been a rollercoaster ride in the markets, and with tariff wars not ending anytime soon, it's best to be prepared to see which assets are most impacted or exposed to these tariffs. Open any AI x Investing tool in the market right now, and they will likely have some functionality to help you gauge exposure of your portfolio (or list of instruments) to the broader markets by either combining: a search tool, some risk analysis tool (correlation or beta analysis), or a factor model approach. Today we'll show you how to build the former two using Avanzai (and free / public data), and trust me we'll get to the factor model approach very soon as well.

Example of query of getting stocks most correlated to NASDAQ
High level, what the tool is doing is taking a natural query and dividing it into three parts:
what are the relevant tickers?
what data do I need?
what analytics is needed to answer the question?
That means when it comes to finding what are best performing healthcare stocks in the past quarter, then the question turns into:
what stocks are in the healthcare industry?
I need daily pricing/daily returns
I need to find cumulative returns for the past quarter
With Avanzai we’ve built a ticker extraction tool that leverages text-to-SQL capabilities for getting a subset of a security master universe. Full code can be found here, and we plan on allowing users to swap out their own universe with the Avanzai demo tool.
Similarly for daily prices, we have historical prices going back to 2010 from YFinance, where we have a similar data agent that pulls this data and filters for the relevant tickers , again full code can be found here.
Frankly I’m not going deep into the above code more because if you’ve read this far, you’ve likely either:
built your own ticker extraction / data extraction tool or:
have read my previous posts where I mention how to do this ;)
The beauty here is setting it up so you can have an agent that take both of these and then can run analysis on top of them, this is why we are now building our ranking and exposure functions. This takes advantage of OpenAI’s new Agent SDK, which makes it super easy to build agents and integrate things like: news search, function calling, and more
Here we show an example where we build a universe ranking function that has sub functions that calculates performance or technical indicators for our universe of instruments and ranks them by top or bottom. This helps answer questions like: what are the top performing X instruments or which stocks have their 50 day SMA cross the 200 day SMA

This implementation shows the complete flow:
rank_by_performance: Core function that computes cumulative returns (1 + r1)(1 + r2)...(1 + rn) - 1 for each ticker
rank_by_technical_indicator: Alternative ranking using either:
Price/SMA ratio (current price relative to moving average)
Golden Cross (short-term MA / long-term MA)
universe_ranking_function: Main function that:
Loads returns from your session
Applies date filters
Calls the appropriate ranking function
Saves results to S3
Returns a structured response
create_returns_analysis_agent: Creates an LLM agent that knows how to:
Parse natural language queries
Pick the right ranking method
Format parameters correctly
Return results in a consistent format
The agent acts as a natural language interface to these underlying ranking functions. When you say "show me the top performers", it:
Recognizes this as a performance ranking task
Sets up the right parameters (metric="performance", sort="top")
Calls universe_ranking_function
Formats the results into a clean dict of ticker → return value
Now we can do the same with exposure as well, since the code is similar I’ll save you the scroll time so you can check it out here:
And that’s pretty much it folks! I’ll be posting more open sourced code example here, but soon anyone can use our Avanzai Open Demo to test these things out yourself. If you’re a firm interested in integrating this within your firm, please feel free to reach out!