Python Notebook to screen stocks using AI Agents¶
Screen the S&P 500 for a combined qualitative investment thesis:
- Recurring revenue >75% - high-quality subscription/contract-based businesses
- Taiwan tensions beneficiary - companies positioned to benefit from US-China geopolitical tensions
This is a filter that no traditional stock screener can perform.
In [ ]:
# Setup
import asyncio
from pathlib import Path
import pandas as pd
from pydantic import BaseModel, Field
from dotenv import load_dotenv
# Load API key from .env
load_dotenv()
from everyrow.ops import screen
In [2]:
# Load S&P 500 companies
stocks = pd.read_csv("../data/S&P 500 Companies.csv")
print(f"Loaded {len(stocks)} companies")
stocks.head()
Out[2]:
In [3]:
# Define the screening criteria
SCREENING_TASK = """
Find companies with high-quality recurring revenue business models that would
also benefit from escalating US-China tensions over Taiwan.
**Recurring revenue >75%**: Subscription services, long-term contracts,
maintenance agreements, royalty streams. Not one-time product sales or
project-based work. Be conservative when estimating.
**Taiwan tensions beneficiary**: Companies that would see increased revenue
or strategic importance from Taiwan tensions - think CHIPS Act beneficiaries,
defense contractors, cybersecurity, reshoring plays, alternative supply chain
providers. Exclude companies dependent on Taiwan manufacturing or with
significant China revenue at risk.
"""
# Output schema - just pass/fail for efficiency
class ScreenResult(BaseModel):
passes: bool = Field(
description="True if company has >75% recurring revenue AND is a Taiwan tensions beneficiary"
)
In [ ]:
# Run the screen
async def run_screen():
print("Screening... (this will take a few minutes)\n")
result = await screen(
task=SCREENING_TASK,
input=stocks,
response_model=ScreenResult,
)
return result.data
# Run it
results = await run_screen()
In [5]:
# Summary
print(f"Companies passing both criteria: {len(results)}")
print(f"Pass rate: {len(results)/len(stocks)*100:.1f}% of S&P 500\n")
print("Passing companies:")
for _, row in results.iterrows():
print(f" {row['ticker']:6} | {row['company'][:40]}")
In [6]:
# View the research for a specific company
if 'research' in results.columns:
sample = results[results['ticker'] == 'NOW'].iloc[0]
print(f"Research for {sample['company']}:\n")
print(sample['research'])
In [7]:
# Breakdown by sector
if 'gics_sector' in results.columns:
print("Passing companies by sector:")
print(results['gics_sector'].value_counts())
In [8]:
# Save results
results.to_csv("thematic_screen_results.csv", index=False)
print(f"Results saved to thematic_screen_results.csv")
What Just Happened?¶
The everyrow.io screen:
- Researched each company using web search to understand their business model
- Evaluated recurring revenue by finding actual revenue breakdowns in 10-Ks and earnings
- Assessed geopolitical positioning by researching supply chains, government contracts, and China exposure
- Made a judgment on whether both criteria were met
This would take an analyst weeks to do manually for 500 companies.
Try Your Own Thesis¶
Just change SCREENING_TASK to any criteria you can describe in plain English:
- "Companies with founder still as CEO"
- "Companies that would benefit from AI infrastructure buildout"
- "Companies with >50% international revenue but <5% China exposure"
If you can describe it, everyrow can screen for it.