-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExchange.py
345 lines (244 loc) · 11.9 KB
/
Exchange.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# -*- coding: utf-8 -*-
import sys
import time
import datetime
import requests
import Exchanges.Binance
import Exchanges.Bitfinex
import Exchanges.Bithumb
import Exchanges.Bitstamp
import Exchanges.Bittrex
import Exchanges.Coinbase
import Exchanges.Coinone
import Exchanges.Gdax
import Exchanges.Gemini
import Exchanges.Hitbtc
import Exchanges.Kraken
import Exchanges.Kucoin
import Exchanges.Poloniex
class Exchange:
exchanges = {'Binance' : Exchanges.Binance.Binance(),
'Bitfinex' : Exchanges.Bitfinex.Bitfinex(),
'Bithumb' : Exchanges.Bithumb.Bithumb(),
'Bitstamp' : Exchanges.Bitstamp.Bitstamp(),
'Bittrex' : Exchanges.Bittrex.Bittrex(),
'Coinbase' : Exchanges.Coinbase.Coinbase(),
'Coinone' : Exchanges.Coinone.Coinone(),
'Gdax' : Exchanges.Gdax.Gdax(),
'Gemini' : Exchanges.Gemini.Gemini(),
'Hitbtc' : Exchanges.Hitbtc.Hitbtc(),
'Kraken' : Exchanges.Kraken.Kraken(),
'Kucoin' : Exchanges.Kucoin.Kucoin(),
'Poloniex' : Exchanges.Poloniex.Poloniex(),
}
def __init__(self):
self.valid_pairs = {self.exchanges[e].name : self.exchanges[e].valid_pairs for e in self.exchanges}
rates = requests.get('https://api.exchangeratesapi.io/latest?base=USD').json()['rates']
self.fiatRates = {r : float(rates[r]) for r in rates}
self.supportedFiat = [r for r in self.fiatRates]
self.supportedFiat.append('USD')
def getFiatRate(self, base, quote):
if base == quote:
return 1.0
if base == 'USD':
return self.fiatRates[quote]
if quote == 'USD':
return 1.0 / self.fiatRates[base]
return self.fiatRates[quote] / self.fiatRates[base]
def getDataTimed(self, exchange, base, quote):
delta = (datetime.datetime.now() - self.exchanges[exchange].lastCheck).total_seconds();
while delta < self.exchanges[exchange].wait_time:
time.sleep(0.1)
delta = (datetime.datetime.now() - self.exchanges[exchange].lastCheck).total_seconds();
data = self.exchanges[exchange].getData(base, quote)
self.exchanges[exchange].lastCheck = datetime.datetime.now()
return data
def reverseData(self, data):
data['base'], data['quote'] = data['quote'], data['base']
data['price'] = 1.0 / data['price']
data['volume24hbase'], data['volume24hquote'] = data['volume24hquote'], data['volume24hbase']
data['high24h'], data['low24h'] = 1.0 / data['low24h'], 1.0 / data['high24h']
if data['change24h'] != -1:
data['change24h'] = -1.0 * data['change24h']
return data
def getData(self, exchange, base, quote):
baseIsFiat = True if base in self.supportedFiat else False
quoteIsFiat = True if quote in self.supportedFiat else False
# Fiat to fiat
if baseIsFiat and quoteIsFiat:
return self.getFiatRate(base, quote)
# Get assets info
class AssetInfo:
def __init__(self):
self.exists = False
self.isBase = False
self.bases = []
baseInfo = AssetInfo()
quoteInfo = AssetInfo()
for i in self.valid_pairs[exchange]:
if i == base:
baseInfo.exists = True
baseInfo.isBase = True
if i == quote:
quoteInfo.exists = True
quoteInfo.isBase = True
if base in self.valid_pairs[exchange][i]:
baseInfo.exists = True
baseInfo.bases.append(i)
if quote in self.valid_pairs[exchange][i]:
quoteInfo.exists = True
quoteInfo.bases.append(i)
# None of assets exist in the exchange
if not baseInfo.exists and not quoteInfo.exists:
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = -1
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = -1
data['volume24hquote'] = -1
return data
# Both assets exist in the exchange
if baseInfo.exists and quoteInfo.exists:
# Direct rate
if baseInfo.isBase and base in quoteInfo.bases:
return self.getDataTimed(exchange, base, quote)
# Inverse rate
if quoteInfo.isBase and quote in baseInfo.bases:
return self.reverseData(self.getDataTimed(exchange, quote, base))
# Both are quotes
if not baseInfo.isBase and not quoteInfo.isBase:
# Common base
for baseBase in baseInfo.bases:
if baseBase in quoteInfo.bases:
dataBase = self.getDataTimed(exchange, baseBase, base)
dataQuote = self.getDataTimed(exchange, baseBase, quote)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = dataQuote['price'] / dataBase['price']
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = dataBase['volume24hquote']
data['volume24hquote'] = dataQuote['volume24hquote']
return data
# Different base
for baseBase in baseInfo.bases:
for quoteBase in quoteInfo.bases:
if baseBase in self.valid_pairs[exchange][quoteBase]:
dataBase = self.getDataTimed(exchange, baseBase, base)
dataQuote = self.getDataTimed(exchange, quoteBase, quote)
dataBaseBase = self.getDataTimed(exchange, quoteBase, baseBase)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = dataQuote['price'] / (dataBase['price'] * dataBaseBase['price'])
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = dataBase['volume24hquote']
data['volume24hquote'] = dataQuote['volume24hquote']
return data
if quoteBase in self.valid_pairs[exchange][baseBase]:
dataBase = self.getDataTimed(exchange, baseBase, base)
dataQuote = self.getDataTimed(exchange, quoteBase, quote)
dataQuoteBase = self.getDataTimed(exchange, baseBase, quoteBase)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = (dataQuote['price'] * dataQuoteBase['price']) / dataBase['price']
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = dataBase['volume24hquote']
data['volume24hquote'] = dataQuote['volume24hquote']
return data
for peer in self.valid_pairs[exchange][baseBase]:
if peer in self.valid_pairs[exchange][quoteBase]:
dataBase = self.getDataTimed(exchange, baseBase, base)
dataQuote = self.getDataTimed(exchange, quoteBase, quote)
dataPeerBase = self.getDataTimed(exchange, baseBase, peer)
dataPeerQuote = self.getDataTimed(exchange, quoteBase, peer)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = (dataQuote['price'] * dataPeerBase['price']) / (dataBase['price'] * dataPeerQuote['price'])
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = dataBase['volume24hquote']
data['volume24hquote'] = dataQuote['volume24hquote']
return data
# One is base, one is quote
if baseInfo.isBase and not quoteInfo.isBase:
for quoteBase in quoteInfo.bases:
if quoteBase in self.valid_pairs[exchange][base]:
dataBase = self.getDataTimed(exchange, base, quoteBase)
dataQuote = self.getDataTimed(exchange, quoteBase, quote)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = dataBase['price'] * dataQuote['price']
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = dataBase['volume24hbase']
data['volume24hquote'] = dataQuote['volume24hquote']
return data
if not baseInfo.isBase and quoteInfo.isBase:
for baseBase in baseInfo.bases:
if baseBase in self.valid_pairs[exchange][quote]:
dataBase = self.getDataTimed(exchange, baseBase, base)
dataQuote = self.getDataTimed(exchange, quote, baseBase)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = 1 / (dataBase['price'] * dataQuote['price'])
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = dataBase['volume24hquote']
data['volume24hquote'] = dataQuote['volume24hbase']
return data
# Only one of the assets exist in the exchange
else:
if not baseInfo.exists and baseIsFiat:
baseRate = self.getFiatRate('USD', base)
dataQuote = self.getData(exchange, 'USD', quote)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = dataQuote['price'] / baseRate
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = -1
data['volume24hquote'] = dataQuote['volume24hquote']
return data
if not quoteInfo.exists and quoteIsFiat:
dataBase = self.getData(exchange, 'USD', base)
quoteRate = self.getFiatRate('USD', quote)
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = quoteRate / dataBase['price']
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = dataBase['volume24hquote']
data['volume24hquote'] = -1
return data
# Couldn't find rate
data = {}
data['base'] = base
data['quote'] = quote
data['price'] = -1
data['low24h' ] = -1
data['high24h'] = -1
data['change24h'] = -1
data['volume24hbase'] = -1
data['volume24hquote'] = -1
return data