A sophisticated quantitative trading strategy implemented in Python that generates alpha by going long on stocks expected to outperform and short on those expected to underperform.
(From Original Requirements)
Create a quantitative trading strategy that maintains Dollar-Neutrality and generates Alpha using a defined universe of 10 large-cap US stocks.
- Source: Yahoo Finance (
yfinance). - Universe: AAPL, MSFT, AMZN, GOOGL, META, TSLA, NVDA, JPM, JNJ, WMT.
- History: 5 Years of Daily OHLCV + Fundamental Data.
The strategy combines three distinct alpha signals:
- Momentum: 20-day returns (Ranked High to Low).
- Mean Reversion: Deviation from 50-day MA (Oversold = Buy).
- Volatility: 30-day historical volatility (Lower volatility = Preferred).
Optimization: We used Walk-Forward Analysis to determine the optimal weights for these signals:
- Momentum: 61%
- Mean Reversion: 23%
- Volatility: 16%
- Long: Top 3 Ranked Stocks.
- Short: Bottom 3 Ranked Stocks.
- Rebalancing: Monthly.
- Regime Filter: Strategies switch to Long-Only during strong Bull Markets (SPY > MA200) to mitigate negative beta exposure.
- Stop-Loss: -10% per individual position.
- Dollar-Neutral: Equal exposure to Long and Short sides (default mode).
-
Clone the repository:
git clone https://github.com/digantk31/Alpha-Long-Short.git cd Alpha-Long-Short -
Set up Virtual Environment: It is recommended to use a virtual environment to manage dependencies.
# Create virtual environment python -m venv venv # Activate virtual environment (Windows) venv\Scripts\activate # Activate virtual environment (Mac/Linux) # source venv/bin/activate
(You should see
(venv)appear in your command prompt)To Deactivate: Simply run
deactivatewhen you are done. -
Install Dependencies:
pip install -r requirements.txt
Executes the full pipeline: Data Collection -> Signal Generation -> Backtesting -> Optimization Analysis -> Performance Reporting.
python main.pyNote: This includes the Bonus Task (Walk-Forward Optimization) which creates the "winning weights" analysis.
If you only want to run the backtest without the time-consuming optimization step:
python main.py --no-optimizeThe strategy generates detailed visualizations in the output/ directory:
Momentum = (Price_today - Price_20_days_ago) / Price_20_days_ago
P_t - P_{t-20}
Mom_t = βββββββββββββββββ
P_{t-20}
Higher momentum β Higher rank β BUY signal
MA_50 = (1/50) Γ Ξ£ P_{t-i} for i = 0 to 49
Deviation = (Price - MA_50) / MA_50
Mean_Reversion = -Deviation
Price below MA β Positive signal β BUY (expect bounce back)
Daily_Return = (P_t - P_{t-1}) / P_{t-1}
Ο_30 = StdDev(Daily_Returns over 30 days)
Annualized_Volatility = Ο_30 Γ β252
Volatility_Signal = -Annualized_Volatility
Lower volatility β Higher score β Preferred
X - ΞΌ
Z = βββββββββββ
Ο
Where: ΞΌ = mean across all stocks, Ο = standard deviation
Alpha = (0.61 Γ Momentum_Z) + (0.23 Γ MeanRev_Z) + (0.16 Γ Volatility_Z)
R_p - R_f
Sharpe = βββββββββββββββββ
Ο_p
Where:
R_p = Annualized portfolio return
R_f = Risk-free rate (2%)
Ο_p = Annualized volatility
Drawdown_t = (Peak_t - Value_t) / Peak_t
Max_Drawdown = min(Drawdown_t) over all t
Ξ± = R_p - [R_f + Ξ² Γ (R_m - R_f)]
Where:
R_m = Market (benchmark) return
Ξ² = Portfolio beta
Cov(R_p, R_m)
Ξ² = βββββββββββββββββββββ
Var(R_m)
Long_Weight = 0.50 / n_long = 0.50 / 3 β 16.67% per stock
Short_Weight = -0.50 / n_short = -0.50 / 3 β -16.67% per stock
Long_Weight = 1.00 / n_long = 1.00 / 3 β 33.33% per stock
Short_Weight = 0%
Cost = Trade_Value Γ 0.0010 (10 basis points)
main.py: Entry point. Orchestrates the entire workflow.quant_strategy/:data_collector.py: Fetches historical data.alpha_signals.py: Logic for Momentum, Mean Reversion, and Volatility.portfolio.py: Ranking and weighting logic (including Regime Filter).backtester.py: Event-driven simulation engine.risk_manager.py: Stop-loss and position sizing.performance.py: Metrics and plotting.optimizer.py: Walk-Forward Analysis logic.



