MA Strategy Backtesting

Python / Streamlit / Excel

Project Overview

The Moving Average (MA) Strategy Backtester is a quantitative trading tool designed to evaluate the efficacy of trend-following strategies. By simulating trades based on the "Crossover" logic of Short-Term and Long-Term moving averages (e.g., SMA-50 and SMA-200), this application allows users to verify whether a simple technical strategy would have been profitable on a specific asset over a historical period. It automates the process of entering entries, exits, and calculating net P&L.

Problem Statement

Validating trading strategies on historical data is crucial before deploying capital. Manual backtesting is time-consuming and prone to errors.

Strategy / Model Definition

The strategy is built on the interaction between two indicators:

1. Fast MA (Short Window): Reacts quickly to price changes (e.g., 20 days).
2. Slow MA (Long Window): Represents the broader trend (e.g., 50 days).

Buy Signal (Golden Cross): When the Fast MA crosses above the Slow MA, indicating short-term momentum is outpacing the long-term trend.
Sell Signal (Death Cross): When the Fast MA crosses below the Slow MA, indicating momentum is breaking down.

Methodology

Process:

Python Streamlit Backtrader
  • Select stock ticker and date range.
  • Define Moving Average windows (e.g., 50-day, 200-day).
  • Simulate trades and calculate P&L.

Metrics & Evaluation Criteria

Evaluates Total Return, Max Drawdown, and Win Rate.

Results & Observations

Backtesting on trending stocks (like Tesla during 2020-2021) yielded impressive results, capturing the bulk of the move while avoiding the noise of minor corrections. However, on range-bound or "choppy" assets, the strategy suffered from false signals. The lag inherent in Moving Averages meant that by the time a cross occurred, the price move might have already exhausted itself, leading to buying tops and selling bottoms in sideways markets.

Interpretation & Limitations

Past performance is not indicative of future results. Slippage and commissions are key factors.

Project Artifacts

MA Strategy Backtesting

Key Takeaways

The primary lesson is that no single strategy works in all market conditions. Trend-following strategies like MA Crossover shine in bull/bear markets but bleed capital in accumulation/distribution phases (sideways movement). A robust trading system requires a "regime filter" to determine if the market is trending before applying a trend-following strategy.

View Live App GitHub Source Code Download Excel Model