A Python-based backtesting tool that implements a 20/50-day Simple Moving Average (SMA) crossover strategy.
It fetches historical stock data via Yahoo Finance, executes trades with next-day discipline to avoid look-ahead bias, logs results into SQLite, and generates equity curve plots comparing the strategy against a Buy & Hold benchmark.
- π Historical stock data retrieval with yFinance
- π Implements short-term vs long-term SMA crossover strategy
- ποΈ Logs trades and returns into SQLite database for reproducibility
- π Plots equity curve vs Buy & Hold benchmark using matplotlib
- β Avoids look-ahead bias by executing trades next day
- Python 3.11
- pandas, NumPy
- yFinance
- matplotlib
- SQLite
Clone the repository and set up your environment:
git clone https://github.com/USERNAME/trading-simulator.git
cd trading-simulator
# Create and activate virtual environment
python -m venv .venv
.venv\Scripts\activate # On Windows
# Install dependencies
pip install -r requirements.txt
Run the backtest:
python backtest.py
---
## π Example Output
Running the backtest on **Nvidia Corporation (NVDA)** produces:
- `equity.png` β Equity curve of strategy vs Buy & Hold
- `trades.db` β SQLite database log of trades
Example equity curve output:
(See equity.png in the folder)
---