-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathliqui.py
81 lines (59 loc) · 2.29 KB
/
liqui.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
import hmac
import hashlib
from time import time
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
import requests
class LiquiApiError(Exception):
pass
class Liqui:
def __init__(self, key=None, secret=None):
self._key = key
self._secret = secret
def info(self):
return self._v3('info')
def ticker(self, pair):
return self._v3('ticker', pair)
def depth(self, pair):
return self._v3('depth', pair)
def trades(self, pair):
return self._v3('trades', pair)
def balances(self):
funds = self.get_info()['funds']
return {currency: balance for currency, balance in funds.items() if balance != 0}
def get_info(self):
return self._tapi(method='getInfo')
def buy(self, pair, rate, amount):
return self.trade(pair, 'buy', rate, amount)
def sell(self, pair, rate, amount):
return self.trade(pair, 'sell', rate, amount)
def trade(self, pair, type, rate, amount):
return self._tapi(method='Trade', pair=pair, type=type, rate=rate, amount=amount)
def active_orders(self, pair=''):
return self._tapi(method='ActiveOrders', pair=pair)
def order_info(self, order_id):
return self._tapi(method='OrderInfo', order_id=order_id)
def cancel_order(self, order_id):
return self._tapi(method='CancelOrder', order_id=order_id)
def trade_history(self, **params):
return self._tapi(method='TradeHistory', **params)
def _v3(self, *args):
url = 'https://api.liqui.io/api/3/' + '/'.join(args)
r = requests.get(url)
return r.json()
def _tapi(self, **params):
if not self._key or not self._secret:
raise LiquiApiError('api keys not configured')
params.update(nonce=int(time()))
headers = {'Key': self._key, 'Sign': self._sign(params)}
resp = requests.post('https://api.liqui.io/tapi', data=params, headers=headers)
data = resp.json()
if 'error' in data:
raise LiquiApiError(data['error'])
return data.get('return', data)
def _sign(self, data):
if isinstance(data, dict):
data = urlencode(data)
return hmac.new(self._secret.encode(), data.encode(), hashlib.sha512).hexdigest()