Documentation

client

Handles Clients in the Coinbase Exchange Library.

class cbexchange.client.APIClient[source]

Base class of all client in the Coinbase Exchange Library.

class cbexchange.client.PaginationClient(api_uri=None, before=True, limit=None, cursor=None)[source]

Handles Pagination https://docs.exchange.coinbase.com/#pagination

This client is iterable in the given direction (before parameter).

Parameters:
  • api_uri (str) – Coinbase Exchange REST API URI.
  • before (bool) – if True goes to the page before (older) else after (newer)
  • limit (int) – number of results per request. Maximum 100. (default 100)
  • cursor – the cursor you want to start at (default latest)
endpoint()[source]

Abstract method - must be overriden. Performs the endpoint operation

get_after_cursor()[source]

Acquires the after cursor.

Returns:the after cursor
get_before_cursor()[source]

Acquires the before cursor.

Returns:the before cursor
is_after()[source]

Returns True if the direction is set to after.

Returns:True if the direction is set to after, otherwise False
Return type:bool
is_before()[source]

Returns True if the direction is set to before.

Returns:True if the direction is set to before, otherwise False
Return type:bool
next()

Iterator function for Python 3.

Returns:the next message in the sequence
Raises:StopIteration – if there are no more messages
set_after()[source]

Sets the direction to after.

set_before()[source]

Sets the direction to before.

class cbexchange.client.RESTClient(api_uri=None)[source]

Base class of all clients using the REST API.

Parameters:api_uri (str) – Coinbase Exchange REST API URI.
API_URI = 'https://api.exchange.coinbase.com'

error

Handles Errors in the Coinbase Exchange Library.

exception cbexchange.error.APIError(response)[source]

Unless otherwise stated, errors to bad requests will respond with HTTP 4xx or status codes. The body will also contain a message parameter indicating the cause. Your language’s http library should be configured to provide message bodies for non-2xx requests so that you can read the message field from the body. Find more here: https://docs.exchange.coinbase.com/#errors

Parameters:response (requests.Response) – HTTP error response
exception cbexchange.error.BadRequestError(response)[source]
exception cbexchange.error.CBExchangeError[source]

All Coinbase Exchange Library related errors extend this class.

exception cbexchange.error.ForbiddenError(response)[source]
exception cbexchange.error.InternalServerError(response)[source]
exception cbexchange.error.NotFoundError(response)[source]
exception cbexchange.error.TooManyRequestsError(response)[source]
exception cbexchange.error.UnauthorizedError(response)[source]
cbexchange.error.get_api_error(response)[source]

Acquires the correct error for a given response.

Parameters:response (requests.Response) – HTTP error response
Returns:the appropriate error for a given response
Return type:APIError

market

The Market Data API is an unauthenticated set of endpoints for retrieving market data. These endpoints provide snapshots of market data.

Find more here: https://docs.exchange.coinbase.com/#market-data

class cbexchange.market.GetTradesPagination(product_id='BTC-USD', api_uri=None, before=True, limit=None, cursor=None)[source]

https://docs.exchange.coinbase.com/#get-trades

endpoint()[source]
class cbexchange.market.MarketClient(api_uri=None)[source]
get_currencies()[source]

https://docs.exchange.coinbase.com/#get-currencies

get_historic_trades(start, end, granularity, product_id='BTC-USD')[source]

https://docs.exchange.coinbase.com/#get-historic-rates

Parameters:
  • start – either datetime.datetime or str in ISO 8601
  • end – either datetime.datetime or str in ISO 8601
Pram int granularity:
 

desired timeslice in seconds

Returns:

desired data

get_product_order_book(level=None, product_id='BTC-USD')[source]

https://docs.exchange.coinbase.com/#get-product-order-book

get_product_ticker(product_id='BTC-USD')[source]

https://docs.exchange.coinbase.com/#get-product-ticker

get_products()[source]

https://docs.exchange.coinbase.com/#get-products

