DEV Community

Madhushan
Madhushan

Posted on

A short Python script that alerts you when a bank's FX spread widens

Banks widen their FX spreads when markets get jumpy, and they don't announce it. If you move money regularly, it's worth knowing when your bank's margin doubles.

Frankfurter can't help here: it has official rates, not bank boards. This script uses bank boards instead.

The script

import json, os, pathlib, requests

KEY = os.environ["ART_KEY"]
COUNTRY, CCY, JUMP = "my", "USD", 1.5   # alert if spread grows 1.5x
STATE = pathlib.Path("spreads.json")

r = requests.get(
    f"https://allratestoday.com/api/v1/bank-rates/{COUNTRY}",
    headers={"Authorization": f"Bearer {KEY}"}, timeout=15,
)
r.raise_for_status()

prev = json.loads(STATE.read_text()) if STATE.exists() else {}
now = {}

for bank in r.json()["banks"]:
    row = next((x for x in bank["rates"] if x["ccy"] == CCY), None)
    if not row or row.get("spread_pct") is None:
        continue
    now[bank["bank"]] = row["spread_pct"]
    old = prev.get(bank["bank"])
    if old and row["spread_pct"] > old * JUMP:
        print(f"ALERT {bank['name']}: {CCY} spread {old:.2f}% -> {row['spread_pct']:.2f}%")

STATE.write_text(json.dumps(now))
Enter fullscreen mode Exit fullscreen mode

Run it hourly from cron:

5 * * * * cd /opt/spreads && ART_KEY=... python3 check.py >> alerts.log
Enter fullscreen mode Exit fullscreen mode

Notes

  • spread_pct is (sell − buy) ÷ mid, already computed per row.
  • Each row also has vs_official_pct, how far the bank's mid sits from the central bank rate. That's a second useful alarm.
  • Bank boards are read hourly, so polling more often doesn't give you anything new.
  • Change COUNTRY to any of the 124 covered countries, such as in, ae, ph or ke.

I work on AllRatesToday.

Top comments (0)