Skip to content

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📈 Quant-Tech-Rotator

Python Alpaca API Status License

🌐 Visit the project website

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.

Table of Contents

The Quant Journey

This project was developed through a rigorous 5-phase research pipeline, moving away from subjective chart reading toward purely data-driven mathematics:

  1. Awareness: Acknowledging the limitations and lag of standard retail indicators (RSI, MACD).
  2. Adaptive Volatility: Understanding market regimes and the dangers of applying static strategies to dynamic markets.
  3. 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.
  4. 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.
  5. 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.

Strategy Logic

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.

Tech Stack & Design Decisions

  • Language: Python 3.13
  • Market Data: yfinance (Pandas-based historical data parsing)
  • Broker Integration: requests (Alpaca REST API)

Why pure requests instead of the official Alpaca SDK?

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.

Project Structure

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

Getting Started

Prerequisites

  • Python 3.10+
  • An Alpaca account (Paper Trading dashboard)

Installation

  1. Clone the repository:
git clone https://github.com/mirconegri/Quant-Tech-Rotator.git
cd Quant-Tech-Rotator
  1. Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install -r requirements.txt
  1. Configure your API keys. Never commit your real keys! Create a .env file 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).

Usage

Run the script on the first trading day of the month, ideally 1 hour before market close:

python monthly_executor.py

Expected 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!

Security

  • Token handling: Never commit your Alpaca API_KEY or SECRET_KEY to GitHub. The repository uses .gitignore to 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.

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Commit your changes with a clear message
  4. Open a Pull Request

Author

Mirco Negri — Computer Science @ UniTrento

License

This project is licensed under the MIT License — see the LICENSE file for details.

About

An automated algorithmic trading script executing a Cross-Sectional Momentum strategy on top US Tech stocks via the Alpaca API

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages