def test_trade_match_result(self): trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key, performance_test=True) tc = TimeCost(function_name=trade_client.create_order.__name__) created_order_id, tc.server_req_cost, tc.server_api_cost = trade_client.create_order( symbol=trade_symbol, account_id=g_account_id, order_type=OrderType.BUY_MARKET, source=OrderSource.API, amount=5.0, price=None) print("create order id for match result: ", created_order_id) tc.run_status = RunStatus.SUCCESS if created_order_id else RunStatus.FAILED tc.add_record() time.sleep(2) # match result is not ready, need sleep a time # case get_match_results_by_order_id tc = TimeCost( function_name=trade_client.get_match_results_by_order_id.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.get_match_results_by_order_id( order_id=created_order_id) tc.run_status = RunStatus.SUCCESS if result and len( result) else RunStatus.FAILED tc.add_record() # case get_match_result tc = TimeCost(function_name=trade_client.get_match_result.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.get_match_result( symbol=trade_symbol, size=10) tc.run_status = RunStatus.SUCCESS if result and len( result) else RunStatus.FAILED tc.add_record()
def create_sell_order(symbol, price, amount): try: account_id = 23455585 trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) order_id = trade_client.create_order(symbol=symbol, account_id=account_id, order_type=OrderType.SELL_LIMIT, source=OrderSource.API, amount=amount, price=price) logger.info("created SELL order id : {id}\n".format(id=order_id)) return True except Exception as e: print("ExecuteError:", e) return False
def create_buy_order(symbol, price, amount): try: account_id = 23455585 trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) order_id = trade_client.create_order(symbol=symbol, account_id=account_id, order_type=OrderType.BUY_LIMIT, source=OrderSource.API, amount=amount, price=price) logger.info("created BUY order id : {id}\n".format(id=order_id)) return True, 0 except Exception as e: print("ExecuteError:", e) max_size = 0 # if "order-holding-limit-failed" in e: # max_size = re.findall("\d+\.*\d*", e) return False, max_size
def create_buy_order(symbol, price, amount): try: account_id = 23455585 trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) order_id = trade_client.create_order(symbol=symbol, account_id=account_id, order_type=OrderType.BUY_LIMIT, source=OrderSource.API, amount=amount, price=price) printme("created BUY order id : %d price=%.*f, amount=%f\n"%(order_id, price_print_format, price, amount)) return True, order_id, 0 except Exception as e: print("ExecuteError:", e, inspect.stack()[1][4]) max_size = 0 # if "order-holding-limit-failed" in e: # max_size = re.findall("\d+\.*\d*", e) return False, 0, max_size
def create_sell_order(symbol, price, amount): try: trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) order_id = trade_client.create_order(symbol=symbol, account_id=account_id, order_type=OrderType.SELL_LIMIT, source=OrderSource.API, amount=amount, price=price) printme("created SELL order id : {id}".format(id=order_id)) return True, order_id except Exception as e: print("ExecuteError:", e, inspect.stack()[1][4]) return False, 0
def must_buy_sell(order_type, symbol, amount, price, amount_precision, price_precision, max_times=20, loop_sleep_time=3): trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) print("START TO %s @Local_Time:" % order_type, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')) while (max_times): while (1): try: order_id = trade_client.create_order(symbol, account_id=g_account_id, order_type=order_type, source=OrderSource.API, amount=precision_cali( amount, amount_precision), price=price) break except requests.exceptions.ProxyError as e: print(e) continue except requests.exceptions.ConnectionError as e: print(e) continue except requests.exceptions.ReadTimeout as e: print(e) continue time.sleep(loop_sleep_time) while (1): try: orderObj = trade_client.get_order(order_id=order_id) break except requests.exceptions.ProxyError as e: print(e) continue except requests.exceptions.ConnectionError as e: print(e) continue except requests.exceptions.ReadTimeout as e: print(e) continue if orderObj.state == "filled": filled_price = precision_cali((float(orderObj.filled_cash_amount) / float(orderObj.filled_amount)), price_precision) filled_amount = precision_cali(float(orderObj.filled_amount), amount_precision) - float( orderObj.filled_fees) print( "No.%d Order %s state is %s @" % (max_times, order_id, orderObj.state), datetime.datetime.fromtimestamp( orderObj.finished_at / 1000).strftime('%Y-%m-%d %H:%M:%S.%f')) print("No.%d Order filled amount is %s @filled price: %.8f" % (max_times, filled_amount, filled_price)) return [filled_price, filled_amount] else: while (1): canceled_order_id = trade_client.cancel_order(symbol, order_id) if canceled_order_id == order_id: print("Canceled order %s done" % canceled_order_id) break else: print("Canceled order %s fail" % canceled_order_id) continue while (1): try: canceled_orderObj = trade_client.get_order( order_id=canceled_order_id) print("No.%d Canceled order filled amount is %s" % (max_times, canceled_orderObj.filled_amount)) break except requests.exceptions.ProxyError as e: print(e) continue except requests.exceptions.ConnectionError as e: print(e) continue except requests.exceptions.ReadTimeout as e: print(e) continue max_times -= 1 amount -= float(canceled_orderObj.filled_amount)
def test_trade_order(self): trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key, performance_test=True) client_order_id = "test_" + str(round(time.time())) + "_id" print("client order id : ", client_order_id) # case create_order tc = TimeCost(function_name=trade_client.create_order.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.order_id = trade_client.create_order( symbol=trade_symbol, account_id=g_account_id, order_type=OrderType.BUY_LIMIT, source=OrderSource.API, amount=55, price=0.1, client_order_id=client_order_id, stop_price=0.08, operator="gte") order_id_tmp = result tc.run_status = RunStatus.SUCCESS if result else RunStatus.FAILED tc.add_record() # case get_order tc = TimeCost(function_name=trade_client.get_order.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.order_id = trade_client.get_order( order_id=order_id_tmp) tc.run_status = RunStatus.SUCCESS if result else RunStatus.FAILED tc.add_record() # case get_order_by_client_order_id tc = TimeCost( function_name=trade_client.get_order_by_client_order_id.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.order_id = trade_client.get_order_by_client_order_id( client_order_id=client_order_id) tc.run_status = RunStatus.SUCCESS if result else RunStatus.FAILED tc.add_record() # case get_open_orders tc = TimeCost(function_name=trade_client.get_open_orders.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.order_id = trade_client.get_open_orders( symbol=trade_symbol, account_id=g_account_id) tc.run_status = RunStatus.SUCCESS if result and len( result) else RunStatus.FAILED tc.add_record() # case get_orders tc = TimeCost(function_name=trade_client.get_orders.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.order_id = trade_client.get_orders( symbol=trade_symbol, order_type=OrderType.BUY_LIMIT, order_state=OrderState.SUBMITTED) tc.run_status = RunStatus.SUCCESS if result and len( result) else RunStatus.FAILED tc.add_record() # case cancel_order tc = TimeCost(function_name=trade_client.cancel_order.__name__) result, tc.server_req_cost, tc.server_api_cost = trade_client.order_id = trade_client.cancel_order( symbol=trade_symbol, order_id=order_id_tmp) tc.run_status = RunStatus.SUCCESS if result else RunStatus.FAILED tc.add_record()
from huobi.client.trade import TradeClient from huobi.constant import * from huobi.utils import * trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) symbol_test = "eosusdt" order_id = trade_client.create_order(symbol=symbol_test, account_id=g_account_id, order_type=OrderType.BUY_LIMIT, source=OrderSource.API, amount=18, price=0.292) LogInfo.output("created order id : {id}".format(id=order_id)) canceled_order_id = trade_client.cancel_order(symbol_test, order_id) if canceled_order_id == order_id: LogInfo.output("cancel order {id} done".format(id=canceled_order_id)) else: LogInfo.output("cancel order {id} fail".format(id=canceled_order_id))
from huobi.client.trade import TradeClient from huobi.constant import * from huobi.utils import * import time symbol_test = "eosusdt" client_order_id_header = str(int(time.time())) client_order_id_test = "client_" + client_order_id_header + "_order" # unique id in 24hours account_id = g_account_id trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) order_id = trade_client.create_order(symbol=symbol_test, account_id=account_id, order_type=OrderType.BUY_LIMIT, source=OrderSource.API, amount=20, price=0.26, client_order_id=client_order_id_test, stop_price=0.11, operator="gte") LogInfo.output( "======= create new order id : {order_id} with client id {client_id} =======" .format(order_id=(order_id), client_id=client_order_id_test)) orderObj = trade_client.get_order(order_id=order_id) LogInfo.output("======= get order by order id : {order_id} =======".format( order_id=order_id)) orderObj.print_object() orderObj = trade_client.get_order_by_client_order_id( client_order_id=client_order_id_test)
from huobi.client.trade import TradeClient from huobi.constant import * from huobi.utils import * from keys.py import * symbol_test = "ftiusdt" trade_client = TradeClient(g_api_key, g_secret_key) order_id = trade_client.create_order(symbol=symbol_test, account_id=g_account_id, order_type=OrderType.SELL_LIMIT, source=OrderSource.API, amount=1.0, price=50000) LogInfo.output("created order id : {id}".format(id=order_id)) orderObj = trade_client.get_order(order_id=order_id) LogInfo.output("======= get order by order id : {order_id} =======".format(order_id=order_id)) orderObj.print_object() #canceled_order_id = trade_client.cancel_order(symbol_test, order_id) #if canceled_order_id == order_id: # LogInfo.output("cancel order {id} done".format(id=canceled_order_id)) #else: # LogInfo.output("cancel order {id} fail".format(id=canceled_order_id))
class Trader(BaseTrader): def __init__(self, api_key, secret_key, account_id, verbose=False): super().__init__() self.account_id = account_id self.trade_client = TradeClient(api_key=api_key, secret_key=secret_key) self.account_client = AccountClient(api_key=api_key, secret_key=secret_key) self.algo_client = AlgoClient(api_key=api_key, secret_key=secret_key) self.market_client = MarketClient() self.holds = {} self.total_fee = 0 self.stop_loss_threads = [] self.long_order_threads = [] self.latest_timestamp = 0 self.client_id_counter = 0 self.verbose = verbose self.subscription = None def add_trade_clearing_subscription(self, symbol, callback, error_handler=None): self.subscription = self.trade_client.sub_trade_clearing( symbol, callback, error_handler) return self.subscription def remove_trade_clearing_subscription(self, subscription): self.subscription.unsubscribe_all() def get_balance(self, symbol='usdt'): balances = self.account_client.get_balance(self.account_id) for balance in balances: if balance.currency == symbol: return float(balance.balance) def get_balance_pair(self, symbol): pair = transaction_pairs[symbol] target, base = pair.target, pair.base balances = self.account_client.get_balance(self.account_id) target_balance, base_balance = None, None for balance in balances: if balance.currency == target and target_balance is None: target_balance = float(balance.balance) elif balance.currency == base and base_balance is None: base_balance = float(balance.balance) return target_balance, base_balance def get_newest_price(self, symbol): newest_trade = self.market_client.get_market_trade(symbol=symbol)[0] return newest_trade.price def submit_orders(self, symbol, prices, amounts, order_type): """Submit a series of orders to the trader and return their ids. symbol -- symbol of trading pair prices -- list of prices of limit orders amounts -- list of amounts of limit orders order_type -- OrderType.BUY_LIMIT or OrderType.SELL_LIMIT """ client_order_id_header = str(int(time.time())) order_ids = [ f'{client_order_id_header}{symbol}{i:02d}' for i in range(len(prices)) ] pair = transaction_pairs[symbol] price_scale, amount_scale = pair.price_scale, pair.amount_scale orders = [{ 'account_id': self.account_id, 'symbol': symbol, 'order_type': order_type, 'source': OrderSource.API, 'amount': f'{self.correct_amount(amount, symbol):.{amount_scale}f}', 'price': f'{price:.{price_scale}f}', 'client_order_id': order_id } for amount, price, order_id in zip(amounts, prices, order_ids)] results = [] for i in range(0, len(orders), MAX_ORDER_NUM): create_results = self.trade_client.batch_create_order( order_config_list=orders[i:i + MAX_ORDER_NUM]) results += create_results LogInfo.output_list(results) return results @staticmethod def get_normalized_amounts_with_eagerness(num_orders, eagerness=1.0): amounts = np.geomspace(eagerness**num_orders, 1, num_orders) return amounts / np.sum(amounts) @staticmethod def get_normalized_amounts_with_normal_distr(num_orders, skewness=1.0): amounts = np.linspace(-skewness, skewness, num_orders) amounts = stats.norm.pdf(amounts, 0, 1) amounts += amounts.min() return amounts / np.sum(amounts) @staticmethod def get_normalized_amounts_with_distr(num_orders, distr): if distr is None: return Trader.get_normalized_amounts_with_eagerness( num_orders, 1.0) elif distr['distr'] == 'geometry': return Trader.get_normalized_amounts_with_eagerness( num_orders, distr.get('eagerness', 1.0)) elif distr['distr'] == 'normal': return Trader.get_normalized_amounts_with_normal_distr( num_orders, distr.get('skewness', 1.0)) @staticmethod def get_price_interval(lower_price, upper_price, num_orders, order_type): if order_type == OrderType.BUY_LIMIT: prices = np.linspace(upper_price, lower_price, num_orders) elif order_type == OrderType.SELL_LIMIT: prices = np.linspace(lower_price, upper_price, num_orders) else: raise ValueError(f'Unknown order type {order_type}') return prices def generate_buy_queue_orders(self, symbol, lower_price, upper_price, num_orders, total_amount=None, total_amount_fraction=None, distr=None): prices = self.get_price_interval(lower_price, upper_price, num_orders, OrderType.BUY_LIMIT) normalized_amounts = self.get_normalized_amounts_with_distr( num_orders, distr) if total_amount is not None: amounts = normalized_amounts * total_amount elif total_amount_fraction is not None: balance = self.get_balance( transaction_pairs[symbol].base) * total_amount_fraction amounts = normalized_amounts * (balance / prices * normalized_amounts).sum() else: raise ValueError( 'One of total_amount or total_amount_fraction should be given') return self.submit_orders(symbol, prices, amounts, OrderType.BUY_LIMIT) def create_smart_buy_queue(self, symbol, lower_price, upper_price, num_orders, profit=1.05, total_amount=None, total_amount_fraction=None, distr=None): orders = self.generate_buy_queue_orders(symbol, lower_price, upper_price, num_orders, total_amount, total_amount_fraction, distr) client_order_id_header = str(int(time.time())) price_scale = transaction_pairs[symbol].price_scale algo_order_ids = [] for i, order in enumerate(orders): client_order_id = f'{client_order_id_header}{symbol}{i:02d}' order_price = float(order['price']) * profit stop_price = order_price * 0.999 self.algo_client.create_order( account_id=self.account_id, symbol=symbol, order_side=OrderSide.SELL, order_type=AlgoOrderType.LIMIT, order_size=order['amount'], order_price=f'{order_price:.{price_scale}f}', stop_price=f'{stop_price:.{price_scale}f}', client_order_id=client_order_id) algo_order_ids.append(client_order_id) return results, orders, algo_order_ids def cancel_all_algo_orders(self, order_ids): results = [ self.algo_client.cancel_orders(order_id) for order_id in order_ids ] return results def update_timestamp(self): now = int(time.time()) if now == self.latest_timestamp: self.client_id_counter += 1 else: self.latest_timestamp = now self.client_id_counter = 0 def get_order(self, order_id): return self.trade_client.get_order(order_id) def create_order(self, symbol, price, order_type, amount=None, amount_fraction=None): pair = transaction_pairs[symbol] if amount is None: if order_type is OrderType.SELL_LIMIT or order_type is OrderType.SELL_MARKET: amount = self.get_balance(pair.target) * amount_fraction else: amount = self.get_balance(pair.base) * amount_fraction amount = f'{float(amount):.{pair.amount_scale}f}' if price is not None: price = f'{float(price):.{pair.price_scale}f}' self.update_timestamp() client_order_id = f'{self.latest_timestamp}{symbol}{self.client_id_counter:02d}' order_id = self.trade_client.create_order( symbol=symbol, account_id=self.account_id, order_type=order_type, price=price, amount=amount, source=OrderSource.API, client_order_id=client_order_id) return order_id @staticmethod def get_time(): return int(time.time()) def get_previous_prices(self, symbol, window_type, window_size): candlesticks = self.market_client.get_candlestick( symbol, window_type, window_size) return [(cs.id, (cs.open + cs.close) / 2) for cs in sorted(candlesticks, key=lambda cs: cs.id)] def create_buy_queue(self, symbol, lower_price, upper_price, num_orders, total_amount=None, total_amount_fraction=None, distr=None): newest_price = self.get_newest_price(symbol) if upper_price > newest_price: raise ValueError( 'Unable to buy at a price higher the the market price') if lower_price >= upper_price: raise ValueError('lower_price should be less than upper_price') orders = self.generate_buy_queue_orders(symbol, lower_price, upper_price, num_orders, total_amount, total_amount_fraction, distr) return orders def create_sell_queue(self, symbol, lower_price, upper_price, num_orders, total_amount=None, total_amount_fraction=None, distr=None): newest_price = self.get_newest_price(symbol) if lower_price < newest_price: raise ValueError( 'Unable to sell at a price lower the the market price') if lower_price >= upper_price: raise ValueError('lower_price should be less than upper_price') prices = self.get_price_interval(lower_price, upper_price, num_orders, OrderType.SELL_LIMIT) normalized_amounts = self.get_normalized_amounts_with_distr( num_orders, distr) if total_amount is not None: amounts = normalized_amounts * total_amount elif total_amount_fraction is not None: balance = self.get_balance( transaction_pairs[symbol].target) * total_amount_fraction amounts = normalized_amounts * balance else: raise ValueError( 'One of total_amount or total_amount_fraction should be given') return self.submit_orders(symbol, prices, amounts, OrderType.SELL_LIMIT) def cancel_orders(self, symbol, order_ids): cancel_results = [] for i in range(0, len(order_ids), MAX_CANCEL_ORDER_NUM): cancel_result = self.trade_client.cancel_orders( symbol, order_ids[i:i + MAX_CANCEL_ORDER_NUM]) cancel_results.append(cancel_result) return cancel_results def cancel_all_orders_with_type(self, symbol, order_type): account_spot = self.account_client.get_account_by_type_and_symbol( AccountType.SPOT, symbol=None) orders = self.trade_client.get_open_orders(symbol=symbol, account_id=account_spot.id, direct=QueryDirection.NEXT) sell_order_ids = [ str(order.id) for order in orders if order.type == order_type ] if len(sell_order_ids) == 0: return return self.cancel_orders(symbol, sell_order_ids) def cancel_all_buy_orders(self, symbol): return self.cancel_all_orders_with_type(symbol, OrderType.BUY_LIMIT) def cancel_all_sell_orders(self, symbol): return self.cancel_all_orders_with_type(symbol, OrderType.SELL_LIMIT) def sell_all_at_market_price(self, symbol): return self.create_order(symbol=symbol, price=None, order_type=OrderType.SELL_MARKET, amount_fraction=0.999) def start_new_stop_loss_thread(self, symbol, stop_loss_price, interval=10, trailing_order=None): from stop_loss import StopLoss thread = StopLoss(symbol, self, stop_loss_price, interval, trailing_order) self.stop_loss_threads.append(thread) def start_long_order_thread(self, symbol, buy_price, profit, amount=None, amount_fraction=None, stop_loss=0.9, interval=10): from long_order import LongOrder thread = LongOrder(symbol, self, buy_price, profit, amount, amount_fraction, stop_loss, interval) self.long_order_threads.append(thread)
class TradeHandler: def __init__(self): self.api_key = "db8f342a-3540b528-hrf5gdfghe-5e793" self.secret_key = "366684d9-94c5fdf7-ad3b02a0-446bf" self.trade_client = TradeClient(api_key=self.api_key, secret_key=self.secret_key) self.algo_client = AlgoClient(api_key=self.api_key, secret_key=self.secret_key) self.trade_log = pd.DataFrame( columns=['Time', 'Amount', 'Fee', 'Price', 'Symbol', 'OrderType']) def get_feerate(self, symbol): list_obj = self.trade_client.get_feerate(symbols=symbol) LogInfo.output_list(list_obj) def get_history_orders(self, symbol_list): for symbol in symbol_list: list_obj = self.trade_client.get_history_orders(symbol=symbol, start_time=None, end_time=None, size=20, direct=None) LogInfo.output_list(list_obj) def get_match_result(self, symbol): list_obj = self.trade_client.get_match_result(symbol=symbol, size=100) for obj in list_obj: self.trade_log = self.trade_log.append(pd.Series( [ obj.created_at, obj.filled_amount, obj.filled_fees, obj.price, obj.symbol, obj.type ], index=[ 'Time', 'Amount', 'Fee', 'Price', 'Symbol', 'OrderType' ]), ignore_index=True) # LogInfo.output_list(list_obj) self.trade_log = self.trade_log.drop_duplicates() def get_open_orders(self, id, symbol): list_obj = self.trade_client.get_open_orders( symbol=symbol, account_id=id, direct=QueryDirection.NEXT) LogInfo.output_list(list_obj) # list_obj = self.trade_client.get_open_orders(symbol=symbol, account_id=id, direct=QueryDirection.PREV) # LogInfo.output_list(list_obj) def get_order(self, order_id): order_obj = self.trade_client.get_order(order_id=order_id) # LogInfo.output("======= get order by order id : {order_id} =======".format(order_id=order_id)) # order_obj.print_object() return order_obj def batch_cancel(self, account_id): # cancel all the open orders under account result = self.trade_client.cancel_open_orders(account_id=account_id) def create_order_limit(self, symbol, account_id, order_type, amount, price): order_id = self.trade_client.create_order(symbol=symbol, account_id=account_id, order_type=order_type, source=OrderSource.API, amount=amount, price=price) LogInfo.output("created order id : {id}".format(id=order_id)) def create_order_market(self, symbol, account_id, order_type, value): order_id = self.trade_client.create_order(symbol=symbol, account_id=account_id, order_type=order_type, source=OrderSource.API, amount=value, price=None) return order_id def cancel_order(self, symbol, order_id): canceled_order_id = self.trade_client.cancel_order(symbol, order_id) if canceled_order_id == order_id: LogInfo.output( "cancel order {id} done".format(id=canceled_order_id)) else: LogInfo.output( "cancel order {id} fail".format(id=canceled_order_id))
from huobi.client.account import AccountClient from huobi.client.trade import TradeClient from huobi.constant import * from huobi.utils import * symbol_test = "eosusdt" account_id = g_account_id trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key) order_id = trade_client.create_order(symbol=symbol_test, account_id=account_id, order_type=OrderType.BUY_LIMIT, source=OrderSource.API, amount=4.0, price=1.292) LogInfo.output("created order id : {id}".format(id=order_id)) canceled_order_id = trade_client.cancel_order(symbol_test, order_id) if canceled_order_id == order_id: LogInfo.output("cancel order {id} done".format(id=canceled_order_id)) else: LogInfo.output("cancel order {id} fail".format(id=canceled_order_id)) order_id = trade_client.create_order(symbol=symbol_test, account_id=account_id, order_type=OrderType.BUY_MARKET, source=OrderSource.API, amount=5.0, price=1.292) LogInfo.output("created order id : {id}".format(id=order_id))