-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbitfinex.py
139 lines (103 loc) · 4.5 KB
/
bitfinex.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
from decimal import Decimal
import requests
import json
import base64
import hmac
import hashlib
import time
import types
__all__ = ['Bitfinex']
BITFINEX = 'api.bitfinex.com/'
DECIMAL_KEYS = set(['amount', 'ask', 'available', 'bid', 'executed_amount', 'high', 'last_price', 'low', 'mid', 'original_amount', 'price', 'remaining_amount', 'timestamp', 'volume'])
def decimalize(obj, keys):
if isinstance(obj, types.ListType):
return [decimalize(xs, keys) for xs in obj]
if not isinstance(obj, types.DictType):
return obj
#print obj
def to_decimal(k, val):
if val == None:
return None
if isinstance(val, types.ListType):
return [decimalize(ys, keys) for ys in val]
if k in keys:
return Decimal(val)
return val
return { k: to_decimal(k, obj[k]) for k in obj }
def undecimalize(obj):
if isinstance(obj, types.ListType):
return map(undecimalize, obj)
if not isinstance(obj, types.DictType):
return obj
#print obj
def from_decimal(val):
if isinstance(val, Decimal):
return str(val)
return val
return { k: from_decimal(obj[k]) for k in obj }
class Bitfinex(object):
def ticker(self, symbol="btcusd"):
r = requests.get("https://"+BITFINEX+"/v1/ticker/"+symbol, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def today(self, symbol="btcusd"):
r = requests.get("https://"+BITFINEX+"/v1/today/"+symbol, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def book(self, payload, symbol="btcusd"):
headers = self._prepare_payload(False, payload)
r = requests.get("https://"+BITFINEX+"/v1/book/"+symbol, headers=headers, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def trades(self, payload, symbol="btcusd"):
headers = self._prepare_payload(False, payload)
r = requests.get("https://"+BITFINEX+"/v1/trades/"+symbol, headers=headers, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def symbols(self):
r = requests.get("https://"+BITFINEX+"/v1/symbols", verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def order_new(self, payload):
payload["request"] = "/v1/order/new"
payload["nonce"] = str(long(time.time() * 100000))
headers = self._prepare_payload(True, payload)
r = requests.post("https://"+BITFINEX+"/v1/order/new", headers=headers, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def order_cancel(self, payload):
payload["request"] = "/v1/order/cancel"
payload["nonce"] = str(long(time.time() * 100000))
headers = self._prepare_payload(True, payload)
r = requests.post("https://"+BITFINEX+"/v1/order/cancel", headers=headers, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def order_status(self, payload):
payload["request"] = "/v1/order/status"
payload["nonce"] = str(long(time.time() * 100000))
headers = self._prepare_payload(True, payload)
r = requests.get("https://"+BITFINEX+"/v1/order/status", headers=headers, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def orders(self):
payload = {}
payload["request"] = "/v1/orders"
payload["nonce"] = str(long(time.time() * 100000))
headers = self._prepare_payload(True, payload)
r = requests.get("https://"+BITFINEX+"/v1/orders", headers=headers, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
def balances(self):
payload = {}
payload["request"] = "/v1/balances"
payload["nonce"] = str(long(time.time() * 100000))
headers = self._prepare_payload(True, payload)
r = requests.get("https://"+BITFINEX+"/v1/balances", headers=headers, verify=False)
return decimalize(r.json(), DECIMAL_KEYS)
# Private
def _prepare_payload(self, should_sign, d):
j = json.dumps(undecimalize(d))
data = base64.standard_b64encode(j)
if should_sign:
h = hmac.new(self.secret, data, hashlib.sha384)
signature = h.hexdigest()
return {
"X-BFX-APIKEY": self.key,
"X-BFX-SIGNATURE": signature,
"X-BFX-PAYLOAD": data,
}
else:
return {
"X-BFX-PAYLOAD": data,
}