This Python script is developed to backtest the Golden Cross trading strategy specifically on the S&P 500 index. The Golden Cross strategy is a popular trading signal that occurs when a short-term moving average crosses above a long-term moving average, indicating a potential bullish turn in the market.
- Data Fetching: Automatically retrieves historical data of the S&P 500.
- Moving Averages Calculation: Calculates the short-term and long-term moving averages.
- Signal Identification: Identifies Golden Cross and Death Cross signals.
- Performance Metrics: Evaluates the performance of the strategy over time.
- Python 3.x
- Pandas: For data manipulation and analysis.
- Matplotlib: For plotting graphs and visualizing data.
- Clone the repository:
- Install the required packages:
pip install pandas matplotlib
Run the script with:
python trader.py
You can configure the following parameters in the script:
- Short-term moving average period (default: 50 days)
- Long-term moving average period (default: 200 days)
The script outputs:
- A graph showing the S&P 500 price along with the short-term and long-term moving averages.
- Trading signals (Golden Cross and Death Cross points).
- The overall profit or loss from following this strategy.
This script is for educational purposes only and is not intended as financial advice. Past performance is not indicative of future results.
Contributions are welcome. Please feel free to submit a pull request or open an issue.