Beispiel #1
0
 def test_depth(self):
     # currently does not work as bellman_ford (of NegativeWeightDepthFinder) is broken.
     for i in range(1, 5):
         G = nx.DiGraph()
         G.add_edge('A', 'B', weight=-0.69, depth=1)
         G.add_edge('B', 'C', weight=-1.1, depth=1)
         G.add_edge('C', 'A', weight=1.39, depth=i)
         paths = bellman_ford(G,
                              'A',
                              unique_paths=True,
                              depth=True,
                              loop_from_source=False)
         total = 0
         for path in paths:
             total += 1
         self.assertEquals(total, 0)
     for i in range(6, 8):
         G = nx.DiGraph()
         G.add_edge('A', 'B', weight=-0.69, depth=1)
         G.add_edge('B', 'C', weight=-1.1, depth=1)
         G.add_edge('C', 'A', weight=1.39, depth=i)
         paths = bellman_ford(G, 'A', unique_paths=True, depth=True)
         total = 0
         for path in paths:
             total += 1
         self.assertEquals(total, 1)
    def test_depth(self):
        # currently does not work as bellman_ford's depth feature is broken/ in development.
        for i in range(1, 4):
            G = nx.DiGraph()
            # for the depth of A->B, 0 == -math.log(1). also note weight of A->B == depth B->C.
            G.add_edge('A', 'B', weight=-math.log(2), depth=0)
            G.add_edge('B', 'C', weight=-math.log(3), depth=-math.log(2))
            # there should be weight of 6 after going through A->B->C.
            G.add_edge('C', 'A', weight=-math.log(1 / 8), depth=-math.log(i))
            finder = NegativeWeightFinder(G, depth=True)
            paths = finder.bellman_ford('A',
                                        loop_from_source=False,
                                        unique_paths=True)

            total = 0
            for path in paths:
                self.assertLessEqual(
                    1,
                    calculate_profit_ratio_for_path(G,
                                                    path,
                                                    depth=True,
                                                    starting_amount=1))

        for i in range(6, 8):
            G = nx.DiGraph()
            G.add_edge('A', 'B', weight=-math.log(2), depth=0)
            G.add_edge('B', 'C', weight=-math.log(3), depth=-math.log(2))
            G.add_edge('C', 'A', weight=-math.log(1 / 4), depth=-math.log(i))
            paths = bellman_ford(G, 'A', unique_paths=True, depth=True)
Beispiel #3
0
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'])
Beispiel #4
0
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
Beispiel #5
0
 def test_ratio(self):
     G = nx.DiGraph()
     G.add_edge('A', 'B', weight=-0.69)
     G.add_edge('B', 'C', weight=-1.1)
     G.add_edge('C', 'A', weight=1.39)
     paths = bellman_ford(G, 'A', unique_paths=True, loop_from_source=False)
     for path in paths:
         self.assertEquals(calculate_profit_ratio_for_path(G, path), 1.5)
Beispiel #6
0
 def test_ensure_profit_yields_profit(self):
     graph = nx.DiGraph()
     graph.add_edge(0, 1, weight=4)
     graph.add_edge(1, 0, weight=3)
     graph.add_edge(1, 2, weight=-1)
     graph.add_edge(2, 3, weight=-1)
     graph.add_edge(3, 1, weight=-1)
     paths = bellman_ford(graph, 0, loop_from_source=True, ensure_profit=True)
     for path in paths:
         weight = 0
         for i in range(len(path) - 1):
             weight += graph[path[i]][path[i + 1]]['weight']
         self.assertLess(weight, 0)
Beispiel #7
0
    def test_ratio(self):
        G = nx.DiGraph()
        G.add_edge('A', 'B', weight=-math.log(2))
        G.add_edge('B', 'C', weight=-math.log(3))
        G.add_edge('C', 'A', weight=-math.log(1 / 4))
        paths = bellman_ford(G, 'A', unique_paths=True)
        path_count = 0

        for path in paths:
            path_count += 1
            self.assertAlmostEqual(calculate_profit_ratio_for_path(G, path), 1.5)

        # assert that unique_paths allows for only one path
        self.assertEqual(path_count, 1)
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)