get_stats(product_id='BTC-USD')[source]

https://docs.exchange.coinbase.com/#get-24hr-stats

get_time()[source]

https://docs.exchange.coinbase.com/#time

get_trades(product_id='BTC-USD')[source]

https://docs.exchange.coinbase.com/#get-trades

class cbexchange.market.MarketPaginationClient(api_uri=None, before=True, limit=None, cursor=None)[source]

orderbook

The websocket feed provides real-time updates on orders and trades. These updates can be applied on to a level 2 or 3 order book snapshot to maintain an accurate and up-to-date copy of the exchange order book.

Find more here: https://docs.exchange.coinbase.com/#real-time-order-book

class cbexchange.orderbook.OrderBook(ws_uri=None, api_uri=None, product_id=None)[source]

https://docs.exchange.coinbase.com/#real-time-order-book

Includes a real-time thread that makes sure the order book is up to date. Supports the ‘with’ statment.

The order book format will be:

>>> {
>>>    "asks": {
>>>      "da863862-25f4-4868-ac41-005d11ab0a5f": {
>>>        "price": "295.97",
>>>        "size": "5.72036512"
>>>      },
>>>      ...
>>>    },
>>>    "bids": {
>>>      "3b0f1225-7f84-490b-a29f-0faef9de823a": {
>>>        "price": "295.96",
>>>        "size": "0.05088265"
>>>      },
>>>      ...
>>>    },
>>>    "sequence": 3
>>> }
Parameters:
  • ws_uri (str) – WebSocket URI.
  • api_url (str) – Coinbase Exchange REST API URI.
  • product_id (str) – the product of interest
API_URI = 'https://api.exchange.coinbase.com'
PRODUCT_ID = 'BTC-USD'
WS_URI = 'wss://ws-feed.exchange.coinbase.com'
end()[source]

Makes sure the real-time thread dies and the WebSocket disconnects.

get_order_book()[source]

Returns the Real-Time Order Book.

Returns:the Real-Time Order Book
Return type:dict
pause()[source]

Stops real-time updates until resume is called.

resume()[source]

Resumes real-time updates.

private

Private endpoints are available for order management, and account management. Every private request must be signed using the described authentication scheme.

Find more here: https://docs.exchange.coinbase.com/#private

class cbexchange.private.AccountPagination(account_id, auth, api_uri=None, before=True, limit=None, cursor=None)[source]
class cbexchange.private.CancelAllPagination(product_id, auth, api_uri=None, before=True, limit=None, cursor=None)[source]

https://docs.exchange.coinbase.com/#cancel-all

endpoint()[source]
class cbexchange.private.CoinbaseExchangeAuth(api_key, secret_key, passphrase)[source]

https://docs.exchange.coinbase.com/#authentication

Parameters:
  • api_key (str) – the API Key
  • secret_key (str) – the base64-encoded signature
  • passphrase (str) – the passphrase you specified when creating the API key
class cbexchange.private.GetAccountHistoryPagination(account_id, auth, api_uri=None, before=True, limit=None, cursor=None)[source]

https://docs.exchange.coinbase.com/#get-account-history

endpoint()[source]
class cbexchange.private.GetHoldsPagination(account_id, auth, api_uri=None, before=True, limit=None, cursor=None)[source]

https://docs.exchange.coinbase.com/#get-holds

endpoint()[source]
class cbexchange.private.ListAccountsPagination(auth, api_uri=None, before=True, limit=None, cursor=None)[source]

https://docs.exchange.coinbase.com/#selecting-a-timestamp

endpoint()[source]
class cbexchange.private.ListFillsPagination(auth, api_uri=None, before=True, limit=None, cursor=None)[source]

https://docs.exchange.coinbase.com/#list-fills

endpoint()[source]
class cbexchange.private.ListOrdersPagination(auth, status=None, api_uri=None, before=True, limit=None, cursor=None)[source]

https://docs.exchange.coinbase.com/#list-orders

