print('--------------------------------------') #getPairUrl print('test getPairUrl: ') print(u.getPairUrl('btc-usd','https://btc-e.com/api/3/depth/%asset1%_%asset2%')) print(u.getPairUrl('btc-uro','https://poloniex.com/public?command=returnOrderBook&currencyPair=%ASSET1%_%ASSET2%&depth=50')) print('--------------------------------------') #init all exchanges print('initExchangesConcurrency: ') u.initExchangesConcurrency(exchanges,assets,logger) print('--------------------------------------') #getAssetsFromPair print('getAssetsFromPair: ') print(u.getAssetsFromPair('btc-usd')) print(u.getAssetsFromPair('btc-_nexus')) print(u.getAssetsFromPair('atomic-btc')) print('--------------------------------------') ## #Exchange - init ## print('test Exchange: ') ## exch = u.getExchange(exchanges,'hitbtc') ## print(exch) ## istatus,err = exch.initPairs(assets) ## if istatus==0: ## print('initPairs ok') ## else: ## print('initPairs failed: ') ## print(err) ## print('--------------------------------------')
def log_arbitrage(exchanges,pairs,logger,profit_usd_min=10.0,details=False): try: resultDetails = 'Arbitrage opportunities details:\n' resultSummary = 'Arbitrage opportunities summary:\n' listResult = [] #we are going to adjust exchanges after each potential buy/sell #so we need a copy here exchanges_copy = exchanges.copy() for pair in pairs: #we assume there is arb opportunity hasArb = True atLeastOne = False total_profit_usd = 0.0 asset1,asset2 = u.getAssetsFromPair(pair) dicExchange = {} while hasArb: #search for lowest offer/higher bid bid,ask,vol_bid,vol_ask,bid_exch,ask_exch = searchBest(pair,exchanges_copy) volume = min(vol_bid,vol_ask) if bid==None or ask==None or volume==None: break #profit in USD differs according to the pair: #case XXX/USD (easiest) if asset2=='usd': profit_usd = (bid-ask)*volume order1 = 0 order2 = 1 else: #here we need btc-usd btc_usd_fxrate = getAveragePrice(exchanges_copy,'btc-usd') #case XXX/BTC if asset2=='btc': profit_usd = (bid-ask)*volume*btc_usd_fxrate order1 = 0 order2 = 1 #case BTC/XXX elif asset1=='btc': #here we need to reverse pair from BTC/XXX to XXX/BTC #to handle it the same way as previous case bidtmp = bid bid = 1/ask ask = 1/bidtmp exchtmp = bid_exch bid_exch = ask_exch ask_exch = exchtmp order1 = 1 order2 = 0 profit_usd = (bid-ask)*volume*btc_usd_fxrate #if USD profit>0, we log and update the exchange accordingly if profit_usd > 0: atLeastOne = True total_profit_usd += profit_usd resultDetails += (pair + ': ' + 'buy ' + str(round(volume,4)) + ' ' + asset1 + ' from ' + ask_exch + ' @' + str(round(ask,4)) + ',' + ' sell it to ' + bid_exch + ' @' + str(round(bid,4)) + ' for a profit of $' + str(round(profit_usd,2)) + ' (total profit $' + str(round(total_profit_usd,2)) + ')\n') u.getExchange(exchanges_copy,ask_exch).executeMarketOrder(order1,pair,volume) u.getExchange(exchanges_copy,bid_exch).executeMarketOrder(order2,pair,volume) updateDicExchangeCouple(dicExchange,ask_exch + '%%%' + bid_exch,profit_usd) #if profit is negative, we are done with this pair.. else: hasArb = False if atLeastOne: resultDetails += '----------------------------------------------------------------------------------------------------\n' if total_profit_usd>profit_usd_min: exchange_couple_list = dicToList(dicExchange) exchange_couple_list.sort(reverse=True) listResult.append((total_profit_usd,pair,exchange_couple_list)) #we log all info at the end of the procedure listResult.sort(reverse=True) if len(listResult)==0: resultSummary = 'There is no arbitrage opportunity superior to $' + str(round(profit_usd_min,2)) else: for v in listResult: resultSummary += v[1] + ': $' + str(round(v[0],2)) for t in v[2]: exch_from,exch_to = getExchFromCouple(t[1]) resultSummary += ' [$' + str(round(t[0],2)) + ' from ' + exch_from + ' to ' + exch_to + ']' resultSummary += '\n' if details: logger.info('\n########################################################\n' + resultSummary + '****************************************************\n' + resultDetails,bPrint=True) else: logger.info('\n########################################################\n' + resultSummary,bPrint=True) return listResult except Exception as e: logger.fatal(str(e)) return None