Skip to main content

Asyncronous Python REST and WebSocket Clients for the Coinbase Pro virtual currency trading platform.

Project description

CoPrA

Asyncronous Python REST and WebSocket Clients for Coinbase Pro


Version Build Status Documentation Status

Quick Links: Documentation - Source Code - PyPi

Introduction

The CoPrA (Coinbase Pro Async) package provides asyncronous REST and WebSocket clients written in Python for use with the Coinbase Pro digital currency trading platform. To learn about Coinbase Pro’s REST and WebSocket APIs as well as how to obtain an API key for authentication to those services, please see Coinbase Pro’s API documentation.

CoPrA Features

  • compatible with Python 3.5 or greater

  • utilizes Python’s asyncio concurrency framework

  • open source (MIT license)

REST Features

  • Asyncronous REST client class with 100% of the account management, trading, and market data functionality offered by the Coinbase Pro REST API.

  • supports user authentication

  • built on aiohttp, the asynchronous HTTP client/server framework for asyncio and Python

WebSocket Features

  • Asyncronous WebSocket client class with callback hooks for managing every phase of a Coinbase Pro WebSocket session

  • supports user authentication

  • built on Autobahn|Python, the open-source (MIT) real-time framework for web, mobile & the Internet of Things.

Examples

REST

Without a Coinbase Pro API key, copra.rest.Client has access to all of the public market data that Coinbase makes available.

# 24hour_stats.py

import asyncio

from copra.rest import Client

loop = asyncio.get_event_loop()

client = Client(loop)

async def get_stats():
    btc_stats = await client.get_24hour_stats('BTC-USD')
    print(btc_stats)

loop.run_until_complete(get_stats())
loop.run_until_complete(client.close())

Running the above:

$ python3 24hour_stats.py
{'open': '3914.96000000', 'high': '3957.10000000', 'low': '3508.00000000', 'volume': '37134.10720409', 'last': '3670.06000000', 'volume_30day': '423047.53794129'}

In conjunction with a Coinbase Pro API key, copra.rest.Client can be used to trade cryptocurrency and manage your Coinbase pro account. This example also shows how copra.rest.Client can be used as a context manager.

# btc_account_info.py

import asyncio

from copra.rest import Client

KEY = YOUR_API_KEY
SECRET = YOUR_API_SECRET
PASSPHRASE = YOUR_API_PASSPHRASE

BTC_ACCOUNT_ID = YOUR_BTC_ACCOUNT_ID

loop = asyncio.get_event_loop()

async def get_btc_account():
    async with Client(loop, auth=True, key=KEY,
                      secret=SECRET, passphrase=PASSPHRASE) as client:

        btc_account = await client.account(BTC_ACCOUNT_ID)
        print(btc_account)

loop.run_until_complete(get_btc_account())

Running the above:

$ python3 btc_account_info.py
{'id': '1b121cbe-bd4-4c42-9e31-7047632fc7c7', 'currency': 'BTC', 'balance': '26.1023109600000000', 'available': '26.09731096', 'hold': '0.0050000000000000', 'profile_id': '151d9abd-abcc-4597-ae40-b6286d72a0bd'}

WebSocket

While copra.websocket.Client is meant to be overridden, but it can be used ‘as is’ to test the module through the command line.

# btc_heartbeat.py

import asyncio

from copra.websocket import Channel, Client

loop = asyncio.get_event_loop()

ws = Client(loop, Channel('heartbeat', 'BTC-USD'))

try:
    loop.run_forever()
except KeyboardInterrupt:
    loop.run_until_complete(ws.close())
    loop.close()

Running the above:

$ python3 btc_heartbeat.py
{'type': 'subscriptions', 'channels': [{'name': 'heartbeat', 'product_ids': ['BTC-USD']}]}
{'type': 'heartbeat', 'last_trade_id': 45950713, 'product_id': 'BTC-USD', 'sequence': 6254273323, 'time': '2018-07-05T22:36:30.823000Z'}
{'type': 'heartbeat', 'last_trade_id': 45950714, 'product_id': 'BTC-USD', 'sequence': 6254273420, 'time': '2018-07-05T22:36:31.823000Z'}
{'type': 'heartbeat', 'last_trade_id': 45950715, 'product_id': 'BTC-USD', 'sequence': 6254273528, 'time': '2018-07-05T22:36:32.823000Z'}
{'type': 'heartbeat', 'last_trade_id': 45950715, 'product_id': 'BTC-USD', 'sequence': 6254273641, 'time': '2018-07-05T22:36:33.823000Z'}
{'type': 'heartbeat', 'last_trade_id': 45950715, 'product_id': 'BTC-USD', 'sequence': 6254273758, 'time': '2018-07-05T22:36:34.823000Z'}
{'type': 'heartbeat', 'last_trade_id': 45950720, 'product_id': 'BTC-USD', 'sequence': 6254273910, 'time': '2018-07-05T22:36:35.824000Z'}
.
.
.

