-
Notifications
You must be signed in to change notification settings - Fork 5
/
FinexAPI.py
384 lines (253 loc) · 7.8 KB
/
FinexAPI.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# Python implementation. Written by jndok - 2014. Distributed under GNU/GPL v3 (http://opensource.org/licenses/GPL-3.0). [v. 0.0.1-4]
# Edited by Dawson Botsford
# report ANY bug @ https://github.com/dawsonbotsford/bitfinex/issues
import requests
import json
import base64
import hmac
import hashlib
import time
__all__ = ['ticker', 'today', 'orderbook', 'lendbook', 'stats', 'trades', 'lends', 'symbols', 'place_order', 'delete_order', 'delete_all_order', 'status_order', 'active_orders', 'active_positions', 'place_offer', 'cancel_offer', 'status_offer', 'active_offers', 'past_trades', 'balances', 'claim_position', 'close_position', 'withdraw']
URL = "https://api.bitfinex.com/v1"
fp = open("keys.txt")
API_KEY = fp.readline().rstrip() # put your API public key here.
API_SECRET = fp.readline().rstrip() # put your API private key here.
print ("Your pub: " + str(API_KEY))
print ("Your priv: " + str(API_SECRET))
#define currency and symbol
currency = 'btc'
symbol = 'ethusd'
# unauthenticated
def ticker(symbol=symbol): # gets the innermost bid and asks and information on the most recent trade.
r = requests.get(URL + "/pubticker/" + symbol, verify=True) # <== UPDATED TO LATEST VERSION OF BFX!
rep = r.json()
try:
rep['last_price']
except KeyError:
return rep['message']
return rep
def stats(symbol=symbol): # Various statistics about the requested pairs.
r = requests.get(URL + "/stats/" + symbol, verify=True) # <== UPDATED TO LATEST VERSION OF BFX!
rep = r.json()
return rep
try:
rep['volume']
except KeyError:
return rep['message']
def today(symbol=symbol): # today's low, high and volume.
r = requests.get(URL + "/today/" + symbol, verify=True)
rep = r.json()
try:
rep['volume']
except KeyError:
return rep['message']
return rep
def orderbook(symbol=symbol): # get the full order book.
r = requests.get(URL + "/book/" + symbol, verify=True)
rep = r.json()
return rep
def lendbook(currency=currency): # get the full lend book.
r = requests.get(URL + "/lendbook/" + currency, verify=True)
rep = r.json()
return rep
def trades(symbol=symbol): # get a list of the most recent trades for the given symbol.
r = requests.get(URL + "/trades/" + symbol, verify=True)
rep = r.json()
return rep
def lends(currency=currency): # get a list of the most recent lending data for the given currency: total amount lent and rate (in % by 365 days).
r = requests.get(URL + "/lends/" + currency, verify=True)
rep = r.json()
return rep
def symbols(): # get a list of valid symbol IDs.
r = requests.get(URL + "/symbols", verify=True)
rep = r.json()
return rep
# authenticated
def genNonce(): # generates a nonce, used for authentication.
return str(int(time.time() * 1000000))
def payloadPacker(payload): # packs and signs the payload of the request.
j = json.dumps(payload)
data = base64.b64encode(j.encode())
h = hmac.new(bytes(API_SECRET , 'latin-1'), data, hashlib.sha384)
signature = h.hexdigest()
return {
"X-BFX-APIKEY": API_KEY,
"X-BFX-SIGNATURE": signature,
"X-BFX-PAYLOAD": data
}
def place_order(amount, price, side, ord_type, symbol=symbol, exchange='bitfinex'): # submit a new order.
payload = {
"request":"/v1/order/new",
"nonce":genNonce(),
"symbol":symbol,
"amount":amount,
"price":price,
"exchange":exchange,
"side":side,
"type":ord_type
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/order/new", headers=signed_payload, verify=True)
rep = r.json()
try:
rep['order_id']
except:
return rep['message']
return rep
def delete_order(order_id): # cancel an order.
payload = {
"request":"/v1/order/cancel",
"nonce":genNonce(),
"order_id":order_id
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/order/cancel", headers=signed_payload, verify=True)
rep = r.json()
try:
rep['avg_execution_price']
except:
return rep['message']
return rep
def delete_all_order(): # cancel an order.
payload = {
"request":"/v1/order/cancel/all",
"nonce":genNonce(),
}
signed_payload = (payload)
r = requests.post(URL + "/order/cancel/all", headers=signed_payload, verify=True)
rep = r.json()
return rep
'''
try:
rep['avg_execution_price']
except:
return rep['message']
'''
def status_order(order_id): # get the status of an order. Is it active? Was it cancelled? To what extent has it been executed? etc.
payload = {
"request":"/v1/order/status",
"nonce":genNonce(),
"order_id":order_id
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/order/status", headers=signed_payload, verify=True)
rep = r.json()
try:
rep['avg_execution_price']
except:
return rep['message']
return rep
def active_orders(): # view your active orders.
payload = {
"request":"/v1/orders",
"nonce":genNonce()
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/orders", headers=signed_payload, verify=True)
rep = r.json()
return rep
def active_positions(): # view your active positions.
payload = {
"request":"/v1/positions",
"nonce":genNonce()
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/positions", headers=signed_payload, verify=True)
rep = r.json()
return rep
def claim_position(position_id): # Claim a position.
payload = {
"request":"/v1/position/claim",
"nonce":genNonce(),
"position_id":position_id
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/position/claim", headers=signed_payload, verify=True)
rep = r.json()
return rep
def close_position(position_id): # Claim a position.
payload = {
"request":"/v1/position/close",
"nonce":genNonce(),
"position_id":position_id
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/position/close", headers=signed_payload, verify=True)
rep = r.json()
return rep
def past_trades(timestamp=0, symbol=symbol): # view your past trades
payload = {
"request":"/v1/mytrades",
"nonce":genNonce(),
"symbol":symbol,
"timestamp":timestamp
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/mytrades", headers=signed_payload, verify=True)
rep = r.json()
return rep
def place_offer(currency, amount, rate, period, direction):
payload = {
"request":"/v1/offer/new",
"nonce":genNonce(),
"currency":currency,
"amount":amount,
"rate":rate,
"period":period,
"direction":direction
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/offer/new", headers=signed_payload, verify=True)
rep = r.json()
return rep
def cancel_offer(offer_id):
payload = {
"request":"/v1/offer/cancel",
"nonce":genNonce(),
"offer_id":offer_id
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/offer/cancel", headers=signed_payload, verify=True)
rep = r.json()
return rep
def status_offer(offer_id):
payload = {
"request":"/v1/offer/status",
"nonce":genNonce(),
"offer_id":offer_id
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/offer/status", headers=signed_payload, verify=True)
rep = r.json()
return rep
def active_offers():
payload = {
"request":"/v1/offers",
"nonce":genNonce()
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/offers", headers=signed_payload, verify=True)
rep = r.json()
return rep
def balances(): # see your balances.
payload = {
"request":"/v1/balances",
"nonce":genNonce()
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/balances", headers=signed_payload, verify=True)
rep = r.json()
return rep
def withdraw(withdraw_type, walletselected, amount, address):
payload = {
"request":"/v1/withdraw",
"nonce":genNonce(),
"withdraw_type":withdraw_type,
"walletselected":walletselected,
"amount":amount,
"address":address
}
signed_payload = payloadPacker(payload)
r = requests.post(URL + "/withdraw", headers=signed_payload, verify=True)
rep = r.json()
return rep