Metadata-Version: 2.4
Name: exchange-radar
Version: 1.9.0
Summary: Live trading information from various cryptocurrency exchanges.
License: GNU GPLv3
License-File: LICENSE
Keywords: cryptocurrency,bitcoin,btc,ethereum,eth,chainlink,link,trading,crypto exchanges,exchange,crypto,binance,coinbase,kraken,kucoin,okx,bybit,bitstamp,mexc,htx
Author: Paulo Antunes
Author-email: pjmlantunes@gmail.com
Requires-Python: >=3.12,<4.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: License :: Other/Proprietary License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: click (>=8.1.5,<9.0.0)
Requires-Dist: copra (>=1.2.9,<2.0.0)
Requires-Dist: environs (>=11.0.0,<12.0.0)
Requires-Dist: huey (>=2.5.0,<3.0.0)
Requires-Dist: jinja2 (>=3.1.2,<4.0.0)
Requires-Dist: pika (>=1.3.2,<2.0.0)
Requires-Dist: pybit (>=5.6.2,<6.0.0)
Requires-Dist: pydantic (>=2.0,<3.0)
Requires-Dist: pymexc (>=1.2.0,<2.0.0)
Requires-Dist: python-binance (>=1.0.17,<2.0.0)
Requires-Dist: python-kucoin (>=2.1.3,<3.0.0)
Requires-Dist: python-okx (>=0.2.6,<0.3.0)
Requires-Dist: python-telegram-bot (>=21.1.1,<22.0.0)
Requires-Dist: pywebpush (>=2.0.3,<3)
Requires-Dist: redis-om (>=0.3.5,<0.4)
Requires-Dist: requests (>=2.31.0,<3.0.0)
Requires-Dist: starlette (>=1.3.1,<2)
Requires-Dist: twisted (>=26.10,<27)
Requires-Dist: types-requests (==2.31.0.0)
Requires-Dist: urllib3 (>=2.8,<3)
Requires-Dist: uvicorn[standard] (>=0.49,<1)
Project-URL: Homepage, https://radar.pauloantunes.com
Project-URL: Repository, https://github.com/pantunes/exchange-radar
Description-Content-Type: text/markdown

# Exchange Radar

[![Version][version-svg]][package-url]
[![License][license-image]][license-url]
[![Build Status][ci-svg]][ci-url]

Real-time cryptocurrency trades, trade history and price alerts from Binance,
Coinbase, Kraken, KuCoin, OKX, Bybit, Bitstamp, MEXC and HTX.

## Local setup

Requires Docker with Compose; Python 3.12 is used inside the application images.

1. Check the service settings in `.envs/.local/`.
2. Copy `compose/rabbitmq/rabbitmq-definitions.example.json` to
   `compose/rabbitmq/rabbitmq-definitions.json`. Replace both password-hash
   placeholders using `rabbitmqctl hash_password`; the `trades` password must
   match the stack's `.rabbitmq` settings. Keep the populated file private.
3. Start the stack:

   ```bash
   docker compose -f local.yml up --build -d
   ```

