from huobi import RequestClient from huobi.constant.test import * from huobi.model import * from huobi.base.printobject import PrintMix request_client = RequestClient(api_key=g_api_key, secret_key=g_secret_key) print("\n==============test case 1===============\n") result = request_client.get_open_orders(symbol="btcusdt", account_type=AccountType.SPOT, direct="next") PrintMix.print_data(result) print("\n==============test case 2===============\n") result = request_client.get_open_orders(symbol="xrpusdt", account_type=AccountType.MARGIN, direct="prev") PrintMix.print_data(result) print("\n==============test case 3===============\n") result = request_client.get_open_orders(symbol="xrpusdt", account_type=AccountType.SUPER_MARGIN, direct="prev") PrintMix.print_data(result)
class OrderRouter(object): def __init__(self, apikey, secretkey, password): self._api = RequestClient(api_key=apikey, secret_key=secretkey) self.account_type = AccountType.SPOT def _format_instrument_id(self, instrument_id): return instrument_id.lower().replace('-', '') def _decode_type(self, order_type): if not order_type: return None, None tmp = order_type.split('-') side = tmp[0] o_type = tmp[1] if side not in ['buy', 'sell']: side = None if o_type not in ['limit', 'market', 'ioc']: o_type = None return side, o_type def _decode_state(self, state): if state == 'submitted': return 'open' return state def _timestamp_to_datetime(self, timestamp): return datetime.datetime.fromtimestamp((timestamp + 28800000) / 1000) def _format_order(self, instrument_id, order_info): def fill_obj(o): side, o_type = self._decode_type(o.order_type) return { 'instrument_id': instrument_id, 'order_id': str(o.order_id), 'client_oid': '', 'price': o.price, 'size': o.amount, 'timestamp': o.created_timestamp, 'finished_timestamp': o.finished_timestamp, 'finished_datetime': self._timestamp_to_datetime(o.finished_timestamp), 'canceled_timestamp': o.canceled_timestamp, 'datetime': self._timestamp_to_datetime(o.created_timestamp), 'filled_size': o.filled_amount, 'filled_notional': o.filled_cash_amount, 'side': side, 'type': o_type, 'state': o.state, 'status': self._decode_state(o.state), 'created_at': datetime.datetime.now(), 'updated_at': datetime.datetime.now() } if isinstance(order_info, list): ret = [] for o in order_info: ret.append(fill_obj(o)) else: ret = fill_obj(order_info) return ret def check_position(self, symbol): psss def submit_spot_order(self, client_oid, type, side, instrument_id, price, size, notional, order_type='0', timeout=20, wait_flag=False): instrument_id = self._format_instrument_id(instrument_id) order_type = '{}-{}'.format(param['side'], param['type']) order_id = self._api.create_order(instrument_id, self.account_type, order_type, size, price) return {'order_id': order_id} # take orders # 市价单 # params = [ # {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"market"," size ":"0.001"," notional ":"10001","margin_trading ":"1"}, # {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"limit"," size ":"0.001","notional":"10002","margin_trading ":"1"} # ] # 限价单 # params = [ # {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"limit","size":"0.001","price":"10001","margin_trading ":"1"}, # {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"limit","size":"0.001","price":"10002","margin_trading ":"1"} # ] def submit_orders(self, params): ret = [] for param in params: instrument_id = self._format_instrument_id(param['instrument_id']) order_type = '{}-{}'.format(param['side'], param['type']) try: order_id = self._api.create_order(instrument_id, self.account_type, order_type, param['size'], param['price']) order_id = str(order_id) print('submit order id: {}, order_type: {}, price: {}'.format( order_id, order_type, param['price'])) ret.append({ 'order_id': order_id, 'side': param['side'], 'price': param['price'] }) except Exception as e: print('submit_orders err: {}'.format(e)) print('account type:{}, order_type:{}, size:{}, price:{}'.format(self.account_type, order_type, param['size'], param['price'])) time.sleep(0.01) return ret def get_order_info(self, order_id, instrument_id, client_oid=''): order_info = self._api.get_order( self._format_instrument_id(instrument_id), order_id) return self._format_order(instrument_id, order_info) def cancel_order(self, order_id, instrument_id): return self._api.cancel_order( self._format_instrument_id(instrument_id), order_id) # revoke orders # params example: # [ # {"instrument_id":"btc-usdt","order_ids":[1600593327162368,1600593327162369]}, # {"instrument_id":"ltc-usdt","order_ids":[243464,234465]} # ] def cancel_orders(self, params): ret = [] for param in params: instrument_id = self._format_instrument_id(param['instrument_id']) ret.append( self._api.cancel_orders(instrument_id, param['order_ids'])) time.sleep(0.01) return ret def get_orders_pending(self, instrument_id): ret = [] open_orders = self._api.get_open_orders( self._format_instrument_id(instrument_id), self.account_type, size=100) return self._format_order(instrument_id, open_orders) def get_kline(self, instrument_id, start, end, granularity): pass def get_ticker(self, instrument_id): last_trade = self._api.get_last_trade( self._format_instrument_id(instrument_id)) return {'last': last_trade.price} def get_coin_info(self, instrument_id='all'): # TODO 把sdk方法get_exchange_info的symobl和currency分离出来 exchange_info = self._api.get_exchange_info() symbol_list = exchange_info.symbol_list for sl in symbol_list: if sl.symbol == self._format_instrument_id(instrument_id): return { 'instrument_id': instrument_id, 'base_currency': sl.base_currency, 'quote_currency': sl.quote_currency, 'tick_size': sl.price_precision, 'size_increment': sl.amount_precision } return None def get_account_info(self): account_balances = self._api.get_account_balance_by_account_type( self.account_type) details_obj = {} for b in account_balances.balances: if b.balance: if b.currency not in details_obj: if b.balance_type == BalanceType.TRADE: details_obj[b.currency] = { 'currency': b.currency.upper(), 'frozen': 0, 'balance': b.balance, 'available': b.balance } elif b.balance_type == BalanceType.FROZEN: details_obj[b.currency] = { 'currency': b.currency.upper(), 'frozen': b.balance, 'balance': b.balance, 'available': 0 } else: if b.balance_type == BalanceType.TRADE: details_obj[b.currency]['available'] += b.balance details_obj[b.currency]['balance'] += b.balance elif b.balance_type == BalanceType.FROZEN: details_obj[b.currency]['frozen'] += b.balance details_obj[b.currency]['balance'] += b.balance return list(details_obj.values()) def get_coin_account_info(self, symbol): balances = self._api.get_account_balance_by_account_type( self.account_type) ret = {'currency': symbol, 'frozen': 0, 'balance': 0, 'available': 0} for b in balances.get_balance(symbol.lower()): if b.balance_type == BalanceType.TRADE: ret['balance'] += b.balance ret['available'] = b.balance elif b.balance_type == BalanceType.FROZEN: ret['balance'] += b.balance ret['frozen'] = b.balance return ret def get_coin_balance(self, symbol): return self.get_coin_account_info(symbol)['balance'] def get_coin_available(self, symbol): return self.get_coin_account_info(symbol)['available'] def get_oneday_orders(self, instrument_id, stime, state): if stime > datetime.datetime.now(): return [] start_date = stime.strftime('%Y-%m-%d') end_date = start_date # (stime + datetime.timedelta(days=1)).strftime('%Y-%m-%d') orders = [] start_id = None size = 100 format_instrument_id = self._format_instrument_id(instrument_id) while True: order = self._api.get_historical_orders( format_instrument_id, state, start_date=start_date, end_date=end_date, start_id=start_id, size=size) #print('Fetched orders, date: {}, pair: {}, total: {}'.format(start_date, instrument_id, len(order))) format_orders = self._format_order(instrument_id, order) orders += format_orders # print('Fetched orders, pair: %s, total: %d', instrument_id, len(format_orders)) if len(format_orders) < size: break else: start_id = min([o['order_id'] for o in format_orders]) time.sleep(0.5) return orders def get_orders(self, instrument_id, stime, etime, state): date_list = pd.date_range( stime.replace(hour=0, minute=0, second=0, microsecond=0), etime.replace(hour=0, minute=0, second=0, microsecond=0)) orders = [] for d in date_list: orders += self.get_oneday_orders(instrument_id, d, state) return [ o for o in orders if o['datetime'] >= stime and o['datetime'] < etime ]
class QsHuobi(object): """ qsq系统提供的火币接口,可通过此接口获取数据和交易等 要想进行交易,必须在qs/config/config.ini中设置好自己的火币api密钥 """ def __init__(self): [self.api_key, self.secret_key, self.client_order_id] = self.get_apikey() self.request_client = RequestClient(api_key=self.api_key, secret_key=self.secret_key) #self.subscription_client = SubscriptionClient() def get_apikey(self): cfg = ConfigParser() cfg.read(QsEnv.g_project_config) return [ cfg.get('huobi', 'huobiapi_key'), cfg.get('huobi', 'huobisecret_key'), cfg.get('huobi', 'client_order_id') ] def get_24h_trade_statistics(self, pair="btcusdt"): """ param: pair 想要获取的交易对,默认为btcusdt 获取24小时的市场数据,返回结构体成员包括有 timestamp, high, low, open, close, volume 可使用trade_statistics.high这样的方式获取对应数据 """ trade_statistics = self.request_client.get_24h_trade_statistics(pair) return trade_statistics def get_latest_candlestick(self, pair="btcusdt", interval=CandlestickInterval.MIN1, size=10): """ param: pair 想要获取的交易对,默认为btcusdt param: interval 时间间隔,默认为MIN1, 可选有MIN1 MIN5 MIN15 MIN30 MIN60 DAY1 MON1 WEEK1 YEAR1 param: size 获取的数据条数,默认为10,选择范围为[1,2000] 获取最近一段时间的K线数据,返回一个list,这个list的每一项的结构体成员包括有 timestamp, high, low, open, close, volume 可使用member.high这样的方式获取对应数据 """ candlestick_list = self.request_client.get_latest_candlestick( pair, interval, size) return candlestick_list def get_exchange_currencies(self): """ 获取火币所有交易币种 """ return self.request_client.get_exchange_currencies() def get_exchange_info(self): """ 获取交易所信息,返回交易对和支持币种,使用案例 for symbol in exchange_info.symbol_list: print(symbol.symbol) for currency in exchange_info.currencies: print(currency) """ return self.request_client.get_exchange_info() def get_fee_rate(self, symbol="btcusdt"): """ 获取交易手续费,返回一个FeeRate对象,成员包括 symbol 对应币种 maker_fee 卖方手续费 taker_fee 买方手续费 实际使用中,symbol也为一个费率 """ result = self.request_client.get_fee_rate(symbols=symbol) return result[0] def get_historical_orders(self, symbol="ethusdt", order_state=OrderState.CANCELED, order_type=None, start_date=None, end_date=None, start_id=None, size=None): """ param: symbol 符号(必须) param: order_state 订单状态(必须),可选参数有SUBMITTED PARTIAL_FILLED CANCELLING PARTIAL_CANCELED FILLED CANCELED INVALID param: order_type 订单类型(可选),可选参数有SELL_LIMIT BUY_LIMIT BUY_MARKET SELL_MARKET BUY_IOC SELL_IOC BUY_LIMIT_MAKER SELL_LIMIT_MAKER BUY_STOP_LIMIT SELL_STOP_LIMIT INVALID param: start_date 开始日期(可选) 格式为 yyyy-mm-dd param: end_date 结束日期(可选) 格式为 yyyy-mm-dd param: start_id(可选) 订单起始id,暂时忽略 param: size(可选) 大小,暂时忽略 获取历史订单 """ orders = self.request_client.get_historical_orders( symbol=symbol, order_state=order_state, order_type=order_type, start_date=start_date, end_date=end_date, start_id=start_id, size=size) return orders def get_historical_trade(self, symbol="btcusdt", size=5): """ param: symbol 符号(必须) param: size 交易列表的大小(必须) 获取历史交易数据,返回trade_list对象列表,每个对象有如下几个成员 timestamp trade_id price amount direction """ trade_list = self.request_client.get_historical_trade(symbol, size) return trade_list def get_market_trade(self, symbol="btcusdt"): """ param: symbol 符号,默认为btcusdt 获取当前市场上的交易,返回一个trade对象组成的tradelist列表,每个trade对象的成员有 price, amount, trade_id timestamp, direction """ trades = self.request_client.get_market_trade(symbol=symbol) return trades def get_open_orders(self, symbol="htusdt", account_type=AccountType.SPOT, direct="next"): """ 获取自己账户当前开放的交易,返回order数组,每个Order对象有 order_id, symbol, price, amount, account_type, created_timestamp, order_type, filled_amount, filled_cash_amount, filled_fees, source, state """ orders = self.request_client.get_open_orders(symbol=symbol, account_type=account_type, direct=direct) return orders def get_order_recent_48hour(self, symbol=None, start_time=None, end_time=None, size=None, direct=None): """ 获取自己账户48小时内的交易记录,返回order数组 """ orders = self.request_client.get_order_recent_48hour( symbol=symbol, start_time=start_time, end_time=end_time, size=size, direct=direct) return orders def get_price_depth(self, symbol, size): """ 获取当前市场的交易深度,symbol 为交易对,size为头部的几个交易,一个用例如下 i = 0 for entry in depth.bids: i = i + 1 print(str(i) + ": price: " + str(entry.price) + ", amount: " + str(entry.amount)) i = 0 for entry in depth.asks: i = i + 1 print(str(i) + ": price: " + str(entry.price) + ", amount: " + str(entry.amount)) """ depth = self.request_client.get_price_depth(symbol, size) return depth def order(self, symbol="btcusdt", account_type=AccountType.SPOT, order_type=OrderType.SELL_LIMIT, amount=1.0, price=None, stop_price=None, operator=None): """ 下单函数,谨慎使用 必须提供的参数为symbol,account_type,order_type,amount param: account_type 账户类型,包括有SPOT MARGIN OTC POINT param: order_type 订单类型,包括有SELL_LIMIT BUY_LIMIT BUY_MARKET SELL_MARKET BUY_IOC SELL_IOC """ order_id = self.request_client.create_order(symbol, account_type, order_type, amount, price, self.client_order_id, stop_price, operator) return order_id def cancle_order(self, symbol="btcusdt", order_id="00000"): """ 取消某笔订单 """ cancle_state = self.request_client.cancel_order(symbol, order_id) return cancle_state def get_order(self, symbol="btcusdt", order_id="000000"): """ 根据order_id获取某笔交易,每个Order对象有 order_id, symbol, price, amount, account_type, created_timestamp, order_type, filled_amount, filled_cash_amount, filled_fees, source, state """ order = self.request_client.get_order(symbol=symbol, order_id=order_id) return order def get_order_by_client_order_id(self): """ 根据client_order_id来获取订单, 每个Order对象有 order_id, symbol, price, amount, account_type, created_timestamp, order_type, filled_amount, filled_cash_amount, filled_fees, source, state """ order = self.request_client.get_order_by_client_order_id( client_order_id=self.client_order_id) return order def cancel_client_order(self): """ 根据client_order_id取消某笔交易 """ self.request_client.cancel_client_order( client_order_id=self.client_order_id)