Skip to content

droidmaximus/topological-arbitrage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Topological Arbitrage

Adaptive market-neutral statistical arbitrage using graph diffusion, multi-layer regime detection, and Riemannian correlation geometry.

Performance (2025)

Metric Value
Annual Return 42.50%
Sharpe Ratio 1.99
Max Drawdown -13.74%
Win Rate 56.0%
Best Quarter Q2: +34.75% (Sharpe 7.02)

Validation on Unseen Data (Apr-Nov 2025): 1.11% return with positive Sharpe


Visualizations

Performance Dashboard

Performance Dashboard

Market Topology Evolution (2025 with 2024 Warmup)

Watch how the stock correlation landscape shifts through time. Peaks represent stocks deviating from normal correlations - these are the trading opportunities. Notice the sustained high peaks in Q2 2025 (when the strategy returned +34.75%) versus the flat terrain in Q1 (when it lost -10.95%).

Topology Terrain 2025 with 2024 Warmup

See all visualizations in visualizations/ and detailed analysis in docs/RESULTS.md.


Core Algorithm

Signal Generation

  1. CAPM Decomposition: Remove market beta to isolate idiosyncratic returns
  2. Correlation Graph: Build correlation matrix of residuals
  3. Graph Laplacian Diffusion: Separate systematic sector moves from idiosyncratic alpha
  4. Rough Signal: Extract mean-reversion opportunities (the actual trading signal)

Position Selection

  • Universe: Top 80 stocks by liquidity (daily point-in-time filtering)
  • Long: 10 stocks with most negative idiosyncratic residuals (reverting down)
  • Short: 10 stocks with most positive idiosyncratic residuals (reverting up)
  • Rebalancing: Daily
  • Sizing: Equal-weighted within each leg

Risk Management

3-layer regime detection prevents trading during structural breaks:

  1. Market Trend (SPY vs 200-day SMA)
  2. Correlation Regime (0.40-0.65 sweet spot detection)
  3. Riemannian Manifold Drift (SPD matrix geodesic distance)

Quick Start

Installation

# Clone repository
git clone https://github.com/yourusername/topological-arbitrage
cd topological-arbitrage

# Create virtual environment
python -m venv venv
source venv/Scripts/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Run Backtest

# Full year 2025 backtest
python backtest.py

# Custom date range
python backtest.py --start 2025-01-01 --end 2025-12-31

# With visualizations
python backtest.py --visualize

Live Paper Trading

# Initialize new portfolio
python live_paper_trading.py --init --capital 100000

# Run daily update
python live_paper_trading.py

# View status
python live_paper_trading.py --status

# Launch web dashboard
python live_dashboard.py  # Open http://localhost:5000

Project Structure

topological-arbitrage/
├── README.md                    # This file
├── RESULTS.md                   # Performance results & quarterly breakdown
├── LICENSE                      # MIT License
├── requirements.txt             # Python dependencies
├── .gitignore
│
├── Core Strategy
├── paper_trading_main.py        # Backtesting orchestrator
├── paper_trading_engine.py      # Portfolio management & execution
├── paper_trading_monitor.py     # Performance tracking
├── paper_trading_config.py      # Configuration & parameters
│
├── Signal Generation
├── graph_diffusion.py           # Laplacian decomposition
├── capm.py                      # CAPM residual calculation
├── data_loader.py               # Market data fetching
│
├── Regime Detection (3-Layer)
├── market_trend_filter.py       # Layer 1: SPY trend
├── correlation_regime.py        # Layer 2: Correlation sweet-spot
├── riemannian_regime.py         # Layer 3: Manifold drift
├── riemannian_geometry.py       # SPD manifold operations
│
├── Live Trading System
├── live_paper_trading.py        # Real-time daily execution
├── live_dashboard.py            # Web monitoring interface
├── schedule_live_trading.py     # Task scheduler integration
│
├── Data & Configuration
├── our_stocks.csv               # 80-stock universe
├── fundamentals.csv             # Stock metadata
├── paper_trading_config.py      # All strategy parameters
│
├── Results & Documentation
├── RESULTS.md                   # Performance analysis
├── docs/
│   ├── METHODOLOGY.md           # Mathematical foundations
│   ├── REGIME_DETECTION.md      # 3-layer detection system
│   └── ARCHITECTURE.md          # Code architecture guide
│
├── examples/
│   ├── basic_backtest.py        # Simple example
│   └── custom_parameters.py     # Parameter configuration
│
├── publication_results/         # 2025 validation data
├── validation_results/          # OOS validation (Apr-Nov 2025)
├── q1_2026_results/             # Q1 2026 results
│
└── data_cache/                  # Cached market data


Key Features

✅ Implemented

  • Graph Laplacian diffusion signal generation
  • 3-layer adaptive regime detection
  • Point-in-time stock universe selection (no lookahead bias)
  • Daily rebalancing with transaction costs
  • Persistent live trading system with state management
  • Web dashboard for monitoring
  • Complete audit trail of all trades and decisions

🔄 Tested & Validated

  • Backtested 2018-2024 (in-sample)
  • Walk-forward validation 2025 (unseen data)
  • Out-of-sample validation Apr-Nov 2025 (positive Sharpe)
  • Live paper trading since Mar 3, 2026 (daily decision logs)

Using the Code

Basic Backtest

from paper_trading_main import run_backtest

