📈 Moving-Average-Crossover-Signals
Python
This Python script generates Buy and Sell signals for stocks based on the classic 50/200 Simple Moving Average (SMA) crossover strategy.
The specific stocks analysed by default are the Magnificent 7 (Mag 7): GOOGL
, AMZN
, AAPL
, META
, MSFT
, NVDA
, and TSLA
.
✏️ Want to Customize It? Just replace the stock tickers in the stocks_to_check
list in the script. Everything else works out of the box.
The core logic involves:
- Fetching dynamically the last year's worth of daily adjusted close price data from Yahoo Finance (
yfinance
). - Calculating the 50-day and 200-day Simple Moving Averages (SMA).
- Generating 'Buy' signals when the 50-day SMA crosses above the 200-day SMA.
- Generating 'Sell' signals when the 50-day SMA crosses below the 200-day SMA.
The script then visualises the results, plotting the closing price, the SMAs, and clearly marking the generated Buy/Sell signals on the chart for each stock using matplotlib
.
This project serves as a practical demonstration of implementing a basic trading signal strategy in Python. It uses historical daily data fetched dinamically from Yahoo Finance (yfinance
). As with any free data source, occasional inaccuracies might be present.
- Automatically downloads 1 year of historical price data for each stock.
- Calculates 50-day and 200-day simple moving averages.
- Generates Buy and Sell signals based on crossover strategy:
- Buy: when 50-day MA crosses above 200-day MA
- Sell: when 50-day MA crosses below 200-day MA
- Visualizes each stock's close price, moving averages, and buy/sell signals.
- Plots are generated for each stock individually.
- Access to Google Colab OR a local Python 3.x environment
- Required Python libraries:
yfinance
pandas
matplotlib
- Open the Notebook:
- Run the Cells:
- Once the notebook is open in Colab, run the code cells sequentially from top to bottom (you can use
Shift+Enter
or click the 'Run' button for each cell).
- Once the notebook is open in Colab, run the code cells sequentially from top to bottom (you can use
- View Output:
- As the code runs, it will process each stock ticker. For each one, it will display a plot showing the price history, moving averages, and the calculated Buy (^) and Sell (v) signals.
🧑💻 Created by tunjis
- 🌍 Based in London
- 🖥️ See my portfolio at Data’s the new oil. I’m the refinery.
- 📫 Contact me via my LinkedIn profile
- 🧠 Learning Data Science
- 🤝 Open to collaborating on interesting projects
- ⚡ AI enthusiast
Python
Microsoft Excel
MySQL
Tableau
Power BI
Microsoft Azure
Google Cloud
Google Colab