Skip to content

Public Channels

poloniex-sdk edited this page Sep 22, 2022 · 1 revision

WS APIs to access public channels

Subscription Examples

Sample Requests and Stream

Request → Receipt Confirmation → Stream (continuous feed)

Example:

import asyncio

from polosdk import WsClientPublic


async def ws_public_example():
    def on_message(msg):
        print(msg)

    def on_error(err):
        print(err)

    ws_client_public = WsClientPublic(on_message, on_error=on_error)
    await ws_client_public.connect()
    await ws_client_public.subscribe(['ticker'], ['ETH_USDT'])


def main():
    asyncio.run(ws_public_example())


if __name__ == '__main__':
    main()

Candlesticks

Continuous feed of candlestick data with default/provided depth.

Valid channels are: candles_minute_1, candles_minute_5, candles_minute_10, candles_minute_15, candles_minute_30, candles_hour_1, candles_hour_2, candles_hour_4, candles_hour_6, candles_hour_12, candles_day_1, candles_day_3, candles_week_1 and candles_month_1

Stream Response Field Data Type Description
symbol String symbol name
amount String quote units traded over the interval
high String highest price over the interval
quantity String base units traded over the interval
tradeCount Integer count of trades
low String lowest price over the interval
closeTime Long close time of interval
startTime Long start time of interval
close String price at the end time
open String price at the start time
ts Long time the record was pushed

Example:

# --> Request
await ws_client_public.subscribe(['candles_minute_1'], ['BTC_USDT'])

# <-- Receipt Confirmation
{
  "channel": "candles_minute_1",
  "event": "subscribe",
  "symbols": ["BTC_USDT"]
}

# <-- Stream
{
  "channel": "candles_minute_1",
  "data": [{
      "symbol": "BTC_USDT",
      "amount": "0",
      "high": "9999.07",
      "quantity": "0",
      "tradeCount": 0,
      "low": "9999.07",
      "closeTime": 1648057199999,
      "startTime": 1648057140000,
      "close": "9999.07",
      "open": "9999.07",
      "ts": 1648057141081
    }
  }]
}

Trades

Continuous feed of recent trades with default/provided depth.

Stream Response Field Data Type Description
symbol String symbol name
amount String quote units traded
takerSide String trade side (buy, sell)
quantity String base units traded
createTime Long time the trade was created
price String trade price
id Long trade id
ts Long time the record was pushed

Example:

# --> Request
await ws_client_public.subscribe(['trades'], ['BTC_USDT'])

# <-- Receipt Confirmation
{
  "channel": "trades",
  "event": "subscribe",
  "symbols": ["BTC_USDT"]
}

# <-- Stream
{
  "channel": "trades",
  "data": [{
    "symbol": "BTC_USDT",
    "amount": "70",
    "takerSide": "buy",
    "quantity": "4",
    "createTime": 1648059516810,
    "price": "104",
    "id": 1648059516810,
    "ts": 1648059516832
  }]
}

Ticker

Continuous feed of current day ticker data

Stream Response Field Data Type Description
symbol String symbol name
dailyChange String daily change in decimal
high String highest price over the last 24h
amount String quote units traded over the last 24h
quantity String base units traded over the last 24h
tradeCount Integer count of trades
low String lowest price over the last 24h
closeTime Long close time for the 24h interval
startTime Long start time for the 24h interval
close String price at the end time
open String price at the start time
ts Long time the record was pushed

Example:

# --> Request
await ws_client_public.subscribe(['ticker'], ['ETH_USDT'])

# <-- Receipt Confirmation
{
  "channel": "ticker",
  "event": "subscribe",
  "symbols": ["ETH_USDT"]
}

# <-- Stream
{
  "channel": "ticker",
  "data": [{
    "symbol": "ETH_USDT",
    "dailyChange": "0.9428",
    "high": "507",
    "amount": "20",
    "quantity": "3",
    "tradeCount": 11,
    "low": "16",
    "closeTime": 1634062351868,
    "startTime": 1633996800000,
    "close": "204",
    "open": "105",
    "ts": 1648052794867
  }]
}

Example (All Symbols):

# --> Request
await ws_client_public.subscribe(['ticker'], ['all'])

# <-- Receipt Confirmation
{
  "channel": "ticker",
  "event": "subscribe",
  "symbols": ["all"]
}

# <-- Stream
{
  "channel": "ticker",
  "data": [{
    "symbol": "BTC_USDT",
    "dailyChange": "-0.7409",
    "high": "38596.3",
    "amount": "26843.353145272",
    "quantity": "101.00438344",
    "tradeCount": 13,
    "low": "0.8654",
    "closeTime": 1648054681007,
    "startTime": 1647968280000,
    "close": "9999.07",
    "open": "38596.3"
    "ts": 1648054693082
  }]
}

# <-- Stream (continued)
{
  "channel": "ticker",
  "data": [{
    "symbol": "ETH_USDT",
    "dailyChange": "0.9428",
    "high": "507",
    "amount": "20",
    "quantity": "3",
    "tradeCount": 11,
    "low": "16",
    "closeTime": 1634062351868,
    "startTime": 1633996800000,
    "close": "204",
    "open": "105",
    "ts": "1648052794867"
  }]
}

Book

Continuous feed of order book data containing asks and bids with default/provided depth. Default depth is 5. Valid depths are 5, 10, or 20. Order book is updated every 100ms.

