-
Notifications
You must be signed in to change notification settings - Fork 23
/
coinex_api.py
142 lines (127 loc) · 6.06 KB
/
coinex_api.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
import collections
import exchange_api
import hashlib
import hmac
import json
try:
import http.client
import urllib.request
import urllib.error
import urllib.parse
except ImportError:
# Python 2.7 compatbility
import httplib
class http: client = httplib
import urllib
import urllib2
class urllib: request = urllib2; error = urllib2; parse = urllib
class Market(exchange_api.Market):
def __init__(self, exchange, source_currency_id, target_currency_id, trade_pair_id,
reverse_market):
exchange_api.Market.__init__(self, exchange)
self._source_currency_id = source_currency_id
self._source_currency = exchange._GetCurrencyName(source_currency_id)
self._target_currency_id = target_currency_id
self._target_currency = exchange._GetCurrencyName(target_currency_id)
self._trade_pair_id = trade_pair_id
self._reverse_market = reverse_market
def GetSourceCurrency(self):
return self._source_currency
def GetTargetCurrency(self):
return self._target_currency
def GetTradeMinimum(self):
return 0.01
def GetPublicOrders(self):
try:
request_vars = {'tradePair' : self._trade_pair_id}
orders = self._exchange._Request('orders', request_vars=request_vars)
return ([exchange_api.Order(self, order['id'], True,
float(order['amount']) / pow(10, 8),
float(order['rate']) / pow(10, 8)) for
order in orders if order['bid']],
[exchange_api.Order(self, order['id'], False,
float(order['amount']) / pow(10, 8),
float(order['rate']) / pow(10, 8)) for
order in orders if not order['bid']])
except (TypeError, LookupError) as e:
raise exchange_api.ExchangeException(e)
def CreateOrder(self, bid_order, amount, price):
if self._reverse_market:
bid_order = not bid_order
order = {'trade_pair_id' : self._trade_pair_id,
'amount' : int(amount * pow(10, 8)),
'bid' : bid_order,
'rate' : max(1, int(price * pow(10, 8)))}
post_data = json.dumps({'order' : order}).encode('utf-8')
try:
order_id = self._exchange._PrivateRequest('orders', post_data, 'order')['id']
return exchange_api.Order(self, order_id, bid_order, amount, price)
except (TypeError, LookupError) as e:
raise exchange_api.ExchangeException(e)
class CoinEx(exchange_api.Exchange):
@staticmethod
def GetName():
return 'CoinEx'
def __init__(self, api_key, api_secret):
self.api_url = 'https://coinex.pw/api/v2/'
self.api_headers = {'Content-type' : 'application/json',
'Accept' : 'application/json',
'User-Agent' : 'autocoin-autosell'}
self.api_key = api_key
self.api_secret = api_secret.encode('utf-8')
try:
self._currency_names = {currency['id'] : currency['name'] for
currency in self._Request('currencies')}
self._markets = collections.defaultdict(dict)
for trade_pair in self._Request('trade_pairs'):
market1 = Market(self, trade_pair['currency_id'], trade_pair['market_id'],
trade_pair['id'], False)
self._markets[market1.GetSourceCurrency()][market1.GetTargetCurrency()] = market1
market2 = Market(self, trade_pair['market_id'], trade_pair['currency_id'],
trade_pair['id'], True)
self._markets[market2.GetSourceCurrency()][market2.GetTargetCurrency()] = market2
except (TypeError, LookupError) as e:
raise exchange_api.ExchangeException(e)
def _GetCurrencyName(self, currency_id):
return self._currency_names.get(currency_id, '#%s' % currency_id)
def _Request(self, method, request_vars=None, headers=None, post_data=None, json_root=None):
request_string = '?' + urllib.parse.urlencode(request_vars) if request_vars else ''
if headers is None:
headers = {}
headers.update(self.api_headers.items())
try:
request = urllib.request.Request(self.api_url + method + request_string,
post_data, headers)
response = urllib.request.urlopen(request)
try:
response_json = json.loads(response.read().decode('utf-8'))
if not json_root:
json_root = method
if not json_root in response_json:
raise exchange_api.ExchangeException('JSON root "%s" not in "%s".' %
(json_root, method))
return response_json[json_root]
finally:
response.close()
except (urllib.error.URLError, urllib.error.HTTPError, http.client.HTTPException,
ValueError) as e:
raise exchange_api.ExchangeException(e)
def _PrivateRequest(self, method, post_data=None, json_root=None):
hmac_data = b'' if not post_data else post_data
digest = hmac.new(self.api_secret, hmac_data, hashlib.sha512).hexdigest()
headers = {'API-Key' : self.api_key,
'API-Sign': digest}
return self._Request(method, headers=headers, post_data=post_data, json_root=json_root)
def GetCurrencies(self):
return self._currency_names.values()
def GetMarkets(self):
return self._markets
def GetBalances(self):
balances = {}
try:
for balance in self._PrivateRequest('balances'):
balances[self._GetCurrencyName(balance['currency_id'])] = (
float(balance['amount']) / pow(10, 8))
except (TypeError, LookupError) as e:
raise exchange_api.ExchangeException(e)
return balances