Skip to main content

An advanced API client for python crypto bot traders

Project description

PyPI PyPI - Python Version PyPI - License Downloads

CI publish Documentation Status uv Ruff

GitHub Repo stars GitHub forks Discord GitHub Sponsor

pybotters

pybotters logo

An advanced API client for python botters. This project is in Japanese.

📌 Description

pybotters is a Python library for 仮想通貨 botter (crypto bot traders).

This library is an HTTP and WebSocket API client. It has the following features, making it useful for developing a trading bot.

🚀 Features

  • ✨ HTTP / WebSocket Client
    • Automatic authentication for private APIs.
    • WebSocket automatic reconnection and automatic heartbeat.
    • A client based on aiohttp.
  • ✨ DataStore
    • WebSocket message data handler.
    • Processing of differential data such as order book updates
    • High-speed data processing and querying
  • ✨ Other Experiences
    • Support for type hints.
    • Asynchronous programming using asyncio.
    • Discord community.

🏦 Exchanges

Name API auth DataStore Exchange API docs
bitFlyer Link
GMO Coin Link
bitbank Link
Coincheck Link
OKJ Not yet Link
BitTrade Not yet Link
Bybit Link
Binance Link
OKX Link
Phemex Link
Bitget Link
MEXC No support Link
KuCoin Link
BitMEX Link

🐍 Requires

Python 3.9+

🔧 Installation

From PyPI (stable version):

pip install pybotters

From GitHub (latest version):

pip install git+https://github.com/pybotters/pybotters.git

⚠️ Compatibility warning

pybotters is planning a completely new code base v2. It is recommended to specify version less than 2.0 (pybotters<2.0) when specifying it as a dependency.

[!IMPORTANT] The roadmap is here: pybotters/pybotters#248

📝 Usage

Example of bitFlyer API:

HTTP API

New interface from version 1.0: Fetch API.

More simple request/response.

import asyncio

import pybotters

apis = {
    "bitflyer": ["YOUER_BITFLYER_API_KEY", "YOUER_BITFLYER_API_SECRET"],
}


async def main():
    async with pybotters.Client(
        apis=apis, base_url="https://api.bitflyer.com"
    ) as client:
        # Fetch balance
        r = await client.fetch("GET", "/v1/me/getbalance")

        print(r.response.status, r.response.reason, r.response.url)
        print(r.data)

        # Create order
        CREATE_ORDER = False  # Set to `True` if you are trying to create an order.
        if CREATE_ORDER:
            r = await client.fetch(
                "POST",
                "/v1/me/sendchildorder",
                data={
                    "product_code": "BTC_JPY",
                    "child_order_type": "MARKET",
                    "side": "BUY",
                    "size": 0.001,
                },
            )

            print(r.response.status, r.response.reason, r.response.url)
            print(r.data)


asyncio.run(main())

aiohttp-based API.

import asyncio

import pybotters

apis = {
    "bitflyer": ["YOUER_BITFLYER_API_KEY", "YOUER_BITFLYER_API_SECRET"],
}


async def main():
    async with pybotters.Client(
        apis=apis, base_url="https://api.bitflyer.com"
    ) as client:
        # Fetch balance
        async with client.get("/v1/me/getbalance") as resp:
            data = await resp.json()

        print(resp.status, resp.reason)
        print(data)

        # Create order
        CREATE_ORDER = False  # Set to `True` if you are trying to create an order.
        if CREATE_ORDER:
            async with client.post(
                "/v1/me/sendchildorder",
                data={
                    "product_code": "BTC_JPY",
                    "child_order_type": "MARKET",
                    "side": "BUY",
                    "size": 0.001,
                },
            ) as resp:
                data = await resp.json()

            print(data)


asyncio.run(main())

WebSocket API

import asyncio

import pybotters


async def main():
    async with pybotters.Client() as client:
        # Create a Queue
        wsqueue = pybotters.WebSocketQueue()

        # Connect to WebSocket and subscribe to Ticker
        await client.ws_connect(
            "wss://ws.lightstream.bitflyer.com/json-rpc",
            send_json={
                "method": "subscribe",
                "params": {"channel": "lightning_ticker_BTC_JPY"},
            },
            hdlr_json=wsqueue.onmessage,
        )

        # Iterate message (Ctrl+C to break)
        async for msg in wsqueue:
            print(msg)


try:
    asyncio.run(main())
except KeyboardInterrupt:
    pass

DataStore

import asyncio

import pybotters


async def main():
    async with pybotters.Client() as client:
        # Create DataStore
        store = pybotters.bitFlyerDataStore()

        # Connect to WebSocket and subscribe to Board
        await client.ws_connect(
            "wss://ws.lightstream.bitflyer.com/json-rpc",
            send_json=[
                {
                    "method": "subscribe",
                    "params": {"channel": "lightning_board_snapshot_BTC_JPY"},
                },
                {
                    "method": "subscribe",
                    "params": {"channel": "lightning_board_BTC_JPY"},
                },
            ],
            hdlr_json=store.onmessage,
        )

        # Watch for the best prices on Board. (Ctrl+C to break)
        with store.board.watch() as stream:
            async for change in stream:
                board = store.board.sorted(limit=2)
                print(board)


try:
    asyncio.run(main())
except KeyboardInterrupt:
    pass

📖 Documentation

🔗 https://pybotters.readthedocs.io/ja/stable/ (Japanese)

🗽 License

MIT

💖 Author

Please sponsor me!:

GitHub Sponsor

X:

X (formerly Twitter) Follow

Discord:

Discord Widget

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

pybotters-1.6.0.tar.gz (177.9 kB view details)

Uploaded Source

Built Distribution

pybotters-1.6.0-py3-none-any.whl (62.7 kB view details)

Uploaded Python 3

File details

Details for the file pybotters-1.6.0.tar.gz.

File metadata

  • Download URL: pybotters-1.6.0.tar.gz
  • Upload date:
  • Size: 177.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.4

File hashes

Hashes for pybotters-1.6.0.tar.gz
Algorithm Hash digest
SHA256 c2560b6a294032e78e7e6b41a6c5a457718f03bfade6c693c9f65a6ed3d3f567
MD5 29236d3f9081d36324f6c1b34d996582
BLAKE2b-256 6185db116212809ef2af8a3093da361c848466cc1110c96c66fa69f88bf51c8f

See more details on using hashes here.

File details

Details for the file pybotters-1.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pybotters-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3d506d8313c861c87bb15df005fd0e7f4f740e3a060233bd80f8326fc0aaae15
MD5 9ae0925af7be9b0d4c1c08d732ef7162
BLAKE2b-256 8c9c731679a7c82aedeae20f9bbbe122b8c91c1f39e0977a091d67b78c062a9b

See more details on using hashes here.

Supported by

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