After diving deep into Jim Simons’ Medallion Fund — the most successful hedge fund in history — I became captivated by its secretive inner workings. While direct access to the fund’s code is impossible, Gregory Zuckerman’s book “The Man Who Solved the Market” offered valuable hints:
They began with Markov chains and gradually layered in machine learning, nonlinear models, and advanced statistics.
Inspired by this, I attempted to reverse-engineer a simplified, interpretable version of the original strategy by:
- Modeling stock market behavior using Markov processes based on "up" and "down" states.
- Testing statistical candle streak patterns using backtesting engines.
- Running experiments across 20 major U.S. stocks and ETFs.
Objective: Use historical stock data to construct a Markov chain-based model of market states.
- Data source:
SPY
ETF from 2010–2025 usingyfinance
. - Labels each day as
"up"
📈 or"down"
📉 based on daily returns. - Constructs a 2x2 transition matrix showing:
P(up → up)
,P(up → down)
P(down → up)
,P(down → down)
- Analyzes streak patterns like 5 red candles ➡️ green:
Probability of 📈 after 📉📉📉📉📉 = 67.3%
Objective: Implement and backtest streak-based trading strategies using candle color logic.
- Downloads data for 20 tickers (AAPL, SPY, QQQ, ARKK, etc.).
- Calculates how often 10 green/red candles are followed by reversal.
- 📗📗📗📗📗📗📗📗📗📗 ➡️ 📕 → 42%
- 📕📕📕📕📕📕📕📕📕📕 ➡️ 📗 → 65%
- Implements two strategies using
Backtesting.py
:
- ConsecutiveRedGreenStrategy: Long after
n
red, short aftern
green candles. - ConsecutiveRedStrategy: Buy after 3 red candles, close same-day.
Backtests were run on all 20 tickers.
Ticker | Return (%) |
---|---|
AAPL | -10.69 |
MSFT | -12.35 |
GOOGL | -10.90 |
AMZN | 56.78 |
META | 71.91 |
TSLA | 60.33 |
NVDA | -13.42 |
JPM | -28.41 |
JNJ | 2.32 |
V | 77.97 |
SPY | -19.96 |
QQQ | -1.30 |
DIA | -38.14 |
IWM | -27.31 |
ARKK | -25.21 |
XLF | -26.34 |
XLK | 1.53 |
XLV | -12.28 |
XLE | -29.17 |
XLY | 11.74 |
Aggregate Performance:
- Total Return:
+27.13%
- Average Return per Asset:
+1.36%
- Maximum Drawdown Observed:
-46.76%
Objective: Replicate and visualize the Markov-based streak strategy from
SP500_index_Test.py
directly on TradingView.
- Detects streaks of consecutive red (📉) or green (📈) candles.
- Generates buy signals after a configurable number of red candles.
- Optionally, generates sell signals after green streaks (mirroring short entry logic).
- Clean visual overlay for easy chart-based backtesting.
- Supports real-time alerts and can be adapted for auto-trading systems.
This indicator helps traders:
- Validate the model visually on SPY.
- Track streak-based edge in live markets.
- Integrate with TradingView’s backtest engine and alert system.
- Open TradingView > Pine Editor.
- Paste the contents of
updated_v5_pinescript_code.txt
. - Add to any SPY chart (or adapt to other tickers).
- Customize streak length, marker visibility, or alert conditions as needed.
📌 Note: This TradingView script completes the project pipeline — from data analysis and modeling in Python to live market observation via charting tools.
- Markov Chain models can capture short-term momentum and reversal regimes.
- Candle streaks reveal hidden market biases not easily spotted with basic indicators.
- Some assets (AMZN, META, TSLA) show high profit potential using simple reversal strategies.
- Even with basic logic, behavioral inefficiencies can be modeled quantitatively.
- Backtesting across diverse sectors gives a better real-world robustness picture.
- Incorporate Hidden Markov Models (HMMs) for more flexible regime detection.
- Add volatility-based filters to avoid whipsaws.
- Explore LSTM-enhanced streak modeling for sequence learning.
- Add position sizing optimization and risk-adjusted returns.
Om Aditya
B.Tech @ IIT Jodhpur | Markets, Quant, and ML Enthusiast
📘 Passionate about decoding financial anomalies
🔗 LinkedIn
💻 Github