示例#1
0
##    istatus,err = exch.initPairs(assets)
##    if istatus==0:
##        print('initPairs ok')
##    else:
##        print('initPairs failed: ')
##        print(err)
##    print('--------------------------------------')

##    #getAllAssetsFromExchanges
##    print('test getAllAssetsFromExchanges: ')
##    print(u.getAllAssetsFromExchanges(exchanges))
##    print('--------------------------------------')
    
    #Exchange - bid offer
    print('test Exchange - getBestBidOffer: ')
    exch = u.getExchange(exchanges,'okcoin')
    pair = 'btc-usd'
    exch.refreshMarketDepth(pair)
    print(exch.getBestBidOffer(pair))
    print(exch.getBestBidOffer(pair,withVolume=True))
    print('--------------------------------------')

    #Exchange - getInfo
    print('test Exchange - getInfo: ')
    print(exch.getInfo(pair))
    print('--------------------------------------')

    #Exchange - executeMarketOrder
    print('test Exchange - executeMarketOrder: ')
    exch = u.getExchange(exchanges,'btc-e')
    pair = 'btc-usd'
                if len(l) > 1:
                    print('exchanges command don\'t take any argument')
                else:
                    print('List of exchanges we are listening to:')
                    for exchange in exchanges:
                        print(exchange.name)
            
            #display best bid/offer for given exchange/assetpair
            #for instance: 'getbo okcoin btc-usd'
            #if call with only exchange in argument, will display b/o for all assetpair for the given exchange
            elif l[0]=='getbo':
                if len(l)==2:
                    if not ExchangeExists(l[1],exchanges):
                        print('exchange not recognized: ' + l[1])
                    else:
                        exch = u.getExchange(exchanges,l[1])
                        print('best bid/offer for ' + exch.name + ' exchange:')
                        [print(pair + ': ' + str(exch.getBestBidOffer(pair))
                               + ' (' + str(exch.getUpdatedTime(pair)) + ')') for pair in exch.pairs]
                        print('#pairs: ' + str(len(exch.pairs)))
                elif len(l)==3:
                    print(u.getExchange(exchanges,l[1]).getBestBidOffer(l[2].lower()))
                else:
                    print('getbo correct usage: getbo %exchange_name% [%assetpair%]')

            #display and log informations about exchange given in argument:
            #volume, bid, offer, last updated time
            #will be logged into 'exchange_info_yyyy-mm-dd.log' file
            #if used without argument, will log info for all exchanges
            elif l[0]=='loginfo':
                if len(l)==1:
示例#3
0
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