results = run_backtest(
    start_date='2025-01-02',
    end_date='2025-12-31',
    initial_capital=100000
)

print(f"Return: {results['total_return']:.2%}")
print(f"Sharpe: {results['sharpe_ratio']:.2f}")
print(f"Max DD: {results['max_drawdown']:.2%}")

Custom Parameters

from paper_trading_config import STRATEGY_PARAMS

# Modify strategy parameters
STRATEGY_PARAMS['n_longs'] = 15
STRATEGY_PARAMS['n_shorts'] = 15
STRATEGY_PARAMS['window_size'] = 90

# Run with new config
results = run_backtest(strategy_params=STRATEGY_PARAMS)

Live Trading

from live_paper_trading import LivePaperTradingEngine

# Initialize
engine = LivePaperTradingEngine(initial_capital=100000)

# Daily update
signals = engine.update()
print(f"Combined exposure: {signals['combined_long_exposure']:.1%}")

Performance Expectations

  • Favorable Regime (corr 0.40-0.65): Sharpe 2.5-3.5, full exposure
  • Neutral Regime (transitional): Sharpe 1.0-2.0, 50% exposure
  • Hostile Regime (structure breakdown): No positions, 100% cash

See RESULTS.md for regime-stratified performance breakdown.


Configuration

All strategy parameters in paper_trading_config.py:

STRATEGY_PARAMS = {
    'n_longs': 10,              # Number of long positions
    'n_shorts': 10,             # Number of short positions
    'rebalance_interval': 1,    # Days between rebalances
    'universe_size': 80,        # Total stocks in universe
    'window_size': 60,          # Days for correlation window
    'laplacian_alpha': 0.8,     # Diffusion parameter
    'transaction_cost_bps': 20, # 0.20% per trade
}

REGIME_CONFIG = {
    'market_trend_enabled': True,
    'correlation_regime_enabled': True,
    'riemannian_regime_enabled': True,
    'correlation_sweet_spot': (0.40, 0.65),  # Favorable range
}

Mathematical Foundation

The strategy is based on spectral graph theory and geometric statistics:

  • Graph Laplacian Diffusion: Separate smooth (sector) from rough (idiosyncratic) signals
  • Riemannian Geometry: Model correlation matrices on SPD manifold with correct geodesic distances
  • CAPM: Remove systematic market factor before analysis
  • Mean Reversion: Bet that deviations from peer correlations revert

See docs/METHODOLOGY.md for mathematical details and equations.


Data

Stock Universe

  • Source: our_stocks.csv (80 stocks, point-in-time selection)
  • Liquidity Filter: Top 80 by 120-day average volume
  • Sectors: Broad market coverage (tech, healthcare, finance, energy, etc.)

Market Data

  • Source: yfinance
  • Frequency: Daily OHLCV
  • Lookback: 120+ days for initialization
  • Update: Daily after market close

Results

2025 Full Year

  • Return: 42.50%
  • Sharpe: 1.99
  • Max DD: -13.74%
  • Win Rate: 56%

Quarterly Performance

Quarter Return Sharpe Max DD
Q1 2025 -10.95% -2.61 -13.39%
Q2 2025 +34.75% 7.02 -2.58%
Q3 2025 +6.46% 1.49 -4.92%
Q4 2025 +10.63% 2.15 -3.98%

Key Insight: Strategy performs best when stocks deviate from normal correlations (volatile periods) - the exact conditions where alternative risk management fails.

See RESULTS.md for:

  • Full historical breakdown (2018-2025)
  • Quarterly visualizations
  • Regime-stratified performance
  • Risk metrics by period

Live Monitoring

Web dashboard tracks:

  • Portfolio value & daily returns
  • Regime detection status (market, correlation, Riemannian)
  • Current positions and weights
  • Trade activity and rebalance timing
  • Performance metrics
python live_dashboard.py
# Open http://localhost:5000

Considerations & Limitations

Assumptions in Backtests

  • Execution: Perfect fills at closing prices
  • Costs: 20 bps per trade (realistic institutional)
  • Shorting: Unlimited short availability (conservative)
  • Slippage: Not modeled (would reduce returns ~50-100 bps/year)
  • Data: Survivorship bias limited by point-in-time universe selection

Risk Factors to Monitor

  1. Correlation Breakdown: When correlations approach 0 or 1
  2. Regime Transitions: Rapid shifts in market structure
  3. Sector Concentration: Strategy may cluster in certain sectors
  4. Liquidity: Performance may degrade with less liquid stocks

Further Reading


License

MIT License - See LICENSE file for details.


Citation

If you use this code in research, please cite:

@software{topological_arbitrage_2025,
  title = {Topological Long-Short Arbitrage Strategy},
  author = {Your Name},
  year = {2025},
  url = {https://github.com/yourusername/topological-arbitrage}
}

Contributing

Issues, pull requests, and improvements welcome! Please ensure:

  1. Code follows existing style (numpy docstrings, type hints)
  2. All backtests reproduce historical results
  3. New features include performance validation

Contact & Support

  • Issues: GitHub issues for bugs and feature requests
  • Questions: See documentation in docs/ directory
  • Live Results: Check web dashboard for current performance

Last Updated: March 3, 2026 Status: Active live trading + daily backtesting validation

About

Adaptive market-neutral statistical arbitrage using graph diffusion, multi-layer regime detection, and Riemannian correlation geometry.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages