def getUSDPrice(symbol): symbol = getSymbol(symbol) if symbol == None: return None url = "https://api.btcmarkets.net/market/%s/AUD/tick"%symbol response = util.requestTimeout(url=url,timeout=waittime,errMsg='cannot get market data in BTCMarkets') if response is None: return None data = {} data['source'] = 'BTCMarkets' data['bid'] = response.json()['bestBid'] data['ask'] = response.json()['bestAsk'] data['trade'] = response.json()['lastPrice'] response = util.requestTimeout(url='http://free.currencyconverterapi.com/api/v5/convert?q=AUD_USD&compact=y',timeout =0.1) if response is not None: exchange_rate = float(response.json()['AUD_USD']['val']) else: response = util.requestTimeout(url='https://api.exchangeratesapi.io/latest?base=AUD',timeout=0.1) if response is not None: exchange_rate = response.json()['rates']['USD'] else: exchange_rate = 0.70 data['bid'] = round(float(data['bid'])*exchange_rate,4) data['ask'] = round(float(data['ask'])*exchange_rate,4) data['trade'] = round(float(data['trade'])*exchange_rate,4) print(data) return data
def getUSDPrice(symbol): symbol = getSymbol(symbol) if symbol ==None: return None url = "https://api.bitfinex.com/v1/pubticker/"+symbol response = util.requestTimeout(url=url,timeout=waittime,errMsg='cannot get market data from bitfinex') if response == None: return None data = {} data['source'] = 'bitfinex' data['bid'] = response.json()['bid'] data['ask'] = response.json()['ask'] data['trade'] = response.json()['last_price'] if symbol.endswith('usd'): print(data) return data else: response = util.requestTimeout(url='https://api.bitfinex.com/v1/pubticker/btcusd',timout =waittime, errMsg='cannot get btcusd in bitfinex') if response == None: return None btc_price = float(response.json()['last_price']) data['source'] = 'bitfinex' data['bid'] = float(data['bid'])*btc_price data['ask'] = float(data['ask'])*btc_price data['trade'] = float(data['trade'])*btc_price print(data) return data
def getUSDPrice(symbol): symbol = symbol.lower() + '_' + 'usdt' url = "https://data.gateio.io/api2/1/ticker/%s" % symbol response = util.requestTimeout(url=url, timeout=waittime, errMsg='cannot get market data from gateio') if response == None: return None data = {} data['source'] = 'gateio.io' try: data['bid'] = response.json()['highestBid'] data['ask'] = response.json()['lowestAsk'] data['trade'] = response.json()['last'] print(data) return data except: return None symbol = symbol.lower() + '_' + 'btc' url = "https://data.gateio.io/api2/1/ticker/%s" % symbol response = util.requestTimeout(url=url, timeout=waittime, errMsg='cannot get market data from gateio') if response is None: return None data = {} data['source'] = 'gateio.io' try: data['bid'] = response.json()['highestBid'] data['ask'] = response.json()['lowestAsk'] data['trade'] = response.json()['last'] except: return None response = util.requestTimeout( url='https://data.gateio.io/api2/1/ticker/btc_usdt', timeout=waittime, errMsg='cannot get market data from gateio') try: btc_price = float(response.json()['price']) except: return None data['bid'] = float(data['bid']) * btc_price data['ask'] = float(data['ask']) * btc_price data['trade'] = float(data['trade']) * btc_price print(data) return data
def getSymbol(code): symbol_list = util.requestTimeout(url='https://api.btcmarkets.net/v2/market/active',timeout =waittime,errMsg='cannot get symbol in BTCMarkets') if symbol_list == None: return None for i in symbol_list.json()['markets']: symbol = i['instrument'] if code == symbol: return symbol return None
def getSymbol(code): symbol_list = util.requestTimeout(url ='https://api.bitfinex.com/v1/symbols',timeout=waittime,errMsg='cannot get symbols in bitfinex!') if symbol_list is None: return None for i in symbol_list.json(): if i.startswith(code.lower()) & i.endswith('usd'): return i for i in symbol_list.json(): if i.startswith(code.lower()) & i.endswith('btc'): return i return None
def getUSDPrice(symbol): symbol = getSymbol(symbol) if symbol ==None: return None url = "https://api.binance.com/api/v3/ticker/bookTicker?symbol=%s"%symbol print(url) response = util.requestTimeout(url=url,timeout=waittime,errMsg='cannot get bookTicker in binance') if response is None: return None data = {} data['source'] = 'binance' data['bid'] = float(response.json()['bidPrice']) data['ask'] = float(response.json()['askPrice']) URL = "https://api.binance.com/api/v1/trades" payload = {'symbol': symbol, 'limit':1} response = util.requestTimeout(url=URL,timeout=waittime,errMsg='cannot get trade in binance',payload=payload) if response is None: return None data['trade'] = float(response.json()[0]['price']) if symbol.endswith('USDT'): print(data) return data else: response = util.requestTimeout(url='https://api.binance.com/api/v1/trades?symbol=BTCUSDT&limit=1',timeout=waittime, errMsg='cannot get btc usd price in binance') if response is None: return None btc_price = float(response.json()[0]['price']) data['source'] = 'binance' data['bid'] = round(float(data['bid'])*btc_price,4) data['ask'] = round(float(data['ask'])*btc_price,4) data['trade'] = round(float(data['trade'])*btc_price,4) print(data) return data
def getUSDPrice(symbol): symbol = getSymbol(symbol) if symbol == None: return None url = "http://api.coinbene.com/v1/market/ticker?symbol=%s" % symbol response = util.requestTimeout( url=url, timeout=waittime, errMsg='cannot get market data from coinbene') if response is None: return None data = {} data['source'] = 'coinbene' data['bid'] = response.json()['ticker'][0]['bid'] data['ask'] = response.json()['ticker'][0]['ask'] data['trade'] = response.json()['ticker'][0]['last'] if symbol.endswith('USDT'): print(data) return data else: response = util.requestTimeout( url='http://api.coinbene.com/v1/market/ticker?symbol=btcusdt', timeout=waittime, errMsg='cannot get market data from coinbene') if response == None: return data btc_price = float(response.json()['ticker'][0]['last']) print(btc_price) data['source'] = 'coinbene' data['bid'] = round(float(data['bid']) * btc_price, 4) data['ask'] = round(float(data['ask']) * btc_price, 4) data['trade'] = round(float(data['trade']) * btc_price, 4) print(data) return data
def getSymbol(code): symbol_list = util.requestTimeout( url= 'https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes', timeout=waittime, errMsg='cannot get symbol from independentreserve') if symbol_list is None: return None symbol_list = symbol_list.json() symbol_list_lower = [x.lower() for x in symbol_list] if code == 'BTC': return 'xbt' else: if code.lower() in symbol_list_lower: return code.lower() return None
def getUSDPrice(symbol): symbol = getSymbol(symbol) url = "https://bittrex.com/api/v1.1/public/getticker?market=%s" % symbol response = util.requestTimeout(url=url, timeout=waittime, errMsg='cannote get market data in bittrex') if response == None: return None if response.json()['result'] == None: return None data = {} data['source'] = 'bittrex' data['bid'] = response.json()['result']['Bid'] data['ask'] = response.json()['result']['Ask'] data['trade'] = response.json()['result']['Last'] print(data) return data
def getUSDPrice(symbol): symbol = getSymbol(symbol) url = "https://poloniex.com/public?command=returnTicker" response = util.requestTimeout(url=url, timeout=waittime, errMsg='cannot get symbol from poloniex') if response is None: return None data = {} data['source'] = 'poloniex' try: data['bid'] = response.json()[symbol]['highestBid'] data['ask'] = response.json()[symbol]['lowestAsk'] data['trade'] = response.json()[symbol]['last'] print(data) return data except: return None
def getUSDPrice(symbol): symbol = getSymbol(symbol) if symbol == None: return None url = "https://www.okcoin.com/api/v1/ticker.do?symbol=%s" % symbol response = util.requestTimeout(url=url, timeout=waittime, errMsg='cannot get market data from okcoin') if response is None: return None data = {} data['source'] = 'OKcoin' data['bid'] = response.json()['ticker']['buy'] data['ask'] = response.json()['ticker']['sell'] data['trade'] = response.json()['ticker']['last'] print(data) return data
def getSymbol(code): symbol_list = util.requestTimeout(url='https://api.binance.com/api/v1/exchangeInfo',timeout=waittime,errMsg='cannot get exchangeInfo in binance!') if symbol_list == None: return None for i in symbol_list.json()['symbols']: symbol = i['symbol'] quoteasset= i['quoteAsset'] if symbol.startswith(code.upper()) & (quoteasset == 'USDT'): return symbol for i in symbol_list.json()['symbols']: symbol = i['symbol'] quoteasset= i['quoteAsset'] if symbol.startswith(code.upper()) & (quoteasset == 'BTC'): return symbol return None
def getUSDPrice(symbol): symbol = getSymbol(symbol) if symbol == None: return None URL = "https://api.independentreserve.com/Public/GetMarketSummary" payload = {'primaryCurrencyCode': symbol, 'secondaryCurrencyCode': 'usd'} response = util.requestTimeout( url=URL, payload=payload, timeout=waittime, errMsg='cannot get market data from independentreserve') if response is None: return None data = {} data['source'] = 'independentreserve' data['bid'] = response.json()['CurrentHighestBidPrice'] data['ask'] = response.json()['CurrentLowestOfferPrice'] data['trade'] = response.json()['LastPrice'] print(data) return data
def getSymbol(code): symbol_list = util.requestTimeout( url='http://api.coinbene.com/v1/market/symbol', timeout=waittime, errMsg='cannot get symbol from coinbene') if symbol_list is None: return None for i in symbol_list.json()['symbol']: symbol = i['baseAsset'] quoteasset = i['quoteAsset'] if (symbol == code) & (quoteasset == 'USDT'): return i['ticker'] for i in symbol_list.json()['symbol']: symbol = i['baseAsset'] quoteasset = i['quoteAsset'] if (symbol == code.upper()) & (quoteasset == 'BTC'): return i['ticker'] return None
def getSymbol(code): symbol_list = util.requestTimeout( url= 'https://api.independentreserve.com/Public/GetValidPrimaryCurrencyCodes', timeout=waittime, errMsg='cannot get symbol from independentreserve')