-
Notifications
You must be signed in to change notification settings - Fork 7
/
cascadebot.py
428 lines (356 loc) · 15.3 KB
/
cascadebot.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Cascade lending bot for Bitfinex. Places lending offers at a high rate, then
gradually lowers them until they're filled.
This is intended as a proof of concept alternative to fractional reserve rate
(FRR) loans. FRR lending heavily distorts the swap market on Bitfinex. My hope
is that Bitfinex will remove the FRR, and implement an on-site version of this
bot for lazy lenders (myself included) to use instead.
Git repo: https://github.com/ah3dce/cascadebot
Bitcoin tips: 1Fk1G8yVtXQLC1Eft4r1kS8e3SZyRaFwbM
Requires Python 3 and the requests library:
https://pypi.python.org/pypi/requests/
Fill in the parameters below, and then run it with:
python3 cascadebot.py
"""
from decimal import Decimal
from datetime import datetime, timedelta
# API key stuff
BITFINEX_API_KEY = "API_KEY_GOES_HERE"
BITFINEX_API_SECRET = b"API_SECRET_GOES_HERE"
# Set this to False if you don't want the bot to make USD offers
LEND_USD = True
# Set this to False if you don't want the bot to make BTC offers
LEND_BTC = True
# Rate to start our USD offers at, in percentage per year
USD_START_RATE_PERCENT = Decimal("365.0")
# Don't reduce our USD offers below this rate, in percentage per year
USD_MINIMUM_RATE_PERCENT = Decimal("YOU HAVE TO PICK THIS ONE YOURSELF")
# How often to reduce the rates on our unfilled USD offers
USD_RATE_REDUCTION_INTERVAL = timedelta(hours=1)
# How much to reduce the rates on our unfilled USD offers, in percentage per
# year
USD_RATE_DECREMENT_PERCENT = Decimal("35.0")
# ADVANCED: Use this to reduce interest rates exponentially instead of
# linearly. If you don't understand what this means, don't change this
# parameter (leave it at "1.0"). Interest rates will decay towards the minimum
# value rather than towards zero:
#
# new_rate = (current_rate - min_rate) * multiplier + min_rate
#
# If you use this, you should set USD_RATE_DECREMENT_PERCENT to zero, otherwise
# both reductions will be applied.
USD_RATE_EXPONENTIAL_DECAY_MULTIPLIER = Decimal("1.0")
# How many days we're willing to lend our USD funds for
USD_LEND_PERIOD_DAYS = 30
# Don't try to make USD offers smaller than this. Bitfinex currently doesn't
# allow loan offers smaller than $50.
USD_MINIMUM_LEND_AMOUNT = Decimal("50.0")
# Rate to start our BTC offers at, in percentage per year
BTC_START_RATE_PERCENT = Decimal("20.0")
# Don't reduce our BTC offers below this rate, in percentage per year
BTC_MINIMUM_RATE_PERCENT = Decimal("YOU HAVE TO PICK THIS ONE YOURSELF")
# How often to reduce the rates on our unfilled BTC offers
BTC_RATE_REDUCTION_INTERVAL = timedelta(hours=1)
# How much to reduce the rates on our unfilled BTC offers, in percentage per
# year
BTC_RATE_DECREMENT_PERCENT = Decimal("2.0")
# ADVANCED: Use this to reduce interest rates exponentially instead of
# linearly. If you don't understand what this means, don't change this
# parameter (leave it at "1.0"). Interest rates will decay towards the minimum
# value rather than towards zero:
#
# new_rate = (current_rate - min_rate) * multiplier + min_rate
#
# If you use this, you should set BTC_RATE_DECREMENT_PERCENT to zero, otherwise
# both reductions will be applied.
BTC_RATE_EXPONENTIAL_DECAY_MULTIPLIER = Decimal("1.0")
# How many days we're willing to lend our BTC funds for
BTC_LEND_PERIOD_DAYS = 30
# Don't try to make BTC offers smaller than this. Bitfinex currently doesn't
# allow loan offers smaller than $50.
BTC_MINIMUM_LEND_AMOUNT = Decimal("0.25")
# How often to retrieve the current statuses of our offers
POLL_INTERVAL = timedelta(minutes=5)
from itertools import count
import time
import base64
import json
import hmac
import hashlib
from collections import defaultdict, deque
import requests
class Offer(object):
"""
An unfilled swap offer.
"""
def __init__(self, offer_dict):
"""
Args:
offer_dict: Dictionary of data for a single swap offer as returned
by the Bitfinex API.
"""
self.id = offer_dict["id"]
self.currency = offer_dict["currency"]
self.rate = Decimal(offer_dict["rate"])
self.submitted_at = datetime.utcfromtimestamp(int(Decimal(
offer_dict["timestamp"]
)))
self.amount = Decimal(offer_dict["remaining_amount"])
def __repr__(self):
return (
"Offer(id={}, currency='{}', rate={}, amount={}, submitted_at={})"
).format(self.id, self.currency, self.rate, self.amount,
self.submitted_at)
def get_new_rate(self):
"""
Calculate what the interest rate on this offer should be changed to,
based on how much time has elapsed since it was submitted.
Returns:
The new interest rate as a Decimal object, or None if the rate
should not be changed.
"""
min_rate, rate_decrement, decrement_interval = None, None, None
if self.currency == "USD":
min_rate = USD_MINIMUM_RATE_PERCENT
rate_decrement = USD_RATE_DECREMENT_PERCENT
decay_multiplier = USD_RATE_EXPONENTIAL_DECAY_MULTIPLIER
decrement_interval = USD_RATE_REDUCTION_INTERVAL
elif self.currency == "BTC":
min_rate = BTC_MINIMUM_RATE_PERCENT
rate_decrement = BTC_RATE_DECREMENT_PERCENT
decay_multiplier = BTC_RATE_EXPONENTIAL_DECAY_MULTIPLIER
decrement_interval = BTC_RATE_REDUCTION_INTERVAL
else:
raise Exception("Unrecognized currency string")
if self.rate <= min_rate:
return None
time_elapsed = datetime.utcnow() - self.submitted_at
intervals_elapsed = time_elapsed // decrement_interval
if intervals_elapsed < 1:
return None
new_rate = self.rate
for i in range(intervals_elapsed):
# Apply the linear reduction first, then the exponential. If the
# user didn't do something weird with the configuration, only one
# will actually have an effect.
new_rate -= rate_decrement
# Asymptote at min_rate rather than at zero
new_rate = (new_rate - min_rate) * decay_multiplier + min_rate
return max(new_rate, min_rate)
class BitfinexAPI(object):
"""
Handles API requests and responses.
"""
base_url = "https://api.bitfinex.com"
rate_limit_interval = timedelta(seconds=70)
max_requests_per_interval = 60
def __init__(self, api_key, api_secret):
"""
Args:
api_key: The API key to use for requests made by this object.
api_secret: THe API secret to use for requests made by this object.
"""
self.api_key = api_key
self.api_secret = api_secret
self.nonce = count(int(time.time()))
self.request_timestamps = deque()
def get_offers(self):
"""
Retrieve current offers.
Returns:
A 2-tuple of lists. The first contains USD offers and the second
contains BTC offers, as Offer objects.
"""
offers_data = self._request("/v1/offers")
usd_offers = []
btc_offers = []
for offer_dict in offers_data:
# Ignore swap demands and FRR offers
if (
offer_dict["direction"] == "lend"
and offer_dict["rate"] != "0.0"
):
offer = Offer(offer_dict)
if offer.currency == "USD":
usd_offers.append(offer)
elif offer.currency == "BTC":
btc_offers.append(offer)
return (usd_offers, btc_offers)
def cancel_offer(self, offer):
"""
Cancel an offer.
Args:
offer: The offer to cancel as an Offer object.
Returns:
An Offer object representing the now-cancelled offer.
"""
return Offer(self._request("/v1/offer/cancel", {"offer_id": offer.id}))
def new_offer(self, currency, amount, rate, period):
"""
Create a new offer.
Args:
currency: Either "USD" or "BTC".
amount: Amount of the offer as a Decimal object.
rate: Interest rate of the offer per year, as a Decimal object.
period: How many days to lend for.
Returns:
An Offer object representing the newly-created offer.
"""
return Offer(self._request("/v1/offer/new", {
"currency": currency,
"amount": str(amount),
"rate": str(rate),
"period": period,
"direction": "lend",
}))
def get_available_balances(self):
"""
Retrieve available balances in deposit wallet.
Returns:
A 2-tuple of the USD balance followed by the BTC balance.
"""
balances_data = self._request("/v1/balances")
usd_available = 0
btc_available = 0
for balance_data in balances_data:
if balance_data["type"] == "deposit":
if balance_data["currency"] == "usd":
usd_available = Decimal(balance_data["available"])
elif balance_data["currency"] == "btc":
btc_available = Decimal(balance_data["available"])
return (usd_available, btc_available)
def _request(self, request_type, parameters=None):
self._rate_limiter()
url = self.base_url + request_type
if parameters is None:
parameters = {}
parameters.update({"request": request_type,
"nonce": str(next(self.nonce))})
payload = base64.b64encode(json.dumps(parameters).encode())
signature = hmac.new(self.api_secret, payload, hashlib.sha384)
headers = {"X-BFX-APIKEY": self.api_key,
"X-BFX-PAYLOAD": payload,
"X-BFX-SIGNATURE": signature.hexdigest()}
request = None
retry_count = 0
while request is None:
status_string = None
try:
request = requests.post(url, headers=headers)
except requests.exceptions.ConnectionError:
status_string = "Connection failed,"
if request and request.status_code == 500:
request = None
status_string = "500 internal server error,"
if request is None:
delay = 2 ** retry_count
print(status_string, "sleeping for", delay,
"seconds before retrying")
time.sleep(delay)
retry_count += 1
# I'm assuming that if we don't manage to connect, or we get a
# 500 internal server error, it doesn't count against our
# request limit. If this isn't the case, then we should call
# _rate_limiter() here too.
if request.status_code != 200:
print(request.text)
request.raise_for_status()
return request.json()
def _rate_limiter(self):
timestamps = self.request_timestamps
while True:
expire = datetime.utcnow() - self.rate_limit_interval
while timestamps and timestamps[0] < expire:
timestamps.popleft()
if len(timestamps) >= self.max_requests_per_interval:
delay = (timestamps[0] - expire).total_seconds()
print("Request rate limit hit, sleeping for", delay, "seconds")
time.sleep(delay)
else:
break
timestamps.append(datetime.utcnow())
def adjust_offers(api, offers, lend_period, minimum_amount):
"""
Check the specified offers and adjust them as needed.
Args:
api: Instance of BitfinexAPI to use.
offers: Current offers to be adjusted.
lend_period: How long we're willing to lend our funds for.
minimum_amount: Make sure any new offers are this amount or higher.
"""
new_offer_amounts = defaultdict(Decimal)
if not offers:
return
currency = offers[0].currency
for offer in offers:
new_rate = offer.get_new_rate()
if new_rate is not None:
cancelled_offer = api.cancel_offer(offer)
new_offer_amounts[new_rate] += cancelled_offer.amount
for rate, amount in new_offer_amounts.items():
# The minimum loan amount can cause some weirdness here. If one of our
# offers gets partially filled and the remainder is below the minimum,
# we won't be able to place it at the new rate after cancelling. It'll
# end up with the rest of our funds which get lent out at our starting
# (highest) rate. The alternative would be to leave small partially
# filled offers alone, which would mean they no longer get moved down.
if amount > minimum_amount:
print(api.new_offer(currency, amount, rate, lend_period))
else:
print("At rate {}, {} offer amount {} is below minimum,"
" skipping".format(rate, currency, amount))
def go():
"""
Main loop.
"""
api = BitfinexAPI(BITFINEX_API_KEY, BITFINEX_API_SECRET)
print("Ctrl+C to quit")
while True:
start_time = datetime.utcnow()
usd_offers, btc_offers = api.get_offers()
print(usd_offers)
print(btc_offers)
if LEND_USD:
adjust_offers(api, usd_offers, USD_LEND_PERIOD_DAYS,
USD_MINIMUM_LEND_AMOUNT)
if LEND_BTC:
adjust_offers(api, btc_offers, BTC_LEND_PERIOD_DAYS,
BTC_MINIMUM_LEND_AMOUNT)
usd_available, btc_available = api.get_available_balances()
if LEND_USD and usd_available >= USD_MINIMUM_LEND_AMOUNT:
print(api.new_offer("USD", usd_available, USD_START_RATE_PERCENT,
USD_LEND_PERIOD_DAYS))
if LEND_BTC and btc_available >= BTC_MINIMUM_LEND_AMOUNT:
print(api.new_offer("BTC", btc_available, BTC_START_RATE_PERCENT,
BTC_LEND_PERIOD_DAYS))
end_time = datetime.utcnow()
elapsed = end_time - start_time
remaining = POLL_INTERVAL - elapsed
delay = max(remaining.total_seconds(), 0)
print("Done processing, sleeping for", delay, "seconds")
time.sleep(delay)
go()
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>