Developing a successful trading algorithm is about finding the right balance between capturing market gains and managing risks. In this capstone project for the Flatiron School Data Science Bootcamp, I created and refined a QuantConnect trading algorithm that combines a dynamic strategy for SPY with a risk-managed approach for TQQQ. The goal was to optimize returns while minimizing drawdowns, especially during volatile market conditions.
SPY Overview
SPY, or the SPDR S&P 500 ETF Trust, is an exchange-traded fund that tracks the S&P 500 Index, one of the most widely followed equity benchmarks in the U.S. The S&P 500 Index includes 500 of the largest publicly traded companies in the U.S., representing various sectors of the economy. SPY is a popular choice for investors looking to mirror the overall performance of the U.S. stock market.
- Assets Under Management (AUM): Approximately $400 billion.
- Average Volatility: Around 15% to 20% annually.
- Sector Exposure: SPY offers broad sector exposure, with significant allocations to sectors like healthcare, industrials, and financials.
TQQQ Overview
TQQQ, or the ProShares UltraPro QQQ, is a leveraged ETF that seeks to deliver three times the daily performance of the NASDAQ-100 Index. The NASDAQ-100 includes 100 of the largest non-financial companies listed on the NASDAQ stock exchange, with a strong emphasis on technology stocks. TQQQ amplifies the returns of the NASDAQ-100, making it highly volatile and more suitable for short-term trading rather than long-term holding.
- Assets Under Management (AUM): Approximately $23 billion.
- Average Volatility: Around 50% to 70% annually.
- Sector Exposure: TQQQ is heavily weighted towards technology, with much higher exposure to tech companies than SPY, making it more sensitive to the performance of the tech sector.
Similarities and Differences
Both SPY and TQQQ include major U.S. technology companies like Apple (AAPL), Microsoft (MSFT), Amazon (AMZN), and NVIDIA (NVDA) as their top holdings. These companies dominate both the S&P 500 and NASDAQ-100 indices due to their large market capitalizations. However, while SPY is more stable and diversified across sectors, TQQQ is focused on tech and is much more volatile, offering higher potential returns but with greater risk.
Project History
1. Initial Machine Learning Approach
I began this project by attempting to create a machine learning model that could predict significant bear markets. The idea was to train the model on quarterly price data and macroeconomic factors like GDP and unemployment rates.
However, this approach faced challenges due to insufficient data and the complexities of training a model within QuantConnect. These difficulties led to runtime errors and an inability to effectively train and test the model. As a result, I transitioned to a more straightforward rule-based algorithm.
2. Transition to Rule-Based Algorithms
SPY Dynamic Threshold Strategy
I developed a dynamic threshold algorithm for SPY that uses moving average crossovers to generate buy and sell signals. The thresholds for these signals are adjusted based on market volatility (ATR) and momentum (RSI). After significant drawdowns, the strategy becomes more conservative, requiring stronger signals to reenter the market.
- Buy Signal: Triggered when the short-term moving average crosses above the long-term moving average, adjusted by ATR and RSI.
- Sell Signal: Triggered when the short-term moving average crosses below the long-term moving average, also adjusted by ATR and RSI.
TQQQ Modified Buy-And-Hold Strategy
For TQQQ, the strategy started with simple percentage-based signals:
- Initial Buy Signal: Reentry into TQQQ was triggered if the price increased by a certain percentage (e.g., 5%) after a stop-loss event.
- Initial Sell Signal: A predefined drawdown threshold (e.g., 30%) from the peak price triggered an exit from TQQQ.
This evolved into a more cautious approach using moving average crossovers:
- Refined Buy Signal: TQQQ is bought when a faster moving average (100 days) crosses above a slower moving average (300 days), aligning with long-term uptrends.
- Same Sell Signal: The drawdown-based exit strategy was maintained, ensuring protection against large losses.
The TQQQ portion of the strategy is mainly intended as a buy-and-hold approach, with exits only during significant drawdowns to manage risk.
3. Parameter Optimization
I identified two key parameters for optimization:
- allocation_spy: The percentage of the portfolio allocated to SPY, with the rest going to TQQQ.
- tqqq_drawdown_threshold: The percentage drop from the peak price that triggers an exit from TQQQ.
4. Performance Results
The flexibility provided by this algorithm allows users to adjust key parameters to fit their specific risk tolerance and investment goals. These parameters significantly impact the balance between risk and return, as shown by the backtest results across different settings.
Aggressive Parameters: allocation_spy = 30%, tqqq_drawdown_threshold = 40%
- Alpha: 0.116
- Maximum Drawdown: 58.2%
- Market Entries: 11 times (4 in SPY, 7 in TQQQ)
- Total Fees: $3,467
- Total Return: 4,132%
- CAGR: 32%
- Net Gains: $4.23 million from a $100,000 initial investment.
Conservative Parameters: allocation_spy = 70%, tqqq_drawdown_threshold = 30%
- Alpha: 0.037
- Maximum Drawdown: 30.1%
- Market Entries: 18 times (4 in SPY, 14 in TQQQ)
- Total Fees: $1,998
- Total Return: 791%
- CAGR: 17.6%
- Net Gains: $891,200 from a $100,000 initial investment.
User Considerations
Risk Tolerance
- Aggressive Strategy: Users with a higher risk tolerance might prefer a lower allocation to SPY (e.g., 30%) and a higher drawdown threshold for TQQQ (e.g., 40%). This setup increases potential returns but comes with higher volatility and larger drawdowns.
- Conservative Strategy: Users seeking more stability might choose a higher allocation to SPY (e.g., 70%) and a lower drawdown threshold for TQQQ (e.g., 30%). This approach reduces drawdowns and volatility but also lowers overall returns.
Return Expectations
- The aggressive strategy offers a higher CAGR and total return, making it suitable for users aiming for substantial growth over a long-term horizon, despite the increased risk.
- The conservative strategy provides more modest returns but with greater protection against large losses, making it better suited for users prioritizing capital preservation.
Conclusion
This trading algorithm shows how different settings can greatly affect performance and risk. By combining the stability of SPY with the high-risk, high-reward potential of TQQQ, and by carefully tuning the parameters, this strategy maximizes returns while successfully managing risk.
This approach lets you adjust the strategy to fit your own risk tolerance and market outlook, making it a flexible tool for various trading goals.