How I built a 200-day moving average scanner for the entire S&P 500 using OpenClaw AI agents, Python, and free public APIs — all controllable from Telegram.TL;DR: I built a 200-day moving average trend filter that scans all 503 S&P 500 stocks, detects golden crosses, death crosses, and trend regime changes, then sends actionable reports to Telegram. It shares a single data cache with my other investment skills — meaning the entire scan runs in seconds, not minutes. Total data cost: $0.
The Idea: Don’t Buy Good Businesses in Broken TrendsIn my previous article, I built a Buffett-style stock screener that finds fundamentally strong companies. But there was a blind spot: timing.
A company can score 9/10 on Buffett’s criteria and still drop 40% if the market is in a downtrend. Fundamental quality tells you what to buy. The 200-day moving average tells you when.
A quote widely attributed to Charlie Munger — though without a verified primary source — captures the idea perfectly:
“If all you ever did was buy high-quality stocks on the 200-week moving average, you would beat the S&P 500 by a large margin over time. The problem is, few human beings have that kind of discipline.”Paul Tudor Jones — billionaire hedge fund manager — put the risk side of it bluntly:
“My metric for everything I look at is the 200-day moving average of closing prices. I’ve seen too many things go to zero, stocks and commodities. The whole trick in investing is: ‘How do I keep from losing everything?’”The 200-day MA is the most widely watched long-term trend indicator on Wall Street. Institutional investors, hedge funds, and algorithmic traders all use it as a line in the sand:
The academic backing is solid. Brock, Lakonishok, and LeBaron (1992) tested simple moving average rules on the Dow Jones from 1897 to 1986. The result: a simple MA rule added roughly 3–4% per year over buy-and-hold, mostly by avoiding catastrophic declines. More recently, Mebane Faber’s 2006 paper “A Quantitative Approach to Tactical Asset Allocation” showed that a 10-month (~200-day) MA applied as a risk filter significantly reduced drawdowns while preserving most of the upside.
The strategy isn’t about beating the market every month. It’s about not buying fundamentally good businesses while they’re falling off a cliff.
Why Most Tools Implement This BadlyTradingView shows you a chart with a moving average line. That’s fine for one stock. But try answering these questions with a charting tool:
Bloomberg can do all of this. For $24,000/year.
I wanted the same capability for $0, controllable from my phone.
What I BuiltA Telegram-accessible trend filter with three commands:
CommandWhat It DoesSpeedmarket breadthFull S&P 500 trend overview — how many stocks above/below 200d MA~3 secondscrossoversStocks near the 200d MA — potential buy/sell signals, golden/death crosses~3 seconds200ma AAPLDeep trend analysis on a single stock — MAs, RSI, Williams %R, 52-week range<1 second
All of this runs on the same $5/month VPS as my Buffett screener. Same data cache. Zero additional API costs.
The Architecture: Shared Data, Independent SkillsHere’s the key insight that made this project fast to build: I didn’t start from scratch.
My Buffett screener already fetched 300 days of price data for 503 stocks and cached it in SQLite. The trend filter just reads from the same cache.
┌─────────────────────────────────────────┐The stock_data_commons module fetches price data once per day at 12:00 EST. Every skill that runs after that — Buffett analysis at 12:15, trend filter at 12:30, GARP screener at 12:45 — reads from SQLite in milliseconds.
Result: 6 different investment skills share one data fetch. Zero duplicate API calls.
The Indicators200-Day Simple Moving AverageThe 200-day SMA is the average closing price over the last 200 trading days (~10 months). It smooths out short-term noise and reveals the long-term direction.
# From stock_data_commons/derived_metrics.pyWhen the 50-day MA crosses above the 200-day MA, it’s called a golden cross — a bullish signal. The reverse (50-day crossing below) is a death cross — bearish.
These aren’t magic signals. They’re lagging indicators that confirm a trend has already changed. But that’s the point: they keep you from fighting the trend.
Market BreadthThe single most useful output of this tool: what percentage of S&P 500 stocks are above their 200-day MA?
This tells you the health of the overall market better than any index number: