Enhanced Multi-Signal Mean Reversion on SPY — Alpaca Paper Account · Extended Hours
A mean reversion equity strategy that buys SPY when multiple indicators signal an oversold extreme and exits when price reverts toward its mean. The script runs a continuous polling loop every 60 seconds during the extended session (4:00 AM – 8:00 PM ET) and uses the Alpaca market calendar to automatically skip weekends and holidays.
The strategy requires a core RSI oversold signal plus at least one confirming signal (MACD momentum turning, Bollinger Band touch, or adequate volume) before entering — reducing false entries compared to a pure RSI strategy. Maximum shares held at any time: 10.
| Condition | Required? | Description |
|---|---|---|
RSI(14) < 30 | Required | Core oversold signal |
| MACD histogram rising | ≥ 1 of these | Momentum turning up |
| Price ≤ lower Bollinger Band | ≥ 1 of these | Price stretched below mean |
| Volume ≥ 0.8× 20-bar avg | ≥ 1 of these | Reasonable participation |
| Condition | Description |
|---|---|
RSI(14) > 70 | Overbought — primary exit |
| Price ≥ upper Bollinger Band | Mean-reversion profit target |
| MACD histogram falling + RSI > 55 | Momentum fading — early exit |
| Price drops 0.3% below entry | Stop-loss — capital protection |
| Metric | Original (RSI only) | Enhanced (Multi-signal) |
|---|---|---|
| Total return | +1.07% | +3.67% |
| Sharpe ratio | 1.26 | 6.31 |
| Max drawdown | −2.38% | −0.88% |
| Win rate | 75.0% | 69.0% |
| Avg loser | −$675 | −$245 |
| Profit factor | 1.16 | 1.83 |
The enhanced multi-signal version improved Sharpe ratio from 1.26 to 6.31 and cut average losses by 64% (−$675 → −$245) while reducing max drawdown by more than half.
| Parameter | Default | Description |
|---|---|---|
SYMBOL | SPY | Ticker to trade |
RSI_PERIOD | 14 | RSI look-back period |
RSI_OVERSOLD | 30 | Buy threshold |
RSI_OVERBOUGHT | 70 | Sell threshold |
MACD_FAST/SLOW/SIGNAL | 12/26/9 | MACD parameters |
BB_PERIOD / BB_STD | 20 / 2.0 | Bollinger Band settings |
VOLUME_MULTIPLIER | 0.8 | Minimum volume vs 20-bar avg |
STOP_LOSS_PCT | 0.003 | Stop-loss threshold (0.3%) |
POSITION_SIZE_PCT | 0.95 | Fraction of cash to deploy |
MAX_SHARES | 10 | Maximum total shares held at once |
INTERVAL_SECONDS | 60 | Polling frequency during session |
| Event | Time (ET) | Days |
|---|---|---|
| Session start | 4:00 AM | Mon–Fri |
| Session end | 8:00 PM | Mon–Fri |
| Poll interval | Every 60 s | During session |
Extended hours trading. The Alpaca market calendar is used to skip weekends and holidays automatically. When the market is closed the script sleeps in 5-minute chunks.
All orders use extended_hours=True and time_in_force=DAY. Unfilled limit orders are cancelled at the start of the next pass.
Period: 14. Computed using exponential weighted moving averages of gains and losses.
Fast: 12, Slow: 26, Signal: 9. The histogram (macd_line − signal_line) is compared bar-to-bar to detect momentum turning points.
Period: 20, Std dev: 2.0. Entry when price ≤ lower band; exit when price ≥ upper band.
20-bar simple moving average of volume. Entry requires current volume ≥ 0.8× this average.
The strategy trades during pre-market and after-hours sessions where spreads are wider and liquidity is lower than during regular market hours. This can lead to worse fills than indicated by backtest results.
Each entry deploys up to 95% of available cash, capped at MAX_SHARES=10. Subsequent entries add to the position up to the cap using a weighted average entry price.
.envPlace in the same directory as the script. Credentials are loaded from Supabase at startup (account Aiv001).
All output is logged to both stdout and logs/mean_reversion.log. Trades are logged with timestamp, entry/exit price, signals fired, and P&L.
This strategy runs on an Alpaca paper trading account. Set USE_PAPER = False at your own risk. Past performance does not guarantee future results.
This strategy trades SPY multiple times a day and will trigger the Pattern Day Trader (PDT) flag, which requires you to maintain a balance of $25,000 in your account.
A pattern day trader is defined by FINRA as any trader who executes four or more day trades within five business days in a margin account, where those trades represent more than 6% of their total trading activity during that period. Once flagged, your account must maintain a minimum equity of $25,000 on any day you trade; failure to do so will restrict the account to closing transactions only.
Ensure your Alpaca paper account reflects at least $25,000 equity before running this strategy to avoid PDT restrictions during testing.