print (u' 获取市场最近成交记录 ') pprint (dm.get_contract_trade('BTC_CW')) print (u' 批量获取最近的交易记录 ') pprint (dm.get_contract_batch_trade(symbol='BTC_CW', size=3)) #%% trade / account api =============== print (u' 获取用户账户信息 ') pprint (dm.get_contract_account_info()) pprint (dm.get_contract_account_info("BTC")) print (u' 获取用户持仓信息 ') pprint (dm.get_contract_position_info()) pprint (dm.get_contract_position_info("BTC")) print (u' 合约下单 ') pprint(dm.send_contract_order(symbol='', contract_type='', contract_code='BTC181228', client_order_id='', price=10000, volume=1, direction='sell', offset='open', lever_rate=5, order_price_type='limit')) print (u' 合约批量下单 ') orders_data = {'orders_data': [ {'symbol': 'BTC', 'contract_type': 'quarter', 'contract_code':'BTC181228', 'client_order_id':'', 'price':10000, 'volume':1, 'direction':'sell', 'offset':'open', 'leverRate':5, 'orderPriceType':'limit'}, {'symbol': 'BTC','contract_type': 'quarter',
print (u' 获取市场最近成交记录 ') pprint (dm.get_contract_trade('BTC_CW')) print (u' 批量获取最近的交易记录 ') pprint (dm.get_contract_batch_trade(symbol='BTC_CW', size=3)) #%% trade / account api =============== print (u' 获取用户账户信息 ') pprint (dm.get_contract_account_info()) pprint (dm.get_contract_account_info("BTC")) print (u' 获取用户持仓信息 ') pprint (dm.get_contract_position_info()) pprint (dm.get_contract_position_info("BTC")) print (u' 合约下单 ') pprint(dm.send_contract_order(symbol='', contract_type='', contract_code='BTC181228', client_order_id='', price=50, volume=1, direction='buy', offset='open', lever_rate=5, order_price_type='limit')) # {'data': {'order_id': 42652161}, 'status': 'ok', 'ts': 1537270256575} print (u' 合约批量下单 ') orders_data = {'orders_data': [ {'symbol': 'BTC', 'contract_type': 'quarter', 'contract_code':'BTC181228', 'client_order_id':'', 'price':1, 'volume':1, 'direction':'buy', 'offset':'open', 'leverRate':5, 'orderPriceType':'limit'},
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']
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 account_info['direction'] = 0 last_price = kline_30min[-1]['close'] else: account_info['volume'] = contract_position_info[0]['available'] account_info['direction'] = contract_position_info[0]['direction'] last_price = contract_position_info[0]['last_price'] except: retryCount += 1 if (retryCount == 20): qqEmail('info_get is error!') with open("test_account_info.json", "w") as dump_f: json.dump(account_info, dump_f)