Skip to main content

An abstract, fully-typed, async Python SDK for automated crypto trading. By Tribulnation.

Project description

Tribulnation SDK

PyPI Python versions License: MIT

Fully-typed, async Python SDK for crypto trading and data.

Market, Wallet, Earn, and Report are abstract interfaces implemented per exchange and chain. Code written against MarketSDK runs unchanged on dYdX, Hyperliquid, MEXC, or any other supported venue.

Installation

pip install "tribulnation-sdk[dydx,hyperliquid,mexc]"

See the support matrix for details on extras.

Trading Quick Start

from dotenv import load_dotenv
from tribulnation.sdk import MarketSDK, accounts

load_dotenv() # load credentials from .env file

sdk = MarketSDK({
  'mexc_account1': accounts.Mexc(api_key='$MEXC_API_KEY', api_secret='$MEXC_API_SECRET'),
  # 'dydx', 'hyperliquid', and 'mexc' are available by default, even without listing them here
})
mexc = await sdk.market('mexc_account1:spot:BTCUSDT')
dydx = await sdk.market('dydx:perp:BTC-USD')

async for my_trade in mexc.trades_stream():
  print(f'Hedging {my_trade}')
  await dydx.place_order({
    'type': 'LIMIT',
    'qty': -my_trade.qty,
    'price': my_trade.price,
  })

accounts.<Venue>() reads credentials from environment variables named after each field (accounts.Mexc() reads $MEXC_API_KEY/$MEXC_API_SECRET) — pass explicit values or other $VAR names to override.

Market IDs & Scoping

<account_id>:<exchange_id>:<market_id>, e.g. mexc_account1:spot:BTCUSDT. account_id is the key you registered in accounts — not necessarily the venue's own name — so you can run several accounts on one venue side by side. Equivalent ways to reach a market:

await sdk.depth('mexc_account1:spot:BTCUSDT')

venue = await sdk.venue('mexc_account1')
await venue.depth('spot:BTCUSDT')

exchange = await venue.exchange('spot')
await exchange.depth('BTCUSDT')

market = await exchange.market('BTCUSDT')
await market.depth()

Hold a Market reference in hot loops; use the scoped one-shot calls otherwise.

Market Interface

  • Public data:
    • depth() -> Book
    • depth_stream() -> Stream[Book]
    • rules() -> Rules: tick/step size, fees, min/max, rounding helpers
  • User data:
    • query_order(id) -> OrderState | None
    • open_orders() -> Sequence[OrderState]
    • trades_history(start, end) -> AsyncIterable[Sequence[Trade]]
    • trades_stream() -> Stream[Trade]
    • position() -> Position
    • available_notional() -> Decimal: max. notional you could open now
  • Trading:
    • place_order(order) -> OrderResponse
    • place_orders(orders) -> Sequence[OrderResponse]
    • cancel_order(id)
    • cancel_orders(ids)
    • cancel_open_orders()
  • Perpetual markets:
    • index() -> Decimal
    • next_funding() -> FundingRate
    • funding_history(start, end) -> AsyncIterable[Sequence[FundingRate]]
    • funding_payments(start, end) -> AsyncIterable[Sequence[FundingPayment]]
    • perp_position() -> PerpPosition: includes entry price

Full reference: docs/market.md.

Mutating methods also take an optional settings dict for venue-specific options, keyed by venue:

await dydx.place_order({
  'type': 'LIMIT', 'qty': 0.01, 'price': 60_000,
}, settings={'dydx': {'order_flags': 'SHORT_TERM', 'short_term_gtb': 2}})

Other SDKs

Same account-mapping shape as MarketSDK:

Error Handling

All errors subclass Error: NetworkError, ValidationError, ApiError (BadRequest, AuthError, RateLimited), LogicError.

Context, Logging & Retries

SDK calls are plain by default — no logging, no retries. Wrap them in a Context to add both:

from tribulnation.sdk import Context, NetworkError, RateLimited

ctx = Context().retried(NetworkError, RateLimited, max_retries=5).logged()
with ctx.use():
  await sdk.place_order('mexc_account1:spot:BTCUSDT', {'type': 'LIMIT', 'qty': 0.01, 'price': 60_000})

Retries back off exponentially and only wrap plain async calls, not streams or paginated history. Nested SDK calls each re-apply the active context, so retries can compound across scoping layers. Details: docs/context.md.

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

tribulnation_sdk-1.3.0.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

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

tribulnation_sdk-1.3.0-py3-none-any.whl (46.4 kB view details)

Uploaded Python 3

File details

Details for the file tribulnation_sdk-1.3.0.tar.gz.

File metadata

  • Download URL: tribulnation_sdk-1.3.0.tar.gz
  • Upload date:
  • Size: 31.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for tribulnation_sdk-1.3.0.tar.gz
Algorithm Hash digest
SHA256 f342a8ebd66b2bd8e595d0a49b6554984a409b74cb34f736ca31318c702f0bde
MD5 18a63c9b50bb3b114d1839ac475f9dc6
BLAKE2b-256 c18857266c5f51816b580ceb40834be0377e295d9b15bca6da17e9d278043a1e

See more details on using hashes here.

File details

Details for the file tribulnation_sdk-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tribulnation_sdk-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e475b8ac62647328066490c2092d0877316f6f4f2d464f42ed36aa78db476b
MD5 bb8db9bb0ffff2026be80e1f286a636a
BLAKE2b-256 31b8763fa12b181018f0e98e745bb97c16accf4714076465b5ef98b0678b5b23

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