-
Notifications
You must be signed in to change notification settings - Fork 68
/
gridbot.py
executable file
·336 lines (273 loc) · 10 KB
/
gridbot.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
#!/usr/bin/env python3
"""Cyberjunky's 3Commas bot helpers."""
import argparse
import configparser
import json
import os
import re
import sys
import time
from pathlib import Path
import requests
from bs4 import BeautifulSoup
from helpers.logging import Logger, NotificationHandler
from helpers.misc import wait_time_interval
from helpers.threecommas import init_threecommas_api
def load_config():
"""Create default or load existing config file."""
cfg = configparser.ConfigParser()
if cfg.read(f"{datadir}/{program}.ini"):
return cfg
cfg["settings"] = {
"timezone": "Europe/Amsterdam",
"timeinterval": 3600,
"debug": False,
"logrotate": 7,
"3c-apikey": "Your 3Commas API Key",
"3c-apisecret": "Your 3Commas API Secret",
"3c-apikey-path": "Path to your own generated RSA private key, or empty",
"notifications": False,
"notify-urls": ["notify-url1", "notify-url2"],
}
cfg["gridbots_redbag_example"] = {
"botids": [12345, 67890],
"mode": "redbag",
}
cfg["gridbots_trade_example"] = {
"botids": [12345, 67890],
"mode": "trade",
}
with open(f"{datadir}/{program}.ini", "w") as cfgfile:
cfg.write(cfgfile)
return None
def upgrade_config(cfg):
"""Upgrade config file if needed."""
if not cfg.has_option("settings", "3c-apikey-path"):
cfg.set("settings", "3c-apikey-path", "")
with open(f"{datadir}/{program}.ini", "w+") as cfgfile:
cfg.write(cfgfile)
logger.info("Upgraded the configuration file (3c-apikey-path)")
return cfg
def strtofloat(txtstr):
"""Convert text string to float."""
val = txtstr.text.strip()
price = val.replace(".", "")
floatval = price.replace(",", ".")
return floatval
def get_gridbots_data(pair):
"""Get the best gridbot settings from grid-bots.com."""
url = "https://www.grid-bots.com"
coin = pair.split("_")[1]
griddata = {}
try:
result = requests.get(url)
result.raise_for_status()
soup = BeautifulSoup(result.text, features="html.parser")
tablerows = [t for t in soup.find_all("tr") if not t.find_all("table")]
for row in tablerows:
rowcolums = row.find_all("td")
if len(rowcolums) > 0:
gridcoin = rowcolums[0].text.strip()
if coin == gridcoin:
griddata["lower"] = strtofloat(rowcolums[2])
griddata["upper"] = strtofloat(rowcolums[3])
griddata["numgrid"] = int(rowcolums[4].text.strip())
griddata["tokensgrid"] = strtofloat(rowcolums[5])
break
logger.debug(griddata)
except requests.exceptions.HTTPError as err:
logger.error("Fetching grid-bots data failed with error: %s" % err)
return griddata
logger.info("Fetched grid-bots data OK")
return griddata
def update_gridbot(gridbot, upperprice, lowerprice):
"""Update gridbot with new grid."""
botname = gridbot["name"]
pair = gridbot["pair"]
error, data = api.request(
entity="grid_bots",
action="manual_update",
action_id=str(gridbot["id"]),
payload={
"bot_id": gridbot["id"],
"name": gridbot["name"],
"account_id": gridbot["account_id"],
"pair": gridbot["pair"],
"upper_price": upperprice,
"lower_price": lowerprice,
"quantity_per_grid": gridbot["quantity_per_grid"],
"grids_quantity": gridbot["grids_quantity"],
},
)
if data:
logger.info(
f"Moved the grid of gridbot '{botname}' using pair {pair} with"
f" upper and lower price: {upperprice} - {lowerprice}",
True,
)
return None
logger.error(
f"Error occurred updating gridbot '{botname}' with new upper price"
f" and lower price of {upperprice} & {lowerprice} : %s" % error["msg"]
)
return error["msg"]
def update_gridbot_activelines(gridbot, maxactivebuylines, maxactiveselllines):
"""Update gridbot with new active line settings."""
botname = gridbot["name"]
pair = gridbot["pair"]
error, data = api.request(
entity="grid_bots",
action="manual_update",
action_id=str(gridbot["id"]),
payload={
"bot_id": gridbot["id"],
"name": gridbot["name"],
"account_id": gridbot["account_id"],
"pair": gridbot["pair"],
"upper_price": gridbot["upper_price"],
"lower_price": gridbot["lower_price"],
'max_active_buy_lines': maxactivebuylines,
'max_active_sell_lines': maxactiveselllines,
"quantity_per_grid": gridbot["quantity_per_grid"],
"grids_quantity": gridbot["grids_quantity"],
},
)
if data:
logger.info(
f"Set active lines of gridbot '{botname}' to"
f" buy: {maxactivebuylines} and sell: {maxactiveselllines}",
True,
)
return None
logger.error(
f"Error occurred updating gridbot '{botname}' with new active lines"
f" buy: {maxactivebuylines} and sell: {maxactiveselllines}: %s" % error["msg"]
)
return error["msg"]
def manage_gridbot(thebot):
"""Move grid to match pricing."""
botname = thebot["name"]
# bot values to calculate with
pair = thebot["pair"]
upperprice = thebot["upper_price"]
lowerprice = thebot["lower_price"]
quantitypergrid = thebot["quantity_per_grid"]
gridsquantity = thebot["grids_quantity"]
strategytype = thebot["strategy_type"]
currentprice = thebot["current_price"]
logger.info("Current settings for '%s':" % botname)
logger.info("Pair: %s" % pair)
logger.info("Upper price: %s" % upperprice)
logger.info("Lower price: %s" % lowerprice)
logger.info("Quantity per grid: %s" % quantitypergrid)
logger.info("Grid quantity: %s" % gridsquantity)
logger.info("Strategy type: %s" % strategytype)
logger.info("Current price for %s is %s" % (pair, currentprice))
gridinfo = get_gridbots_data(pair)
if gridinfo is None:
logger.info(f"No grid setup information found for {pair}, skipping update")
return
newupperprice = gridinfo["upper"]
newlowerprice = gridinfo["lower"]
# newtokensgrid = gridinfo["tokensgrid"]
# newnumgrid = gridinfo["numgrid"]
# Test updating active lines for @IamtheOnewhoKnocks:
# maxactivebuylines = 3
# maxactiveselllines = 3
# update_gridbot_activelines(thebot, maxactivebuylines, maxactiveselllines)
if float(upperprice) == float(newupperprice):
logger.info(
f"Grid of gridbot '{botname}' with pair {pair}\nis already using"
" correct upper and price, skipping update.",
True,
)
return
logger.info(
f"Grid of gridbot '{botname}' with pair {pair} will be adjusted like this:\n"
f"Upper: {upperprice} -> {newupperprice} Lower: {lowerprice} -> {newlowerprice}",
True,
)
return
# Update the bot with new limits
result = update_gridbot(thebot, newupperprice, newlowerprice)
if result and "Upper price should be at least " in result:
uprice = re.search("Upper price should be at least ([0-9.]+)", result)
if uprice:
upperprice = uprice[1]
logger.info(
f"New upper price was not accepted, retrying with suggested minimum price of {upperprice}"
)
result = update_gridbot(thebot, upperprice, newlowerprice)
if result:
logger.error(
f"Failed to update gridbot with suggested minimum upper price of {upperprice}"
)
# Start application
program = Path(__file__).stem
# Parse and interpret options.
parser = argparse.ArgumentParser(description="Cyberjunky's 3Commas bot helper.")
parser.add_argument("-d", "--datadir", help="data directory to use", type=str)
args = parser.parse_args()
if args.datadir:
datadir = args.datadir
else:
datadir = os.getcwd()
# Create or load configuration file
config = load_config()
if not config:
# Initialise temp logging
logger = Logger(datadir, program, None, 7, False, False)
logger.info(
f"Created example config file '{datadir}/{program}.ini', edit it and restart the program"
)
sys.exit(0)
else:
# Handle timezone
if hasattr(time, "tzset"):
os.environ["TZ"] = config.get(
"settings", "timezone", fallback="Europe/Amsterdam"
)
time.tzset()
# Init notification handler
notification = NotificationHandler(
program,
config.getboolean("settings", "notifications"),
config.get("settings", "notify-urls"),
)
# Initialise logging
logger = Logger(
datadir,
program,
notification,
int(config.get("settings", "logrotate", fallback=7)),
config.getboolean("settings", "debug"),
config.getboolean("settings", "notifications"),
)
# Upgrade config file if needed
config = upgrade_config(config)
logger.info(f"Loaded configuration from '{datadir}/{program}.ini'")
# Initialize 3Commas API
api = init_threecommas_api(logger, config)
if not api:
sys.exit(0)
# Auto tune a running gridbot
while True:
config = load_config()
logger.info(f"Reloaded configuration from '{datadir}/{program}.ini'")
# Configuration settings
timeint = int(config.get("settings", "timeinterval"))
botids = json.loads(config.get("settings", "botids"))
# Walk through all bots specified
for bot in botids:
boterror, botdata = api.request(
entity="grid_bots",
action="get",
action_id=str(bot),
)
if botdata:
logger.debug("Raw Gridbot data: %s" % botdata)
manage_gridbot(botdata)
else:
logger.error("Error occurred managing gridbots: %s" % boterror["msg"])
if not wait_time_interval(logger, notification, timeint):
break