A Coinbase Pro API key allows copra.websocket.Client to authenticate with the Coinbase WebSocket server giving you access to feeds specific to your user account.

# user_channel.py

import asyncio

from copra.websocket import Channel, Client

KEY = YOUR_API_KEY
SECRET = YOUR_API_SECRET
PASSPHRASE = YOUR_API_PASSPHRASE

loop = asyncio.get_event_loop()

channel = Channel('user', 'LTC-USD')

ws = Client(loop, channel, auth=True, key=KEY, secret=SECRET, passphrase=PASSPHRASE)

try:
    loop.run_forever()
except KeyboardInterrupt:
    loop.run_until_complete(ws.close())
    loop.close()

Running the above:

$ python3 user_channel.py
{'type': 'subscriptions', 'channels': [{'name': 'user', 'product_ids': ['LTC-USD']}]}
{'type': 'received', 'order_id': '42d2677d-0d37-435f-a776-e9e7f81ff22b', 'order_type': 'limit', 'size': '50.00000000', 'price': '1.00000000', 'side': 'buy', 'client_oid': '00098b59-4ac9-4ff8-ba16-bd2ef673f7b7', 'product_id': 'LTC-USD', 'sequence': 2311323871, 'user_id': '642394321fdf8242c4006432', 'profile_id': '039ee148-d490-44f9-9aed-0d1f6412884', 'time': '2018-07-07T17:33:29.755000Z'}
{'type': 'open', 'side': 'buy', 'price': '1.00000000', 'order_id': '42d2677d-0d37-435f-a776-e9e7f81ff22b', 'remaining_size': '50.00000000', 'product_id': 'LTC-USD', 'sequence': 2311323872, 'user_id': '642394321fdf8242c4006432', 'profile_id': '039ee148-d490-44f9-9aed-0d1f6412884', 'time': '2018-07-07T17:33:29.755000Z'}
.
.
.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the MIT License - see the LICENSE file for details

Authors

Tony Podlaski - http://www.neuraldump.net

See also the list of contributers who participated in this project.

Contributing

Please read CONTRIBUTING.rst for details on our code of conduct, and the process for submitting pull requests to us.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.1.0 (2018-07-06)

  • First release on PyPI.

0.2.0 (2018-07-07)

  • Added Client authentication.

0.3.0 (2018-07-09)

  • Added reconnect option to Client.

0.4.0 (2018-07-10)

  • Added subscribe and unsubscribe methods to Client.

1.0.0 (2018-07-12)

  • Added full documentation of the CoPrA API.

1.0.1 (2018-07-12)

  • Fixed typos in the documentation.

1.0.2 (2018-07-12)

  • Added Examples page to the documentation.

1.0.3 (2018-07-16)

  • More documentation typos fixed.

1.0.4 - 1.0.5 (2018-07-17)

  • Non-API changes.

1.0.6 (2018-08-19)

  • Updated Autobahn requirement to 18.8.1

1.0.7 (2018-08-19)

  • Modified Travis config to test against Python 3.7.

1.1.0 (2018-11-27)

  • Added REST client.

1.1.2 (2018-12-01)

  • Updated documentation formatting.

1.2.0 (2019-01-04)

  • Created copra.rest package and moved old copra.rest module to copra.rest.client.

  • Created copra.websocket package and moved old copra.websocket module to copra.websocket.client.

  • Add imports to copra.rest.__init__ and copra.websocket.__init__ so that classes and attributes can still be imported as they were before.

  • Rewrote and completed unit tests from copra.websocket.

1.2.5 (2019-01-05)

  • Updated copra.websocket.client unit tests to ignore those that are incompatible with Python 3.5 due to Mock methods that were not yet implemented.

1.2.6 (2019-01-07)

  • Updated the REST client to attach an additional query string parameter to all GET requests. The parameter, ‘no-cache’, is a timestamp and ensures that the Coinbase server responds to all GET requests with fresh and not cached content.

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

CoinWebsock-1.3.2.tar.gz (58.2 kB view details)

Uploaded Source

File details

Details for the file CoinWebsock-1.3.2.tar.gz.

File metadata

  • Download URL: CoinWebsock-1.3.2.tar.gz
  • Upload date:
  • Size: 58.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.9

File hashes

Hashes for CoinWebsock-1.3.2.tar.gz
Algorithm Hash digest
SHA256 1e98db2ac23ac6644401a9af4369d91c2503022d7fd40438bfced411c3bd6076
MD5 eabe74bb2bc95b445db2fb93b9c82d3c
BLAKE2b-256 a7dbe5d6716b96d8ef70b94697db43f8de2d9b9a06853cf4fafacca87fa95a0f

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