-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathbitfinex.py
193 lines (133 loc) · 5.76 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
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
import requests
import json
PROTOCOL = "https"
HOST = "api.bitfinex.com"
VERSION = "v1"
PATH_SYMBOLS = "symbols"
PATH_TICKER = "ticker/%s"
PATH_TODAY = "today/%s"
PATH_STATS = "stats/%s"
PATH_LENDBOOK = "lendbook/%s"
PATH_ORDERBOOK = "book/%s"
class Client(object):
"""
Client for the bitfinex.com API.
See https://www.bitfinex.com/pages/api for API documentation.
"""
def __initialize__(self):
pass
def server(self):
return "%s://%s/%s" % (PROTOCOL, HOST, VERSION)
def url_for(self, path, path_arg=None, parameters=None):
# build the basic url
url = "%s/%s" % (self.server(), path)
# If there is a path_arh, interpolate it into the URL.
# In this case the path that was provided will need to have string
# interpolation characters in it, such as PATH_TICKER
if path_arg:
url = url % (path_arg)
# Append any parameters to the URL.
if parameters:
url = "%s?%s" % (url, self._build_parameters(parameters))
return url
def symbols(self):
"""
GET /symbols
curl https://api.bitfinex.com/v1/symbols
['btcusd','ltcusd','ltcbtc']
"""
return self._get(self.url_for(PATH_SYMBOLS))
def ticker(self, symbol):
'''
GET /ticker/:symbol
curl https://api.bitfinex.com/v1/ticker/btcusd
{
'ask': '562.9999',
'timestamp': '1395552290.70933607',
'bid': '562.25',
'last_price': u'562.25',
'mid': u'562.62495'}
'''
data = self._get(self.url_for(PATH_TICKER, (symbol)))
# convert all values to floats
return self._convert_to_floats(data)
def today(self, symbol):
'''
GET /today/:symbol
curl "https://api.bitfinex.com/v1/today/btcusd"
{"low":"550.09","high":"572.2398","volume":"7305.33119836"}
'''
data = self._get(self.url_for(PATH_TODAY, (symbol)))
# convert all values to floats
return self._convert_to_floats(data)
def stats(self, symbol):
'''
curl https://api.bitfinex.com/v1/stats/btcusd
[
{"period":1,"volume":"7410.27250155"},
{"period":7,"volume":"52251.37118006"},
{"period":30,"volume":"464505.07753251"}
]
'''
data = self._get(self.url_for(PATH_STATS, (symbol)))
for period in data:
for key, value in period.items():
if key == 'period':
new_value = int(value)
elif key == 'volume':
new_value = float(value)
period[key] = new_value
return data
def lendbook(self, currency, parameters=None):
'''
curl "https://api.bitfinex.com/v1/lendbook/btc"
{"bids":[{"rate":"5.475","amount":"15.03894663","period":30,"timestamp":"1395112149.0","frr":"No"},{"rate":"2.409","amount":"14.5121868","period":7,"timestamp":"1395497599.0","frr":"No"}],"asks":[{"rate":"6.351","amount":"15.5180735","period":5,"timestamp":"1395549996.0","frr":"No"},{"rate":"6.3588","amount":"626.94808249","period":30,"timestamp":"1395400654.0","frr":"Yes"}]}
Optional parameters
limit_bids (int): Optional. Limit the number of bids (loan demands) returned. May be 0 in which case the array of bids is empty. Default is 50.
limit_asks (int): Optional. Limit the number of asks (loan offers) returned. May be 0 in which case the array of asks is empty. Default is 50.
'''
data = self._get(self.url_for(PATH_LENDBOOK, path_arg=currency, parameters=parameters))
for lend_type in data.keys():
for lend in data[lend_type]:
for key, value in lend.items():
if key in ['rate', 'amount', 'timestamp']:
new_value = float(value)
elif key == 'period':
new_value = int(value)
elif key == 'frr':
new_value = value == 'Yes'
lend[key] = new_value
return data
def orderbook(self, symbol, parameters=None):
'''
curl "https://api.bitfinex.com/v1/book/btcusd"
{"bids":[{"price":"561.1101","amount":"0.985","timestamp":"1395557729.0"}],"asks":[{"price":"562.9999","amount":"0.985","timestamp":"1395557711.0"}]}
The 'bids' and 'asks' arrays will have multiple bid and ask dicts.
Optional parameters
limit_bids (int): Optional. Limit the number of bids returned. May be 0 in which case the array of bids is empty. Default is 50.
limit_asks (int): Optional. Limit the number of asks returned. May be 0 in which case the array of asks is empty. Default is 50.
eg.
curl "https://api.bitfinex.com/v1/book/btcusd?limit_bids=1&limit_asks=0"
{"bids":[{"price":"561.1101","amount":"0.985","timestamp":"1395557729.0"}],"asks":[]}
'''
data = self._get(self.url_for(PATH_ORDERBOOK, path_arg=symbol, parameters=parameters))
for type_ in data.keys():
for list_ in data[type_]:
for key, value in list_.items():
list_[key] = float(value)
return data
def _convert_to_floats(self, data):
"""
Convert all values in a dict to floats
"""
for key, value in data.items():
data[key] = float(value)
return data
def _get(self, url):
return json.loads(requests.get(url).content.decode())
def _build_parameters(self, parameters):
# sort the keys so we can test easily in Python 3.3 (dicts are not
# ordered)
keys = list(parameters.keys())
keys.sort()
return '&'.join(["%s=%s" % (k, parameters[k]) for k in keys])