Skip to content

Latest commit

 

History

History
89 lines (55 loc) · 3.45 KB

File metadata and controls

89 lines (55 loc) · 3.45 KB

📊 Algorithmic Trading Backtest — Golden Cross Strategy

A Python backtesting framework that tests the Golden Cross moving average strategy against a buy-and-hold benchmark using 5 years of real historical data.

About Me

I am a Mechatronics Engineering graduate and a postgraduate in Finance from Henley Business School, looking to enter the field of finance in data science, machine learning, or quantitative finance.


What Does This Project Do?

Rather than making investment decisions based on intuition, this project uses past data to test whether a trading strategy would have actually made money on real historical prices — before risking any real capital. It compares the strategy's performance against simply buying and holding the stock for the same period.


The Golden Cross Strategy

A trend-following strategy based on two moving averages:

  • Buy signal (Golden Cross): When the 50-day MA crosses above the 200-day MA — bullish signal, enter the market
  • Sell signal (Death Cross): When the 50-day MA crosses below the 200-day MA — bearish signal, exit the market

The idea is to ride uptrends and avoid downtrends by following momentum rather than predicting price direction.


Results — Apple (AAPL), 5 Years

Metric Value
Initial Capital $10,000
Buy & Hold Final Value $15,795
Strategy Final Value $8,299
Strategy vs Buy & Hold -47.46%

What this tells us: The Golden Cross strategy significantly underperformed buy and hold for Apple. This is because Apple has such a strong long-term uptrend that being out of the market during Death Cross periods means missing the biggest gains. The strategy is too slow to react to Apple's momentum-driven price action.

This is an honest result — and a valuable one. It shows that backtesting is essential precisely because strategies that sound logical in theory can lose money in practice.


Key Concepts

Backtesting — testing a trading strategy on historical data to evaluate performance before deploying real capital. The litmus test for any algorithmic strategy.

Lookahead Bias — a common backtesting mistake where future information is accidentally used to make past trading decisions, producing unrealistically good results. Prevented here by using Signal.shift(1) — trading on yesterday's signal, not today's.

Benchmark — the buy-and-hold return used as a comparison. A strategy that doesn't beat the benchmark has no practical value.

Missing Costs — this backtest does not account for transaction costs, slippage, or taxes. Real results would be worse.


How To Run

git clone https://github.com/sauravsen3/algorithmic-backtest.git
cd algorithmic-backtest
pip install -r requirements.txt
python backtest.py

Project Structure

algorithmic-backtest/
│
├── backtest.py           # Strategy logic, signals, backtest, chart
├── requirements.txt      # Python dependencies
└── backtest_results.png  # Generated on first run

Tech Stack

  • yfinance — 5 years of historical OHLCV data
  • pandas / numpy — signal generation and return calculation
  • matplotlib — strategy vs benchmark performance chart

Part of a series of quantitative finance projects. Previous: Portfolio Optimisation. Next: Pairs Trading Strategy.