@author: zhaobo,API调试过程中有问题或者有疑问可反馈微信号shaoxiaofeng1118 """ from HuobiDMService import HuobiDM from pprint import pprint #### input huobi dm url URL = '' #### input your access_key and secret_key below: ACCESS_KEY = '' SECRET_KEY = '' dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) #### another account: #dm2 = HuobiDM(URL, "ANOTHER ACCOUNT's ACCESS_KEY", "ANOTHER ACCOUNT's SECRET_KEY") #%% market data api =============== print (u' 获取合约信息 ') pprint (dm.get_contract_info(symbol="BTC", contract_type="quarter")) pprint (dm.get_contract_info(contract_code="BTC181228")) print (u' 获取合约指数信息 ') pprint (dm.get_contract_index("BTC"))
from HuobiDMService import HuobiDM from pprint import pprint import pandas as pd import time #### input huobi dm url URL = 'https://api.hbdm.com' #### input your access_key and secret_key below: ACCESS_KEY = 'f65d11ed-36c9546b-ghxertfvbf-a3941' SECRET_KEY = 'b6e59f8b-89b2d764-8c2950ce-22965' dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) #print (u' 获取K线数据 ') #pprint (dm.get_contract_kline(symbol='BTC_CQ', period='60min', size=750)) def get_data(name='BTC_CQ'): BTC = dm.get_contract_kline(symbol=name, period='60min', size=750) df = pd.DataFrame(BTC['data'][182:537]) df['date'] = df['id'] df['date'] = df['date'].apply(lambda x:time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(x))) df.to_csv(name+'.csv') get_data('BTC_CQ') get_data('XRP_CQ') get_data('LTC_CQ')
@author: zhaobo """ from HuobiDMService import HuobiDM from pprint import pprint #### input huobi dm url URL = 'api.btcgateway.pro' #### input your access_key and secret_key below: ACCESS_KEY = '10f8d42d-49959935-bg2hyw2dfg-faaad' SECRET_KEY = '770611ca-19b79818-737bc1fe-1abe3' dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) #### another account: #dm2 = HuobiDM(URL, "ANOTHER ACCOUNT's ACCESS_KEY", "ANOTHER ACCOUNT's SECRET_KEY") print(dm) #%% market data api =============== #%% trade / account api =============== print (u' 获取用户账户信息 ') pprint (dm.get_contract_account_info()) pprint (dm.get_contract_account_info("BTC"))
class RestClient: def __init__(self): self.__client = HuobiDM('https://api.hbdm.com', 'f2643261-4cfd9d14-ca908a0f-dbye2sf5t7', '34ae74a1-853da993-eac728aa-f812f') # 季度合约K线数据 def get_contract_kline(self, period, start, end, size=150): """ :param symbol BTC_CW, BTC_NW, BTC_CQ , ... :param period: 可选值:{1min, 5min, 15min, 30min, 60min, 4hour, 1day, 1week, 1mon } :param size: [1,2000] :return: """ params = { 'symbol': 'BTC_CQ', 'period': period, 'start': start, 'end': end } if size: params['size'] = size url = self.__url + '/market/history/kline' resp = self.__client.http_get_request(url, params) return resp['data'] # 合约下单 def send_contract_order(self, price, volume, direction, offset): """ 当季合约下单, Args: price: 价格. volume: 委托数量(张). direction: "buy":买 "sell":卖. offset: "open":开 "close":平 order_price_type: 订单报价类型 "limit":限价; "opponent":对手价 Returns: 下单结果. """ print(u' 合约下单BTC ', " price =", price, " volume =", volume, " direction =", direction, " offset =", offset) symbol = 'BTC' contract_type = 'quarter' client_order_id = '' contract_code = '' #client_order_id='' # direction='buy' #offset='open' resp = self.__client.send_contract_order(symbol, contract_type, contract_code, client_order_id, price, volume, direction, offset, lever_rate=5, order_price_type='limit') return resp['data'] # 获取用户持仓信息 def get_contract_position_info(self): resp = self.__client.get_contract_position_info('BTC') return resp['data'] # 获取基差数据 def get_history_base(self): resp = self.__client.get_history_base() return resp['data']
def __init__(self): self.__client = HuobiDM('https://api.hbdm.com', 'f2643261-4cfd9d14-ca908a0f-dbye2sf5t7', '34ae74a1-853da993-eac728aa-f812f')
count = 0 retryCount = 0 id_30 = 0 positionBuyD = 0 positionSellD = 0 positionBuyK = 0 positionSellK = 0 priceD1 = 0 priceD2 = 0 priceKD = 0 while (1): try: dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) kline_1min = (dm.get_contract_kline(symbol='ETH_CQ', period='1min'))['data'] kline_30min = (dm.get_contract_kline(symbol='ETH_CQ', period='30min', size=360))['data'] except: retryCount += 1 if (retryCount == 20): with open("test_account_info.json", "w") as dump_f: json.dump(account_info, dump_f) print('connect ws error!') break continue retryCount = 0
return 1 return 0 with open("test_account_info.json", 'r') as load_f: account_info = json.load(load_f) URL = 'https://www.hbdm.com/' ACCESS_KEY = '' SECRET_KEY = '' count = 0 retryCount = 0 while (1): try: dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) kline_1min = (dm.get_contract_kline(symbol='BTC_CQ', period='1min'))['data'] except: retryCount += 1 if (retryCount == 20): with open("test_account_info.json", "w") as dump_f: json.dump(account_info, dump_f) print('connect ws error!') break continue retryCount = 0 kline = (pd.DataFrame.from_dict(kline_1min))[[ 'id', 'close', 'high', 'low', 'open', 'amount'
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Sep 17 15:48:13 2018 @author: zhaobo """ from HuobiDMService import HuobiDM from pprint import pprint URL = "https://api.hbdm.com" #### input your access_key and secret_key below: ACCESS_KEY = "hrf5gdfghe-d9f5079e-c2cab0ea-b77dd" SECRET_KEY = "a3e807f4-eec81c9e-9d0b1b73-5fa73" dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) #### another account: #dm2 = HuobiDM(URL, "ANOTHER ACCOUNT's ACCESS_KEY", "ANOTHER ACCOUNT's SECRET_KEY") # print (u' 获取K线数据 ') # pprint (dm.get_contract_kline(symbol='BTC_CW', period='60min', size=20)) print (u' 获取市场最近成交记录 ') pprint (dm.get_contract_trade('BTC_CW',10)) # print (u' 批量获取最近的交易记录 ') # pprint (dm.get_contract_batch_trade(symbol='BTC_CW', size=3))
_host = cfg.get('ws', '_host') path = cfg.get('ws', 'path') apiUrl = 'https://api.btcgateway.pro' logger = logging.getLogger("sub") fh = logging.FileHandler("subscribe.log") logger.setLevel(logging.INFO) logger.addHandler(fh) step = 0.3 / 100 #网格大小 # 组建获取请求的url url = protocol + _host + path dm = HuobiDM(apiUrl, accessKey, secretKey) class Message: # 鉴权信息 def _auth(self, auth): # 获取需要签名的信息 authenticaton_data = auth[1] # 获取 secretkey _accessKeySecret = auth[0] # 计算签名Signature authenticaton_data['Signature'] = _sign(authenticaton_data, _accessKeySecret) print(authenticaton_data)
trade_sell_volume = None BuyOpen = None price = '' direction = None offset = None order_price_type = None # 订单报价类型 "limit":限价 "opponent":对手价 "post_only":只做maker单,post only下单只受用户持仓数量限制, # optimal_5:最优5档、optimal_10:最优10档、optimal_20:最优20档,ioc:IOC订单,fok:FOK订单 sell_BuyOpen = False sell_direction = None sell_offset = None sell_order_price_type = None MA_EMA = None Already_SELL_Judged = False time1 = 0 dm = HuobiDM(url, accessKey, secretKey) def BooleanNOT(x): # not x if x: return False else: return True while 1: time6 = time.clock() print(time6 - time1) if 0 < time6 - time1 <= 10: time.sleep(10 - (time6 - time1))
""" Created on Mon Sep 17 15:48:13 2018 @author: zhaobo """ from HuobiDMService import HuobiDM from pprint import pprint URL = "https://api.hbdm.com" #### input your access_key and secret_key below: ACCESS_KEY = '' SECRET_KEY = '' dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) #### another account: #dm2 = HuobiDM(URL, "ANOTHER ACCOUNT's ACCESS_KEY", "ANOTHER ACCOUNT's SECRET_KEY") #%% market data api =============== print (u' 获取合约信息 ') pprint (dm.get_contract_info(symbol="BTC", contract_type="quarter")) pprint (dm.get_contract_info(contract_code="BTC181228")) print (u' 获取合约指数信息 ') pprint (dm.get_contract_index("BTC"))
with open("test_account_info.json", 'r') as load_f: account_info = json.load(load_f) # account_info = {'margin_available':0,'margin_frozen':0,'volume':0,'cost_price':0,'stop_price':0,'direction':0,'firstBuy':0} URL = 'https://api.btcgateway.pro' ACCESS_KEY = '' SECRET_KEY = '' count = 0 retryCount = 0 id_30 = 0 while (1): try: dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) kline_30min = (dm.get_contract_kline(symbol='ETH_CQ', period='30min', size=300))['data'] contract_info = dm.get_contract_info(symbol="ETH", contract_type="quarter")['data'] contract_size = contract_info[0]['contract_size'] contract_account_info = dm.get_contract_account_info("ETH")['data'] account_info['margin_available'] = contract_account_info[0][ 'margin_available'] account_info['margin_frozen'] = contract_account_info[0][ 'margin_position'] contract_position_info = dm.get_contract_position_info("ETH")['data'] if contract_position_info == []: account_info['volume'] = 0
Created on Mon Sep 17 15:48:13 2018 @author: zhaobo """ from HuobiDMService import HuobiDM from pprint import pprint #### input huobi dm url URL = 'https://api.btcgateway.pro' #### input your access_key and secret_key below: ACCESS_KEY = '---' SECRET_KEY = '---' dm = HuobiDM(URL, ACCESS_KEY, SECRET_KEY) #### another account: # dm2 = HuobiDM(URL, "ANOTHER ACCOUNT's ACCESS_KEY", "ANOTHER ACCOUNT's SECRET_KEY") # %% market data api =============== print(u' 获取市场价格 ') priceInfo = dm.get_contract_trade(symbol="XRP_CQ") pprint(priceInfo) if priceInfo['status'] != "ok": print("get price failed") exit(2) currPrice = float(priceInfo['tick']['data'][0]['price'])
offset = 'close' order_split = send_order(dm,'','',contract_code,volume,direction,offset,client_order_id='',price='',lever_rate=lvg,order_price_type='opponent',wait_num=wait_num) trade_price = order_split.trade_volume.sum()/(order_split.trade_volume/order_split.trade_avg_price).sum() # 成交均价 pnl.loc['close'] = [0,0,trade_price] return contract_code,pnl print('>>>>>>>>>>>>>>>>>>>>>>账户设置>>>>>>>>>>>>>>>>>>>>>>') url = 'https://api.hbdm.com' # 火币合约API链接 access_key = '' secrect_key = '' dm = HuobiDM(url, access_key, secrect_key) # 创建账户实例 print('>>>>>>>>>>>>>>>>>>>>>>合约选择>>>>>>>>>>>>>>>>>>>>>>') #### 合约选择(默认季度合约,季度合约存续期是3个月,节约换仓成本) # 注:交割月份的倒数第三个周五16:00结算后会生成新的季度合约,因此在交割月份的最后一个周五前换仓,即平掉旧合约并在新合约上开相同仓位 symb = 'BTC' # BTC ETF ---- BTC_CW, BTC_NW, BTC_CQ , ... type = 'quarter' print('>>>>>>>>>>>>>>>>>>>>>>转账>>>>>>>>>>>>>>>>>>>>>>') ## 法币交易购买USDT,币币交易购买BTC/USDT,BTC由币币账户转入合约账户 ## 法币交易购买BTC,BTC由币币账户转入合约账户 initial_eqt = 0.1 # 初始转入0.1颗币