endpoint()[source]
class cbexchange.private.PrivateClient(auth, api_uri=None)[source]

https://docs.exchange.coinbase.com/#authentication

Parameters:
  • auth (CoinbaseExchangeAuth) – authentication for the Private API
  • api_uri (str) – Coinbase Exchange REST API URI
cancel_all(product_id=None)[source]

https://docs.exchange.coinbase.com/#cancel-all

cancel_order(order_id)[source]

https://docs.exchange.coinbase.com/#cancel-an-order

deposit(amount, coinbase_account_id)[source]

https://docs.exchange.coinbase.com/#depositwithdraw

get_account(account_id)[source]

https://docs.exchange.coinbase.com/#get-an-account

get_account_history(account_id)[source]

https://docs.exchange.coinbase.com/#get-account-history

get_holds(account_id)[source]

https://docs.exchange.coinbase.com/#get-holds

get_order(order_id)[source]

https://docs.exchange.coinbase.com/#get-an-order

get_report_status(report_id)[source]

https://docs.exchange.coinbase.com/#get-report-status

list_accounts()[source]

https://docs.exchange.coinbase.com/#selecting-a-timestamp

list_fills()[source]

https://docs.exchange.coinbase.com/#list-fills

list_orders(status=None)[source]

https://docs.exchange.coinbase.com/#list-orders

new_accounts_report(start_date, end_date, account_id, product_id='BTC-USD', format=None, email=None)[source]

https://docs.exchange.coinbase.com/#create-a-new-report

new_fills_report(start_date, end_date, account_id=None, product_id='BTC-USD', format=None, email=None)[source]

https://docs.exchange.coinbase.com/#create-a-new-report

place_limit_order(side, price, size, product_id='BTC-USD', client_oid=None, stp=None, time_in_force=None, cancel_after=None, post_only=None)[source]

https://docs.exchange.coinbase.com/#orders

place_market_order(side, product_id='BTC-USD', size=None, funds=None, client_oid=None, stp=None)[source]

https://docs.exchange.coinbase.com/#orders

withdraw(amount, coinbase_account_id)[source]

https://docs.exchange.coinbase.com/#depositwithdraw

class cbexchange.private.PrivatePaginationClient(auth, api_uri=None, before=True, limit=None, cursor=None)[source]

websock

The WebSocket Feed provides real-time market data updates for orders and trades.

Find more here: https://docs.exchange.coinbase.com/#websocket-feed

class cbexchange.websock.WSClient(ws_uri=None, ws_type=None, ws_product_id=None)[source]

API Client for Coinbase Exchange WebSocket Feed.

This class starts in a disconnected state so make sure to connect before attempting to receive any messages. When using the ‘with’ statement the client connects and disconnects automatically.

Once connected the client starts a thread which keeps the WebSocket alive using periodic pings. There will be only one keep alive thread per client instance. If the WebSocket connection is somehow lost, the keep alive thread will clean up and exit.

The client is iterable over the messages in the feed:

Example:
>>> from cbexchange.websock import WSClient
>>> client = WSClient()
>>> client.connect()
>>> for message in client:
>>>   print(message)

The client supports the ‘with’ statment:

Example:
>>> from cbexchange.websock import WSClient
>>> with WSClient() as client:
>>>   print(client.receive())
Parameters:
WS_PRODUCT_ID = 'BTC-USD'
WS_TYPE = 'subscribe'
WS_URI = 'wss://ws-feed.exchange.coinbase.com'
connect()[source]

Connects and subscribes to the WebSocket Feed.

connected()[source]

Checks if we are connected to the WebSocket Feed.

Returns:True if connected, otherwise False
Return type:bool
disconnect()[source]

Disconnects from the WebSocket Feed.

next()

Iterator function for Python 3.

Returns:the next message in the sequence
Return type:dict
Raises:StopIteration – if the WebSocket is not connected
receive()[source]

Receive the next message in the sequence.

Returns:the next message in the sequence, None if not connected
Return type:dict