def __init__(self, init_symbols=False, do_fill_history=False): super().__init__(name='huobi websocket', daemon=True) self.hb = HBWebsocket() self.api = HBRestAPI() self.init_symbols = init_symbols self.logger = logging.getLogger(self.__class__.__name__) self.do_fill_history = do_fill_history
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/8 0008 13:47 # @Author : Hadrianl # @File : example.py # @Contact : [email protected] from huobitrade.service import HBWebsocket, HBRestAPI from huobitrade.handler import DBHandler from huobitrade import setKey, logger import time logger.setLevel('DEBUG') # setKey('access_key', 'secret_key') hb = HBWebsocket() hb.run() time.sleep(1) # run之后连接需要一丢丢时间,sleep一下再订阅 hb.sub_kline('ethbtc', '1min') hb.req_kline('ethbtc', '1min') handler = DBHandler() hb.register_handler(handler) @hb.register_handle_func('market.ethbtc.kline.1min') def handle(msg): print('handle:', msg) # api = HBRestAPI() # print(api.get_timestamp())
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/25 0025 12:39 # @Author : Hadrianl # @File : auth_test.py # @Contact : [email protected] """ 该例子仅限于用来测试鉴权接口是否能鉴权成功 """ from huobitrade.service import HBWebsocket from huobitrade import setKey, logger logger.setLevel('DEBUG') setKey('access_key', 'secret_key') auhb = HBWebsocket(auth=True) auhb.after_auth(auhb.sub_accounts) @auhb.after_auth def init_sub(): auhb.sub_accounts() auhb.sub_orders() auhb.run()
print(topic) # 初始化restapi restapi = HBRestAPI(get_acc=True) print(restapi.get_accounts()) # 请求账户 # 构造异步请求 rep1 = restapi.get_timestamp(_async=True) rep2 = restapi.get_all_last_24h_kline(_async=True) result = restapi.async_request([rep1, rep2]) # 一起发起请求 for r in result: print(r) # 初始化多个ws auhb = HBWebsocket(auth=True) hb = HBWebsocket() hb2 = HBWebsocket() # 注册鉴权或连接后的订阅行为, 断线重连后依然会重新订阅 auhb.after_auth(auhb.sub_accounts) hb.after_open(partial(hb.sub_kline, 'dcreth', '1min')) hb2.after_open(partial(hb2.sub_depth, 'dcreth')) # 把handler与handle_func注册进相应的ws handler = MyHandler(None) # topic为None即订阅全部topic auhb.register_handler(handler) hb.register_handler(handler) hb2.register_handler(handler) @hb2.register_handle_func('market.dcreth.depth.step0') def print_msg(msg):
buy_price = ... # 买入价 api.send_order(api.acc_id, buy_amount, symbol, 'buy-limit', buy_price) if sell_cond: sell_amount = ... sell_price = ... api.send_order(api.acc_id, sell_amount, symbol, 'sell-limit', sell_price) if __name__ == '__main__': from huobitrade.service import HBWebsocket, HBRestAPI from huobitrade import setUrl, setKey setKey('', '') # setUrl('https://api.huobi.pro', 'https://api.huobi.pro') ws = HBWebsocket('api.huobi.br.com') # 生产环境请不要用api.huobi.br.com api = HBRestAPI( get_acc=True) # get_acc为true,初始化时候会获取acount_id中的第一个id并赋值给acc_id属性 @ws.after_open # 连接成功后会调用此函数,一般在这个位置进行初始化订阅 def sub_depth(): ws.sub_kline('dcreth', '1min') ws.run() demo_handler = DemoHandler('dcreth') ws.register_handler(demo_handler)