-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiengine.py
158 lines (131 loc) · 4.99 KB
/
apiengine.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import csv
import poloniex as pol
import cexapi as cex
import statuses as s
def start_apis():
keys = {}
with open('keys.csv', 'rb') as csvfile:
r = csv.reader(csvfile)
i = 0
a = []
for row in r:
if i == 0:
a = row
else:
keys[row[0]] = {}
dic = keys[row[0]]
for col in range(1,len(row)):
if col != "":
dic[a[col]] = row[col]
i += 1
c = keys["cex"]
capi = cex.cex(c["username"],c["public key"],c["private key"])
capi.INDEX = 0
if(capi.balance().has_key('username')):
print(s.SUCCESS + "Connected to CEX.io")
else:
print(s.ERROR + "Unable to connected to CEX.io")
p = keys["poloniex"]
papi = pol.poloniex(p["public key"],p["private key"])
papi.INDEX = 1
if(papi.returnBalances().has_key('BTC')):
print(s.SUCCESS + "Connected to Poloniex")
else:
print(s.ERROR + "Unable to connected to Poloniex")
context = (capi,papi)
return context
CONTEXT = start_apis()
def get_currencies(context):
#TODO: would like for this to work automatically some day
return [("BTC","USD"),("LTC","USD"),("ETH","USD")]
CURRENCIES = get_currencies(CONTEXT)
def get_balance(context,currencies):
balance = []
for x in range(len(context)):
name = context[x].__class__.__name__
api = context[x]
if(name == 'cex'): #CEX.io api
bal = api.balance()
for c in currencies:
if(float(bal[c[0]]['available']) < 0.2 and c[1] == "USD"):
print(s.WARNING + "Cex.io: Low " + c[0] + " balance")
balance.append(bal)
elif(name == 'poloniex'): #Poloniex api
bal = api.api_query('returnCompleteBalances')
for c in currencies:
if(float(bal[c[0]]['available']) < 0.2 and c[1] == "USD"):
print(s.WARNING + "Poloniex: Low " + c[0] + " balance")
balance.append(bal)
return balance
BALANCE = get_balance(CONTEXT,CURRENCIES)
def get_addresses(context, currencies):
addresses = []
for x in range(len(context)):
name = context[x].__class__.__name__
api = context[x]
if(name == 'cex'): #CEX.io api
addrs = []
for curr in currencies:
for x in curr:
if(x != "USD"):
addr = api.api_call('get_address', {"currency":x}, 1)
if(addr['ok'] == 'ok'):
addrs.append((x,addr['data']))
else:
print(s.ERROR + "CEX.io - " + "Currency \"" + x + "\" expected but no address found")
addresses.append(addrs)
elif(name == 'poloniex'): #Poloniex api
call = api.api_query('returnDepositAddresses')
addrs = []
for curr in currencies:
for x in curr:
if(x != "USD"):
for y,data in call.iteritems():
if(x==y):
addrs.append((y,data))
addresses.append(addrs)
return addresses
ADDRESSES = get_addresses(CONTEXT,CURRENCIES)
def get_books(context,currencies,start,end): #start is inclusive, end is exclusive
books = []
for x in range(len(context)):
name = context[x].__class__.__name__
api = context[x]
if(name == 'cex'): #CEX.io api
b = []
for curr in currencies:
book = api.order_book(curr[0] + "/" + curr[1])
if(book.has_key("error")):
print(s.ERROR + book["error"] + " Order: " + curr[0] + "/" + curr[1])
else:
bok = (book['bids'][start:end][start:end],book['asks'][start:end][start:end])
b.append(bok)
books.append(b)
elif(name == 'poloniex'): #Poloniex api
b = []
for curr in currencies:
c = curr[1]
if(curr[1] == "USD"):
c = c + "T"
book = api.returnOrderBook(c + "_" + curr[0])
if(book.has_key("error")):
print(s.ERROR + book["error"] + " Order: " + c + "_" + curr[0])
else:
bok = (book['bids'][start:end][start:end],book['asks'][start:end][start:end])
b.append(bok)
books.append(b)
return books
#TODO: get the real fees
def get_fees(context):
fees = []
for x in range(len(context)):
name = context[x].__class__.__name__
api = context[x]
if(name == 'cex'): #CEX.io api
f = []
fees.append(f)
elif(name == 'poloniex'): #Poloniex api
f = []
fees.append(f)
return fees
FEES = get_fees(CONTEXT)