forked from cyberjunky/3commas-cyber-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchlist_hodloo.py
executable file
·355 lines (289 loc) · 10.6 KB
/
watchlist_hodloo.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
#!/usr/bin/env python3
"""Cyberjunky's 3Commas bot helpers."""
import argparse
import configparser
import json
import os
import sys
import time
from pathlib import Path
from telethon import TelegramClient, events
from helpers.logging import Logger, NotificationHandler
from helpers.misc import format_pair
from helpers.threecommas import (
close_threecommas_deal,
get_threecommas_account_marketcode,
init_threecommas_api,
load_blacklist,
trigger_threecommas_bot_deal,
)
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",
"debug": False,
"logrotate": 7,
"3c-apikey": "Your 3Commas API Key",
"3c-apisecret": "Your 3Commas API Secret",
"tgram-phone-number": "Your Telegram Phone number",
"tgram-api-id": "Your Telegram API ID",
"tgram-api-hash": "Your Telegram API Hash",
"notifications": False,
"notify-urls": ["notify-url1"],
"exchange": "Bittrex / Binance / Kucoin",
"mode": "Telegram"
}
cfg["hodloo_5"] = {
"bnb-botids": [12345, 67890],
"btc-botids": [12345, 67890],
"busd-botids": [12345, 67890],
"eth-botids": [12345, 67890],
"eur-botids": [12345, 67890],
"usdt-botids": [12345, 67890],
}
cfg["hodloo_10"] = {
"bnb-botids": [12345, 67890],
"btc-botids": [12345, 67890],
"busd-botids": [12345, 67890],
"eth-botids": [12345, 67890],
"eur-botids": [12345, 67890],
"usdt-botids": [12345, 67890],
}
with open(f"{datadir}/{program}.ini", "w") as cfgfile:
cfg.write(cfgfile)
return None
async def handle_event(category, event):
"""Handle the received Telegram event"""
logger.debug(
"Received telegram message on %s: '%s'"
% (category, event.message.text.replace("\n", " - "))
)
# Parse the event and do some error checking
trigger = event.raw_text.splitlines()
pair = trigger[0].replace("\n", "").replace("**", "")
base = pair.split("/")[1]
coin = pair.split("/")[0]
logger.info(
f"Received message on {category}% for {base}_{coin}"
)
if base.lower() not in ("bnb", "btc", "busd", "eth", "eur", "usdt"):
logger.debug(
f"{base}_{coin}: base '{base}' is not yet supported."
)
return
botids = get_botids(category, base)
if len(botids) == 0:
logger.debug(
f"{base}_{coin}: no valid botids configured for base '{base}'."
)
return
await client.loop.run_in_executor(
None, process_botlist, botids, coin
)
notification.send_notification()
def process_botlist(botidlist, coin):
"""Process the list of bots for the given coin"""
for botid in botidlist:
if botid:
error, data = api.request(
entity="bots",
action="show",
action_id=str(botid),
)
if data:
# Check number of deals, otherwise error will occur anyway (save some processing)
if data["active_deals_count"] >= data["max_active_deals"]:
logger.info(
f"Bot '{data['name']}' reached maximum number of "
f"deals ({data['max_active_deals']}). "
f"Cannot start a new deal for {coin}!",
True
)
else:
process_bot_deal(data, coin, "LONG")
else:
if error and "msg" in error:
logger.error(
"Error occurred fetching bot (%s) data: %s" % (str(botid), error["msg"])
)
else:
logger.error(
"Error occurred fetching bot (%s) data" % str(botid)
)
def process_bot_deal(thebot, coin, trade):
"""Check pair and trigger the bot deal."""
# Gather some bot values
base = thebot["pairs"][0].split("_")[0]
bot_exchange = thebot["account_name"]
minvolume = thebot["min_volume_btc_24h"]
logger.debug("Base coin for this bot: %s" % base)
logger.debug("Minimal 24h volume of %s BTC" % minvolume)
# Get marketcode from array
marketcode = marketcodes.get(thebot["id"])
if not marketcode:
return
logger.info("Bot: %s" % thebot["name"])
logger.info("Exchange %s (%s) used" % (bot_exchange, marketcode))
# Construct pair based on bot settings and marketcode (BTC stays BTC, but USDT can become BUSD)
pair = format_pair(logger, marketcode, base, coin)
if trade == "LONG":
# Check if pair is on 3Commas blacklist
if pair in blacklist:
logger.debug(
f"Pair '{pair}' is on your 3Commas blacklist and was skipped",
True
)
return
# Check if pair is in bot's pairlist
if pair not in thebot["pairs"]:
logger.info(
f"Pair '{pair}' is not in '{thebot['name']}' pairlist and was skipped",
True
)
return
# We have valid pair for our bot so we trigger an open asap action
logger.info("Triggering your 3Commas bot for a start deal of '%s'" % pair)
trigger_threecommas_bot_deal(logger, api, thebot, pair, (len(blacklistfile) > 0))
else:
# Find active deal(s) for this bot so we can close deal(s) for pair
deals = thebot["active_deals"]
if deals:
for deal in deals:
if deal["pair"] == pair:
logger.info("Triggering your 3Commas bot for a (panic) sell of '%s'" % pair)
close_threecommas_deal(logger, api, deal["id"], pair)
return
logger.info(
"No deal(s) running for bot '%s' and pair '%s'"
% (thebot["name"], pair), True
)
else:
logger.info("No deal(s) running for bot '%s'" % thebot["name"], True)
def prefetch_marketcodes():
"""Gather and store marketcodes for all bots."""
marketcodearray = {}
botids = list()
for category in ("5", "10"):
for base in ("bnb", "btc", "busd", "eth", "eur", "usdt"):
botids += get_botids(category, base)
logger.debug(
f"Botids collected: {botids}"
)
for botid in botids:
if botid:
boterror, botdata = api.request(
entity="bots",
action="show",
action_id=str(botid),
)
if botdata:
accountid = botdata["account_id"]
# Get marketcode (exchange) from account if not already fetched
marketcode = get_threecommas_account_marketcode(logger, api, accountid)
marketcodearray[botdata["id"]] = marketcode
else:
if boterror and "msg" in boterror:
logger.error(
"Error occurred fetching marketcode data: %s" % boterror["msg"]
)
else:
logger.error("Error occurred fetching marketcode data")
return marketcodearray
def get_botids(category, base):
"""Get list of botids from configuration based on category and base"""
return json.loads(config.get(f"hodloo_{category}", f"{base.lower()}-botids"))
# 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)
parser.add_argument("-b", "--blacklist", help="blacklist to use", type=str)
args = parser.parse_args()
if args.datadir:
datadir = args.datadir
else:
datadir = os.getcwd()
# pylint: disable-msg=C0103
if args.blacklist:
blacklistfile = f"{datadir}/{args.blacklist}"
else:
blacklistfile = ""
# Create or load configuration file
config = load_config()
if not config:
logger = Logger(datadir, program, None, 7, False, False)
logger.info(
f"Created example config file '{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"),
)
# Which Exchange to use?
exchange = config.get("settings", "exchange")
if exchange not in ("Bittrex", "Binance", "Kucoin"):
logger.error(
f"Exchange {exchange} not supported. Must be 'Bittrex', 'Binance' or 'Kucoin'!"
)
sys.exit(0)
# Which Mode to use?
mode = config.get("settings", "mode")
if mode not in ("Telegram", "Websocket"):
logger.error(
f"Mode {mode} not supported. Must be 'Telegram' or 'Websocket'!"
)
sys.exit(0)
# Initialize 3Commas API
api = init_threecommas_api(config)
# Prefect marketcodes for all bots
marketcodes = prefetch_marketcodes()
# Prefect blacklists
blacklist = load_blacklist(logger, api, blacklistfile)
if mode == "Telegram":
# Watchlist telegram trigger
client = TelegramClient(
f"{datadir}/{program}",
config.get("settings", "tgram-api-id"),
config.get("settings", "tgram-api-hash"),
).start(config.get("settings", "tgram-phone-number"))
@client.on(events.NewMessage(chats=f"Hodloo {exchange} 5%"))
async def callback_5(event):
"""Receive Telegram message."""
await handle_event("5", event)
notification.send_notification()
@client.on(events.NewMessage(chats=f"Hodloo {exchange} 10%"))
async def callback_10(event):
"""Receive Telegram message."""
await handle_event("10", event)
notification.send_notification()
# Start telegram client
client.start()
logger.info(
f"Listening to telegram chat 'Hodloo {exchange} 5%' and "
f"'Hodloo {exchange} 10%' for triggers",
True,
)
client.run_until_disconnected()
# No else case, mode is already checked before this statement