forked from cyberjunky/3commas-cyber-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compound.py
executable file
·778 lines (655 loc) · 27.5 KB
/
compound.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#!/usr/bin/env python3
"""Cyberjunky's 3Commas bot helpers."""
import argparse
import configparser
import json
import math
import os
import sqlite3
import sys
import time
from pathlib import Path
from helpers.logging import Logger, NotificationHandler
from helpers.misc import check_deal, get_round_digits, remove_prefix, wait_time_interval
from helpers.threecommas import get_threecommas_deals, 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,
"default-profittocompound": 1.0,
"3c-apikey": "Your 3Commas API Key",
"3c-apisecret": "Your 3Commas API Secret",
"notifications": False,
"notify-urls": ["notify-url1", "notify-url2"],
}
cfg["bot_id"] = {
"compoundmode": "boso",
"profittocompound": 1.0,
"usermaxactivedeals": 5,
"usermaxsafetyorders": 5,
"comment": "Just a description of the bot(s)",
}
with open(f"{datadir}/{program}.ini", "w") as cfgfile:
cfg.write(cfgfile)
return None
def upgrade_config(thelogger, theapi, cfg):
"""Upgrade config file if needed."""
if cfg.has_option("settings", "profittocompound"):
cfg.set(
"settings",
"default-profittocompound",
config.get("settings", "profittocompound"),
)
cfg.remove_option("settings", "profittocompound")
with open(f"{datadir}/{program}.ini", "w+") as cfgfile:
cfg.write(cfgfile)
thelogger.info("Upgraded the configuration file (default-profittocompound)")
if cfg.has_option("settings", "botids"):
thebotids = json.loads(cfg.get("settings", "botids"))
default_profit_percentage = float(
config.get("settings", "default-profittocompound")
)
# Walk through all bots configured
for thebot in thebotids:
if not cfg.has_section(f"bot_{thebot}"):
error, data = theapi.request(
entity="bots",
action="show",
action_id=str(thebot),
)
if data:
# Add new config section
cfg[f"bot_{thebot}"] = {
"compoundmode": "boso",
"profittocompound": default_profit_percentage,
"usermaxactivedeals": int(data["max_active_deals"]) + 5,
"usermaxsafetyorders": int(data["max_safety_orders"]) + 5,
"comment": data["name"].replace("%", "%%"),
}
else:
if error and "msg" in error:
logger.error(
"Error occurred upgrading config: %s" % error["msg"]
)
else:
logger.error("Error occurred upgrading config")
cfg.remove_option("settings", "botids")
with open(f"{datadir}/{program}.ini", "w+") as cfgfile:
cfg.write(cfgfile)
thelogger.info("Upgraded the configuration file (create sections)")
return cfg
def get_logged_profit_for_bot(bot_id):
"""Get the sum of all logged profit"""
data = cursor.execute(
f"SELECT sum(profit) FROM deals WHERE botid = {bot_id}"
).fetchone()[0]
if data is None:
return float(0)
return data
def update_bot_order_volumes(
thebot,
new_base_order_volume,
new_safety_order_volume,
profit_sum,
deals_count,
max_safety_orders,
):
"""Update bot with new order volumes."""
bot_name = thebot["name"]
base_order_volume = float(thebot["base_order_volume"])
safety_order_volume = float(thebot["safety_order_volume"])
logger.info(
"Calculated BO volume changed from: %s to %s"
% (base_order_volume, new_base_order_volume)
)
if max_safety_orders >= 1:
logger.info(
"Calculated SO volume changed from: %s to %s"
% (safety_order_volume, new_safety_order_volume)
)
error, data = api.request(
entity="bots",
action="update",
action_id=str(thebot["id"]),
payload={
"bot_id": thebot["id"],
"name": thebot["name"],
"pairs": thebot["pairs"],
"base_order_volume": new_base_order_volume, # new base order volume
"safety_order_volume": new_safety_order_volume, # new safety order volume
"take_profit": thebot["take_profit"],
"martingale_volume_coefficient": thebot["martingale_volume_coefficient"],
"martingale_step_coefficient": thebot["martingale_step_coefficient"],
"max_active_deals": thebot["max_active_deals"],
"max_safety_orders": thebot["max_safety_orders"],
"safety_order_step_percentage": thebot["safety_order_step_percentage"],
"take_profit_type": thebot["take_profit_type"],
"strategy_list": thebot["strategy_list"],
"active_safety_orders_count": thebot["active_safety_orders_count"],
"leverage_type": thebot["leverage_type"],
"leverage_custom_value": thebot["leverage_custom_value"],
},
)
if data:
rounddigits = get_round_digits(thebot["pairs"][0])
if max_safety_orders >= 1:
logger.info(
f"Compounded ₿{round(profit_sum, rounddigits)} in profit from {deals_count} deal(s) "
f"made by '{bot_name}'\nChanged BO from ₿{round(base_order_volume, rounddigits)} to "
f"₿{round(new_base_order_volume, rounddigits)}\nChanged SO from "
f"₿{round(safety_order_volume, rounddigits)} to ₿{round(new_safety_order_volume, rounddigits)}",
True,
)
else:
logger.info(
f"Compounded ₿{round(profit_sum, rounddigits)} in profit from {deals_count} deal(s) "
f"made by '{bot_name}'\nChanged BO from ₿{round(base_order_volume, rounddigits)} to "
f"₿{round(new_base_order_volume, rounddigits)}",
True,
)
else:
if error and "msg" in error:
logger.error(
"Error occurred updating bot with new BO/SO values: %s" % error["msg"]
)
else:
logger.error("Error occurred updating bot with new BO/SO values")
def process_deals(deals):
"""Check deals from bot."""
deals_count = 0
profit_sum = 0.0
for deal in deals:
deal_id = deal["id"]
bot_id = deal["bot_id"]
# Register deal in database
exist = check_deal(cursor, deal_id)
if exist:
logger.debug("Deal with id '%s' already processed, skipping." % deal_id)
else:
# Deal not processed yet
profit = float(deal["final_profit"])
deals_count += 1
profit_sum += profit
db.execute(
f"INSERT INTO deals (dealid, profit, botid) VALUES ({deal_id}, {profit}, {bot_id})"
)
logger.info("Finished deals: %s total profit: %s" % (deals_count, profit_sum))
db.commit()
# Calculate profit part to compound
logger.info("Profit available to compound: %s" % profit_sum)
return (deals_count, profit_sum)
def get_bot_values(thebot):
"""Load start boso values from database or calculate and store them."""
startbo = 0.0
startso = 0.0
startactivedeals = thebot["max_active_deals"]
bot_id = thebot["id"]
data = cursor.execute(
f"SELECT startbo, startso, startactivedeals FROM bots WHERE botid = {bot_id}"
).fetchone()
if data:
# Fetch values from database
startbo = data[0]
startso = data[1]
startactivedeals = data[2]
logger.info(
"Fetched bot start BO, SO values and max. active deals: %s %s %s"
% (startbo, startso, startactivedeals)
)
else:
# Store values in database
startbo = float(thebot["base_order_volume"])
startso = float(thebot["safety_order_volume"])
startactivedeals = thebot["max_active_deals"]
db.execute(
f"INSERT INTO bots (botid, startbo, startso, startactivedeals) "
f"VALUES ({bot_id}, {startbo}, {startso}, {startactivedeals})"
)
logger.info(
"Stored bot start BO, SO values and max. active deals: %s %s %s"
% (startbo, startso, startactivedeals)
)
db.commit()
return (startbo, startso, startactivedeals)
def update_bot_max_deals(thebot, org_base_order, org_safety_order, new_max_deals):
"""Update bot with new max deals and old bo/so values."""
bot_name = thebot["name"]
base_order_volume = float(thebot["base_order_volume"])
safety_order_volume = float(thebot["safety_order_volume"])
max_active_deals = thebot["max_active_deals"]
logger.info(
"Calculated max. active deals changed from: %s to %s"
% (max_active_deals, new_max_deals)
)
logger.info(
"Calculated BO volume changed from: %s to %s"
% (base_order_volume, org_base_order)
)
logger.info(
"Calculated SO volume changed from: %s to %s"
% (safety_order_volume, org_safety_order)
)
error, data = api.request(
entity="bots",
action="update",
action_id=str(thebot["id"]),
payload={
"bot_id": thebot["id"],
"name": thebot["name"],
"pairs": thebot["pairs"],
"base_order_volume": org_base_order, # original base order volume
"safety_order_volume": org_safety_order, # original safety order volume
"take_profit": thebot["take_profit"],
"martingale_volume_coefficient": thebot["martingale_volume_coefficient"],
"martingale_step_coefficient": thebot["martingale_step_coefficient"],
"max_active_deals": new_max_deals, # new max. deals value
"max_safety_orders": thebot["max_safety_orders"],
"safety_order_step_percentage": thebot["safety_order_step_percentage"],
"take_profit_type": thebot["take_profit_type"],
"strategy_list": thebot["strategy_list"],
"active_safety_orders_count": thebot["active_safety_orders_count"],
"leverage_type": thebot["leverage_type"],
"leverage_custom_value": thebot["leverage_custom_value"],
},
)
if data:
rounddigits = get_round_digits(thebot["pairs"][0])
logger.info(
f"Changed max. active deals from: %s to %s for bot\n'{bot_name}'\n"
f"Changed BO from ${round(base_order_volume, rounddigits)} to "
f"${round(org_base_order, rounddigits)}\nChanged SO from "
f"${round(safety_order_volume, rounddigits)} to ${round(org_safety_order, rounddigits)}"
% (max_active_deals, new_max_deals)
)
else:
if error and "msg" in error:
logger.error(
"Error occurred updating bot with new max. deals and BO/SO values: %s"
% error["msg"]
)
else:
logger.error(
"Error occurred updating bot with new max. deals and BO/SO values"
)
def update_bot_max_safety_orders(
thebot, org_base_order, org_safety_order, new_max_safety_orders
):
"""Update bot with new max safety orders and old bo/so values."""
bot_name = thebot["name"]
base_order_volume = float(thebot["base_order_volume"])
safety_order_volume = float(thebot["safety_order_volume"])
max_safety_orders = thebot["max_safety_orders"]
logger.info(
"Calculated max. safety orders changed from: %s to %s"
% (max_safety_orders, new_max_safety_orders)
)
logger.info(
"Calculated BO volume changed from: %s to %s"
% (base_order_volume, org_base_order)
)
logger.info(
"Calculated SO volume changed from: %s to %s"
% (safety_order_volume, org_safety_order)
)
error, data = api.request(
entity="bots",
action="update",
action_id=str(thebot["id"]),
payload={
"bot_id": thebot["id"],
"name": thebot["name"],
"pairs": thebot["pairs"],
"base_order_volume": org_base_order, # original base order volume
"safety_order_volume": org_safety_order, # original safety order volume
"take_profit": thebot["take_profit"],
"martingale_volume_coefficient": thebot["martingale_volume_coefficient"],
"martingale_step_coefficient": thebot["martingale_step_coefficient"],
"max_active_deals": thebot["max_active_deals"],
"max_safety_orders": new_max_safety_orders, # new max. safety orders value
"safety_order_step_percentage": thebot["safety_order_step_percentage"],
"take_profit_type": thebot["take_profit_type"],
"strategy_list": thebot["strategy_list"],
"active_safety_orders_count": thebot["active_safety_orders_count"],
"leverage_type": thebot["leverage_type"],
"leverage_custom_value": thebot["leverage_custom_value"],
},
)
if data:
rounddigits = get_round_digits(thebot["pairs"][0])
logger.info(
f"Changed max. active safety orders from: %s to %s for bot\n'{bot_name}'\n"
f"Changed BO from ${round(base_order_volume, rounddigits)} to "
f"${round(org_base_order, rounddigits)}\nChanged SO from "
f"${round(safety_order_volume, rounddigits)} to ${round(org_safety_order, rounddigits)}"
% (max_safety_orders, max_safety_orders)
)
else:
if error and "msg" in error:
logger.error(
"Error occurred updating bot with new max. safety orders and BO/SO values: %s"
% error["msg"]
)
else:
logger.error(
"Error occurred updating bot with new max. safety orders and BO/SO values"
)
def compound_bot(cfg, thebot):
"""Find profit from deals and calculate new SO and BO values."""
bot_name = thebot["name"]
bot_id = thebot["id"]
deals = get_threecommas_deals(logger, api, bot_id)
bot_profit_percentage = float(
cfg.get(
f"bot_{bot_id}",
"profittocompound",
fallback=cfg.get("settings", "default-profittocompound"),
)
)
if cfg.get(f"bot_{bot_id}", "compoundmode", fallback="boso") == "safetyorders":
logger.info("Compound mode for this bot is: Safety Orders")
# Get starting BO and SO values
(startbo, startso, startactivedeals) = get_bot_values(thebot)
# Get active safety order settings
user_defined_max_safety_orders = int(
cfg.get(f"bot_{bot_id}", "usermaxsafetyorders")
)
# Get active deal settings
user_defined_max_active_deals = int(
cfg.get(f"bot_{bot_id}", "usermaxactivedeals")
)
# Calculate amount used per deal
max_safety_orders = float(thebot["max_safety_orders"])
martingale_volume_coefficient = float(
thebot["martingale_volume_coefficient"]
) # Safety order volume scale
# Always add start_base_order_size
totalusedperdeal = startbo
isafetyorder = 1
while isafetyorder <= max_safety_orders:
# For the first Safety order, just use the startso
if isafetyorder == 1:
total_safety_order_volume = startso
# After the first SO, multiple the previous SO with the safety order volume scale
if isafetyorder > 1:
total_safety_order_volume *= martingale_volume_coefficient
totalusedperdeal += total_safety_order_volume
# Calculate profit needed to add a SO to all startactivedeals
if isafetyorder == max_safety_orders:
total_safety_order_volume *= (
martingale_volume_coefficient # order size van volgende SO
)
profit_needed_to_add_so = total_safety_order_volume * startactivedeals
isafetyorder += 1
# Calculate % to compound (per bot)
totalprofitforbot = get_logged_profit_for_bot(thebot["id"])
profitusedtocompound = totalprofitforbot * bot_profit_percentage
# If we have more profitusedtocompound
new_max_safety_orders = max_safety_orders
if profitusedtocompound > profit_needed_to_add_so:
new_max_safety_orders = max_safety_orders + 1
if new_max_safety_orders > user_defined_max_safety_orders:
logger.info(
f"Already reached max set number of safety orders ({user_defined_max_safety_orders}), "
f"skipping deal compounding"
)
if new_max_safety_orders > max_safety_orders:
if new_max_safety_orders <= user_defined_max_safety_orders:
logger.info("Enough profit has been made to add a safety order")
# Update the bot
update_bot_max_safety_orders(
thebot, startbo, startso, new_max_safety_orders
)
if cfg.get(f"bot_{bot_id}", "compoundmode", fallback="boso") == "deals":
logger.info("Compound mode for this bot is: DEALS")
# Get starting BO and SO values
(startbo, startso, startactivedeals) = get_bot_values(thebot)
# Get active deal settings
user_defined_max_active_deals = int(
cfg.get(f"bot_{bot_id}", "usermaxactivedeals")
)
# Calculate amount used per deal
max_safety_orders = float(thebot["max_safety_orders"])
martingale_volume_coefficient = float(
thebot["martingale_volume_coefficient"]
) # Safety order volume scale
# Always add start_base_order_size
totalusedperdeal = startbo
isafetyorder = 1
while isafetyorder <= max_safety_orders:
# For the first Safety order, just use the startso
if isafetyorder == 1:
total_safety_order_volume = startso
# After the first SO, multiple the previous SO with the safety order volume scale
if isafetyorder > 1:
total_safety_order_volume *= martingale_volume_coefficient
totalusedperdeal += total_safety_order_volume
isafetyorder += 1
# Calculate % to compound (per bot)
totalprofitforbot = get_logged_profit_for_bot(thebot["id"])
profitusedtocompound = totalprofitforbot * bot_profit_percentage
new_max_active_deals = (
math.floor(profitusedtocompound / totalusedperdeal) + startactivedeals
)
current_active_deals = thebot["max_active_deals"]
if new_max_active_deals > user_defined_max_active_deals:
logger.info(
f"Already reached max set number of deals ({user_defined_max_active_deals}), "
f"skipping deal compounding"
)
elif (
new_max_active_deals
> current_active_deals & new_max_active_deals
<= user_defined_max_active_deals
):
logger.info(
"Enough profit has been made to add a deal and lower BO & SO to their orginal values"
)
# Update the bot
update_bot_max_deals(thebot, startbo, startso, new_max_active_deals)
if deals:
(deals_count, profit_sum) = process_deals(deals)
profit_sum *= bot_profit_percentage
logger.info(
"Profit available after applying percentage value (%s): %s "
% (bot_profit_percentage, profit_sum)
)
if profit_sum:
# Bot values to calculate with
base_order_volume = float(thebot["base_order_volume"])
safety_order_volume = float(thebot["safety_order_volume"])
max_active_deals = thebot["max_active_deals"]
max_safety_orders = thebot["max_safety_orders"]
martingale_volume_coefficient = float(
thebot["martingale_volume_coefficient"]
)
leverage_type = thebot["leverage_type"]
if leverage_type == "not_specified":
leverage_custom_value = (
1 # leverage customer value of 1 means no leverage
)
else:
leverage_custom_value = float(thebot["leverage_custom_value"])
funds_so_needed = safety_order_volume
total_so_funds = safety_order_volume
if max_safety_orders < 1:
total_so_funds = 0
if max_safety_orders > 1:
for i in range(1, max_safety_orders):
funds_so_needed *= float(martingale_volume_coefficient)
total_so_funds += funds_so_needed
logger.info("Current bot settings :")
logger.info("Base order volume : %s" % base_order_volume)
logger.info("Safety order volume : %s" % safety_order_volume)
logger.info("Max active deals : %s" % max_active_deals)
logger.info("Max safety orders : %s" % max_safety_orders)
logger.info("SO volume scale : %s" % martingale_volume_coefficient)
if leverage_type == "not_specified":
logger.info("Leverage type : %s" % leverage_type)
else:
logger.info("Leverage type : %s" % leverage_type)
logger.info("Leverage custom value : %s" % leverage_custom_value)
# Calculate the BO/SO ratio
bo_percentage = (
100
* float(base_order_volume)
/ (float(base_order_volume) + float(total_so_funds))
)
so_percentage = (
100
* float(total_so_funds)
/ (float(total_so_funds) + float(base_order_volume))
)
logger.info("BO percentage: %s" % bo_percentage)
if max_safety_orders >= 1:
logger.info("SO percentage: %s" % so_percentage)
# Calculate compound values
bo_profit = (
((profit_sum * bo_percentage) / 100) / max_active_deals
) * leverage_custom_value
logger.info("BO compound value: %s" % bo_profit)
if max_safety_orders >= 1:
so_profit = bo_profit * (safety_order_volume / base_order_volume)
logger.info("SO compound value: %s" % so_profit)
else:
so_profit = 0
# Update the bot
update_bot_order_volumes(
thebot,
(base_order_volume + bo_profit),
(safety_order_volume + so_profit),
profit_sum,
deals_count,
max_safety_orders,
)
else:
logger.info(
f"{bot_name}\nNo (new) profit made, no BO/SO value updates needed!",
True,
)
else:
logger.info(f"{bot_name}\nNo (new) deals found for this bot!", True)
def init_compound_db():
"""Create or open database to store bot and deals data."""
try:
dbname = f"{program}.sqlite3"
dbpath = f"file:{datadir}/{dbname}?mode=rw"
dbconnection = sqlite3.connect(dbpath, uri=True)
logger.info(f"Database '{datadir}/{dbname}' opened successfully")
except sqlite3.OperationalError:
dbconnection = sqlite3.connect(f"{datadir}/{dbname}")
dbcursor = dbconnection.cursor()
logger.info(f"Database '{datadir}/{dbname}' created successfully")
dbcursor.execute(
"CREATE TABLE deals (dealid INT Primary Key, profit REAL, botid int)"
)
dbcursor.execute(
"CREATE TABLE bots (botid INT Primary Key, startbo REAL, startso REAL, startactivedeals int)"
)
logger.info("Database tables created successfully")
return dbconnection
def upgrade_compound_db():
"""Upgrade database if needed."""
try:
cursor.execute("ALTER TABLE deals ADD COLUMN profit REAL")
logger.info("Database table deals upgraded (column profit)")
except sqlite3.OperationalError:
pass
try:
cursor.execute("ALTER TABLE deals ADD COLUMN botid int")
logger.info("Database table deals upgraded (column botid)")
except sqlite3.OperationalError:
pass
try:
cursor.execute(
"CREATE TABLE bots (botid INT Primary Key, startbo REAL, startso REAL, startactivedeals int)"
)
logger.info("Database schema upgraded (table bots)")
except sqlite3.OperationalError:
pass
# 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"),
)
# Initialize 3Commas API
api = init_threecommas_api(config)
# Upgrade config file if needed
config = upgrade_config(logger, api, config)
logger.info(f"Loaded configuration from '{datadir}/{program}.ini'")
# Initialize or open the database
db = init_compound_db()
cursor = db.cursor()
# Upgrade the database if needed
upgrade_compound_db()
# Auto compound profit by tweaking SO/BO
while True:
# Reload config files and data to catch changes
config = load_config()
logger.info(f"Reloaded configuration from '{datadir}/{program}.ini'")
# Configuration settings
timeint = int(config.get("settings", "timeinterval"))
for section in config.sections():
# Each section is a bot
if section.startswith("bot_"):
botid = remove_prefix(section, "bot_")
if botid:
boterror, botdata = api.request(
entity="bots",
action="show",
action_id=str(botid),
)
if botdata:
compound_bot(config, botdata)
else:
if boterror and "msg" in boterror:
logger.error(
"Error occurred updating bots: %s" % boterror["msg"]
)
else:
logger.error("Error occurred updating bots")
else:
logger.error("Invalid botid found: %s" % botid)
if not wait_time_interval(logger, notification, timeint):
break