def __init__(self, exchange_name, ticker, delay=1000): threading.Thread.__init__(self) self.exchange_name = exchange_name self.ticker = ticker self.api = exchanges.get_exchange(exchange_name) self.delay = delay self.runloop = True
def exchange(exchange_list): results = [] for en in exchange_list: try: e = exchanges.get_exchange(en.lower()) except: results.append("Uknown exchange %s" % en) continue results.append("%s:\n" % en) for ul in e.get_supported_underlyings(): bid = e.get_quote(ul, 'bid') ask = e.get_quote(ul, 'ask') last = e.get_quote(ul, 'last') if bid == 0 or ask == 0: spread = 1 else: spread = (ask - bid) / ((ask + bid) / 2) if last > 1: results.append( "%s: Last trade %.2f. Market %.2f/ %.2f (%.1f%% wide)" % (ul, last, bid, ask, spread * 100)) else: results.append( "%s: Last trade %.3g. Market %.3g / %.3g (%.1f%% wide)" % (ul, last, bid, ask, spread * 100)) results.append("--------------") return results
def bitmex(fut): if fut[:3] == 'XBT': exch = ['gdax', 'bitstamp'] spot = 'BTCUSD' else: exch = ['poloniex'] spot = fut[:3] + 'BTC' fut_stream = bm.get_stream(fut) if fut_stream == None: fut_stream = bm.init_symbol(fut) fut_bid = bm.get_quote(fut, 'bid') fut_ask = bm.get_quote(fut, 'ask') if fut_bid == None or fut_ask == None: message = "Something went wrong, check future code" return message spot_bid = 0 spot_ask = 0 i = 0 for en in exch: e = exchanges.get_exchange(en) spot_bid += e.get_quote(spot, 'bid') spot_ask += e.get_quote(spot, 'ask') i += 1 spot_bid = float(spot_bid / i) spot_ask = float(spot_ask / i) basis_bid = fut_bid - spot_ask basis_ask = fut_ask - spot_bid spot_mid = (spot_bid + spot_ask) / 2 fut_mid = (fut_ask + fut_bid) / 2 basis_mid = (basis_ask + basis_bid) / 2 if basis_ask < 0: basis_premium = 100 * basis_ask / spot_mid else: basis_premium = 100 * basis_bid / spot_mid if spot_bid > 1: message = "Spot %s price is %.2f: %.2f / %.2f" % ( spot, spot_mid, spot_bid, spot_ask) + '\n' message += "Fut %s price is %.2f: %.2f / %.2f" % ( fut, fut_mid, fut_bid, fut_ask) + '\n' message += "Basis is %.2f: %.2f / %.2f. Premium of %.1f%%" % ( basis_mid, basis_bid, basis_ask, basis_premium) else: message = "Spot %s price is %.4g: %.4g / %.4g" % ( spot, spot_mid, spot_bid, spot_ask) + '\n' message += "Fut %s price is %.4g: %.4g / %.4g" % ( fut, fut_mid, fut_bid, fut_ask) + '\n' message += "Basis is %.4g: %.4g / %.4g. Premium of %.1f%%" % ( basis_mid, basis_bid, basis_ask, basis_premium) return message
def fx(underlying, exchange, cross_ccy): FORCCY = underlying[:3].upper() DOMCCY = underlying[-3:].upper() if cross_ccy in ('USD', 'EUR', 'JPY'): FORPAIR = DOMCCY + cross_ccy DOMPAIR = FORCCY + cross_ccy else: FORPAIR = cross_ccy + FORCCY DOMPAIR = cross_ccy + DOMCCY if exchange == "all": forExchanges = exchanges.get_exchanges_list_for_underlying(FORPAIR) domExchanges = exchanges.get_exchanges_list_for_underlying(DOMPAIR) intersectExchanges = list(set(forExchanges).intersection(domExchanges)) elif exchange.lower() not in exchanges.get_exchanges_list(): return ['Unsupported exchange %s' % exchange] else: if (FORPAIR in exchanges.get_exchange( exchange.lower()).get_supported_underlyings()) and ( DOMPAIR in exchanges.get_exchange( exchange.lower()).get_supported_underlyings()): intersectExchanges = [exchange] else: return [ "Exchange %s does not support %s and %s" % (exchange, FORPAIR, DOMPAIR) ] results = [] if cross_ccy in ('USD', 'EUR', 'JPY'): e = exchanges.get_exchange(intersectExchanges[0].lower()) fxRate = float(e.get_quote(underlying, 'last')) results.append('%s rate for %s is %.5g' % (intersectExchanges[0], underlying, fxRate)) else: url = 'http://www.apilayer.net/api/live?access_key=' + CL_API_KEY + '¤cies=' + FORCCY + ',' + DOMCCY r = requests.get(url) r.raise_for_status() j = r.json() if j['success']: domOfficialRate = r.json()['quotes']['USD' + DOMCCY] forOfficialRate = r.json()['quotes']['USD' + FORCCY] fxRate = domOfficialRate / forOfficialRate results.append('Currency Layer %s FX rate is %.5g' % (underlying, fxRate)) else: fxRate = 0 results.append('Could not retrieve %s FX rate from CurrencyLayer' % underlying) results.append('Using %s as the cross currency: %s and %s' % (cross_ccy.upper(), FORPAIR, DOMPAIR)) for exch in intersectExchanges: e = exchanges.get_exchange(exch.lower()) try: fx_bid = e.get_quote(DOMPAIR, 'bid') / e.get_quote(FORPAIR, 'ask') fx_ask = e.get_quote(DOMPAIR, 'ask') / e.get_quote(FORPAIR, 'bid') if fxRate != 0 and (fx_bid > fxRate or fx_ask < fxRate): if fx_bid > fxRate: arb = 100 * (float(fx_bid) / fxRate - 1) else: arb = 100 * (fxRate / float(fx_ask) - 1) results.append( '%s on %s: bid %.5g / %.5g ask. %.2f%% arb vs %.5g official rate' % (underlying, exch, fx_bid, fx_ask, arb, fxRate)) else: results.append('%s on %s: bid %.5g / %.5g ask' % (underlying, exch, fx_bid, fx_ask)) except ZeroDivisionError: results.append('%s: one of the quotes is worth 0' % exch) return results
def price(underlying, exchange_list): if underlying == 'BITCOIN': underlying = 'BTCUSD' all_requested = False if exchange_list == ['all']: all_requested = True exchange_list = exchanges.get_exchanges_list_for_underlying(underlying) if exchange_list == []: return ['No exchange support %s' % underlying] bestbid = 0.00000001 bestask = 1000000000 bestspread = 100 bestbid_exch = exchange_list[0] bestask_exch = exchange_list[0] bestspread_exch = exchange_list[0] results = [] i = 0 for exchange_name in exchange_list: if exchange_name.lower() in exchanges.exchange_list.keys(): exchange = exchanges.get_exchange(exchange_name.lower()) if underlying in exchange.get_supported_underlyings(): bid = exchange.get_quote(underlying, 'bid') ask = exchange.get_quote(underlying, 'ask') try: price = (bid + ask) / 2 spread = 100 * (ask - bid) / price if not all_requested or spread < 3.5: if bid > bestbid: bestbid = bid bestbid_exch = exchange_name if ask < bestask: bestask = ask bestask_exch = exchange_name if spread < bestspread: bestspread = spread bestspread_exch = exchange_name i = i + 1 if price > 1: results.append( "%s %s price is %.2f: %.2f / %.2f (%.2f%% wide)" % (exchange_name, underlying, price, bid, ask, spread)) else: results.append( "%s %s price is %.4g: %.4g / %.4g (%.2f%% wide)" % (exchange_name, underlying, price, bid, ask, spread)) except: results.append("%s price update failed" % exchange_name) else: results.append('%s not supported for %s' % (underlying, exchange_name)) else: results.append("Unknown exchange: %s" % exchange_name) if i >= 2: spread = 100 * (bestask / bestbid - 1) if price > 1: results.append( "Max bid is on %s: %.2f\nMin offer is on %s: %.2f.\nBest spread is on %s: %.2f%%\nAggregated price is %.1f%% wide (negative means arbitrageable)" % (bestbid_exch, bestbid, bestask_exch, bestask, bestspread_exch, bestspread, spread)) else: results.append( "Max bid is on %s: %.4g\nMin offer is on %s: %.4g.\nBest spread is on %s: %.2f%%\nAggregated price is %.1f%% wide (negative means arbitrageable)" % (bestbid_exch, bestbid, bestask_exch, bestask, bestspread_exch, bestspread, spread)) return results
import exchanges import requests import math logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s-%(message)s', level=logging.INFO) logger = logging.getLogger(__name__) BOTNAME = '@BotName_Bot' TOKEN = 'TOKEN' CL_API_KEY = 'CLAPIKEY' millnames = ['', ' k', ' mio', ' bio', ' trillion'] bm = exchanges.get_exchange('bitmex') def millify(n): try: n = float(n) except: n = 0 millidx = max( 0, min( len(millnames) - 1, int(math.floor(0 if n == 0 else math.log10(abs(n)) / 3)))) return '{:.1f}{}'.format(n / 10**(3 * millidx), millnames[millidx])
import exchanges print(exchanges.get_exchanges_list()) print(exchanges.get_underlyings_list()) print(exchanges.get_exchanges_list_for_underlying('ETHBTC')) # exchanges.get_all_quotes(['XRPBTC']) for en in ['kraken', 'bitfinex', 'bittrex']: e = exchanges.get_exchange(en) print('== %s ==' % en) for u in e.get_supported_underlyings(): print(u) bid = e.get_quote(u, 'bid') ask = e.get_quote(u, 'ask') print('[%s, %s]' % (bid, ask))