Skip to main content

Python SDK for the CTG.EXCHANGE exchange API (REST + WebSocket)

Project description

CTG.EXCHANGE Python SDK

Python client for the CTG.EXCHANGE exchange API — a thin, typed wrapper over the public REST + WebSocket interface.

Install

pip install ctg-exchange

Requires Python 3.9+.

Quick start

Public market data — no key needed

from ctg-exchange import Client

with Client() as client:
    for sym in client.get_symbols():
        print(sym.symbol, sym.tick_size)

    book = client.get_order_book("CTGUSDT")
    print(book.bids[0].price, book.asks[0].price)

Private account & trading

API keys are created in the CTG.EXCHANGE web app (Account → API keys). Pass them to the client — never hard-code them; read them from the environment.

import os
from ctg-exchange import Client

with Client(api_key=os.environ["CTG_EXCHANGE_API_KEY"],
            api_secret=os.environ["CTG_EXCHANGE_API_SECRET"]) as client:
    for balance in client.get_balances():
        print(balance.asset, balance.available)

    result = client.place_order(
        "CTGUSDT", side="buy", type="limit", price="100.00", qty="1",
    )
    print(result.order.id, result.order.status)

WebSocket streams

import asyncio
from ctg-exchange import MarketDataStream

async def main():
    async with MarketDataStream(channels=["trades@CTGUSDT"]) as stream:
        async for msg in stream:
            print(msg.channel, msg.type, msg.data)

asyncio.run(main())

The private UserStream takes api_key / api_secret and authenticates in-band with a signed first frame. Subscribe to any of orders, trades, balances:

import asyncio, os
from ctg-exchange import UserStream

async def main():
    async with UserStream(
        api_key=os.environ["CTG_EXCHANGE_API_KEY"],
        api_secret=os.environ["CTG_EXCHANGE_API_SECRET"],
        channels=["orders", "balances"],
    ) as stream:
        async for msg in stream:
            print(msg.channel, msg.type, msg.data)

asyncio.run(main())

Both streams auto-reconnect and re-subscribe on a dropped socket.

The decimal contract

Every monetary value — price, qty, volume, fee amounts — is a decimal.Decimal, never a float. The API sends and receives these as JSON strings; this SDK parses them losslessly. When placing orders, price / qty accept a Decimal, str or int.

Errors

Non-2xx responses raise a typed exception — BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError, RateLimitError, ServerError — all subclasses of APIError. Each carries status_code and the API's request_id; RateLimitError adds retry_after. Pass max_retries= to Client to auto-retry 429s.

What this SDK does not do

Withdrawals are not part of the CTG.EXCHANGE API and not in this SDK — they require a wallet signature and happen only in the web app.

Development

pip install -e ".[dev]"
pytest            # offline tests run with no credentials
ruff check .

Integration tests run only when CTG_EXCHANGE_API_KEY / CTG_EXCHANGE_API_SECRET are set, and are read-only — they never place orders.

Links

License

Apache-2.0

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

ctg_exchange-0.1.2.tar.gz (23.0 kB view details)

Uploaded Source

Built Distribution

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

ctg_exchange-0.1.2-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file ctg_exchange-0.1.2.tar.gz.

File metadata

  • Download URL: ctg_exchange-0.1.2.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ctg_exchange-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2906ba65db6d873828f5a4c5ad75d673759c7c61e77fc4f750b3fb63ce53bf23
MD5 74754b0680989207ff39f738609db317
BLAKE2b-256 8db5b4a161007870aea0cf49f822f3ad7cb28efe2e86ccb2f11f50daefe815f4

See more details on using hashes here.

File details

Details for the file ctg_exchange-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ctg_exchange-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ctg_exchange-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 951c245a97685f29f8d3f1944c78f6e2d34e140ebf687ceacf1db7c607ee1a38
MD5 92730edf81d000b6d970e41b7e188490
BLAKE2b-256 9053d4fbf161334644d11cafc9eac5f7444c9ea133aa39de16bd2328a658ff71

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