An automated quantitative trading script executing a Cross-Sectional Momentum strategy on top US Tech stocks. Powered by the Alpaca Paper Trading API, it dynamically rebalances a portfolio monthly while employing strict macroeconomic crash filters.
This project is the culmination of a 5-phase research journey into algorithmic trading, moving from theoretical retail indicators to institutional-grade systematic execution.
- The Quant Journey
- Strategy Logic
- Tech Stack & Design Decisions
- Project Structure
- Getting Started
- Usage
- Security
- License
This project was developed through a rigorous 5-phase research pipeline, moving away from subjective chart reading toward purely data-driven mathematics:
- Awareness: Acknowledging the limitations and lag of standard retail indicators (RSI, MACD).
- Adaptive Volatility: Understanding market regimes and the dangers of applying static strategies to dynamic markets.
- Multi-Factor Models & Alpha Decay: Testing institutional equations (e.g., GTJA191 dataset) and discovering how public formulas lose their edge over time due to high-frequency arbitrage.
- Multi-Asset Architecture: Developing a custom AI-assisted Cross-Sectional algorithm. Backtesting on the S&P 500 yielded a 1.21 Sharpe Ratio and a +40% annual return (2024 data) with minimal drawdown.
- Paper Trading (Current): Connecting the mathematical brain to real-time market data via the Alpaca REST API to test execution, slippage, and latency with simulated funds.
The algorithm operates on a strict monthly rebalancing schedule (Stateless execution).
- Universe: 10 Mega-Cap Tech stocks (
AAPL, MSFT, NVDA, GOOGL, AMZN, META, TSLA, NFLX, AVGO, TSM). - Momentum Calculation: Measures the 60-day Rate of Change (RoC) for all assets.
- Selection: Ranks the universe and selects the Top 3 performing stocks.
- Crash Filter (Risk Management): If a stock is in the Top 3 but its 60-day momentum is negative, it is discarded. If all stocks are negative, the bot sells everything and holds 100% Cash to survive market crashes.
- Allocation: Equal weight (33.3% of total equity) distributed via fractional shares.
- Language: Python 3.13
- Market Data:
yfinance(Pandas-based historical data parsing) - Broker Integration:
requests(Alpaca REST API)
During development on Python 3.13, the official alpaca-trade-api caused fatal compilation errors due to its PyYAML dependency requiring heavy C++ Build Tools. To keep the script lightweight, portable, and completely dependency-free regarding OS-level compilers, the broker communication was rewritten using pure HTTP REST calls via the standard requests library.
Quant-Tech-Rotator/
├── monthly_executor.py # Main bot logic (data fetching, math, API execution)
├── requirements.txt # Python dependencies
├── .env.example # Template for Alpaca API keys
└── README.md
- Python 3.10+
- An Alpaca account (Paper Trading dashboard)
- Clone the repository:
git clone https://github.com/mirconegri/Quant-Tech-Rotator.git
cd Quant-Tech-Rotator- Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt- Configure your API keys. Never commit your real keys! Create a
.envfile or directly export them in your terminal:
$env:ALPACA_API_KEY="your_api_key_here"
$env:ALPACA_SECRET_KEY="your_secret_key_here"(Alternatively, safely edit monthly_executor.py locally to include them, ensuring the file is git-ignored).
Run the script on the first trading day of the month, ideally 1 hour before market close:
python monthly_executor.pyExpected Output:
Starting market scan (Direct REST Method)...
[*********************100%***********************] 10 of 10 completed
Top 3 selected for this month: ['MSFT', 'TSM', 'AMZN']
Closing existing positions...
Total equity: $100000.00. Buying $33333.33 per position.
✅ Order Sent: BOUGHT MSFT
✅ Order Sent: BOUGHT TSM
✅ Order Sent: BOUGHT AMZN
Monthly rebalancing completed successfully!
- Token handling: Never commit your Alpaca
API_KEYorSECRET_KEYto GitHub. The repository uses.gitignoreto prevent uploading environment files. - Execution: The script is entirely Stateless. It wipes current positions and repurchases the new allocation. Do not run this script multiple times a day, or you will lose capital to broker spread/slippage. Run it strictly once a month.
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature) - Commit your changes with a clear message
- Open a Pull Request
Mirco Negri — Computer Science @ UniTrento
This project is licensed under the MIT License — see the LICENSE file for details.