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
import requests
import sets
from collections import defaultdict


FIATS = ['USD', 'SGD', 'MYR', 'CAD']

# change the following fees based on your own account level. 
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('https://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 arbitrage opportunities -----------------------------------"
    for c1 in curs:
        for c2 in curs:
            if c1 == c2:
                continue

            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)