🪧 What's Changed
* Binance DataStore が Spot / COIN-M をサポートしました by ko0hi in https://github.com/MtkN1/pybotters/pull/189
* KuCoin をサポートしました by ko0hi in https://github.com/MtkN1/pybotters/pull/185
* Bitget Trade DataStoreで side の項目に間違った値が入っている問題を修正しました by ko0hi in https://github.com/MtkN1/pybotters/pull/183
* bitFlyer Positions DataStore で watch 機能を利用した際、ポジションが解消されたイベントが受け取れない問題を修正しました by ko0hi in https://github.com/MtkN1/pybotters/pull/182
🚀 New Features
✨ Binance DataStore が Spot / COIN-M をサポートしました
BinanceDataStore は USDⓈ-M Futures トレードの WebSocket 配信情報のみをサポートしていましたが、このアップデートにより Spot と COIN-M のサポートを追加しました!
追加された Spot と COIN-M のストアも、従来の USDⓈ-M ストア同様に WebSocket から配信される板情報やオーダー情報などを利用できます。
1. 非推奨
既存のクラス `pybotters.BinanceDataStore` は USDⓈ-M のストアとして実装されていますが、このクラスは非推奨となり将来的に削除されます。
Spot / USDⓈ-M / COIN-M のストアはそれぞれ新しいクラス名で提供されます。 (以下参照)
2. リファレンス
実装されているストアのプロパティ変数名と、それに対応する WebSocket Streams を表します。
**Spot**
- _class_ pybotters.BinanceSpotDataStore
- _coroutine_ **initialize**(_*aws_)
- [Order Book](https://binance-docs.github.io/apidocs/spot/en/#order-book)
- **orderbook**
- [Kline/Candlestick Data](https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-data)
- **kline**
- [Current Open Orders](https://binance-docs.github.io/apidocs/spot/en/#current-open-orders-user_data)
- **order**
- [Query Open OCO](https://binance-docs.github.io/apidocs/spot/en/#query-open-oco-user_data)
- **ocoorder**
- [Account Information](https://binance-docs.github.io/apidocs/spot/en/#account-information-user_data)
- **account**
- [LISTEN KEY (SPOT)](https://binance-docs.github.io/apidocs/spot/en/#listen-key-spot)
- **listenkey**
- 自動で定期的に listenkey を延長する
- **trade**
- [Aggregate Trade Streams](https://binance-docs.github.io/apidocs/spot/en/#aggregate-trade-streams)
- [Trade Streams](https://binance-docs.github.io/apidocs/spot/en/#trade-streams)
- **kline**
- [Kline/Candlestick Streams](https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-streams)
- **ticker**
- [Mini Ticker Stream](https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-mini-ticker-stream)
- [Ticker Streams](https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-ticker-streams)
- **bookticker**
- [Book Ticker Streams](https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-book-ticker-streams)
- **orderbook**
- [Diff. Depth Stream](https://binance-docs.github.io/apidocs/spot/en/#diff-depth-stream)
- **account**
- [Account Update](https://binance-docs.github.io/apidocs/spot/en/#payload-account-update)
- **order**
- [Order Update](https://binance-docs.github.io/apidocs/spot/en/#payload-order-update) の `executionReport` イベント
- **ocoorder**
- [Order Update](https://binance-docs.github.io/apidocs/spot/en/#payload-order-update) の `listStatus` イベント
- **listenkey**
- [LISTEN KEY (SPOT)](https://binance-docs.github.io/apidocs/spot/en/#listen-key-spot)
**USDⓈ-M**
- _class_ pybotters.BinanceUSDSMDataStore
- _coroutine_ **initialize**(_*aws_)
- [Order Book](https://binance-docs.github.io/apidocs/futures/en/#order-book)
- **orderbook**
- [Kline/Candlestick Data](https://binance-docs.github.io/apidocs/futures/en/#kline-candlestick-data)
- **kline**
- [Current All Open Orders](https://binance-docs.github.io/apidocs/futures/en/#current-all-open-orders-user_data)
- **order**
- [Futures Account Balance V2](https://binance-docs.github.io/apidocs/futures/en/#futures-account-balance-v2-user_data)
- **balance**
- [Position Information V2](https://binance-docs.github.io/apidocs/futures/en/#position-information-v2-user_data)
- **position**
- [Start User Data Stream](https://binance-docs.github.io/apidocs/futures/en/#start-user-data-stream-user_stream)
- **listenkey**
- 自動で定期的に listenkey を延長する
- **trade**
- [Aggregate Trade Streams](https://binance-docs.github.io/apidocs/futures/en/#aggregate-trade-streams)
- **markprice**
- [Mark Price Stream](https://binance-docs.github.io/apidocs/futures/en/#mark-price-stream)
- **kline**
- [Kline/Candlestick Streams](https://binance-docs.github.io/apidocs/futures/en/#kline-candlestick-streams)
- **continuouskline**
- [Continuous Contract Kline/Candlestick Streams](https://binance-docs.github.io/apidocs/futures/en/#continuous-contract-kline-candlestick-streams)
- **ticker**
- [Mini Ticker Stream](https://binance-docs.github.io/apidocs/futures/en/#individual-symbol-mini-ticker-stream)
- [Ticker Streams](https://binance-docs.github.io/apidocs/futures/en/#individual-symbol-ticker-streams)
- **bookticker**
- [Book Ticker Streams](https://binance-docs.github.io/apidocs/futures/en/#individual-symbol-book-ticker-streams)
- **liquidation**
- [Liquidation Order Streams](https://binance-docs.github.io/apidocs/futures/en/#liquidation-order-streams)
- **orderbook**
- [Diff. Depth Stream](https://binance-docs.github.io/apidocs/futures/en/#diff-book-depth-streams)
- **compositeindex**
- [Composite Index Symbol Information Streams](https://binance-docs.github.io/apidocs/futures/en/#composite-index-symbol-information-streams)
- **balance**
- [Balance and Position Update](https://binance-docs.github.io/apidocs/futures/en/#event-balance-and-position-update) の `"B"` キー
- **position**
- [Balance and Position Update](https://binance-docs.github.io/apidocs/futures/en/#event-balance-and-position-update) の `"P"` キー
- **order**
- [Order Update](https://binance-docs.github.io/apidocs/futures/en/#event-order-update)
- **listenkey**
- [Start User Data Stream](https://binance-docs.github.io/apidocs/futures/en/#start-user-data-stream-user_stream)
**COIN-M**
- _class_ pybotters.BinanceCOINMDataStore
- _coroutine_ **initialize**(_*aws_)
- [Order Book](https://binance-docs.github.io/apidocs/delivery/en/#order-book)
- **orderbook**
- [Kline/Candlestick Data](https://binance-docs.github.io/apidocs/delivery/en/#kline-candlestick-data)
- **kline**
- [Index Price Kline/Candlestick Data](https://binance-docs.github.io/apidocs/delivery/en/#index-price-kline-candlestick-data)
- **indexpricekline**
- [Mark Price Kline/Candlestick Data](https://binance-docs.github.io/apidocs/delivery/en/#mark-price-kline-candlestick-data)
- **markpricekline**
- [Current All Open Orders](https://binance-docs.github.io/apidocs/delivery/en/#current-all-open-orders-user_data)
- **order**
- [Futures Account Balance](https://binance-docs.github.io/apidocs/delivery/en/#futures-account-balance-user_data)
- **balance**
- [Position Information](https://binance-docs.github.io/apidocs/delivery/en/#position-information-user_data)
- **position**
- [Start User Data Stream](hthttps://binance-docs.github.io/apidocs/delivery/en/#start-user-data-stream-user_streamream)
- **listenkey**
- 自動で定期的に listenkey を延長する
- **trade**
- [Aggregate Trade Streams](https://binance-docs.github.io/apidocs/delivery/en/#aggregate-trade-streams)
- **indexprice**
- [Index Price Stream](https://binance-docs.github.io/apidocs/delivery/en/#index-price-stream)
- **markprice**
- [Mark Price Stream](https://binance-docs.github.io/apidocs/delivery/en/#mark-price-stream)
- [Mark Price of All Symbols of a Pair](https://binance-docs.github.io/apidocs/delivery/en/#mark-price-of-all-symbols-of-a-pair)
- **kline**
- [Kline/Candlestick Streams](https://binance-docs.github.io/apidocs/delivery/en/#kline-candlestick-streams)
- **continuouskline**
- [Continuous Contract Kline/Candlestick Streams](https://binance-docs.github.io/apidocs/delivery/en/#continuous-contract-kline-candlestick-streams)
- **indexpricekline**
- [Index Kline/Candlestick Streams](https://binance-docs.github.io/apidocs/delivery/en/#index-kline-candlestick-streams)
- **markpricekline**
- [Mark Price Kline/Candlestick Streams](https://binance-docs.github.io/apidocs/delivery/en/#mark-price-kline-candlestick-streams)
- **ticker**
- [Mini Ticker Stream](https://binance-docs.github.io/apidocs/delivery/en/#individual-symbol-mini-ticker-stream)
- [Ticker Streams](https://binance-docs.github.io/apidocs/delivery/en/#individual-symbol-ticker-streams)
- **bookticker**
- [Book Ticker Streams](https://binance-docs.github.io/apidocs/delivery/en/#individual-symbol-book-ticker-streams)
- **liquidation**
- [Liquidation Order Streams](https://binance-docs.github.io/apidocs/delivery/en/#liquidation-order-streams)
- **orderbook**
- [Diff. Depth Stream](https://binance-docs.github.io/apidocs/delivery/en/#diff-book-depth-streams)
- **balance**
- [Balance and Position Update](https://binance-docs.github.io/apidocs/delivery/en/#event-balance-and-position-update) の `"B"` キー
- **position**
- [Balance and Position Update](https://binance-docs.github.io/apidocs/delivery/en/#event-balance-and-position-update) の `"P"` キー
- **order**
- [Order Update](https://binance-docs.github.io/apidocs/delivery/en/#event-order-update)
- **listenkey**
- [Start User Data Stream](https://binance-docs.github.io/apidocs/delivery/en/#start-user-data-stream-user_stream)
3. サンプルコード
Spot, USDⓈ-M, COIN-M の User Data (残高、オーダー、ポジション) を watch 機能で監視するサンプルコードです。
このコードを実行中に Web 画面でオーダーを送信するなどすると、その情報が画面に表示されます。
py
import pybotters
import asyncio
from rich.pretty import pprint
apis = {"binance": ["KEY", "SECRET"]}
async def watcher(_stream):
with _stream as stream:
async for change in stream:
pprint(change)
async def main():
async with pybotters.Client(apis=apis) as client:
pybotters.BinanceDataStore()
sstore = pybotters.BinanceSpotDataStore()
fstore = pybotters.BinanceUSDSMDataStore()
dstore = pybotters.BinanceCOINMDataStore()
await asyncio.gather(
sstore.initialize(
client.get("https://api.binance.com/api/v3/openOrders"),
client.get("https://api.binance.com/api/v3/openOrderList"),
client.get("https://api.binance.com/api/v3/account"),
client.post("https://api.binance.com/api/v3/userDataStream"),
),
fstore.initialize(
client.get("https://fapi.binance.com/fapi/v1/openOrders"),
client.get("https://fapi.binance.com/fapi/v2/balance"),
client.get("https://fapi.binance.com/fapi/v2/positionRisk"),
client.post("https://fapi.binance.com/fapi/v1/listenKey"),
),
dstore.initialize(
client.get("https://dapi.binance.com/dapi/v1/openOrders"),
client.get("https://dapi.binance.com/dapi/v1/balance"),
client.get("https://dapi.binance.com/dapi/v1/positionRisk"),
client.post("https://dapi.binance.com/dapi/v1/listenKey"),
),
)
await asyncio.gather(
client.ws_connect(
f"wss://stream.binance.com:9443/stream?streams={sstore.listenkey}",
hdlr_json=sstore.onmessage,
),
client.ws_connect(
f"wss://fstream.binance.com/stream?streams={fstore.listenkey}",
hdlr_json=fstore.onmessage,
),
client.ws_connect(
f"wss://dstream.binance.com/stream?streams={dstore.listenkey}",
hdlr_json=dstore.onmessage,
),
)
await asyncio.gather(
Spot
watcher(sstore.account.watch()),
watcher(sstore.ocoorder.watch()),
watcher(sstore.order.watch()),
USDⓈ-M
watcher(fstore.balance.watch()),
watcher(fstore.position.watch()),
watcher(fstore.order.watch()),
COIN-M
watcher(dstore.balance.watch()),
watcher(dstore.position.watch()),
watcher(dstore.order.watch()),
)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
✨ KuCoin の API 認証 / DataStore をサポートしました
700 種類以上のコインが上場し、CoinMarketCap の取引高 5 位の KuCoin をサポートしました!
Spot / Futures 両方対応しており、API 認証及び DataStore を利用できます。
1. リファレンス
- _class_ pybotters.KuCoinDataStore
- _coroutine_ **initialize**(_*aws_)
- **kline**
- **positions**
- **endpoint**
- `/api/v1/bullet-public`, `/api/v1/bullet-private` で取得された動的 WebSocket エンドポイントを格納する
- **ticker**
- **kline**
- **symbolsnapshot**
- **orderbook5**
- **orderbook50**
- **execution**
- **indexprice**
- **markprice**
- **orderevents**
- **orders**
- **balance**
- **marginfundingbook**
- **marginpositions**
- **marginpositionevents**
- **marginorderevents**
- **marginorders**
- **instrument**
- **announcements**
- **transactionstats**
- **balanceevents**
- **positions**
- **endpoint**
- WebSocket の動的エンドポイント
※詳細な情報 (対応するエンドポイント及び WebSocket イベント) は docstrings に記載されています。
2. サンプルコード
Kline の取得、約定監視、板監視、オーダー のサンプルコードです。
py
import asyncio
import pybotters
import uuid
import rich
async def sample1():
kline取得
async with pybotters.Client(base_url="https://api.kucoin.com") as client:
store = pybotters.KucoinDataStore()
await store.initialize(
client.get("/api/v1/market/candles?type=1min&symbol=BTC-USDT"),
client.post("/api/v1/bullet-public"),
)
await client.ws_connect(
store.endpoint,
send_json=[
{
"id": str(uuid.uuid4()),
"type": "subscribe",
"topic": "/market/candles:BTC-USDT_1min",
"response": True,
},
{
"id": str(uuid.uuid4()),
"type": "subscribe",
"topic": "/market/candles:BTC-USDT_3min",
"response": True,
},
],
hdlr_json=store.onmessage,
)
while True:
rich.print(
[
未確定足
store.kline.latest("BTC-USDT", "1min"),
store.kline.latest("BTC-USDT", "3min"),
確定足
store.kline.find({"symbol": "BTC-USDT", "interval": "1min"})[-2:],
store.kline.find({"symbol": "BTC-USDT", "interval": "3min"})[-2:],
]
)
await asyncio.sleep(1)
async def sample2():
約定監視
async with pybotters.Client(base_url="https://api-futures.kucoin.com") as client:
store = pybotters.KucoinDataStore()
await store.initialize(client.post("/api/v1/bullet-public"))
await client.ws_connect(
store.endpoint,
send_json=[
{
"id": str(uuid.uuid4()),
"type": "subscribe",
"topic": "/contractMarket/execution:XBTUSDTM",
"response": True,
}
],
hdlr_json=store.onmessage,
)
with store.execution.watch() as stream:
async for msg in stream:
rich.print(msg)
async def sample3():
板監視
async with pybotters.Client(base_url="https://api-futures.kucoin.com") as client:
store = pybotters.KucoinDataStore()
await store.initialize(client.post("/api/v1/bullet-public"))
await client.ws_connect(
store.endpoint,
send_json=[
{
"id": str(uuid.uuid4()),
"type": "subscribe",
"topic": "/contractMarket/level2Depth5:XBTUSDTM",
"response": True,
}
],
hdlr_json=store.onmessage,
)
while True:
await store.orderbook5.wait()
rich.print(store.orderbook5.sorted())
async def sample4():
注文
async with pybotters.Client(
base_url="https://api-futures.kucoin.com",
apis={
"kucoinfuture": [
"YOUR_API_KEY",
"YOUR_SECRET_KEY",
"YOUR_PASSPHRASE",
]
},
) as client:
store = pybotters.KucoinDataStore()
await store.initialize(client.post("/api/v1/bullet-private"))
await client.ws_connect(
store.endpoint,
send_json=[
{
"id": str(uuid.uuid4()),
"type": "subscribe",
"topic": "/contractMarket/tradeOrders",
"privateChannel": True,
"response": True,
},
],
hdlr_json=store.onmessage,
)
await client.post(
"/api/v1/orders",
data=dict(
clientOid=str(uuid.uuid4()),
side="buy",
symbol="XBTUSDTM",
type="limit",
price="18000",
leverage=1,
size=1,
),
)
await asyncio.sleep(1)
rich.print(store.orders.find())
await client.delete("/api/v1/orders")
await asyncio.sleep(1)
rich.print(store.orders.find())
💊 Bugfix
- BitgetDataStore.trade の "side" フィールドに "price" の値が入っていたバグを修正しました
- DataStore において _remove メソッドが watch にデータをキューイングしていないバグを修正しました
New Contributors
* ko0hi made their first contribution in https://github.com/MtkN1/pybotters/pull/182
**Full Changelog**: https://github.com/MtkN1/pybotters/compare/v0.13.1...v0.14.0