Skip to content

Add Binance Spot live trading support (runtime selectable with Alpaca)#478

Open
kiennt23 wants to merge 2 commits into
mainfrom
feat/binance-spot-trading
Open

Add Binance Spot live trading support (runtime selectable with Alpaca)#478
kiennt23 wants to merge 2 commits into
mainfrom
feat/binance-spot-trading

Conversation

@kiennt23
Copy link
Copy Markdown
Contributor

@kiennt23 kiennt23 commented May 8, 2026

Summary

Implements Binance Spot live trading as a runtime-selectable alternative to Alpaca paper trading. Closes #477.

Changes

New file: src/binance_spot.zig

  • Implements Exchange.VTable for Binance Spot
  • HMAC-SHA256 request signing (std.crypto.auth.hmac.sha2.HmacSha256)
  • Async order flow: submitOrdercheckOrdercancelOrder
  • Targets /api/v3/order endpoints with form-encoded signed requests
  • getPosition returns null — Spot has no native position API
  • JSON parsing helpers for Binance string-number format
  • parseFillCommission extracts fee data from fills array
  • parseAccountBalance queries /api/v3/account for discrepancy alerts

Updated: src/main.zig

  • Runtime exchange selection via EXCHANGE env var (alpaca | binance_spot)
  • Skip exchange position reconciliation for Spot — Turso transfers remain source of truth
  • Funding rate filter stays active for all exchanges (market signal)

Updated: src/tests.zig

  • 5 new tests for Binance Spot JSON parsing helpers
  • All 187 tests pass

Updated: AGENTS.md

  • Document EXCHANGE, BINANCE_API_KEY, BINANCE_API_SECRET env vars
  • Document Spot position model (Turso as source of truth)

Usage

# .env
EXCHANGE=binance_spot
BINANCE_API_KEY=your_key
BINANCE_API_SECRET=your_secret

# Testnet (order-flow validation only)
BINANCE_API_HOST=testnet.binance.vision
BINANCE_WS_HOST=testnet.binance.vision

Testnet note

When using testnet, both REST and WS must point to testnet. Prices are simulated — use only for validating order submission, fills, and cancel mechanics.

- New src/binance_spot.zig implementing Exchange.VTable
  - HMAC-SHA256 signing using std.crypto.auth.hmac.sha2.HmacSha256
  - submitOrder/checkOrder/cancelOrder targeting /api/v3 endpoints
  - getPosition returns null (Spot has no native position API)
  - buy/sell sync polling wrappers
  - JSON parsing helpers for Binance string-number format
  - parseFillCommission and parseAccountBalance for fee/balance data

- Update src/main.zig
  - Runtime exchange selection via EXCHANGE env var
  - Skip exchange position reconciliation for Spot (Turso is source of truth)
  - Keep funding rate filter active as market signal for all exchanges

- Add 5 Binance Spot JSON parsing tests to src/tests.zig

- Update AGENTS.md
  - Document EXCHANGE, BINANCE_API_KEY, BINANCE_API_SECRET env vars
  - Document Spot position model (Turso transfers = source of truth)
  - Document exchange selection pattern

Refs #477
@kiennt23 kiennt23 force-pushed the feat/binance-spot-trading branch from 98726a0 to 675205d Compare May 9, 2026 02:03
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.

[RFC] Add Binance Spot live trading support (runtime selectable with Alpaca)

1 participant