forked from MvantLoo/adx-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adx_bot.py
508 lines (432 loc) · 18.7 KB
/
adx_bot.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
import ccxt
import time
import math
import config
import sys
from time import gmtime, strftime
from py3cw.request import Py3CW
from pathlib import Path
from datetime import timezone
import datetime
import numpy as np
import pandas as pd
import pandas_ta as ta
import operator
# Setup
p3cw = Py3CW(
key=config.TC_API_KEY,
secret=config.TC_API_SECRET,
request_options={
'request_timeout': 30,
'nr_of_retries': 5,
'retry_status_codes': [500, 502, 503, 504]
}
)
if config.EXCHANGE == 'FTX':
exchange = ccxt.ftx({
'apiKey': config.API_KEY,
'secret': config.SECRET_KEY,
'headers': {'FTX-SUBACCOUNT': config.SUB_ACCOUNT}
})
elif config.EXCHANGE == 'BINANCE':
exchange = ccxt.binance({
'apiKey': config.API_KEY,
'secret': config.SECRET_KEY
})
#exchange.verbose = True
def get_markets():
trycnt = 4
while trycnt > 0:
try:
all_markets = exchange.load_markets(True)
trycnt = 0
except Exception as e:
print("Connection error, trying again...")
f = open("3ctrigger_log.txt", "a")
f.write(f'Exchange cononnection error at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC\n')
f.close()
trycnt -= 1
if trycnt == 3:
time.sleep(3)
elif trycnt == 2:
time.sleep(15)
elif trycnt == 1:
time.sleep(45)
else:
return all_markets
def get_tradeable_balance():
trycnt = 4
while trycnt > 0:
try:
account_balances = exchange.fetch_balance()
balance = account_balances["total"]["USD"]
print(f'Balance: {balance}')
trycnt = 0
except Exception as e:
print(e)
f = open("3ctrigger_log.txt", "a")
f.write(f'Exchange connection error at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC: {e}\n')
f.close()
trycnt -= 1
if trycnt == 3:
print(f'Connection error, trying again... (count down: {trycnt})')
time.sleep(5)
elif trycnt == 2:
print(f'Connection error, trying again... (count down: {trycnt})')
time.sleep(15)
elif trycnt == 1:
print(f'Connection error, trying again... (count down: {trycnt})')
time.sleep(45)
elif trycnt == 0:
print('Giving up....')
exit()
else:
return balance
def start_bot(pair, ids):
bot_id = ids[pair]
f = open("3ctrigger_log.txt", "a")
f.write(f'Enable bot {bot_id} for {pair} at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC\n')
f.close()
error, bot_trigger = p3cw.request(
entity = 'bots',
action = 'enable',
additional_headers={"Forced-Mode": config.TC_MODE},
action_id = bot_id
)
print(f'Bot Enabled for {pair} - {bot_id}')
return bot_trigger
def close_deal(pair, bot_id):
if type(bot_id) is not int:
bot_id = bot_id[pair]
f = open("3ctrigger_log.txt", "a")
f.write(f'Panic Close - {pair} - {bot_id} at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC\n')
f.close()
error, deal_close = p3cw.request(
entity = 'bots',
action = 'panic_sell_all_deals',
additional_headers={"Forced-Mode": config.TC_MODE},
action_id = str(bot_id)
)
print(f'Panic Close - {pair}')
time.sleep(5)
return deal_close
def get_positions():
open_positions = {}
all_positions = exchange.fetchPositions(None, {"showAvgPrice": True})
try:
if 'info' in all_positions[0]:
for y in all_positions:
x=y['info']
future = (x["future"])
size = (x["size"])
side = (x["side"])
cost = (x["cost"])
recentAverageOpenPrice = (x["recentAverageOpenPrice"])
if size != '0.0':
open_positions[future] = size, side, cost, recentAverageOpenPrice
else:
for x in all_positions:
future = (x["future"])
size = (x["size"])
side = (x["side"])
cost = (x["cost"])
recentAverageOpenPrice = (x["recentAverageOpenPrice"])
if size != '0.0':
open_positions[future] = size, side, cost, recentAverageOpenPrice
except Exception as e:
pass
return open_positions
def load_bot_ids(filename):
d = {}
with open(filename) as f:
for line in f:
(key, val) = line.split(':')
d[key] = val.rstrip('\n')
return d
def get_max_bot_usage(balance):
if config.MARTINGALE_VOLUME_COEFFICIENT == 1.0:
max_bot_usage = (config.BASE_ORDER_VOLUME + (config.SAFETY_ORDER_VOLUME*config.MAX_SAFETY_ORERS)) / config.LEVERAGE_CUSTOM_VALUE
else:
max_bot_usage = (config.BASE_ORDER_VOLUME + (config.SAFETY_ORDER_VOLUME*(config.MARTINGALE_VOLUME_COEFFICIENT**config.MAX_SAFETY_ORERS - 1) / (config.MARTINGALE_VOLUME_COEFFICIENT - 1))) / config.LEVERAGE_CUSTOM_VALUE
return max_bot_usage
def disable_bot(pair, bot_id):
f = open("3ctrigger_log.txt", "a")
f.write(f'Disable bot {bot_id} for {pair} - {bot_id} at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC\n')
f.close()
error, data = p3cw.request(
entity='bots',
action='disable',
additional_headers={"Forced-Mode": config.TC_MODE},
action_id = str(bot_id),
)
print(f'Error: {error}')
print(f'Bot Disabled for {pair} - {bot_id}')
def get_bot_info():
data = []
bot_info = []
first_run = True
base_offset = 100
offset = 0
while len(data) == 100 or first_run:
first_run = False
error, data = p3cw.request(
entity='bots',
action='',
additional_headers={"Forced-Mode": config.TC_MODE},
payload={
"account_id": config.TC_ACCOUNT_ID,
"limit": base_offset,
"offset": offset,
}
)
if type(data) is not list:
print("Data from 3Commas is not a list.")
print(data)
data = []
bot_info = bot_info + data
offset += base_offset
return bot_info
def get_enabled_bots():
enabled_bots = {}
bot_list = get_bot_info()
for bot in bot_list:
if bot["is_enabled"] == True:
if config.LONG_PREFIX in bot["name"] or config.SHORT_PREFIX in bot["name"]:
bot_id = bot["id"]
bot_strategy = bot["strategy"]
# Default for FTX Futures
bot_pair = bot["pairs"][0]
if config.LEVERAGE_CUSTOM_VALUE == 0:
bot_pair_base = bot["pairs"][0].split("_")[0]
bot_pair_quote = bot["pairs"][0].split("_")[1]
# Spot for FTX or BINANCE
if config.EXCHANGE == 'FTX':
bot_pair = f'{bot_pair_quote}/{bot_pair_base}'
elif config.EXCHANGE == 'BINANCE':
bot_pair = f'{bot_pair_quote}{bot_pair_base}'
enabled_bots[bot_pair] = bot_id, bot_strategy
else:
enabled_bots[bot_pair[4:]] = bot_id, bot_strategy
return enabled_bots
def perp_stats(perp):
stats = []
time_frame_mins = config.TF
adx_length = config.ADX_LENGTH
ema_length = config.EMA_LENGTH
look_back = max(adx_length, ema_length)*4
current_time = datetime.datetime.now()
from_time = current_time - datetime.timedelta(minutes = time_frame_mins*look_back)
from_time_stamp = int(from_time.timestamp() * 1000)
if time_frame_mins < 60:
time_frame = time_frame_mins
time_frame_units = 'm'
elif time_frame_mins >= 60:
time_frame = int(time_frame_mins//60)
time_frame_units = 'h'
trycnt = 4
while trycnt > 0:
try:
candles = exchange.fetch_ohlcv(perp, str(int(time_frame)) + time_frame_units, from_time_stamp)
trycnt = 0
except Exception as e:
print("Connection error, trying again...")
f = open("3ctrigger_log.txt", "a")
f.write(f'Exchange cononnection error at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC: {e}\n')
f.close()
trycnt -= 1
if trycnt == 3:
time.sleep(3)
elif trycnt == 2:
time.sleep(15)
elif trycnt == 1:
time.sleep(45)
else:
# Load data into a pandas daataframe
df = pd.DataFrame(np.array(candles), columns=['Time', 'Open', 'High', 'Low', 'Close', 'Volume'])
# Get ADX and DMI values
df.ta.adx(close='Close', length=adx_length, append=True)
# Get ADX Slope
df['ADX_SLOPE'] = df['ADX_'+str(adx_length)].diff()
# Get EMA Values
df.ta.ema(close='Close', length=ema_length, append=True)
# Calculate normalized Slope of EMA values
df['EMA_SLOPE'] = df['EMA_'+str(ema_length)].diff().abs() / df['EMA_'+str(ema_length)]
# Calculate EMA(3) of slope values
df['EMA_SMOOTH'] = df['EMA_SLOPE'].ewm(span=config.EMA_SMOOTHING).mean()
#if df.loc[(df.shape[0]-2), 'ADX_SLOPE'] > 0:
# adx_direction = 1
#elif df.loc[(df.shape[0]-2), 'ADX_SLOPE'] =< 0:
# adx_direction = -1
if config.EARLY_CLOSE and df.loc[(df.shape[0]-2), 'ADX_SLOPE'] < df.loc[(df.shape[0]-3), 'ADX_SLOPE'] and df.loc[(df.shape[0]-3), 'ADX_SLOPE'] < df.loc[(df.shape[0]-4), 'ADX_SLOPE'] and df.loc[(df.shape[0]-4), 'ADX_SLOPE'] and df.loc[(df.shape[0]-5), 'ADX_SLOPE']:
adx_direction = -1
else:
adx_direction = df.loc[(df.shape[0]-2), 'ADX_SLOPE']
# Add values to a list
stats.append(perp)
stats.append(df.loc[(df.shape[0]-2), 'ADX_'+str(adx_length)])
stats.append(adx_direction)
stats.append(df.loc[(df.shape[0]-2), 'DMP_'+str(adx_length)])
stats.append(df.loc[(df.shape[0]-2), 'DMN_'+str(adx_length)])
stats.append(df.loc[(df.shape[0]-2), 'EMA_SMOOTH'])
return stats
open_positions = {}
unsorted_tradable_perps = []
sorted_tradable_perps = []
tradable_perps = {}
enabled_bots = {}
# Check for existing list of bot id's and load into a dictionary
longbots_file = Path("lbotid_list.txt")
shortbots_file = Path("sbotid_list.txt")
if not longbots_file.is_file() or not shortbots_file.is_file():
print("No bot ID lists were found. Create bots and ID list before continuing.")
print("Bye!")
sys.exit()
#load files into a dictionary
long_bot_ids = {}
short_bot_ids = {}
long_bot_ids = load_bot_ids("lbotid_list.txt")
short_bot_ids = load_bot_ids("sbotid_list.txt")
# Check perp lists for mismatches? Or just assume they are the same - or run it at bot creation?
tradeable_balance = get_tradeable_balance()
bot_usage = get_max_bot_usage(tradeable_balance)
print(f'Max bot usage: {bot_usage}')
# Calc max number of bots - constrained by bot usage
if math.floor((float(tradeable_balance) * config.FUNDS_USAGE) / bot_usage) < config.MAX_OPEN_POSITIONS:
max_positions = math.floor((float(tradeable_balance) * config.FUNDS_USAGE) / bot_usage)
else:
max_positions = config.MAX_OPEN_POSITIONS
print(f'Max positions: {max_positions}')
if max_positions < 1:
print('ERROR: Not enough liquidity to start a bot')
exit()
last_balance_check = strftime("%Y-%m-%d", gmtime())
f = open("3ctrigger_log.txt", "a")
f.write("<<<<>>>>\n")
f.write(f'Script Run at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC\n')
f.write(f'Time-frame: {config.TF}\n')
f.write(f'Balance: {tradeable_balance}, Max Positions: {max_positions}\n')
f.write("<<<<>>>>\n")
f.close()
update_stats_time = True
## <<<<<<<<<<< START LOOP HERE >>>>>>>>>>>>>>>>>
while True:
time.sleep(0.5) # Give the CPU a break.
while update_stats_time:
print('Getting perps OHLCV data...')
for perp in long_bot_ids:
print(".", end =" ")
unfiltered_stats = perp_stats(perp)
ADX = unfiltered_stats[1]
ADX_Slope = unfiltered_stats[2]
DM_plus = unfiltered_stats[3]
DM_minus = unfiltered_stats[4]
if ADX > config.ADX_MIN_LONG and (ADX_Slope > 0 and ((DM_plus > DM_minus and ADX > DM_minus) or (ADX < DM_plus and ADX < DM_minus))) and DM_plus > DM_minus:
unfiltered_stats.append("long")
unsorted_tradable_perps.append(unfiltered_stats)
elif ADX > config.ADX_MIN_SHORT and (ADX_Slope > 0 and ((DM_plus < DM_minus and ADX > DM_plus) or (ADX < DM_plus and ADX < DM_minus))) and DM_plus < DM_minus:
unfiltered_stats.append("short")
unsorted_tradable_perps.append(unfiltered_stats)
elif ADX_Slope < 0:
unfiltered_stats.append("disable")
unsorted_tradable_perps.append(unfiltered_stats)
else:
unfiltered_stats.append("ignore")
unsorted_tradable_perps.append(unfiltered_stats)
print(".")
# Sort the lists by EMA Slope
sorted_tradable_perps = sorted(unsorted_tradable_perps, key=operator.itemgetter(5), reverse = True)
# Convert list to dictionary
number_of_perps = len(sorted_tradable_perps)
i = 0
for i in range(number_of_perps):
tradable_perps[sorted_tradable_perps[i][0]] = sorted_tradable_perps[i][1], sorted_tradable_perps[i][2], sorted_tradable_perps[i][3], sorted_tradable_perps[i][4], sorted_tradable_perps[i][5], sorted_tradable_perps[i][6]
open_positions = get_positions()
print("Open Positions:")
print(open_positions)
enabled_bots = get_enabled_bots()
print("Enabled Bots:")
print(enabled_bots)
available_bots = max_positions - max(len(enabled_bots), len(open_positions))
# Check for orphaned deals - Only useful for when CLOSE_DEALS_WITH_BOT = True
for open_perp in open_positions:
if open_perp not in enabled_bots and config.CLOSE_DEALS_WITH_BOT:
if open_positions[open_perp][1] == "buy":
close_deal(open_perp, long_bot_ids)
if open_positions[open_perp][1] == "sell":
close_deal(open_perp, short_bot_ids)
# Handle Open positions without active/enabled bots.
for disabled_perp in open_positions:
if disabled_perp not in enabled_bots:
if config.CLOSE_DEALS:
if tradable_perps[disabled_perp][2] > tradable_perps[disabled_perp][3] and open_positions[disabled_perp][1] == "sell":
close_deal(disabled_perp, short_bot_ids)
elif tradable_perps[disabled_perp][2] < tradable_perps[disabled_perp][3] and open_positions[disabled_perp][1] == "buy":
close_deal(disabled_perp, long_bot_ids)
elif tradable_perps[disabled_perp][0] < 15 and open_positions[disabled_perp][1] == "sell":
close_deal(disabled_perp, short_bot_ids)
elif tradable_perps[disabled_perp][0] < 15 and open_positions[disabled_perp][1] == "buy":
close_deal(disabled_perp, long_bot_ids)
# Disable a bot if ADX slope goes negative.
for adx_perp in enabled_bots:
if adx_perp in tradable_perps:
if tradable_perps[adx_perp][5] == "disable" or tradable_perps[adx_perp][5] == "ignore":
disable_bot(adx_perp, enabled_bots[adx_perp][0])
if config.CLOSE_DEALS_WITH_BOT:
close_deal(adx_perp, enabled_bots[adx_perp][0])
time.sleep(10) # Small delay, closing positions above lag execution of following two lines.
open_positions = get_positions()
enabled_bots = get_enabled_bots()
available_bots = max_positions - max(len(enabled_bots), len(open_positions))
if available_bots == 0 and ((len(open_positions) - len(enabled_bots))/max_positions) > config.DEAL_BOT_RATIO_WARNING:
print("Deal to active bot ratio warning!!")
print(f'Open positions: {len(open_positions)}, Enabled bots: {len(enabled_bots)}')
print("Consider manually closing open positions.")
print(f'Open positions: {len(open_positions)}')
print(f'Enabled Bots: {len(enabled_bots)}')
print(f'Available Bots: {available_bots}')
print(f'Max Positions: {max_positions}')
if tradable_perps and available_bots > 0:
print("Getting ready to enable bots....")
for trade_perp in tradable_perps:
if trade_perp not in enabled_bots and available_bots > 0:
if tradable_perps[trade_perp][5] == "long":
trigger_long = start_bot(trade_perp, long_bot_ids)
available_bots -= 1
print("<<<>>>")
elif tradable_perps[trade_perp][5] == "short":
trigger_short = start_bot(trade_perp, short_bot_ids)
available_bots -= 1
print("<<<>>>")
update_stats_time = False
# write to file next expected checkin time.
next_update = datetime.datetime.now() + datetime.timedelta(seconds=config.TF*60)
f = open("adx_bot_status.txt", "w")
f.write(str(next_update))
f.close()
print("Waiting for next TF....")
time.sleep(60)
open_positions.clear()
unsorted_tradable_perps.clear()
sorted_tradable_perps.clear()
tradable_perps.clear()
enabled_bots.clear()
#now = datetime.datetime.now()
if strftime("%Y-%m-%d", gmtime()) > last_balance_check:
tradeable_balance = get_tradeable_balance()
bot_usage = get_max_bot_usage(tradeable_balance)
if math.floor((float(tradeable_balance) * config.FUNDS_USAGE) / bot_usage) < config.MAX_OPEN_POSITIONS:
max_positions = math.floor((float(tradeable_balance) * config.FUNDS_USAGE) / bot_usage)
else:
max_positions = config.MAX_OPEN_POSITIONS
last_balance_check = strftime("%Y-%m-%d", gmtime())
f = open("3ctrigger_log.txt", "a")
f.write(f'Updated Balance: {tradeable_balance}, Max Positions: {max_positions} at {strftime("%Y-%m-%d %H:%M:%S", gmtime())} UTC\n')
f.write(">>>>\n")
f.close()
print(f'Updated Balance: {tradeable_balance}, Max Positions: {max_positions}')
now = datetime.datetime.now(timezone.utc)
midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
minutes = ((now - midnight).seconds) // 60
if (minutes % config.TF) == 0 :
time.sleep(5)
update_stats_time = True