-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathdealcluster.py
executable file
·592 lines (458 loc) · 17.6 KB
/
dealcluster.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/usr/bin/env python3
"""Cyberjunky's 3Commas bot helpers."""
import argparse
import configparser
import json
import os
import sqlite3
import sys
import time
from pathlib import Path
from constants.pair import PAIREXCLUDE_EXT
from helpers.logging import (
Logger,
NotificationHandler
)
from helpers.misc import (
check_deal,
remove_excluded_pairs,
wait_time_interval
)
from helpers.threecommas import (
get_threecommas_account_marketcode,
init_threecommas_api,
init_threecommas_websocket,
prefetch_marketcodes,
set_threecommas_bot_pairs
)
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": 86400,
"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"],
}
cfg["cluster_default"] = {
"botids": [12345, 67890],
"max-same-deals": "1",
}
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 init_cluster_db():
"""Create or open database to store cluster and coin data."""
try:
dbname = f"{program}.sqlite3"
dbpath = f"file:{datadir}/{dbname}?mode=rw"
dbconnection = sqlite3.connect(dbpath, uri=True)
dbconnection.row_factory = sqlite3.Row
logger.info(f"Database '{datadir}/{dbname}' opened successfully")
except sqlite3.OperationalError:
dbconnection = sqlite3.connect(f"{datadir}/{dbname}")
dbconnection.row_factory = sqlite3.Row
dbcursor = dbconnection.cursor()
logger.info(f"Database '{datadir}/{dbname}' created successfully")
dbcursor.execute(
"CREATE TABLE IF NOT EXISTS deals ("
"dealid INT Primary Key, "
"coin STRING, "
"clusterid STRING, "
"botid INT, "
"active BIT"
")"
)
dbcursor.execute(
"CREATE TABLE IF NOT EXISTS cluster_coins ("
"clusterid STRING, "
"coin STRING, "
"number_active INT, "
"PRIMARY KEY(clusterid, coin)"
")"
)
logger.info("Database tables created successfully")
return dbconnection
def init_thread_db():
"""Open connection with database for usage in thread"""
dbname = f"{program}.sqlite3"
dbpath = f"file:{datadir}/{dbname}?mode=rw"
dbconnection = sqlite3.connect(dbpath, uri=True)
dbconnection.row_factory = sqlite3.Row
return dbconnection
def upgrade_cluster_db():
"""Upgrade database if needed."""
try:
try:
# DROP column supported from sqlite 3.35.0 (2021.03.12)
cursor.execute("ALTER TABLE deals DROP COLUMN pair")
except sqlite3.OperationalError:
logger.debug("Older SQLite version; not used columns not removed")
cursor.execute("ALTER TABLE deals ADD COLUMN coin STRING")
cursor.execute("DROP TABLE cluster_pairs")
cursor.execute(
"CREATE TABLE IF NOT EXISTS cluster_coins ("
"clusterid STRING, "
"coin STRING, "
"number_active INT, "
"PRIMARY KEY(clusterid, coin)"
")"
)
cursor.execute("DROP TABLE bot_pairs")
logger.info("Database schema upgraded (pair to coins)")
except sqlite3.OperationalError:
logger.debug("Database schema is up-to-date")
def add_cluster_deal(db_connection, deal_data, cluster_id):
"""Add a new deal within the cluster"""
deal_id = deal_data["id"]
existing_deal = check_deal(db_connection.cursor(), deal_id)
if not existing_deal:
coin = deal_data['pair'].split("_")[1]
db_connection.execute(
f"INSERT INTO deals (dealid, coin, clusterid, botid, active) "
f"VALUES ({deal_id}, '{coin}', '{cluster_id}', {deal_data['bot_id']}, {1})"
)
# db.commit() on higher level
logger.info(
f"New deal found {deal_id}/{deal_data['pair']} on bot \"{deal_data['bot_name']}",
True
)
else:
logger.debug(
f"Deal {deal_id} already registered and still active"
)
def process_bot_deals(cluster_id, bot_data):
"""Check deals from bot and update deals in db."""
bot_id = bot_data["id"]
# Process current deals and deals which have been closed since the last processing time
current_deals = []
if bot_data["active_deals"]:
for dealdata in bot_data["active_deals"]:
current_deals.append(dealdata["id"])
add_cluster_deal(db, dealdata, cluster_id)
# Remove all other deals as not active anymore.
if current_deals:
# Remove start and end square bracket so we can properly use it
current_deals_str = str(current_deals)[1:-1]
logger.debug(f"Delete finished deals from {bot_id} except {current_deals_str}")
db.execute(
f"DELETE FROM deals "
f"WHERE botid = {bot_id} AND dealid NOT IN ({current_deals_str})"
)
# No deals for this bot anymore, so remove them all (if any left)
if not current_deals:
logger.debug(f"No deals active for {bot_id}")
db.execute(
f"DELETE FROM deals "
f"WHERE botid = {bot_id}"
)
# Commit the added and removed deals to the db
db.commit()
def aggregrate_cluster(db_connection, cluster_id, bot_list):
"""Aggregate deals within cluster."""
logger.debug(f"Cleaning and aggregating data for '{cluster_id}'")
# Get the current cluster data and pair numbers
oldclusterdata = [c[0] for c in db_connection.execute(
f"SELECT coin FROM cluster_coins "
f"WHERE clusterid = '{cluster_id}' AND "
f"number_active >= {int(config.get(cluster_id, 'max-same-deals'))} "
f"ORDER BY coin ASC"
).fetchall()]
# Remove current data
db_connection.execute(
f"DELETE from cluster_coins WHERE clusterid = '{cluster_id}'"
)
# Create the cluster data, how many of the same coins are active within the
# cluster based on the active deals
db_connection.execute(
f"INSERT INTO cluster_coins (clusterid, coin, number_active) "
f"SELECT clusterid, coin, "
f"SUM ( CASE WHEN active = {1} THEN 1 ELSE 0 END) AS number_active "
f"FROM deals "
f"WHERE clusterid = '{cluster_id}' "
f"GROUP BY coin"
)
db_connection.commit()
# Get the new cluster data and pair numbers
newclusterdata = [c[0] for c in db_connection.execute(
f"SELECT coin FROM cluster_coins "
f"WHERE clusterid = '{cluster_id}' AND "
f"number_active >= {int(config.get(cluster_id, 'max-same-deals'))} "
f"ORDER BY coin ASC"
).fetchall()]
log_cluster_changes(cluster_id, oldclusterdata, newclusterdata)
# Write the exclude file for the coins, to be used for updating bot
# pairs (this, and other, scripts)
write_cluster_exclude_files(bot_list, newclusterdata)
def log_cluster_changes(cluster_id, old_data, new_data):
"""Log the changes in the cluster"""
logger.debug(
f"Old cluster data: {old_data}. New cluster data: {new_data}"
)
# Check which coins have gone from the data (will be enabled)
enabledcoins = [x for x in old_data if x not in new_data]
if enabledcoins:
logger.info(
f"Enabling coin(s) {enabledcoins} for cluster: {cluster_id}",
True
)
# Check which coins are new in the data (will be disabled)
disabledcoins = [x for x in new_data if x not in old_data]
if disabledcoins:
logger.info(
f"Disabling coin(s) {disabledcoins} for cluster: {cluster_id}",
True
)
def write_cluster_exclude_files(bot_list, disabled_coins):
"""Write exclude files for each bot in the specified cluster"""
for botid in bot_list:
write_bot_exclude_file(botid, disabled_coins)
def write_bot_exclude_file(bot_id, coins):
"""Write the exclude file for the specified bot"""
excludefilename = f"{sharedir}/{bot_id}.{PAIREXCLUDE_EXT}"
logger.debug(
f"Writing coins {coins} to file {excludefilename}"
)
with open(excludefilename, 'w') as filehandle:
for coin in coins:
filehandle.write('%s\n' % coin)
def websocket_update(deal_data):
"""Handle the received deal data from the websocket"""
# Indicator if the cluster should be aggregrated again. Updates over the websocket
# also contain filled SO's which don't have impact on the cluster
aggregrate = False
clusterid = ""
threaddb = init_thread_db()
existingdeal = check_deal(threaddb.cursor(), deal_data["id"])
if existingdeal and deal_data["finished?"]:
# Deal is finished, remove it from the db
threaddb.execute(
f"DELETE FROM deals "
f"WHERE botid = {deal_data['bot_id']} AND dealid = {deal_data['id']}"
)
threaddb.commit()
logger.info(
f"Deal {deal_data['id']}/{deal_data['pair']} on "
f"bot \"{deal_data['bot_name']} finished!",
True
)
# Finished deal, get the cluster the bot belongs to
clusterid = get_bot_cluster(deal_data["bot_id"])
aggregrate = True
else:
if not existingdeal:
# New deal, check if the bot is part of any cluster
clusterid = get_bot_cluster(deal_data["bot_id"])
if clusterid:
add_cluster_deal(threaddb, deal_data, clusterid)
threaddb.commit()
aggregrate = True
#else:
# Here we could inform the user about opened deals outside any cluster
#else:
# Here we could inform the user about deal updates (filled SO, trailing activated)
if aggregrate:
if clusterid:
# Get Bot list
botlist = json.loads(config.get(clusterid, "botids"))
aggregrate_cluster(threaddb, clusterid, botlist)
process_cluster_bots(clusterid, botlist, "update")
else:
logger.error(
f"Deal {deal_data['id']}/{deal_data['pair']} needs "
f"to be aggregated but cluster is unknown!"
)
# Send notifications, if there are any
notification.send_notification()
def process_cluster_bots(cluster_id, bot_list, action):
"""Update the bots in the cluster with the enabled/disabled pairs"""
for bot in bot_list:
boterror, botdata = api.request(
entity="bots",
action="show",
action_id=str(bot),
)
if botdata:
if action == "deals":
process_bot_deals(cluster_id, botdata)
elif action == "update":
update_bot_config(botdata)
else:
logger.error(
f"Cannot handle unknown action '{action}'!"
)
else:
if boterror and "msg" in boterror:
logger.error("Error occurred updating bots: %s" % boterror["msg"])
else:
logger.error("Error occurred updating bots")
def get_bot_cluster(bot_id):
"""Find the cluster the bot belongs to"""
clusterid = ""
for sectionid in config.sections():
if sectionid.startswith("cluster_"):
# Bot configuration for section
botlist = json.loads(config.get(sectionid, "botids"))
if bot_id in botlist:
# Cluster (section) found
clusterid = sectionid
break
return clusterid
def update_bot_config(bot_data):
"""Update bots at 3C"""
marketcode = marketcodecache.get(bot_data["id"])
if not marketcode:
marketcode = get_threecommas_account_marketcode(logger, api, bot_data["account_id"])
marketcodecache[bot_data["id"]] = marketcode
logger.info(
f"Marketcode for bot {bot_data['id']} was not cached. Cache updated "
f"with marketcode {marketcode}."
)
# If sharedir is set, other scripts could provide a file with pairs to exclude
if marketcode:
newpairs = bot_data["pairs"].copy()
botbase = newpairs[0].split("_")[0]
logger.debug(
f"Pairs before excluding: {newpairs}"
)
remove_excluded_pairs(logger, sharedir, bot_data["id"], marketcode, botbase, newpairs)
logger.debug(
f"Pairs after excluding: {newpairs}"
)
set_threecommas_bot_pairs(logger, api, bot_data, newpairs, False, True, False)
else:
logger.error(
f"Marketcode not found for bot {bot_data['id']}. Cannot update pairs!"
)
def create_marketcode_cache():
"""Create the cache met MarketCode per bot"""
allbotids = []
logger.info("Prefetching marketcodes for all configured bots...")
for initialsection in config.sections():
if initialsection.startswith("cluster_"):
allbotids += json.loads(config.get(initialsection, "botids"))
return prefetch_marketcodes(logger, api, allbotids)
# 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="directory to use for config and logs files", type=str
)
parser.add_argument(
"-s", "--sharedir", help="directory to use for shared files", type=str
)
parser.add_argument(
"-b", "--blacklist", help="local blacklist to use instead of 3Commas's", type=str
)
args = parser.parse_args()
if args.datadir:
datadir = args.datadir
else:
datadir = os.getcwd()
# pylint: disable-msg=C0103
if args.sharedir:
sharedir = args.sharedir
else:
print("This script requires the sharedir to be used (-s option)!")
sys.exit(0)
# pylint: disable-msg=C0103
if args.blacklist:
blacklistfile = f"{datadir}/{args.blacklist}"
else:
blacklistfile = None
# 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)
# Initialize or open the database
db = init_cluster_db()
cursor = db.cursor()
# Upgrade the database if needed
upgrade_cluster_db()
# Prefetch all Marketcodes to reduce API calls and improve speed
# New bot(s) will also be added later, for example when the configuration
# has been changed after starting this script
marketcodecache = create_marketcode_cache()
# Initialize 3Commas WebSocket connection
websocket = init_threecommas_websocket(logger, config, websocket_update)
if not websocket:
sys.exit(0)
websocket.start_listener(seperate_thread = True)
# DCA Deal Cluster
while True:
# Reload config files and refetch data to catch changes
config = load_config()
logger.info(f"Reloaded configuration from '{datadir}/{program}.ini'")
# Configuration settings
timeint = int(config.get("settings", "timeinterval"))
debug = config.getboolean("settings", "debug")
for section in config.sections():
if section.startswith("cluster_"):
# Bot configuration for section
botids = json.loads(config.get(section, "botids"))
# Walk through all bots configured and check deals
process_cluster_bots(section, botids, "deals")
# Aggregrate data on cluster level. Will also take care of writing .pairexclude file
aggregrate_cluster(db, section, botids)
# Update the bots in this cluster with the enabled/disabled pairs
process_cluster_bots(section, botids, "update")
# Send notifications for the processed cluster, otherwise the message can
# become too long resulting in a bad request in apprise
notification.send_notification()
elif section not in ("settings"):
logger.warning(
f"Section '{section}' not processed (prefix 'cluster_' missing)!",
False
)
if not wait_time_interval(logger, notification, timeint, False):
break