def triangular(update, context): try: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) graph = asyncio.get_event_loop().run_until_complete( load_exchange_graph('binance', fees=True)) paths = bellman_ford(graph, 'BTC') i = 0 for path in paths: lines = get_opportunity_for_path(graph, path) if i % 3 == 0: try: context.bot.send_message(chat_id=update.message.chat_id, text=lines) except: time.sleep(5) context.bot.send_message(chat_id=update.message.chat_id, text=lines) pass lines = "" if i > 30: break i += 1 replyStr = "\nAbra sua conta na binance: https://www.binance.com/?ref=16836135" update.message.reply_text(replyStr) except Exception as e: print(str(e)) pass
def trade_from_source(exchange, source, amount): """ Should not be used in its current form due to the following: 1. It is possible that the trades upon which the algorithm detected an arbitrage opportunity have been executed (and thus the opportunity may or may not still exist) 2. The Bellman Ford implementation does not (currently) account for the amounts specified in each order. This is the biggest issue. 3. It is not maximally profitable as it iterates through each negative cycle (arbitrage opportunity) only once. This is a bare-bones proof-of-concept: it shows how the algorithm could be used for financial gain. todo: implement an asynchronous version for exchanges which allow margin trading. :param exchange: A ccxt Exchange object. Should be "pre-loaded" with all necessary data (such as the API key). :param source: The ticker for any currency in exchange. :param amount: Starting amount of source that will be traded. """ loop = asyncio.get_event_loop() graph = loop.run_until_complete( load_exchange_graph(exchange, name=False, fees=True)) paths = bellman_ford(graph, source, loop_from_source=True, unique_paths=True) for path in paths: for i in range(len(path) - 1): loop.run_until_complete( exchange.create_order( path[i] + '/' + path[i + 1], 'limit', 'sell', amount, math.exp(-graph[path[i]][path[i + 1]]['weight'])), ) amount *= math.exp(-graph[path[i]][path[i + 1]]['weight'])
import asyncio from peregrinearb import load_exchange_graph, print_profit_opportunity_for_path, bellman_ford loop = asyncio.get_event_loop() graph = loop.run_until_complete(load_exchange_graph('hitbtc')) paths = bellman_ford(graph, 'BTC', unique_paths=True) for path in paths: print_profit_opportunity_for_path(graph, path)
def test_load_exchange_graph(self): currencies = ['BTC', 'ETH', 'USD', 'LTC'] tickers = { 'BTC/USD': { 'bid': 5995, 'ask': 6000, 'bidVolume': 0.5, 'askVolume': 0.9 }, 'ETH/BTC': { 'bid': 0.069, 'ask': 0.07, 'bidVolume': 0.5, 'askVolume': 21 }, 'ETH/USD': { 'bid': 495, 'ask': 500, 'bidVolume': 30, 'askVolume': 0.9 }, 'LTC/USD': { 'bid': 81, 'ask': 82, 'bidVolume': 0.5, 'askVolume': 0.9 }, 'LTC/BTC': { 'bid': 0.121, 'ask': 0.122, 'bidVolume': 0.5, 'askVolume': 0.9 }, 'LTC/ETH': { 'bid': 90, 'ask': 100, 'bidVolume': 0.5, 'askVolume': 0.9 } } symbols = [symbol for symbol in tickers.keys()] markets = {symbol: {'taker': 0.001} for symbol in symbols} exchange = TestExchange(name='a', currencies=currencies, tickers=tickers, symbols=symbols, markets=markets) graph = asyncio.get_event_loop().run_until_complete( load_exchange_graph(exchange, name=False, fees=True, suppress=[''], depth=True, tickers=tickers)) for symbol, quote_data in tickers.items(): base, quote = symbol.split('/') self.assertEqual( graph[base][quote]['weight'], -math.log(quote_data['bid'] * (1 - markets[symbol]['taker']))) self.assertEqual(graph[base][quote]['depth'], -math.log(quote_data['bidVolume'])) self.assertEqual( graph[quote][base]['weight'], -math.log( (1 - markets[symbol]['taker']) / quote_data['ask'])) self.assertEqual( graph[quote][base]['depth'], -math.log(quote_data['askVolume'] * quote_data['ask'])) self.assertEqual(symbol, graph[base][quote]['market_name'])
import asyncio from peregrinearb import load_exchange_graph, print_profit_opportunity_for_path, bellman_ford loop = asyncio.get_event_loop() graph = loop.run_until_complete(load_exchange_graph('binance')) paths = bellman_ford(graph, 'BTC', unique_paths=True) for path in paths: print_profit_opportunity_for_path(graph, path)