Skip to main content

Unified instrument spec parser and venue symbol resolver for crypto markets

Project description

marketspec

Unified instrument spec parser and venue symbol resolver for crypto markets.

  • Parse BTC/USDT:USDT, BTC/USD:USD-20251227, ETH-20251226-2500-C.
  • Resolve symbols for Binance and Bybit.
  • No runtime dependencies. Typed. MIT.

Install

pip install marketspec

Quick start

from marketspec import venue_symbol, parse, resolve_symbol

print(venue_symbol("binance", "BTC/USDT:USDT"))          # BTCUSDT
print(venue_symbol("bybit",   "BTC/USD:USD-20251227"))   # BTCUSD_20251227

s = parse("ETH-20251226-2500-C")   # -> Spec
print(resolve_symbol("binance", s))  # ETH-20251226-2500-C
print(s.unified())                   # round-trip -> "ETH-20251226-2500-C"

Grammar

  • Spot: BASE/QUOTEBTC/USDT
  • Swap (linear): BASE/QUOTE:SETTLE where SETTLE == QUOTE and is a stable → BTC/USDT:USDT
  • Swap (inverse): BASE/QUOTE:USDBTC/USD:USD
  • Future: append -YYYYMMDD to the swap form → BTC/USDT:USDT-20251227
  • Option: BASE-YYYYMMDD-STRIKE-C|PETH-20251226-2500-C

Dates accept YYYYMMDD, YYYY-MM-DD, YYMMDD (assumes 20YY), or date/datetime.

API

from marketspec import parse, resolve_symbol, venue_symbol, set_stables
from marketspec.unified import override_stables  # optional scoped override
from marketspec.types import Spec

# One-call convenience
venue_symbol("binance", "BTC/USDT:USDT")  # -> "BTCUSDT"

# Stable parser + resolver
s: Spec = parse("BTC/USD:USD-20251227")
resolve_symbol("bybit", s)                # -> "BTCUSD_20251227"

# Round-trip for logs/tests
s.unified()                               # -> "BTC/USD:USD-20251227"

# Configure stables (global)
set_stables({"USDT", "USDC", "FDUSD"})

# Scoped override for tests or multi-tenant code
from marketspec.unified import override_stables
with override_stables({"USDT", "USDC"}):
    resolve_symbol("binance", parse("BTC/USDT:USDT"))

Exceptions

  • Parsing errors raise ValueError.
  • Resolution errors raise marketspec.registry.ResolveError (subclasses ValueError).

Supported venues

  • binance
  • bybit

PRs welcome for more venues.

Development

python -m venv .venv && source .venv/bin/activate  # Windows: .venv\Scriptsctivate
pip install -e . pytest ruff mypy
pytest -q
ruff check .
mypy src

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

marketspec-1.0.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

marketspec-1.0.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file marketspec-1.0.0.tar.gz.

File metadata

  • Download URL: marketspec-1.0.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for marketspec-1.0.0.tar.gz
Algorithm Hash digest
SHA256 41392017a5bcae771ac7086f858704651b98bb1384042e51fd4d7ef8af3f27de
MD5 49b1a1ba143037c052fea36880538493
BLAKE2b-256 bc0bf0e10c1a416c5653c2cabca935f267b34726ecb9c6ba6ded609df2c958ea

See more details on using hashes here.

File details

Details for the file marketspec-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: marketspec-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for marketspec-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3067806a82a27928d3f1325806debb11e53d4335ccac94f197700e1ff96f48e
MD5 5f5cb728daf789bcd706136192f92b71
BLAKE2b-256 05aca3fc9897c2b13b98b71b088e3215ca051e08d8fc8af6db40e97b1fbc2130

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page