def cancel_order_bittrex(key, order_id): # https://bittrex.com/api/v1.1/market/cancel?apikey=API_KEY&uuid=ORDER_UUID final_url = BITTREX_CANCEL_ORDER + key.api_key + "&nonce=" + str( generate_nonce()) body = { "uuid": order_id, } final_url += _urlencode(body) headers = {"apisign": signed_string(final_url, key.secret)} post_details = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "cancel_order_bittrex: {res}".format(res=post_details) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") err_msg = "cancel bittrex order with id {id}".format(id=order_id) res = send_get_request_with_header(post_details.final_url, post_details.headers, err_msg, timeout=BITTREX_DEAL_TIMEOUT) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: print_to_console(res, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(res, "market_utils.log") return res
def cancel_order_huobi(key, order_id): HUOBI_CANCEL_PATH = HUOBI_CANCEL_ORDER + str(order_id) + "/submitcancel" final_url = HUOBI_API_URL + HUOBI_CANCEL_PATH + "?" body = init_body(key) message = _urlencode(body).encode('utf8') msg = "POST\n{base_url}\n{path}\n{msg1}".format(base_url=HUOBI_API_ONLY, path=HUOBI_CANCEL_PATH, msg1=message) signature = sign_string_256_base64(key.secret, msg) body.append(("Signature", signature)) final_url += _urlencode(body).encode('utf8') body = {} post_details = PostRequestDetails(final_url, HUOBI_POST_HEADERS, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "cancel_order_huobi: url - {url} headers - {headers} body - {body}".format( url=final_url, headers=HUOBI_POST_HEADERS, body=body) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") err_msg = "cancel huobi order with id {id}".format(id=order_id) return send_post_request_with_logging(post_details, err_msg)
def generate_post_request(final_url, body, key): signature = signed_body_256(body, key.secret) body["signature"] = signature final_url += _urlencode(body) headers = {"X-MBX-APIKEY": key.api_key} # Yeah, body after that should be empty body = {} return PostRequestDetails(final_url, headers, body)
def get_balance_poloniex_post_details(key): body = {'command': 'returnCompleteBalances', 'nonce': generate_nonce()} headers = {"Key": key.api_key, "Sign": signed_body(body, key.secret)} # https://poloniex.com/tradingApi final_url = POLONIEX_CHECK_BALANCE res = PostRequestDetails(final_url, headers, body) if should_print_debug(): print_to_console(res, LOG_ALL_MARKET_NETWORK_RELATED_CRAP) return res
def get_balance_huobi_post_details(key): path = HUOBI_CHECK_BALANCE + get_huobi_account(key) + "/balance" final_url = HUOBI_API_URL + path + "?" body, url = generate_body_and_url_get_request(key, HUOBI_API_ONLY, path) final_url += url res = PostRequestDetails(final_url, HUOBI_GET_HEADERS, body) if should_print_debug(): print_to_console(res, LOG_ALL_MARKET_NETWORK_RELATED_CRAP) return res
def add_sell_order_huobi_url(key, pair_name, price, amount): final_url = SELL_URL + generate_url(key, HUOBI_API_ONLY, HUOBI_SELL_ORDER) params = json.dumps({ 'amount': float_to_str(amount), 'price': float_to_str(price), 'symbol': pair_name, 'source': 'api', 'type': 'sell-limit', 'account-id': get_huobi_account(key), }) res = PostRequestDetails(final_url, HUOBI_POST_HEADERS, params) return res
def add_buy_order_poloniex_url(key, pair_name, price, amount): body = generate_body(pair_name, price, amount, "buy") headers = {"Key": key.api_key, "Sign": signed_body(body, key.secret)} # https://poloniex.com/tradingApi final_url = POLONIEX_BUY_ORDER res = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_buy_order_poloniex: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def get_balance_bittrex_post_details(key): final_url = BITTREX_CHECK_BALANCE + key.api_key + "&nonce=" + str( generate_nonce()) body = {} final_url += _urlencode(body) headers = {"apisign": signed_string(final_url, key.secret)} res = PostRequestDetails(final_url, headers, body) if should_print_debug(): print_to_console(res, LOG_ALL_MARKET_NETWORK_RELATED_CRAP) return res
def get_open_orders_kraken_post_details(key, pair_name=None): final_url = KRAKEN_BASE_API_URL + KRAKEN_GET_OPEN_ORDERS body = {"nonce": generate_nonce()} headers = { "API-Key": key.api_key, "API-Sign": sign_kraken(body, KRAKEN_GET_OPEN_ORDERS, key.secret) } res = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "ger_open_orders_kraken: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def add_buy_order_kraken_url(key, pair_name, price, amount): # https://api.kraken.com/0/private/AddOrder final_url = KRAKEN_BASE_API_URL + KRAKEN_BUY_ORDER body = generate_body(pair_name, price, amount, "buy") headers = { "API-Key": key.api_key, "API-Sign": sign_kraken(body, KRAKEN_BUY_ORDER, key.secret) } res = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_buy_order_kraken: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def get_open_orders_poloniex_post_details(key, pair_name): body = { 'command': 'returnOpenOrders', 'currencyPair': pair_name, 'nonce': generate_nonce() } headers = {"Key": key.api_key, "Sign": signed_body(body, key.secret)} # https://poloniex.com/tradingApi final_url = POLONIEX_GET_OPEN_ORDERS res = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "get_open_order_poloniex: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def get_closed_orders_kraken_post_details(key, pair_name=None, time_start=0, time_end=get_now_seconds_utc()): final_url = KRAKEN_BASE_API_URL + KRAKEN_GET_CLOSE_ORDERS body = {"nonce": generate_nonce(), "start": time_start, "end": time_end} headers = { "API-Key": key.api_key, "API-Sign": sign_kraken(body, KRAKEN_GET_CLOSE_ORDERS, key.secret) } res = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "get_closed_orders_kraken: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def add_buy_order_huobi_url(key, pair_name, price, amount): final_url = BUY_URL + generate_url(key, HUOBI_API_ONLY, HUOBI_BUY_ORDER) params = json.dumps({ "account-id": get_huobi_account(key), "amount": float_to_str(amount), "price": float_to_str(price), "source": "api", "symbol": pair_name, "type": "buy-limit" }) res = PostRequestDetails(final_url, HUOBI_POST_HEADERS, params) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_buy_order_huobi: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def cancel_order_kraken(key, order_id): # https://api.kraken.com/0/private/CancelOrder final_url = KRAKEN_BASE_API_URL + KRAKEN_CANCEL_ORDER body = { "txid": order_id, "nonce": generate_nonce() } headers = {"API-Key": key.api_key, "API-Sign": sign_kraken(body, KRAKEN_CANCEL_ORDER, key.secret)} if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "cancel_order_kraken: url - {url} headers - {headers} body - {body}".format( url=final_url, headers=headers, body=body) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") post_details = PostRequestDetails(final_url, headers, body) err_msg = "cancel kraken called for {order_id}".format(order_id=order_id) return send_post_request_with_logging(post_details, err_msg)
def get_order_history_bittrex_post_details(key, pair_name): final_url = BITTREX_GET_TRADE_HISTORY + key.api_key + "&nonce=" + str( generate_nonce()) if pair_name != "all": body = {"market": pair_name} else: body = {} final_url += _urlencode(body) headers = {"apisign": signed_string(final_url, key.secret)} post_details = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "get_order_history_bittrex_post_details: {res}".format( res=post_details) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return post_details
def add_sell_order_bittrex_url(key, pair_name, price, amount): # https://bittrex.com/api/v1.1/market/selllimit?apikey=API_KEY&market=BTC-LTC&quantity=1.2&rate=1.3 final_url = BITTREX_SELL_ORDER + key.api_key + "&nonce=" + str(generate_nonce()) body = { "market": pair_name, "quantity": float_to_str(amount), "rate": float_to_str(price) } final_url += _urlencode(body) headers = {"apisign": signed_string(final_url, key.secret)} res = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_sell_order_bittrex: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def cancel_order_poloniex(key, order_id): body = { "command": "cancelOrder", "orderNumber": order_id, "nonce": generate_nonce() } headers = {"Key": key.api_key, "Sign": signed_body(body, key.secret)} # https://poloniex.com/tradingApi final_url = POLONIEX_CANCEL_ORDER post_details = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_sell_order_poloniex: {res}".format(res=post_details) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") err_msg = "cancel poloniex called for {order_id}".format(order_id=order_id) return send_post_request_with_logging(post_details, err_msg)
def get_order_history_poloniex_post_details(key, pair_name, time_start, time_end, limit): body = { 'command': 'returnTradeHistory', 'currencyPair': pair_name, 'start': time_start, 'end': time_end, 'limit': limit, 'nonce': generate_nonce() } headers = {"Key": key.api_key, "Sign": signed_body(body, key.secret)} # https://poloniex.com/tradingApi final_url = POLONIEX_GET_ORDER_HISTORY post_details = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "get orders history poloniex: {res}".format(res=post_details) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return post_details
def get_huobi_account_impl(key): final_url = HUOBI_API_URL + HUOBI_GET_ACCOUNT_INFO + "?" body, url = generate_body_and_url_get_request(key, HUOBI_API_ONLY, HUOBI_GET_ACCOUNT_INFO) final_url += url post_details = PostRequestDetails(final_url, HUOBI_GET_HEADERS, body) err_msg = "get_huobi_account" error_code, res = send_get_request_with_header(post_details.final_url, post_details.headers, err_msg, timeout=HUOBI_DEAL_TIMEOUT) # {u'status': u'ok', u'data': [{u'subtype': u'', u'state': u'working', u'type': u'spot', u'id': 1245038}]} if error_code == STATUS.SUCCESS and "data" in res and len(res["data"]) > 0: return res["data"][0]["id"] return None
def get_order_history_huobi_post_details(key, pair_name, time_start, time_end): """ NOTE: limit can be used as well limit=HUOBI_ORDER_HISTORY_LIMIT """ final_url = HUOBI_API_URL + HUOBI_GET_TRADE_HISTORY + "?" # ('states', 'filled,partial-canceled'), ts1 = None ts2 = None if time_start == 0: time_start = time_end - 3600 * 24 # day ago elif time_end - time_start > 3600 * 24: msg = "Huobi allow time range not bigger than 24 hours! start: {} end: {}".format( time_start, time_end) print_to_console(msg, LOG_ALL_ERRORS) if 0 < time_start <= time_end: ts1 = ts_to_string_utc(time_start, format_string='%Y-%m-%d') ts2 = ts_to_string_utc(time_end, format_string='%Y-%m-%d') body = init_body(key) body.append(('direct', '')) if ts1 is None or ts2 is None: body.append(('end-date', '')) else: body.append(('end-date', ts2)) body.extend([('from', ''), ('size', '')]) if ts1 is None or ts2 is None: body.append(('start-date', '')) else: body.append(('start-date', ts1)) body.extend([('states', 'filled,partial-canceled'), ("symbol", pair_name), ('types', '')]) message = _urlencode(body).encode('utf8') msg = "GET\n{base_url}\n{path}\n{msg1}".format( base_url=HUOBI_API_ONLY, path=HUOBI_GET_TRADE_HISTORY, msg1=message) signature = sign_string_256_base64(key.secret, msg) body.append(("Signature", signature)) final_url += _urlencode(body) params = {} post_details = PostRequestDetails(final_url, HUOBI_GET_HEADERS, params) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "get orders history huobi: {res}".format(res=post_details) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return post_details