Skip to main content

huobi_api for python

Project description

火币API的Python版

  • websocket封装成HBWebsocket类,用run开启连接线程
  • HBWebsocket通过注册Handler的方式来处理数据,消息通过pub_msg来分发到个各topic下的Handler线程来处理
  • restful api基本参照火币网的demo封装成HBRestAPI
  • 没有test和debug,估计含有巨量的BUG,慎用!

Installation

pip install huobitrade

Usage

WebSocket API

from huobitrade.service import HBWebsocket

hb = HBWebsocket()  # 可以填入url参数,默认是https://api.huobi.br.com
hb.run()  # 开启websocket进程

# --------------------------------------------
hb.sub_kline('ethbtc', '1min')  # 订阅数据
@hb.register_handle_func('market.ethbtc.kline.1min')  # 注册一个处理函数,最好的处理方法应该是实现一个handler
def handle(msg):
    print('handle:', msg)

hb.unregister_handle_func(handle, 'market.ethbtc.kline.1min')  # 释放处理函数

Restful API

  • restapi需要先用setKey设置密钥
  • 默认交易和行情url都是https://api.huobi.br.com (调试用),实盘要设置url用from huobitrade import setUrl
from huobitrade.service import HBRestAPI
from huobitrade import setKey

setKey('your acess_key', 'you secret_key')
api = HBRestAPI()
print(api.get_timestamp())

Message Handler

  • handler是用来处理websocket的原始返回消息的,通过继承basehandler实现handle函数以及注册进HBWebsocket相关的topic来使用
from huobitrade.handler import baseHandler

class MyHandler(baseHandler):
    def __init__(self, *args, **kwargs):
        baseHandler.__init__(self, name='just Thread name')

    def handle(self, msg):  # 实现handle来处理websocket推送的msg
        print(msg)


handler = MyHandler()
hb.register_handler(handler, 'market.ethbtc.kline.1min')  # 通过register来把handler注册到相应的topic
  • 内置实现了一个mongodb的DBHandler
from huobitrade.handler import DBHandler
handler = DBHandler()
hb.register_handler(handler, 'market.ethbtc.kline.1min')

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

huobitrade-0.1.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

huobitrade-0.1.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file huobitrade-0.1.2.tar.gz.

File metadata

  • Download URL: huobitrade-0.1.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for huobitrade-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2635fa37fdcbeefdc72343b6e36ec5680a68817ffcba8f9f17c618cfafc57caa
MD5 0db792533c7f00371160a142773774ce
BLAKE2b-256 d2bdadda7adfa5b8bbaa297c3383a790d6bd7fa187277246a33b8dd2525a63f3

See more details on using hashes here.

File details

Details for the file huobitrade-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for huobitrade-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 38177ad11dca1509defa839efecf4d05742b59928f0e680ba9bee86709d50704
MD5 0940f9d4445035e5c9435ae73e5ae64e
BLAKE2b-256 693197553fff5d3527a3cfb06809f7e41820b6e3cf2b731f70ce8395d3ec9dec

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page