For one exchange, use `docker compose -f local.yml up --build consumer producer-okx`.
Open [BTC trades](http://127.0.0.1:9000/BTC); append `/whales`, `/dolphins` or
`/octopuses` for filtered feeds. Replace BTC with another configured coin.
The API schema is at `/api/schema.yaml`.

## Production deployment

Before deploying:

- Configure the private `.envs/.production/` service files and RabbitMQ definitions.
  Rotate any previously exposed broker credentials; see [SECURITY.md](SECURITY.md).
- Put a strong `INTERNAL_API_TOKEN` in the root `.env`. Web and consumer must share
  it and be updated together. Generate it privately with
  `python3 -c "import secrets; print(secrets.token_urlsafe(32))"`.
- Set `ALLOWED_HOSTS` to your public hostname plus `web` and `exchange-radar-web`.
  Set `FORWARDED_ALLOW_IPS` to the actual proxy source address seen by the web
  container. Correct forwarding is required for HTTPS push origin checks.
- Serve through an HTTPS reverse proxy. Published ports bind to `127.0.0.1`.
  For a proxy on another host, set `WEB_BIND_ADDRESS` to a private interface and
  restrict access to that proxy. Keep Redis and RabbitMQ private.

Keep these settings in root `.env`, preserve existing push keys, and restrict
secret-file permissions (`chmod 600 .env`). Never commit the populated files.

On an existing server, deploy with:

```bash
make deploy
```

This pulls with fast-forward only, validates configuration, builds images, waits
for a Redis snapshot, stops consumers and updates the stack with two replicas.
Failures stop later steps. It preserves volumes and performs no Docker pruning.
The Redis snapshot is not an off-host backup or a RabbitMQ backup.

For a first install, use
`docker compose -f production.yml up -d --build --scale consumer=2`.
After deployment, check live trades, saved history, alerts and notification
**Send test**.

### Recovery and data

Services use `restart: unless-stopped`. To restart an existing stopped stack while
keeping its containers and replica count:

```bash
docker compose -f production.yml start
```

Preserve the Compose project name, Redis/RabbitMQ volumes and RabbitMQ hostname
`exchange-radar-rabbitmq`. `down -v` deletes persistent data. Back up both services
before maintenance. Recreating producers loses their in-memory backlog; prefer
updating only affected services during routine maintenance.

RabbitMQ is still pinned to unsupported **3.12.13**. Its staged upgrade is separate
from application deployment; see [SECURITY.md](SECURITY.md#rabbitmq).

## Alerts and browser notifications

The scheduler checks for price changes of at least **1% over one minute** or
**2% over ten minutes**. The alert log shows the actual comparison interval.
Restarting the scheduler or missing coin statistics resets that coin's baseline.

Use **Notifications** in the top bar to choose BTC, ETH, LINK or PEPE, alert rules,
and a 0/5/15-minute minimum gap per coin and direction. Settings belong to the
current browser; **Turn off** removes its subscription. HTTPS is required
(localhost also works). On iPhone/iPad, first add the site to the Home Screen.
If permission is blocked, change browser site settings and reload.

Push is disabled by default. To enable it:

1. After building the production images, generate keys once in a private terminal:

   ```bash
   docker compose -f production.yml run --rm --no-deps web poetry run python -m exchange_radar.push_keys
   ```

2. Add the output to root `.env`, along with:

   ```dotenv
   PUSH_ENABLED=true
   PUSH_VAPID_SUBJECT=mailto:your-contact@example.com
   ```

   The generated values are `PUSH_VAPID_PUBLIC_KEY` and `PUSH_VAPID_PRIVATE_KEY`.
   Keep the private key secret and preserve both keys across deployments;
   replacing them requires browsers to subscribe again.
3. Recreate web and scheduler:
   `docker compose -f production.yml up -d --build web scheduler`.
4. Enable notifications and use **Send test**. Check receipt with the page closed
   and confirm that clicking opens the coin's alert log.

Delivery uses existing Redis and the scheduler, with no extra container. It is
best effort: transient failures retry, jobs expire after five minutes, and browser
or OS settings can prevent display. The alert log remains the record of events.
Subscriptions expire after 180 days without a settings refresh. Disable push with
`PUSH_ENABLED=false` and recreate web/scheduler; alert history continues normally.

## Operations and development

- Producers → RabbitMQ → consumers → web → browser; Redis stores statistics and
  history. Delivery is at least once, so duplicates are possible. Producer buffers
  are finite and in memory; restarts, overload and exchange outages can lose trades.
  HTX reconnect recovery covers only its recent-history window within a running process.
- Consumer batches contain up to 50 trades and flush after at most 20 ms of
  collection. Growing producer backlog and growing broker queues are different
  bottlenecks; adding consumers helps the latter.
- Filtered feeds retain 256 rows per coin/category by default (`FEED_RETENTION_ROWS`)
  and respect `REDIS_EXPIRATION`. The all-trades feed is live-only.
- Exchange/coin lists in private `.producer`, `.web` and `.scheduler` files need
  manual updates. Remove retired EQTY, FTM and S entries; remove `SUSDT`, `SBTC`
  and `S-USDT` subscriptions where present before clearing their old data.

| Task | Command |
| --- | --- |
| Tests | `make tests` |
| Coverage | `make coverage` |
| Lint | `make pre-commit-verify` |
| Benchmarks | `make benchmark` ([sample output](benchmarks/results.out)) |

Optional integration tests use `TEST_REDIS_URL` and `TEST_AMQP_URL`. Point them at
**test services only**; tests create unique keys/queues and clean up their own data.

[version-svg]: https://img.shields.io/pypi/v/exchange-radar?style=flat-square
[package-url]: https://pypi.org/project/exchange-radar/
[ci-svg]: https://github.com/pantunes/exchange-radar/actions/workflows/ci-cd.yml/badge.svg
[ci-url]: https://github.com/pantunes/exchange-radar/actions/workflows/ci-cd.yml
[license-image]: https://shields.io/badge/license-GNU%20General%20Public%20License%20v3.0-green
[license-url]: LICENSE

