# Markets & Macro News Dashboard
## mawdz09.uk — Free tier, no API keys required

---

### Files

| File                    | Purpose                                               |
|-------------------------|-------------------------------------------------------|
| `001_create_market_tables.sql` | Run once to create + seed the MariaDB table    |
| `fetch_markets.php`     | Cron script — fetches Yahoo Finance, writes to DB     |
| `api_markets.php`       | JSON API endpoint — serves data to dashboard          |
| `api_news.php`          | RSS aggregator — fetches + caches macro news as JSON  |
| `markets.html`          | Dashboard widget (drop into your pages/ structure)    |

---

### 1. Database setup

```bash
mysql -u root -p mawdz < 001_create_market_tables.sql
```

Update `DB_NAME`, `DB_USER`, `DB_PASS` in both `fetch_markets.php` and `api_markets.php`.

---

### 2. Deploy files

```bash
# Suggested path
/var/www/html/markets/
├── fetch_markets.php
├── api_markets.php
├── api_news.php
└── markets.html
```

---

### 3. Set up the cron job

```bash
crontab -e
```

Add these lines:

```
# Markets: every 15 min, Mon-Fri 7am-10pm (covers all timezones for open markets)
*/15 7-22 * * 1-5 php /var/www/html/markets/fetch_markets.php >> /var/log/markets_fetch.log 2>&1

# Markets: hourly overnight and weekends (for Asian/futures markets)
0 * * * 6,0 php /var/www/html/markets/fetch_markets.php >> /var/log/markets_fetch.log 2>&1
0 22-23,0-6 * * 1-5 php /var/www/html/markets/fetch_markets.php >> /var/log/markets_fetch.log 2>&1
```

---

### 4. Test the fetcher manually

```bash
php /var/www/html/markets/fetch_markets.php --verbose
```

Expected output:
```
[2026-03-21 14:30:01] ^FTSE       8742.500  +0.31%  [REGULAR]
[2026-03-21 14:30:01] ^GSPC       5612.000  -0.12%  [REGULAR]
...
[2026-03-21 14:30:02] Done. Updated 15 of 15 symbols.
```

---

### 5. Test the API

```bash
curl http://localhost/markets/api_markets.php | python3 -m json.tool
```

---

### Symbols tracked

| Symbol      | Name             | Category  |
|-------------|------------------|-----------|
| ^FTSE       | FTSE 100         | index     |
| ^GSPC       | S&P 500          | index     |
| ^DJI        | Dow Jones        | index     |
| ^IXIC       | Nasdaq           | index     |
| ^STOXX50E   | Euro Stoxx 50    | index     |
| ^N225       | Nikkei 225       | index     |
| ^TNX        | US 10yr Yield    | bond      |
| ^TYX        | US 30yr Yield    | bond      |
| ^IRX        | US 13wk T-Bill   | bond      |
| IGLT.L      | UK Gilts ETF     | bond_etf  |
| SLXX.L      | GBP Corp Bond    | bond_etf  |
| LQD         | USD Corp Bond    | bond_etf  |
| EMB         | EM Bonds         | bond_etf  |
| BZ=F        | Brent Crude      | commodity |
| GC=F        | Gold             | commodity |

To add more: `INSERT INTO market_data (symbol, name, category) VALUES ('^HSI', 'Hang Seng', 'index');`
The next cron run picks it up automatically.

---

### News sources (no API key)

- BBC Business RSS
- Reuters Business RSS
- Guardian Business RSS
- FT Markets RSS
- Bank of England news RSS
- ONS Statistical releases RSS
- Investing.com RSS

News is cached in `/tmp/mawdz_news_cache.json` for 20 minutes.
To change sources, edit the `$feeds` array in `api_news.php`.

---

### Adding to your main dashboard

In any PHP page on mawdz09.uk, embed as an iframe or include directly:

```html
<!-- As iframe -->
<iframe src="/markets/markets.html" style="width:100%;border:none;height:600px"></iframe>

<!-- Or just link to it as a standalone section -->
<a href="/markets/markets.html">Markets & News</a>
```

Or drop the `<style>` + HTML + `<script>` blocks from `markets.html` directly
into your existing page template if you want it inline with your nav.
