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.1.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.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: marketspec-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 45ac440312d076410c73b8c0515fd4153fd16d462388d9c0a8062c6759174605
MD5 7fd53047d724e61f9533ce8596386e29
BLAKE2b-256 00c1214641ac55b5f6252442d3b790037408ed00de3db1e717a4f82bd67a8247

See more details on using hashes here.

File details

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

File metadata

  • Download URL: marketspec-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eb1e234f1c0dd60c7f63a2c4d7c907b27e9a042df0b59592b47a8e3f1893da34
MD5 dfd3dcefc4818903ab6c3ae6c0844e8c
BLAKE2b-256 988117b028d13a0594949b133a546173e97fb032ae89e8d53498774aa23c3350

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