Mean-reversion strategy using Exponential Moving Average with adaptive variance & z-score signals. Trades every 10 seconds during market hours on NVDA.
This Python-based trading strategy uses an Exponential Moving Average (EMA) combined with an adaptive variance estimator to generate z-score signals. When the z-score exceeds the threshold, the strategy enters a mean-reversion trade — buying when price dips significantly below the EMA and selling when price spikes above it.
The strategy trades NVDA every 10 seconds during US market hours (9:30 AM – 4:00 PM ET), Monday through Friday, excluding market holidays. It uses the Alpaca IEX data feed for real-time quotes and paper trading for order execution.
Backtest results from backtest_ema_nvda.py. Run the script to generate or update results.
| Date | Open | Close | High | Low | Trades | Position | Total P&L |
|---|
| Timestamp | Side | Qty | Price | Position After | Realized P&L |
|---|
The Exponential Moving Average uses λ=0.75 decay: EMA = λ × prev_EMA + (1-λ) × price. This gives recent prices 25% weight each tick, creating a responsive but smooth signal.
Variance is estimated using EWMA: σ² = λ × prev_σ² + (1-λ) × (price - EMA)², clamped between floor (0.05) and cap (100). This adapts to changing volatility regimes.
The z-score is computed as z = (price - EMA) / √σ². When |z| exceeds 2.0, a trade signal fires — buy when z < -2.0 (price below EMA), sell when z > 2.0 (price above EMA).
Position limits enforce max 400 shares long or short. Trade size is 25 shares per signal. The strategy respects market hours and holidays automatically.
The live trading script. Run with python ema_nvda_trader.py or python ema_nvda_trader.py --dry-run for simulation.
Loading...
Run with python backtest_ema_nvda.py to generate ema_nvda_backtest_results.json.
Loading...
Easily modify the strategy parameters at the top of ema_nvda_trader.py:
Update SYMBOL = "NVDA" to any Alpaca-supported ticker to trade a different stock.
Adjust LAMBDA_F (0→1): lower values make the EMA more responsive to recent prices; higher values smooth more.
Raise Z_SCORE_THRESH for fewer, higher-conviction trades. Lower it for more frequent signals.
Change TRADE_QTY, LIMIT_LONG, and LIMIT_SHORT to control trade size and max exposure.
Deploy the NVDA EMA z-score strategy on your MachineTrader™ instance with paper or live trading.