def get_account_info(self): logger.info( u'---------------------------------------spot account info------------------------------------------------' ) try: accounts = self.API.get_accounts() if accounts.get('status') == 'ok': spot_account = list( filter(lambda x: x["type"] == 'spot', accounts.get("data"))) self.ACCOUNT_ID = spot_account[0].get('id') my_account_info = self.API.get_balance(self.ACCOUNT_ID) symbols = [self.BALANCE_E, self.BALANCE_T] if my_account_info.get('status') == 'ok': data = from_dict(my_account_info, "data", "list") for symbol in symbols: symbol_infos = list( filter(lambda x: x["currency"] == symbol, data)) symbol_info = self.accountInfo[symbol] symbol_info["available"] = float( symbol_infos[0]["balance"]) symbol_info["freezed"] = float( symbol_infos[1]["balance"]) symbol_info["total"] = symbol_info[ "available"] + symbol_info["freezed"] logger_join(symbol.upper(), symbol_info["total"], "available", symbol_info["available"], "freezed", symbol_info["freezed"]) else: self.get_account_info() else: self.get_account_info() except Exception as err: logger.error(err) self.get_account_info()
def make_order(self, my_order_info): logger.info( u'\n-------------------------------------------spot order------------------------------------------------' ) try: result = self.API.send_order(self.ACCOUNT_ID, my_order_info.amount, my_order_info.symbol, my_order_info.orderType, my_order_info.price, my_order_info.offset) except Exception as e: logger.error("***send_order:{}".format(e, traceback.format_exc())) send_msg("%s:send_order failed:%s" % (my_order_info.symbol, e)) exit() result = self.check_order_list(my_order_info) if result is not None and result.get('status') == 'ok': logger_join("OrderId", result['data'], my_order_info.symbol, my_order_info.orderType, my_order_info.price, my_order_info.amount, " ", from_time_stamp()) return result['data'] else: logger_join("order failed!", my_order_info.symbol, my_order_info.orderType, my_order_info.price, my_order_info.amount) return "-1"
def check_order_list(self, my_order_info): result = {} try: result = self.API.orders_list(my_order_info.symbol, 'pre-submitted,submitting,submitted,partial-filled,filled', my_order_info.orderType, 1) except Exception as e: logger.error("***orders_list:%s" % e) if result is not None and result.get('status') == 'ok': order = result.get("data")[0] if float(order.get("price")) == my_order_info.price: return {'status': 'ok', 'data': order['id']} else: return {} else: logger_join(my_order_info.symbol, "check_order_list failed,try again.") return self.check_order_list(my_order_info)
def cancel_my_order(self, my_order_info): logger.info( u'\n---------------------------------------spot cancel order--------------------------------------------') result = {} try: result = self.API.cancel_order(my_order_info.orderId) except Exception as e: logger.info("***cancel_order:%s" % e) if result is None or result.get('status') != 'ok': logger_join(u"order", my_order_info.orderId, "not canceled or cancel failed!!!") state = self.check_order_status(my_order_info) if state == 'canceled' or state == 'partial-canceled': write_log("order " + my_order_info.orderId + " canceled") if "_type" in result and result["_type"] == "contract": from api.HuobiContractAPI import security security.close(my_order_info.amount) elif state != self.FILLED_STATUS: # not canceled or cancel failed(part dealed) and not complete continue cancelling return self.cancel_my_order(my_order_info) return state
def check_order_status(self, my_order_info, wait_count=0): order_id = my_order_info.orderId order_result = {} try: order_result = self.API.order_info(order_id) except Exception as e: logger.info("***order_info:%s" % e) if order_result is not None and order_result.get('status') == 'ok': order = order_result["data"] order_id = order["id"] state = order["state"] my_order_info.set_deal_amount(float(order["field-amount"])) if my_order_info.dealAmount > 0: if self.IS_SPOT: my_order_info.set_avg_price( float(order["field-cash-amount"]) / float(order["field-amount"])) else: my_order_info.set_avg_price(float(order["price"])) if state == 'canceled': logger_join("order", order_id, "canceled") elif state == 'partial-canceled': logger_join("part dealed ", my_order_info.dealAmount, " and canceled") if my_order_info.dealAmount == 0.0: logger.info("data error!check order status again!") return self.check_order_status(my_order_info, wait_count) elif state == 'partial-filled': if wait_count == self.TRADE_WAIT_COUNT: logger_join("part dealed ", my_order_info.dealAmount) else: logger_join("part dealed ", my_order_info.dealAmount) sys.stdout.flush() elif state == 'filled': logger_join("order", order_id, "complete deal") else: if wait_count == self.TRADE_WAIT_COUNT: logger.info("timeout no deal") else: logger.info("no deal") sys.stdout.flush() return state else: logger_join(order_id, "checkOrderStatus failed,try again.") return self.check_order_status(my_order_info, wait_count)
encrypt_f(KEY_PATH) config = configparser.ConfigParser() config.read(get_cf_path()) class TradeType: SPOT = "spot" FUTURE = "future" TRADE_TYPE = TradeType.SPOT TRADE_LIMIT = 1000 TRADE_LEVEL = 1 try: TRADE_TYPE = config.get("trade", "type") except configparser.NoOptionError: logger_join("DEFAULT TRADE TYPE", TRADE_TYPE) try: TRADE_LIMIT = int(config.get("trade", "limit")) logger_join("FUTURE TRADE LIMIT", TRADE_LIMIT) except configparser.NoOptionError: logger_join("DEFAULT TRADE LIMIT", TRADE_LIMIT) try: TRADE_LEVEL = int(config.get("trade", "level")) except configparser.NoOptionError: logger_join("DEFAULT TRADE LEVEL", TRADE_LEVEL)