Cryptowatch API的Python绑定。加密货币市场、资产、工具和交换数据。
cryptowatch-sdk的Python项目详细描述
Cryptowatch Python SDK
cryptowatchpython库提供了从用Python语言编写的应用程序中访问Cryptowatch API的方便方法。在
它包括以下功能:
- 将API响应自动序列化为Python对象
- 对protobuf消息进行透明处理的Websocket客户端
- 自动从
~/.cw/credentials.yml
配置文件读取API凭据 - API特定问题的自定义异常(例如:请求允许)
- API连接丢失时的智能退出重试
安装
pip install cryptowatch-sdk
注:
^{pip uninstall cryptowatch
将其删除。在
正确的库名是cryptowatch-sdk
。在
示例
显示了所有在本周蜡烛上已经上涨至少5%的德国股市。在
^{pr2}$要求
- python v3.7+
- 请求v0.8.8+
- 棉花糖v3.2.2+
- pyyaml v5.1.2+
- websocket客户端v0.56+
- 协议v3.11.3+
API认证
使用凭证文件将允许您验证您的请求,并授予您帐户层(免费、基本或专业)的API访问权限。在
设置凭证文件
- 在
从your account生成Cryptowatch API密钥
在 - 在
通过按以下顺序运行在计算机上创建凭据文件:
2.1
mkdir $HOME/.cw
2.2
在echo "apikey: 123" > $HOME/.cw/credentials.yml
(其中123
是您的20位公钥) - 在
用
在cat $HOME/.cw/credentials.yml
验证您是否看到如下内容(123
是您的公钥):
apikey: 123
只要在脚本中运行import cryptowatch
,SDK就会读取您的公钥。在
使用
REST API
importcryptowatchascw# Set your API Key, it is by default read from your ~/.cw/credentials.yml filecw.api_key="123"# Assetscw.assets.list()cw.assets.get("BTC")# Exchangescw.exchanges.list()cw.exchanges.get("KRAKEN")# Instrumentscw.instruments.list()cw.instruments.get("BTCUSD")# Marketscw.markets.list()# Returns list of all markets on all exchangescw.markets.list("BINANCE")# Returns all markets on Binance# Returns market summary (last, high, low, change, volume)cw.markets.get("KRAKEN:BTCUSD")# Return market candlestick info (open, high, low, close, volume) on some timeframescw.markets.get("KRAKEN:BTCUSD",ohlc=True,periods=["4h","1h","1d"])# Returns market last tradescw.markets.get("KRAKEN:BTCUSD",trades=True)# Return market current orderbookcw.markets.get("KRAKEN:BTCUSD",orderbook=True)# Return market current orderbook liquiditycw.markets.get("KRAKEN:BTCUSD",liquidity=True)
您可以访问通过_http_response
属性接收的原始HTTP响应,该属性是^{
importcryptowatchascwbitcoin=cw.assets.get('btc')print(bitcoin._http_response)
Websocket
importcryptowatchascw# Set your API Keycw.api_key="123"# Subscribe to resources (https://docs.cryptowat.ch/websocket-api/data-subscriptions#resources)cw.stream.subscriptions=["markets:*:trades"]# What to do on each trade updatedefhandle_trades_update(trade_update):""" trade_update follows Cryptowatch protocol buffer format: https://github.com/cryptowatch/proto/blob/master/public/markets/market.proto """market_msg=">>> Market#{} Exchange#{} Pair#{}: {} New Trades".format(trade_update.marketUpdate.market.marketId,trade_update.marketUpdate.market.exchangeId,trade_update.marketUpdate.market.currencyPairId,len(trade_update.marketUpdate.tradesUpdate.trades),)print(market_msg)fortradeintrade_update.marketUpdate.tradesUpdate.trades:trade_msg="\tID:{} TIMESTAMP:{} TIMESTAMPNANO:{} PRICE:{} AMOUNT:{}".format(trade.externalId,trade.timestamp,trade.timestampNano,trade.priceStr,trade.amountStr,)print(trade_msg)cw.stream.on_trades_update=handle_trades_update# Start receivingcw.stream.connect()# Call disconnect to close the stream connection# cw.stream.disconnect()
请参见this script了解更多流式处理示例。在
将protobuf消息转换为JSON
如果需要将protobuf消息转换为JSON,可以使用MessageToJson
进行转换。参见以下示例:
fromgoogle.protobuf.json_formatimportMessageToJsonimportcryptowatchascw# Set your API Keycw.api_key="123"# Subscribe to resources (https://docs.cryptowat.ch/websocket-api/data-subscriptions#resources)cw.stream.subscriptions=["markets:*:trades"]# What to do on each trade updatedefhandle_trades_update(trade_update):""" trade_update follows Cryptowatch protocol buffer format: https://github.com/cryptowatch/proto/blob/master/public/markets/market.proto """MessageToJson(trade_update)cw.stream.on_trades_update=handle_trades_update# Start receivingcw.stream.connect()
日志
可以通过Python的logging
模块启用日志记录:
importlogginglogging.basicConfig()logging.getLogger("cryptowatch").setLevel(logging.DEBUG)
CLI
该库公开了一个名为cryptowatch
的简单实用程序,用于返回最后的市场价格。在
默认情况下,它返回Kraken的BTCUSD市场
> cryptowatch
7425.0
添加另一个Kraken market以返回该市场的最新价格
> cryptowatch btceur
6758.1
您还可以指定您自己的交换机
> cryptowatch binance:ethbtc
0.020359
当市场不存在时,将设置1
的返回码(0
否则):
> cryptowatch binance:nosuchmarketusd
> echo $?
1
测试
单元测试位于tests文件夹下,使用pytest
,使用以下命令运行它们:
make test
向Cryptowatch API发送真实HTTP请求的集成测试可以使用以下工具运行:
make test-http-real
发展
测试和开发依赖项在requirements.txt文件中,安装时使用:
pip install -r requirements.txt
代码库使用Blacklinter,使用以下命令运行它:
make lint
许可证
- 项目
标签: