Skip to main content

An advanced API client for python crypto bot traders

Project description

PyPI PyPI - Python Version PyPI - License Downloads

CI publish codecov Documentation Status Hatch project 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
Bybit Link
Binance Link
OKX Link
Phemex Link
Bitget Link
MEXC No support Link
KuCoin Link
BitMEX Link

🐍 Requires

Python 3.8+

🔧 Installation

From PyPI (stable version):

pip install pybotters

From GitHub (latest version):

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

📝 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.1.1.tar.gz (165.7 kB view details)

Uploaded Source

Built Distribution

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

pybotters-1.1.1-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pybotters-1.1.1.tar.gz
  • Upload date:
  • Size: 165.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pybotters-1.1.1.tar.gz
Algorithm Hash digest
SHA256 74a47b52c5ad4387f368716082c6098e9a34fa5e771cc50928770c89bacff23d
MD5 90ce05d262ad303eaf77c1f96f6a7f36
BLAKE2b-256 296800d88067860aa54ddfc7fadbb3883e76de9fd0067d6be79001bc2398bb7c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pybotters-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 55.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pybotters-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ba188382adc28fe379578fc81af2f80f751263a7039e629eac2a946fdd512bad
MD5 34f220a559e82cd595f7a71ee7c86d9c
BLAKE2b-256 83a6b3a1e1ce525d881445489ceef1016cc799648791620ab69a2c02c258b99e

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