OPEN-SOURCE SCRIPT

SPY Quant ML + Session Filter Strategy [CocoChoco]

74
S&P 500 Quant: Machine Learning & Mean Reversion (Session-Filtered)

Overview
This is a professional-grade quantitative strategy designed specifically for the S&P 500. It combines classical statistical mean reversion (Z-Score) with a modern Machine Learning filter and rigorous institutional-grade risk management.

The strategy is optimized for traders who prioritize high win rates and capital preservation, specifically avoiding the "gap risk" associated with holding positions overnight.

Core Methodology
1. Statistical Entry (The Z-Score Engine)
The strategy identifies "oversold" conditions in a bullish context. It calculates the Z-Score of the price relative to its 20-period Mean (SMA). By default, it looks for a -1.2 Standard Deviation extension, signaling a high-probability "dip" ripe for a snap-back to the mean.

2. Trend & ML Filters
To avoid "catching a falling knife," the strategy uses two layers of confirmation:

Trend Filter: Only takes Long positions when the price is above the 200-period SMA, ensuring we only buy dips in a confirmed uptrend.

ML Correlation Filter: A Machine Learning-inspired module that analyzes the correlation between RSI and Volatility (ATR). It only permits entries when market internal dynamics suggest a reversal is technically "healthy."

3. Institutional Risk Management
This script is built for "safety-first" automation:

Hard Stop Loss: Fixed at 1.5% to protect against sudden market shocks.

Active Trailing: A dual-trigger trailing stop. It activates once the price touches the 20 SMA (The Mean) OR once a trade reaches a 0.50% profit threshold. This ensures near-winners are protected and large runners are captured.

Intraday Circuit Breaker: Includes a Max Daily Drawdown (2%) limit. If hit, the script automatically closes losing positions and halts trading for the day, while allowing winning positions to continue.

Key Features
Session-Specific: Tailored for the US Trading Session (UTC/NY times).

Zero Overnight Risk: Automatically flattens all positions before the market close (16:00 NY Time).

Holiday Intelligence: Hard-coded logic for US Market Holidays and Early Closes (2026–2028), ensuring the bot doesn't get stuck in illiquid holiday markets.

Hourly Entry Cap: Limits entries to one per hour to prevent over-concentration during a single price leg.

How to Use
Timeframe: I suggest you use it on the 5-minute or 1-hour timeframe for optimal results.

Instrument: Designed for the S&P 500, but highly effective on SPY, IVV, and ES (Futures).

Pyramiding: Designed to handle up to 3 concurrent positions, allowing the strategy to scale into a move as the Z-Score deepens.

Automation Ready
This script is fully compatible with webhook-based automation tools. All signals (Entry, SL, Trail, Market Close, and Daily Limit) are clearly labeled in the Alert comments for seamless execution. I haven't tasted it though. This is not financial advice. Please perform your own tests and manage your risk.

Disclaimer
Past performance does not guarantee future results. This script is a tool for quantitative analysis and should be used as part of a broader diversified trading plan.

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมใน ข้อกำหนดการใช้งาน