Skip to content

Add WebSocket streaming, mocked tests, and exchange metadata support#3

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-fastapi-candle-service
Draft

Add WebSocket streaming, mocked tests, and exchange metadata support#3
Copilot wants to merge 3 commits intomainfrom
copilot/add-fastapi-candle-service

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 19, 2026

Adds missing components to the FastAPI crypto data service: deterministic mocked tests for CI, real-time WebSocket streaming, and exchange metadata in API responses.

Changes

WebSocket Streaming (src/app/api/v1/ws_live_data.py)

  • New /api/v1/live_data/ws endpoint for real-time data streaming
  • Configurable update interval via query param (10-300s)
  • Graceful shutdown on client "close" message
  • Error recovery with exponential backoff

Mocked Tests (tests/test_live_data_mocked.py)

  • 6 test scenarios using monkeypatch to stub ExchangeClient.fetch_ohlcv
  • Validates NaN→None conversion, error handling (503/400), and validation (422)
  • Eliminates network dependency for CI reliability

Exchange Metadata

  • ExchangeClient.get_meta_info() exposes exchange configuration
  • Response meta field now includes default_type (spot/future/etc.)
  • Updated process_ohlcv_data signature to accept metadata
# WebSocket usage
ws = websocket.connect("ws://localhost:8000/api/v1/live_data/ws?symbol=BTC/USDT&interval=30")
data = ws.recv()  # Receives updates every 30s

# Meta information in response
response = requests.get("/api/v1/live_data")
print(response.json()["meta"])  # {"default_type": "spot"}

Code Quality

  • Flattened nested conditionals in get_meta_info()
  • Added clarifying comments for timeout handling in WebSocket loop
  • All tests passing (9/9), linting clean, mypy strict mode compliant

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.binance.com
    • Triggering command: /home/REDACTED/.local/bin/pytest pytest -v (dns block)
    • Triggering command: /home/REDACTED/.local/bin/uvicorn uvicorn app.main:app --host 0.0.0.0 --port 8000 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Add a complete FastAPI service for fetching OHLCV candles and computing indicators (EMA/RSI/ATR) with Docker, CI, typing, and tests.

Include the following features and files (create new branch and open a PR against 'main'):

  1. Project files
  • pyproject.toml (deps: fastapi, uvicorn[standard], pydantic, pydantic-settings, ccxt, pandas, ta; dev: pytest, httpx, ruff, mypy, types-requests)
  • README.md (quickstart, Docker, notes)
  • .env.example
  • .gitignore
  • Dockerfile
  • docker-compose.yml
  • .github/workflows/ci.yml
  1. Application package (src/app)
  • src/app/init.py
  • src/app/main.py (FastAPI app, health endpoint, include router)
  1. core
  • src/app/core/init.py
  • src/app/core/config.py (pydantic-settings Settings)
  • src/app/core/logging.py (basic logging config)
  1. api
  • src/app/api/init.py
  • src/app/api/v1/init.py
  • src/app/api/v1/routes_live_data.py (GET /live_data route, validation, response_model)
  1. integrations
  • src/app/integrations/init.py
  • src/app/integrations/exchanges.py (ExchangeClient wrapper, build_exchange)
  1. schemas
  • src/app/schemas/init.py
  • src/app/schemas/live_data.py (Candle, LatestIndicators, LiveDataResponse Pydantic models)
  1. services
  • src/app/services/init.py
  • src/app/services/indicators.py (add_indicators, latest_indicator_values, NaN->None handling)
  • src/app/services/market_data.py (get_live_data, fetch ohlcv via ccxt, build response)
  1. WebSocket streaming
  • src/app/api/v1/ws_live_data.py (optional: /api/v1/live_data/ws WebSocket endpoint that streams recent candle data every N seconds; configurable via query param interval)
  1. tests
  • tests/init.py
  • tests/test_health.py
  • tests/test_live_data.py (network smoke test)
  • tests/test_live_data_mocked.py (mocked ccxt tests using pytest + monkeypatch to avoid network in CI)

Implementation details and behavior:

  • Default exchange: binance (configurable via .env)
  • Indicators: EMA 20/50/200, RSI 14, ATR 14 using ta and pandas
  • Handle NaN and infinite values by converting to None before JSON serialization
  • Endpoint validates symbol, timeframe, limit, exchange via fastapi Query
  • Error mapping: ccxt.NetworkError -> HTTP 503, ccxt.ExchangeError -> HTTP 400, other exceptions -> HTTP 500
  • recent_candles: return last 20 candles
  • Provide meta information including exchange.options.get('defaultType') if available
  • Configure logging and settings via pydantic-settings
  • Dockerfile installs editable package with dev deps and runs uvicorn
  • GitHub Actions CI: checkout, setup Python 3.11, install deps, run ruff, mypy, pytest

Testing and CI notes:

  • Add mocked ccxt tests (tests/test_live_data_mocked.py) to stub exchange.fetch_ohlcv to ensure CI deterministic
  • Keep one smoke network test but mark or guard it in CI if necessary

Branch and PR details:

  • Create a new branch named: add/fastapi-live-data
  • Open a pull request against base 'main'
  • PR title: "Add professional FastAPI crypto data service with OHLCV + indicators"
  • PR description: include this problem statement and list of files added
  • Do not assign reviewers or assignees or labels by default
  • Preserve multiple logical commits (do not squash)

Please create the PR in the repository: Nfectious/crypto-trade-analysis

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 19, 2026 03:57
- Added tests/test_live_data_mocked.py with comprehensive mocked tests for CI
- Added src/app/api/v1/ws_live_data.py for WebSocket streaming endpoint
- Updated ExchangeClient to provide get_meta_info() method
- Updated market_data service to include exchange metadata in response
- All tests passing, linting and type checking successful

Co-authored-by: Nfectious <19631929+Nfectious@users.noreply.github.com>
- Flatten nested conditionals in get_meta_info() for better readability
- Add clarifying comment about TimeoutError in WebSocket endpoint
- Improve comment in NaN handling test for clarity
Copilot AI changed the title [WIP] Add complete FastAPI service for OHLCV candles and indicators Add WebSocket streaming, mocked tests, and exchange metadata support Feb 19, 2026
Copilot AI requested a review from Nfectious February 19, 2026 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants