CEX API - LabMazurokCom/Blockchain GitHub Wiki

Intro

Website: https://cex.io/

Documentation: https://cex.io/rest-api

Limits: UNKNOWN.

Fee used in bot: 0.25% (taker for monthly volume less than 5 BTC).

Order book

https://cex.io/api/order_book/BTC/USD/?depth=50

If limit is omitted then all orders will be returned.

Response: JSON

{
  "timestamp": 1513173506,
  "bids": [
      [7572.3, 0.04],
      [7572.0, 2.401],
      ...
  ],
  "asks": [
      [7603.3, 2.08128],
      [7603.4, 0.3],
      ...
  ],
  "pair": "BTC:USD",
  "id": 158217212,
  "sell_total": "1299.73578729",
  "buy_total": "10006393.37"
}
Field Type Meaning
timestamp number UNIX timestamp
bids [number]
asks [number]
pair string Pair name symbol1:symbol2
id integer Incremental version id of order-book snapshot, may be used to check if order-book changed
sell_total string Total available in symbol1 (e.g. BTC)
buy_total string Total available in symbol2 (e.g. USD)

Ticker

https://cex.io/api/ticker/BTC/USD

Response: JSON

{
    'timestamp': '1523544161',
    'low': '6734',
    'high': '7950.8',
    'last': '7621.7',
    'volume': '1669.39889875',
    'volume30d': '36675.76574049',
    'bid': 7617.8,
    'ask': 7620.8
}

Order types

Limit order

A limit order is placed to buy or sell a pre-defined amount of cryptocurrency at a set price. Execution of this order type is not guaranteed until a price requested by the placing user can be matched with an opposite order from another user. Until full execution, the order may be canceled by the user at any time.

These orders may be partially completed if there is currently not sufficient volume at a matching price to cover the entire order. The remainder of the order will be left open until closed by another matching order. The remainder of the order may be canceled as well, returning the remainder to user's balance.

The executed part of a limit order may not be reversed or returned.

Market (immediate) order

Market order (instant order) involves buying or selling cryptocurrency immediately at the best available price. Selling with a market order is the same as Limit Order, except the best available price is used automatically. Market order is executed instantly in most cases (only a complete absence of orders in the order book will leave it hanging) and cannot be reversed.

Currency limits: https://cex.io/api/currency_limits

Private API request example

import requests
import hashlib
import hmac
import time

id='ID'
api_key='API_KEY'
api_secret=b'API_SECRET'

nonce = int(round(time.time() * 1000))

params = str(nonce) + id + api_key

params = bytes(params, encoding='utf-8')

H = hmac.new(api_secret, params, digestmod = hashlib.sha256)
sign = H.hexdigest().upper()

print(sign)
print(' ' + sign[1:-1] + ' ')
print(len(sign[1:-1]))

body = {
    "key": api_key,
    "signature": sign,
    "nonce": str(nonce)
}

endpoint = "https://cex.io/api/"
req = "balance/"

r = requests.post(endpoint + req, data=body)

print(r.text)

Place order

https://cex.io/api/place_order/{symbol1}/{symbol2}

Parameters

Position Name Type Description
path symbol1 string The first currency code
path symbol2 string The second currency code
body body PlaceOrderRequest

Body example

{
  "key": "HLixH1aYdOIWOoRyoHkFpHlawrM",
  "signature": "0BDEDB9339DCE83D46F09A1527663762B52C8B12BCD1910E685BEAFEEABECFF9",
  "nonce": "1513175715",
  "type": "buy",
  "amount": 12,
  "price": 1155.67
}

Body parameters

Name Type Description
key string Your own API key. To get an API key, go to Account -> API Access Tab. Set permissions and click "Generate key".
signature string An HMAC-SHA256 encoded message containing - a nonce, user ID and API key. The HMAC-SHA256 code must be generated using a secret key that was generated with your API key. This code must be converted to its hexadecimal representation (64 uppercase characters).
nonce string The nonce is a regular integer number. It must be increasing with every request you make. Read more about it here. Example - if you set nonce to 1 in your first request, you must set it to at least 2 in your second request. You are not required to start with 1. A common practice is to use unix time for that parameter.
order_type string Probably not used. See example
type string Order type
amount number amount
price string price

Responses

Code 200: OK

Schema: ShortOrder (some fields omitted)

Name Type Description
id string order id
time string timestamp
type string Order type
price string price
amount string amount
pending string pending amount (if partially executed)
symbol1 string Currency code
symbol2 string Currency code
symbol1Amount string
symbol2Amount string

Examples

application/json

{
  "complete": false,
  "id": "89067468",
  "time": 1512054972480,
  "pending": "12.00000000",
  "amount": "12.00000000",
  "type": "buy",
  "price": "1155.67"
}

application/json, market order

{
  "symbol2Amount": "10000",
  "symbol1Amount": "19970000",
  "time": 1506615736816,
  "message": "Your order has been completed. Bought 0.19970000 BTC for 100.00 USD",
  "type": "buy",
  "id": "88640269"
}

Cancel order

https://cex.io/api/cancel_order/