-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
544 lines (438 loc) · 21 KB
/
main.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
import pandas as pd
import sys
import math
import logging
import timeit
from backtesting.lib import crossover
import utils.config as config
import utils.database as database
import exchanges.binance as binance
import utils.telegram as telegram
# sets the output display precision in terms of decimal places to 8.
# this is helpful when trading against BTC. The value in the dataframe has the precision 8 but when we display it
# by printing or sending to telegram only shows precision 6
pd.set_option("display.precision", 8)
# log file to store error messages
log_filename = "main.log"
logging.basicConfig(filename=log_filename, level=logging.INFO,
format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p -')
# Global Vars
telegram_token = telegram.telegram_token_main
# sl = single line message; ml = multi line message
telegram_prefix_sl = ''
telegram_prefix_ml = ''
# strategy
# strategy_name = ''
def read_arguments():
# total arguments
n = len(sys.argv)
if n < 2:
print("Argument is missing")
time_frame = input('Enter time frame (1d, 4h or 1h):')
# run_mode = input('Enter run mode (test, prod):')
else:
# argv[0] in Python is always the name of the script.
time_frame = sys.argv[1]
# run modes
# test - does not execute orders on the exchange
# prod - execute orders on the exchange
# run_mode = sys.argv[2]
return time_frame #, run_mode
def apply_arguments(time_frame):
global telegram_token, telegram_prefix_ml, telegram_prefix_sl
if time_frame == "1h":
telegram_prefix_sl = telegram.telegram_prefix_bot_1h_sl
telegram_prefix_ml = telegram.telegram_prefix_bot_1h_ml
elif time_frame == "4h":
telegram_prefix_sl = telegram.telegram_prefix_bot_4h_sl
telegram_prefix_ml = telegram.telegram_prefix_bot_4h_ml
elif time_frame == "1d":
telegram_prefix_sl = telegram.telegram_prefix_bot_1d_sl
telegram_prefix_ml = telegram.telegram_prefix_bot_1d_ml
else:
msg = "Incorrect time frame. Bye"
def get_backtesting_results(strategy_id, symbol, time_frame):
# get best ema
df = database.get_backtesting_results_by_symbol_timeframe_strategy(connection=database.conn,
symbol=symbol,
time_frame=time_frame,
strategy_id=strategy_id)
if not df.empty:
fast_ema = int(df.Ema_Fast.values[0])
slow_ema = int(df.Ema_Slow.values[0])
else:
fast_ema = int("0")
slow_ema = int("0")
# if bestEMA does not exist return empty dataframe in order to no use that trading pair
return fast_ema, slow_ema
def get_data(symbol, time_frame):
# makes 3 attempts to get historical data
max_retry = 3
retry_count = 1
success = False
from datetime import date
from dateutil.relativedelta import relativedelta
import time
# calc start date from were we need historical data candles
# we do this to make sure we get same ema/sma values as those at tradingview
#-------------------------------------
today = date.today()
if time_frame == "1h":
pastdate = today - relativedelta(hours=200*8)
elif time_frame == "4h":
pastdate = today - relativedelta(hours=200*8)
elif time_frame == "1d":
pastdate = today - relativedelta(days=200*8)
tuple = pastdate.timetuple()
timestamp = time.mktime(tuple)
startdate = str(timestamp)
while retry_count < max_retry and not success:
try:
df = pd.DataFrame(binance.client.get_historical_klines(symbol=symbol, interval=time_frame, start_str=startdate))
success = True
except Exception as e:
retry_count += 1
msg = sys._getframe( ).f_code.co_name+" - "+symbol+" - "+repr(e)
print(msg)
if not success:
msg = f"Failed after {max_retry} tries to get historical data. Unable to retrieve data. "
msg = msg + sys._getframe( ).f_code.co_name+" - "+symbol
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, telegram.EMOJI_WARNING, msg)
return pd.DataFrame()
# Check if df is empty
if df.empty:
msg = f"Received empty data for {symbol} at {time_frame} timeframe."
print(msg)
telegram.send_telegram_message(telegram_token, telegram.EMOJI_WARNING, msg)
return pd.DataFrame() # Return an empty dataframe if no data is retrieved
# Proceed with processing if df is not empty
try:
df = df[[0,4]]
df.columns = ['Time','Close']
# using dictionary to convert specific columns
convert_dict = {'Close': float}
df = df.astype(convert_dict)
df.Time = pd.to_datetime(df.Time, unit='ms')
# Remove the last row
# This functionality is valuable because our data collection doesn't always coincide precisely with the closing time of a candle.
# As a result, the last row in our dataset represents the most current price information.
# This becomes significant when applying technical analysis, as it directly influences the accuracy of metrics and indicators.
# The implications extend to the decision-making process for buying or selling, making it essential to account for the real-time nature of the last row in our data.
df = df.drop(df.index[-1])
return df
except Exception as e:
msg = sys._getframe( ).f_code.co_name+" - "+repr(e)
msg = telegram_prefix_sl + msg
print(msg)
return pd.DataFrame() # Return an empty dataframe in case of KeyError
# calculates moving averages
def apply_technicals(df, fast_ema=0, slow_ema=0):
df['FastEMA'] = df['Close'].ewm(span=fast_ema, adjust=False).mean()
df['SlowEMA'] = df['Close'].ewm(span=slow_ema, adjust=False).mean()
df['SMA50'] = df['Close'].rolling(50).mean()
df['SMA200'] = df['Close'].rolling(200).mean()
# calc current pnl
def get_current_pnl(symbol, current_price, time_frame):
try:
# get buy price
df_buy_price = database.get_positions_by_bot_symbol_position(database.conn, bot=time_frame, symbol=symbol, position=1)
buy_price = 0
pnl_perc = 0
if not df_buy_price.empty:
# get buy price
buy_price = df_buy_price['Buy_Price'].iloc[0]
# check if buy price is fulfilled
if not math.isnan(buy_price) and buy_price > 0:
# calc pnl percentage
pnl_perc = ((current_price - buy_price) / buy_price) * 100
pnl_perc = round(pnl_perc, 2)
return pnl_perc
except Exception as e:
msg = sys._getframe( ).f_code.co_name+" - "+repr(e)
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, telegram.EMOJI_WARNING, msg)
def trade(time_frame, run_mode):
# Make sure we are only trying to buy positions on symbols included on market phases table
database.delete_positions_not_top_rank(database.conn)
# list of symbols in position - SELL
df_sell = database.get_positions_by_bot_position(database.conn, bot=time_frame, position=1)
list_to_sell = df_sell.Symbol.tolist()
# list of symbols in position - BUY
df_buy = database.get_positions_by_bot_position(database.conn, bot=time_frame, position=0)
list_to_buy = df_buy.Symbol.tolist()
# if trading by time frame is
# Enabled: Buy new positions and sell existing ones.
# Disabled: Will not buy new positions but will continue to attempt to sell existing positions based on sell strategy conditions.disabled => Will not buy new positions but will continue to attempt to sell existing positions based on sell strategy conditions.
if (time_frame == "1h" and not config.bot_1h) or (time_frame == "4h" and not config.bot_4h) or (time_frame == "1d" and not config.bot_1d):
list_to_buy = []
# check open positions and SELL if conditions are fulfilled
for symbol in list_to_sell:
# initialize vars
fast_ema = 0
slow_ema = 0
# get best backtesting results for the strategy
if config.strategy_id in ["ema_cross_with_market_phases", "ema_cross"]:
fast_ema, slow_ema = get_backtesting_results(strategy_id=config.strategy_id, symbol=symbol, time_frame=time_frame)
if fast_ema == 0 or slow_ema == 0:
msg = f'{symbol} - {config.strategy_name} - Best EMA values missing'
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, telegram.EMOJI_WARNING, msg)
continue
# get latest price data
df = get_data(symbol=symbol, time_frame=time_frame)
# Check if df is empty
if df.empty:
continue # Skip to the next iteration
apply_technicals(df, fast_ema, slow_ema)
# last row
lastrow = df.iloc[-1]
# Current price
current_price = lastrow.Close
# Current PnL
current_pnl = get_current_pnl(symbol, current_price, time_frame)
# if using stop loss
sell_stop_loss = False
if config.stop_loss > 0:
sell_stop_loss = current_pnl <= -config.stop_loss
# if using take profit 1
sell_tp_1 = False
if config.take_profit_1_pnl_perc > 0:
# check if tp1 occurred already
# Filter
df_tp1 = df_sell.loc[df_sell['Symbol'] == symbol, 'Take_Profit_1']
# Extract the single value from the result (assuming only one row matches)
tp1_occurred = df_tp1.values[0]
# if not occurred
if tp1_occurred == 0:
sell_tp_1 = current_pnl >= config.take_profit_1_pnl_perc
# if using take profit 2
sell_tp_2 = False
if config.take_profit_2_pnl_perc > 0:
# check if tp1 occurred already
# Filter
df_tp2 = df_sell.loc[df_sell['Symbol'] == symbol, 'Take_Profit_2']
# Extract the single value from the result (assuming only one row matches)
tp2_occurred = df_tp2.values[0]
# if not occurred
if tp2_occurred == 0:
sell_tp_2 = current_pnl >= config.take_profit_2_pnl_perc
# if using take profit 3
sell_tp_3 = False
if config.take_profit_3_pnl_perc > 0:
# check if tp1 occurred already
# Filter
df_tp3 = df_sell.loc[df_sell['Symbol'] == symbol, 'Take_Profit_3']
# Extract the single value from the result (assuming only one row matches)
tp3_occurred = df_tp3.values[0]
# if not occurred
if tp3_occurred == 0:
sell_tp_3 = current_pnl >= config.take_profit_3_pnl_perc
# if using take profit 3
sell_tp_4 = False
if config.take_profit_4_pnl_perc > 0:
# check if tp1 occurred already
# Filter
df_tp4 = df_sell.loc[df_sell['Symbol'] == symbol, 'Take_Profit_4']
# Extract the single value from the result (assuming only one row matches)
tp4_occurred = df_tp4.values[0]
# if not occurred
if tp4_occurred == 0:
sell_tp_4 = current_pnl >= config.take_profit_4_pnl_perc
# check sell condition for the strategy
if config.strategy_id in ["ema_cross_with_market_phases", "ema_cross"]:
condition_crossover = (lastrow.SlowEMA > lastrow.FastEMA)
sell_condition = condition_crossover
elif config.strategy_id in ["market_phases"]:
sell_condition = (lastrow.Close < lastrow.SMA50) or (lastrow.Close < lastrow.SMA200)
# set current PnL
current_price = lastrow.Close
database.update_position_pnl(database.conn,
bot=time_frame,
symbol=symbol,
curr_price=current_price)
if sell_condition or sell_stop_loss or sell_tp_1 or sell_tp_2 or sell_tp_3 or sell_tp_4:
# stop loss
if sell_stop_loss:
binance.create_sell_order(
symbol=symbol,
bot=time_frame,
fast_ema=fast_ema,
slow_ema=slow_ema,
reason=f"Stop loss {config.stop_loss}%"
)
# sell_codition ema crossover
elif sell_condition:
binance.create_sell_order(
symbol=symbol,
bot=time_frame,
fast_ema=fast_ema,
slow_ema=slow_ema,
)
# sell take profit 1
if sell_tp_1:
binance.create_sell_order(
symbol=symbol,
bot=time_frame,
fast_ema=fast_ema,
slow_ema=slow_ema,
reason=f"Take-Profit Level 1 - {config.take_profit_1_pnl_perc}% PnL - {config.take_profit_1_amount_perc}% Amount",
percentage=config.take_profit_1_amount_perc,
take_profit_num=1
)
# sell take profit 2
if sell_tp_2:
binance.create_sell_order(
symbol=symbol,
bot=time_frame,
fast_ema=fast_ema,
slow_ema=slow_ema,
reason=f"Take-Profit Level 2 - {config.take_profit_2_pnl_perc}% PnL - {config.take_profit_2_amount_perc}% Amount",
percentage=config.take_profit_2_amount_perc,
take_profit_num=2
)
# sell take profit 2
if sell_tp_3:
binance.create_sell_order(
symbol=symbol,
bot=time_frame,
fast_ema=fast_ema,
slow_ema=slow_ema,
reason=f"Take-Profit Level 3 - {config.take_profit_3_pnl_perc}% PnL - {config.take_profit_3_amount_perc}% Amount",
percentage=config.take_profit_3_amount_perc,
take_profit_num=3
)
# sell take profit 2
if sell_tp_4:
binance.create_sell_order(
symbol=symbol,
bot=time_frame,
fast_ema=fast_ema,
slow_ema=slow_ema,
reason=f"Take-Profit Level 4 - {config.take_profit_4_pnl_perc}% PnL - {config.take_profit_4_amount_perc}% Amount",
percentage=config.take_profit_4_amount_perc,
take_profit_num=4
)
else:
best_emas = "" if fast_ema == 0 or slow_ema == 0 else f"{fast_ema}/{slow_ema}"
msg = f'{symbol} - {best_emas} {config.strategy_name} - Sell condition not fulfilled'
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, "", msg)
# check symbols not in positions and BUY if conditions are fulfilled
for symbol in list_to_buy:
# initialize vars
fast_ema = 0
slow_ema = 0
# get best backtesting results for the strategy
if config.strategy_id in ["ema_cross_with_market_phases", "ema_cross"]:
fast_ema, slow_ema = get_backtesting_results(strategy_id=config.strategy_id, symbol=symbol, time_frame=time_frame)
if fast_ema == 0 or slow_ema == 0:
msg = f'{symbol} - {config.strategy_name} - Best EMA values missing'
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, telegram.EMOJI_WARNING, msg)
continue
df = get_data(symbol=symbol, time_frame=time_frame)
apply_technicals(df, fast_ema, slow_ema)
# last row
lastrow = df.iloc[-1]
# check buy condition for the strategy
if config.strategy_id in ["ema_cross_with_market_phases"]:
accumulation_phase = (lastrow.Close > lastrow.SMA50) and (lastrow.Close > lastrow.SMA200) and (lastrow.SMA50 < lastrow.SMA200)
bullish_phase = (lastrow.Close > lastrow.SMA50) and (lastrow.Close > lastrow.SMA200) and (lastrow.SMA50 > lastrow.SMA200)
condition_phase = accumulation_phase or bullish_phase
condition_crossover = crossover(df.FastEMA, df.SlowEMA)
buy_condition = condition_phase and condition_crossover
elif config.strategy_id in ["market_phases"]:
accumulation_phase = (lastrow.Close > lastrow.SMA50) and (lastrow.Close > lastrow.SMA200) and (lastrow.SMA50 < lastrow.SMA200)
bullish_phase = (lastrow.Close > lastrow.SMA50) and (lastrow.Close > lastrow.SMA200) and (lastrow.SMA50 > lastrow.SMA200)
buy_condition = accumulation_phase or bullish_phase
if buy_condition:
binance.create_buy_order(symbol=symbol, bot=time_frame, fast_ema=fast_ema, slow_ema=slow_ema)
else:
best_emas = "" if fast_ema == 0 or slow_ema == 0 else f"{fast_ema}/{slow_ema}"
msg = f'{symbol} - {best_emas} {config.strategy_name} - Buy condition not fulfilled'
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, "", msg)
def positions_summary(time_frame):
df_summary = database.get_positions_by_bot_position(database.conn,
bot=time_frame,
position=1)
# remove unwanted columns
df_dropped = df_summary.drop(columns=['Id','Date','Bot','Position','Rank','Qty','Ema_Fast','Ema_Slow','Buy_Order_Id','Duration'])
# sort by symbol
df_sorted = df_dropped.sort_values("Symbol")
# df_cp_to_print.rename(columns={"Currency": "Symbol", "Close": "Price", }, inplace=True)
df_sorted.reset_index(drop=True, inplace=True) # gives consecutive numbers to each row
if df_sorted.empty:
msg = "Positions Summary: no open positions"
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, "", msg)
else:
msg = df_sorted.to_string()
msg = telegram_prefix_sl + "Positions Summary:\n" + msg
print(msg)
telegram.send_telegram_message(telegram_token, "", msg)
if config.stake_amount_type == "unlimited":
num_open_positions = database.get_num_open_positions(database.conn)
msg = f"{str(num_open_positions)}/{str(config.max_number_of_open_positions)} positions occupied"
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, "", msg)
def run(time_frame, run_mode):
# if time_frame == "1h" and not config.bot_1h:
# msg = f"Bot {time_frame} is inactive. Check Dashboard - Settings. Bye"
# print(msg)
# return
# elif time_frame == "4h" and not config.bot_4h:
# msg = f"Bot {time_frame} is inactive. Check Dashboard - Settings. Bye"
# print(msg)
# return
# elif time_frame == "1d" and not config.bot_1d:
# msg = f"Bot {time_frame} is inactive. Check Dashboard - Settings. Bye"
# print(msg)
# return
# calculate program run time
start = timeit.default_timer()
# inform that bot has started
msg = "Start"
msg = telegram_prefix_sl + msg
telegram.send_telegram_message(telegram_token, telegram.EMOJI_START, msg)
# Check if connection is already established
if database.is_connection_open(database.conn):
print("Database connection is already established.")
else:
# Create a new connection
database.conn = database.connect()
trade(time_frame, run_mode)
positions_summary(time_frame)
# exchange.create_balance_snapshot(telegram_prefix="")
# Close the database connection
database.conn.close()
# calculate execution time
stop = timeit.default_timer()
total_seconds = stop - start
duration = database.calc_duration(total_seconds)
msg = f'Execution Time: {duration}'
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, "", msg)
# inform that bot has finished
msg = "End"
msg = telegram_prefix_sl + msg
print(msg)
telegram.send_telegram_message(telegram_token, telegram.EMOJI_STOP, msg)
def scheduled_run(time_frame, run_mode):
apply_arguments(time_frame)
run(time_frame=time_frame, run_mode=run_mode)
if __name__ == "__main__":
time_frame = read_arguments()
run_mode = config.get_setting("run_mode")
apply_arguments(time_frame)
run(time_frame=time_frame, run_mode=run_mode)