Stream Response Field Data Type Description
symbol String symbol name
createTime Long time the record was created
asks List sell orders, in ascending order of price
bids List buy orders, in descending order of price
id Long id of the record (SeqId)
ts Long send timestamp

Example:

# --> Request
await ws_client_public.subscribe(['book'], ['BTC_USDT'])

# --> Request With Depth Parameter
await ws_client_public.subscribe(['book'], ['BTC_USDT'], depth=10)

# <-- Receipt Confirmation
{
  "channel": "book",
  "event": "subscribe",
  "symbols": ["BTC_USDT"]
}

# <-- Stream
{
  "channel": "book",
  "data": [{
    "symbol": "BTC_USDT",
    "createTime": 1648052239156,
    "asks": [],
    "bids": [
      ["40001.5", "2.87"],
      ["39999.4", "1"]
    ],
    "id": 123456,
    "ts": 1648052239192
  },
  …
  {
    "symbol": "BTC_USDT",
    "createTime": 1648052239156,
    "asks": [],
    "bids": [
      ["40001", "2.87"],
      ["39999", "1"]
    ],
    "id": 345678,
    "ts": 1648052239192
  }]
}

Book Level 2

Receive a snapshot of the full 20 level order book. Then, continuously in realtime receive an updated order book when the first 20 levels change.

To maintain a copy of the order book locally: 1. Send a book_lv2 subscription message. 2. Receive a snapshot message from the server. 3. Use an appropriate data structure to store the received book. 4. Receive an incremental order book message (update) from the server and make changes depending on [price, quantity] pair data: * When quantity is positive, update the corresponding price of your order book with this quantity. * When quantity is 0, delete this price from your order book. 5. Receive an order book message (snapshot) from the server, reset your order book data structure to match this new order book.

Note: If id of the last message does not match lastId of the current message then the client has lost connection with the server and must re-subscribe to the channel.

Stream Response Field Data Type Description
symbol String symbol name
asks List sell orders, in ascending order of price
bids List buy orders, in descending order of price
createTime Long time the record was created
lastId Long the id of the previous message
id Long id of the record (SeqId)
ts Long send timestamp

Example:

# --> Request
await ws_client_public.subscribe(['book_lv2'], ['BTC_USDT'])

# <-- Receipt confirmation
{
  "channel": "book_lv2",
  "event": "subscribe",
  "symbols": ["BTC_USDT"]
}

# <-- Stream (snapshot)
{
  "channel": "book_lv2",
  "action": "snapshot",
  "data": [{
    "symbol": "BTC_USDT",
    "asks": [
      ["6.16", "0.6"],
      ["6.17", "1"],
      ["6.18", "1"],
      ...
    ],
    "bids": [
      ["5.65", "0.02"],
      ["5.61", "1.68"],
      ["5.6", "25.38"],
      ...
    ],
    "lastId": 10409,
    "id": 10410,
    "ts": 1652774727337
  }]
}


# <-- Stream (update)
{
  "channel": "book_lv2",
  "action": "update",
  "data": [{
    "symbol": "BTC_USDT",
    "asks": [
      ["6.35", "3"]
    ],
    "bids": [
      ["5.65", "0.02"]
    ],
    "createTime": 1653029116345,
    "lastId": 10410,
    "id": 10421,
    "ts": 1653029116529
  }]
}

Subscribing to Multiple Channels

# --> Request
await ws_client_public.subscribe(['book', 'ticker'], ['BTC_USDT'])

# <-- Receipt Confirmation
{
  "channel": "book",
  "event": "subscribe",
  "symbols": ["BTC_USDT"]
}}

# <-- Receipt Confirmation
{
  "channel": "ticker",
  "event": "subscribe",
  "symbols": ["BTC_USDT"]
}

# <-- Stream
{
  "channel": "book",
  "data": [{
    "symbol": "BTC_USDT",
    "createTime": 1648052239156,
    "asks": [],
    "bids": [
      ["40001.5", "2.87"],
      ["39999.4", "1"]
    ],
    "id": 123456,
    "ts": 1648052239192
  },
  …
  {
    "symbol": "BTC_USDT",
    "createTime": 1648052239156,
    "asks": [],
    "bids": [
      ["40001", "2.87"],
      ["39999", "1"]
    ],
    "id": 345678,
    "ts": 1648052239192
  }]
}
…

# <-- Stream (continued)
{
  "channel": "ticker",
  "data": [{
    "symbol": "BTC_USDT",
    "dailyChange": "-0.7409",
    "high": "38596.3",
    "amount": "26843.353145272",
    "quantity": "101.00438344",
    "tradeCount": 13,
    "low": "0.8654",
    "closeTime": 1648054681007,
    "startTime": 1647968280000,
    "close": "9999.07",
    "open": "38596.3",
    "ts": 1648054693082
  }]
}

# <-- Stream (continued)
{
  "channel": "ticker",
  "data": [{
    "symbol": "ETH_USDT",
    "dailyChange": "0.9428",
    "high": "507",
    "amount": "20",
    "quantity": "3",
    "tradeCount": 11,
    "low": "16",
    "closeTime": 1634062351868,
    "startTime": 1633996800000,
    "close": "204",
    "open": "105",
    "ts": "1648052794867"
  }]
}
Clone this wiki locally