← Back to list

The SMA Crossover Secret: How I Built a Python Algorithm to Find Hidden Trading Opportunities

Ever wondered what happens exactly 4 trading days after a bearish crossover? The answer might surprise you…

Unicorn Day · 2025-09-23 00:38 · 82 claps · 3.4 min read paywalled
#trading-algorithms #technical-analysis #simple-moving-average #algorithmic-trading #candlestick-patterns
Open on Medium ↗
Wiki topics: RAG · RAG & Retrieval 💻 · Programming

The SMA Crossover Secret: How I Built a Python Algorithm to Find Hidden Trading Opportunities

Ever wondered what happens exactly 4 trading days after a bearish crossover? The answer might surprise you…

The Code That Started Everything 💻

I was tired of manually scanning charts for SMA crossovers, so I decided to automate the entire process. What started as a simple Python script to identify when SMA 5 crosses below SMA 10 turned into something much more interesting…

My algorithm didn’t just find the crossovers. It revealed what happens after them.

And that’s where things got fascinating. 🔍

The Technical Foundation 📊

Using Yahoo Finance data through the yfinance library, my script pulls clean stock data and calculates three moving averages:

  • SMA 5: The fast-moving average (5-day window)
  • SMA 10: The medium average (10-day window)
  • SMA 20: The slower trend line (20-day window)
# Calculate SMAs
stock['SMA_5'] = stock['Close'].rolling(window=5, min_periods=1).mean()
stock['SMA_10'] = stock['Close'].rolling(window=10, min_periods=1).mean()
stock['SMA_20'] = stock['Close'].rolling(window=20, min_periods=1).mean()

The magic happens when SMA 5 crosses below SMA 10 — traditionally considered a bearish signal. But my algorithm doesn’t stop there…

The Discovery Algorithm 🔍

Here’s where my script gets interesting. Instead of just flagging crossovers, it tracks what happens in the following trading sessions:

# Identify cross down signals (SMA5 crosses below SMA10)
stock['SMA_5_above'] = stock['SMA_5'] > stock['SMA_10']
stock['SMA_5_below'] = stock['SMA_5'] < stock['SMA_10']
stock['Cross_Down_Signal'] = (stock['SMA_5_above'].shift(1)) & (stock['SMA_5_below'])

For every crossover signal, the algorithm looks ahead exactly 4 trading days and applies two strict filters:

Filter 1: The candle must be bullish (Close > Open) Filter 2: The closing price must be above the original crossover candle’s close

Why These Two Conditions Matter 🎯

Through extensive backtesting, these conditions proved crucial:

The Bullish Candle Requirement indicates that buying pressure is returning after the initial selloff. It’s not enough for the stock to recover — the bulls need to be actively stepping in.

Price Recovery Validation: The close must exceed the original “red dot” crossover price, and the stock is not bouncing weakly but actually recovering meaningfully.

# The two critical conditions
is_bullish = fourth_candle_row['Close'] > fourth_candle_row['Open']
above_red_dot_close = fourth_candle_row['Close'] > original_red_dot_close

if is_bullish and above_red_dot_close:
    # This is our signal!

The Visual Analysis System 📈

My script generates interactive Plotly charts with a 2:1 price-to-volume ratio, displaying:

  • Candlestick patterns for price action
  • Three SMA lines (5, 10, 20) for trend analysis
  • Red circles marking each crossover event
  • Green squares highlighting qualifying recovery signals
  • Volume bars for confirmation

It makes the pattern immediate and clear.

Real Example: Google (GOOG) Success Story

Let me show you this algorithm in action with a real example from Google’s recent trading history…

Looking at the GOOG chart above, you can see the power of this systematic approach:

📍 The Setup: Around August 21st, 2025, SMA 5 crossed below SMA 10 (red circle) at approximately $200 — the classic “bearish” signal that scared most traders away.

🎯 The Signal: Exactly 4 trading days later, our algorithm identified the green square at around $208 — a bullish candle that closed above the original crossover price.

📈 The Result: After this entry signal, GOOG continued its upward trajectory, rising from $208 to over $250 — a 20%+ gain for those who recognized the pattern!

This is the beauty of the algorithm… While others saw the red circle as a sell signal, the systematic approach identified it as a setup for the real opportunity 4 days later.

The green square wasn’t just a recovery — it was the beginning of a major uptrend.

Try It Yourself — Live Code Access 💻

Want to test this algorithm on your favorite stocks? I’ve made the complete Python script available on Google Colab:

🔗 Access the Live Code Here: https://colab.research.google.com/drive/1dmXWFb5doFELwvVn1daOW1UhB9uPAkv#scrollTo=HLmIaBjSl81

Simply:

  1. Click the link above
  2. Run the cells in order
  3. Enter any stock symbol when prompted
  4. Watch the algorithm work its magic!

The notebook includes all the code, explanations, and interactive charts. You can experiment with different stocks and timeframes to see the pattern in action.

Disclaimer: Past performance doesn’t guarantee future results. Always do your own research and never risk more than you can afford to lose. This article is for educational purposes only and not financial advice.


메타데이터
post_id
4dcb4a1fccbe
slug
the-sma-crossover-secret-how-i-built-a-python-algorithm-to-find-hidden-trading-opportunities-4dcb4a1fccbe
url
https://medium.com/@wl8380/the-sma-crossover-secret-how-i-built-a-python-algorithm-to-find-hidden-trading-opportunities-4dcb4a1fccbe
canonical_url
https://medium.com/@wl8380/the-sma-crossover-secret-how-i-built-a-python-algorithm-to-find-hidden-trading-opportunities-4dcb4a1fccbe
author_url
https://medium.com/@wl8380
status
ok
fetched_at
2026-08-09 00:55:21