The script attempts to scrape live data to calculate Log Returns and Annualized Volatility. If live data cannot be accessed (due to website structure changes or connectivity issues), the system automatically generates synthetic "dummy" data to ensure the visualization pipeline remains functional.
# 🌍 African Stock Markets: Data Scraper & Volatility Analyzer
This project is a Python-based tool designed to extract, analyze, and visualize stock market data from three major African exchanges: **Johannesburg (JSE)**, **Nigeria (NGX)**, and **Nairobi (NSE)**.
The script attempts to scrape live data to calculate **Log Returns** and **Annualized Volatility**. If live data cannot be accessed (due to website structure changes or connectivity issues), the system automatically generates synthetic "dummy" data to ensure the visualization pipeline remains functional.
## 🚀 Features
* **Multi-Source Scraping:** Targeted functions for JSE (South Africa), NGX (Nigeria), and NSE (Kenya).
* **Financial Analysis:**
* Calculates **Logarithmic Returns** for accurate financial modeling.
* Computes **20-Day Rolling Volatility**, annualized (assuming 252 trading days).
* **Resilient Design:** Includes a `generate_dummy_data()` fallback mechanism. If scraping fails, the simulation takes over so the user still sees a result.
* **Visualization:** Generates dual-subplot charts comparing **Close Prices** vs. **Volatility** over time.
## 🛠️ Dependencies
To run this script, you need Python installed along with the following libraries:
* **Pandas:** Data manipulation and time-series analysis.
* **NumPy:** Mathematical calculations (logarithms, square roots).
* **Matplotlib:** Plotting and visualization.
* **BeautifulSoup4 (bs4):** HTML parsing for web scraping.
* **Requests:** Handling HTTP requests to websites.
* **yfinance:** (Imported for potential future API integration).
### Installation
Install the required packages using pip:
```bash
pip install pandas numpy matplotlib beautifulsoup4 requests yfinance
```
## đź’» Usage
1. **Save the script:** Save the code to a file named `african_markets.py`.
2. **Run the script:**
```bash
python african_markets.py
```
3. **View Results:**
* The script will attempt to connect to the JSE, NGX, and NSE websites.
* …