loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests
import sets
from collections import defaultdict


FIATS = ['USD', 'SGD', 'MYR', 'CAD']
CRYPTO_FEE = 0.00095
FIAT_FEE = 0.0018

def get_fee(c1, c2):
    if c1 in FIATS or c2 in FIATS:
        return FIAT_FEE
    else:
        return CRYPTO_FEE

s = requests.Session() # use HTTP/1.1 to share the same connection across requests to minimise latency
insts = s.post('http://api.coinut.com/', '{"request": "inst_list", "sec_type": "SPOT", "nonce": 1234}').json()['SPOT']

while True:
    r = s.get('https://api.coinut.com/spot').json()
    curs = sets.Set()
    rates = defaultdict(dict)
    for a in r:
        inst = insts[a['asset']][0]
        base = inst['base']
        quote = inst['quote']

        curs.add(base)
        curs.add(quote)
        rates[base][quote] = float(a['highest_buy'])
        rates[quote][base] = 1.0/float(a['lowest_sell'])

    print "--------------------------- Triangular (Three-point) arbitrage opportunities -----------------------------------"
    for c1 in curs:
        for c2 in curs:
            for mid in curs:
                if c1 in rates and mid in rates and c2 in rates and mid in rates[c1] and c2 in rates[mid] and c1 in rates[c2]:
                    pair1_rate = rates[c1][mid] * (1-get_fee(c1, mid))
                    pair2_rate = rates[mid][c2] * (1-get_fee(mid, c2))
                    pair3_rate = rates[c2][c1] * (1-get_fee(c2, c1))
                    total_rate = pair1_rate * pair2_rate * pair3_rate
                    if total_rate > 1:
                        print "%4s %4s %4s %15.8f %15.8f %15.8f %15.8f" % (c1, mid, c2, rates[c1][mid], rates[mid][c2], rates[c2][c1], total_rate)


    print "--------------------------- Four-point arbitrage opportunities -----------------------------------"
    for c1 in curs:
        for c2 in curs:
            for c3 in curs:
                for c4 in curs:
                    if c1 in rates and c2 in rates and c3 in rates and c2 in rates[c1] and c3 in rates[c2] and c4 in rates[c3] and c1 in rates[c4]:
                        pair1_rate = rates[c1][c2] * (1-get_fee(c1, c2))
                        pair2_rate = rates[c2][c3] * (1-get_fee(c2, c3))
                        pair3_rate = rates[c3][c4] * (1-get_fee(c3, c4))
                        pair4_rate = rates[c4][c1] * (1-get_fee(c4, c1))
                        total_rate = pair1_rate * pair2_rate * pair3_rate * pair4_rate

                        if total_rate > 1:
                            print "%4s %4s %4s %4s %15.8f %15.8f %15.8f %15.8f %15.8f" % (c1, c2, c3, c4, rates[c1][c2], rates[c2][c3], rates[c3][c4], rates[c4][c1], total_rate)