def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ suffix = "/v2/trading/placeOrder" payload = { "instrument": "{}-{}".format(coin, cur), "side": dir, "volume": amount, "price": price } headers = self.create_signature_headers(suffix) req = Request(base_url, suffix) rsl = req.post(headers, payload) order_id = rsl["id"] time.sleep(10) cancelled = self.cancel_order(order_id) return cancelled
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ suffix = "/exchange/{}limit".format(dir) data = { "currencyPair": "{}/{}".format(coin, cur), "price": price, "quantity": amount } req = Request(base_url, suffix) headers = self.create_signature_headers(suffix, data) rsl = req.post(headers, data) if rsl["success"] == True: return True return False
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ suffix = "/v1/Trade/OrderSubmit" nonce = str(int(time.time() * 1000)) data = { "nonce": nonce, "market": "{}_{}".format(coin, cur), "orderType": "Limit", "type": dir, "amount": amount, "price": price, } headers = self.create_signature_headers({"data": data}) req = Request(base_url, suffix) rsl = req.post(headers, data) order_id = rsl["orderId"] if not order_id: return False return self.cancel_order(order_id)
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ endpoint = "/v2/auth/w/order/submit" if dir == "sell": amount = float(amount) * -1 data = { "type": "IOC", "symbol": "t{}{}".format(coin, cur), "price": price, "amount": amount } headers = self.create_signature_headers(endpoint, data) req = Request(api_auth_url, endpoint) rsl = req.post(headers, data) if len(rsl) >= 7: return rsl[7] logging.info(rsl) return rsl[2]
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ suffix = "/api/spot/v3/orders" data = { "type": "limit", "side": dir, "instrument_id": "{}-{}".format(coin, cur), "size": amount, "price": price, "order_type": 3 } headers = self.create_signature_headers(suffix, data) req = Request(base_url, suffix) rsl = req.post(headers, data) if rsl["result"] == True: return True return False
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ suffix = "/v1/order/ioc?" nonce = str(int(time.time() * 1000)) payload = { "access_id": self.key, "tonce": nonce, "market": cur + coin, "type": dir, "amount": amount, "price": price } query_str = urlencode(payload) headers = self.create_signature_headers(query_str) req = Request(base_url, suffix + query_str) rsl = req.post(headers, payload) if rsl["message"] == "ok": return True return False
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ endpoint = "/api/{}/{}_{}".format(dir, cur, coin) payload = { "amount": amount, "price": price, "ioc_order": True } headers = self.create_signature_headers(endpoint, payload) req = Request(base_url, endpoint) rsl = req.post(headers, payload) if "error" in rsl.keys(): logging.error(rsl) logging.info(rsl) return rsl
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ suffix = "/api/v1/orders" headers = self.create_signature_headers(suffix) data = { "side": dir, "symbol": "{}-{}".format(coin, cur), "price": price, "size": amount, "timeInForce": "IOC" } req = Request(base_url, suffix) rsl = req.post(headers, data) if "orderId" in rsl.keys(): return True return False
def get_balances(self): """Get Balances from exchange. Only exchange account is supported.""" endpoint = "/v2/auth/r/wallets" headers = self.create_signature_headers(endpoint) req = Request(api_auth_url, endpoint) rsl = req.post(headers) balances = [] for e in rsl: if e[0] == "exchange": balances.append({ "coin": e[1].upper(), "balance": e[2] }) return balances
def get_balances(self): """Get Balances from exchange""" endpoint = "/api/v2/balance/" headers = create_signature_headers(endpoint, "") req = Request(base_url, endpoint) rsl = req.post(headers) balances = [] for entry in rsl: if "balance" in entry.split("_")[1]: balances.append({ "coin": entry.split("_")[0].upper(), "balance": rsl[entry] }) return balances
def cancel_order(order_id): suffix = "/v1/Trade/OrderCancel" nonce = str(int(time.time() * 1000)) data = { "nonce": nonce, "market": "{}_{}".format(coin, cur), "cancelType": "Trade", "orderId": order_id } headers = self.create_signature_headers({"data": data}) req = Request(base_url, endpoint) rsl = req.post(headers, data) if "error" in rsl.keys(): return False return True
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ endpoint = "/api/v1/order/new" symbol = "{}_{}".format(coin, cur) data = { "market": symbol, "side": dir, "amount": amount, "price": price, "request": endpoint, "nonce": str(int(time.time() * 1000)) } headers = self.create_signature_headers(data) req = Request(base_url, endpoint) rsl = req.post(headers, data) if rsl["success"] == True: order_id = rsl["result"]["orderId"] time.sleep(5) if self.cancel_order(order_id, symbol): return True logging.error("Error canceling order {} in {}".format( rsl, __name__.upper() )) return False
def create_order(self, cur, coin, amount, price, dir): """ Create order. Only spot account supported. :param cur Currency to use to create the order. :param coin Coin to use to create the order. :param amount Amount to use to create the order. TODO Should check if within user balance else use user balance. :param price Not used, kept for compatibility with other wrappers. :param dir Direction of the order. Needs to be one of "buy" or "sell". """ if dir.lower() not in ["buy", "sell"]: return None if None in [cur, coin, amount]: return None endpoint = "/api/v3/order" symbol = coin + cur query = { "side": dir.upper(), "symbol": symbol.upper(), "timestamp": int(time.time() * 1000), "type": "MARKET", "quantity": float(amount) } headers = {"X-MBX-APIKEY": self.key} signature = self.create_signature(query) query["signature"] = signature req = Request(base_url, endpoint) try: rsl = req.post(headers, query) logging.error(rsl) return rsl except Exception as e: logging.error("Error {} in {}".format(e, __name__.upper())) return e
def get_balances(self): # FIXME not working but why?? """Get Balances from exchange""" nonce = int(time.time() * 1000) endpoint = "/api/v1/fund/allAccount" query = "{}?accessKey={}&nonce={}".format(endpoint, self.key, nonce) signature = self.create_signature(query) query_signed = "{}&signData={}".format(query, signature) req = Request(base_url, query_signed) rsl = req.post(headers=None) balances = [] for i in rsl: if isinstance(i, dict): logging.info(i) balances.append({ "coin": i["currency"].upper(), "balance": i["fix"] }) if len(balances) > 0: return balances else: return rsl
def create_order(self, cur, coin, amount, price, dir): """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ endpoint = "/api/v3/orders.json?" nonce = str(int(time.time() * 1000)) data = { "access_key": self.key, "tonce": nonce, "market": coin.lower() + cur.lower(), "price": price, "side": dir, "volume": amount, "order_type": "IOC" } query = urlencode(data) signature = self.create_signature("POST", endpoint, query) query_signed = "{}&signature={}".format(query, signature) data["signature"] = signature req = Request(base_url, endpoint) rsl = req.post(headers=None, data=data) if "error" in rsl.keys(): logging.error("Error passing order in {}".format(__name__.upper())) return rsl
def create_order(self, cur, coin, amount, price, dir): # FIXME returns invalid signData. """ Create a trade order. In bitfinex direction is indicated by amount being positive or negative. :param cur Currency to user to create the trade order. :param coin Coin to use to create the trade order. :param amount Amount to be used to create the order. If the direction equal "buy" amount is positive, else if direction equals "sell" amount is negative or multiplied by -1. :param price Price to use to create the trade order. :param dir Unused paramenter, kept for compatibility. """ if dir == "buy": dir = 1 elif dir == "sell": dir = 2 symbol = "coin-{}-{}".format(cur.lower(), coin.lower()) nonce = int(time.time()) * 1000 endpoint = "/api/v1/trade/placeOrder" query = "{}?accessKey={}&amount={}&price={}&symbol={}&tradeType={}&nonce={}".format( endpoint, self.key, amount, price, symbol, dir, nonce) logging.info(query) signature = self.create_signature(query) query_signed = "{}&signData={}".format(query, signature) req = Request(base_url, query_signed) rsl = req.post(headers=None) if rsl["success"] == True: time.sleep(5) self.cancel_order(rsl, symbol) return rsl["message"]
def cancel_order(self, order_id, symbol): """ Cancel given order. :param order_id Id of the order to be cancelled. :param symbol Market symbol where to cancel the order. """ endpoint = "/api/v1/order/cancel" data = { "market": symbol, "orderId": order_id, "request": endpoint, "nonce": str(int(time.time() * 1000)) } headers = self.create_signature_headers(data) req = Request(base_url, endpoint) rsl = req.post(headers, data) if rsl["success"] == False: logging.error("Error canceling order {} in {}".format( rsl, __name__.upper() )) return rsl
def cancel_order(self, order_id, symbol): """ Cancel order. :param order_id Order id to be cancelled. :param cur Cur to use in market selection. :param coin Coin to use in market selection. """ nonce = int(time.time()) * 1000 endpoint = "/api/v1/trade/cancelOrder" query = "{}?accessKey={}&orderId={}&symbol={}&nonce={}".format( endpoint, self.key, order_id, symbol, nonce) signature = self.create_signature(query) query_signed = query + "&signData={}".format(signature) req = Request(base_url, query_signed) rsl = req.post(headers=None) if "error" in rsl.keys(): logging.error("Error {} in {}".format(rsl.__dict__, __name__.upper())) return rsl return rsl
def get_book(cls, symbol): endpoint = "/returnOrderBook" data = {"market": symbol, "count": 1} req = Request(base_url, endpoint) rsl = req.post(None, data) return rsl
def get_balances(self): """Get Balances from exchange""" endpoint = "/returnCompleteBalances" req = Request(base_url, endpoint) rsl = req.post(None, {"address": self.key}) return Normalizer(rsl).normalize_balances()