forked from donewiththedollar/directionalscalper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multi_bot_signalscreener_targetcoin.py
1139 lines (916 loc) · 53.9 KB
/
multi_bot_signalscreener_targetcoin.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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures import Future
from collections import defaultdict
import threading
from threading import Thread
import random
import colorama
from colorama import Fore, Style
from pathlib import Path
project_dir = str(Path(__file__).resolve().parent)
print("Project directory:", project_dir)
sys.path.insert(0, project_dir)
import traceback
import inquirer
from rich.live import Live
import argparse
from pathlib import Path
from config import load_config, Config, VERSION
from api.manager import Manager
from directionalscalper.core.exchanges import *
import directionalscalper.core.strategies.bybit.gridbased as gridbased
import directionalscalper.core.strategies.bybit.hedging as bybit_hedging
from directionalscalper.core.strategies.binance import *
from directionalscalper.core.strategies.huobi import *
from live_table_manager import LiveTableManager, shared_symbols_data
from directionalscalper.core.strategies.logger import Logger
from rate_limit import RateLimit
from collections import deque
general_rate_limiter = RateLimit(50, 1)
order_rate_limiter = RateLimit(5, 1)
thread_management_lock = threading.Lock()
thread_to_symbol = {}
thread_to_symbol_lock = threading.Lock()
active_symbols = set()
active_threads = defaultdict(dict)
long_threads = {}
short_threads = {}
threads = {} # Threads for each symbol
thread_start_time = {} # Dictionary to track the start time for each symbol's thread
symbol_last_started_time = {}
extra_symbols = set() # To track symbols opened past the limit
under_review_symbols = set()
latest_rotator_symbols = set()
last_rotator_update_time = time.time()
tried_symbols = set()
logging = Logger(logger_name="MultiBot", filename="MultiBot.log", stream=True)
colorama.init()
def print_cool_trading_info(symbol, exchange_name, strategy_name, account_name):
ascii_art = r"""
______ _____
| _ \/ ___|
| | | |\ `--.
| | | | `--. \
| |/ / /\__/ /
|___/ \____/
Created by Tyler Simpson
"""
print(Fore.GREEN + ascii_art)
print(Style.BRIGHT + Fore.YELLOW + "DirectionalScalper is trading..")
print(Fore.CYAN + f"Trading symbol: {symbol}")
print(Fore.MAGENTA + f"Exchange name: {exchange_name}")
print(Fore.BLUE + f"Strategy name: {strategy_name}")
print(Fore.GREEN + f"Account name: {account_name}")
print(Style.RESET_ALL)
def standardize_symbol(symbol):
return symbol.replace('/', '').split(':')[0]
def get_available_strategies():
return [
'qsgridob',
#'qsgridoblsignal',
# 'qstrendobdynamictp',
# 'qsgridinstantsignal',
# 'qsgridobtight',
# 'qsgriddynamicstatic',
# 'qsgridobdca',
# 'qsgriddynmaicgridspaninstant',
# 'qsdynamicgridspan',
# 'qsgriddynamictplinspaced',
# 'dynamicgridob',
# 'dynamicgridobsratrp',
# 'qsgriddynamictp',
# 'qsgriddynamic',
# 'qsgridbasic',
# 'basicgridpersist',
# 'qstrend',
# 'qstrendob',
# 'qstrenderi',
# 'qstrendemas',
# 'qstrend',
# 'qsematrend',
# 'qstrendemas',
# 'mfieritrend',
# 'qstrendlongonly',
# 'qstrendshortonly',
# 'qstrend_unified',
# 'qstrendspot',
]
def choose_strategy():
questions = [
inquirer.List('strategy',
message='Which strategy would you like to run?',
choices=get_available_strategies())
]
answers = inquirer.prompt(questions)
return answers['strategy']
def get_available_exchanges():
return ['bybit', 'hyperliquid']
def ask_for_missing_arguments(args):
questions = []
if not args.exchange:
questions.append(inquirer.List('exchange', message="Which exchange do you want to use?", choices=get_available_exchanges()))
if not args.strategy:
questions.append(inquirer.List('strategy', message="Which strategy do you want to use?", choices=get_available_strategies()))
if not args.account_name:
questions.append(inquirer.Text('account_name', message="Please enter the name of the account:"))
if questions:
answers = inquirer.prompt(questions)
args.exchange = args.exchange or answers.get('exchange')
args.strategy = args.strategy or answers.get('strategy')
args.account_name = args.account_name or answers.get('account_name')
return args
class DirectionalMarketMaker:
def __init__(self, config: Config, exchange_name: str, account_name: str):
self.config = config
self.exchange_name = exchange_name
self.account_name = account_name
exchange_config = next((exch for exch in config.exchanges if exch.name == exchange_name and exch.account_name == account_name), None)
if not exchange_config:
raise ValueError(f"Exchange {exchange_name} with account {account_name} not found in the configuration file.")
api_key = exchange_config.api_key
secret_key = exchange_config.api_secret
passphrase = getattr(exchange_config, 'passphrase', None) # Use getattr to get passphrase if it exists
exchange_classes = {
'bybit': BybitExchange,
'bybit_spot': BybitExchange,
'hyperliquid': HyperLiquidExchange,
'huobi': HuobiExchange,
'bitget': BitgetExchange,
'binance': BinanceExchange,
'mexc': MexcExchange,
'lbank': LBankExchange,
'blofin': BlofinExchange
}
exchange_class = exchange_classes.get(exchange_name.lower(), Exchange)
# Initialize the exchange based on whether a passphrase is required
if exchange_name.lower() in ['bybit', 'binance']: # Add other exchanges here that do not require a passphrase
self.exchange = exchange_class(api_key, secret_key)
elif exchange_name.lower() == 'bybit_spot':
self.exchange = exchange_class(api_key, secret_key, 'spot')
else:
self.exchange = exchange_class(api_key, secret_key, passphrase)
def run_strategy(self, symbol, strategy_name, config, account_name, symbols_to_trade=None, rotator_symbols_standardized=None, mfirsi_signal=None, action=None):
logging.info(f"Received rotator symbols in run_strategy for {symbol}: {rotator_symbols_standardized}")
symbols_allowed = next((exch.symbols_allowed for exch in config.exchanges if exch.name == self.exchange_name and exch.account_name == account_name), None)
logging.info(f"Matched exchange: {self.exchange_name}, account: {account_name}. Symbols allowed: {symbols_allowed}")
if symbols_to_trade:
logging.info(f"Calling run method with symbols: {symbols_to_trade}")
try:
print_cool_trading_info(symbol, self.exchange_name, strategy_name, account_name)
logging.info(f"Printed trading info for {symbol}")
except Exception as e:
logging.error(f"Error in printing info: {e}")
strategy_classes = {
'bybit_1m_qfl_mfi_eri_autohedge_walls_atr': bybit_hedging.BybitMMOneMinuteQFLMFIERIAutoHedgeWallsATR,
'qsgridinstantsignal': gridbased.BybitDynamicGridSpanOBSRStaticIS,
'qsgriddynmaicgridspaninstant': gridbased.BybitDynamicGridSpanIS,
#'qsgridob': instant_signals.BybitDynamicGridSpanOBLevels,
'qstrendobdynamictp': gridbased.BybitQuickScalpTrendDynamicTP,
'qsgridob': gridbased.BybitDynamicGridSpanOBLevelsLSignal
}
strategy_class = strategy_classes.get(strategy_name.lower())
if strategy_class:
strategy = strategy_class(self.exchange, self.manager, config.bot, symbols_allowed)
try:
logging.info(f"Running strategy for symbol {symbol} with action {action}")
if action == "long":
future_long = Future()
Thread(target=self.run_with_future, args=(strategy, symbol, rotator_symbols_standardized, mfirsi_signal, "long", future_long)).start()
return future_long
elif action == "short":
future_short = Future()
Thread(target=self.run_with_future, args=(strategy, symbol, rotator_symbols_standardized, mfirsi_signal, "short", future_short)).start()
return future_short
else:
future = Future()
future.set_result(True)
return future
except Exception as e:
future = Future()
future.set_exception(e)
return future
else:
logging.error(f"Strategy {strategy_name} not found.")
future = Future()
future.set_exception(ValueError(f"Strategy {strategy_name} not found."))
return future
def run_with_future(self, strategy, symbol, rotator_symbols_standardized, mfirsi_signal, action, future):
try:
strategy.run(symbol, rotator_symbols_standardized=rotator_symbols_standardized, mfirsi_signal=mfirsi_signal, action=action)
future.set_result(True)
except Exception as e:
future.set_exception(e)
def get_balance(self, quote, market_type=None, sub_type=None):
if self.exchange_name == 'bitget':
return self.exchange.get_balance_bitget(quote)
elif self.exchange_name == 'bybit':
#self.exchange.retry_api_call(self.exchange.get_balance_bybit, quote)
# return self.exchange.retry_api_call(self.exchange.get_balance_bybit(quote))
return self.exchange.get_balance_bybit(quote)
elif self.exchange_name == 'bybit_unified':
return self.exchange.retry_api_call(self.exchange.get_balance_bybit(quote))
elif self.exchange_name == 'mexc':
return self.exchange.get_balance_mexc(quote, market_type='swap')
elif self.exchange_name == 'huobi':
print("Huobi starting..")
elif self.exchange_name == 'okx':
print(f"Unsupported for now")
elif self.exchange_name == 'binance':
return self.exchange.get_balance_binance(quote)
elif self.exchange_name == 'phemex':
print(f"Unsupported for now")
def create_order(self, symbol, order_type, side, amount, price=None):
return self.exchange.create_order(symbol, order_type, side, amount, price)
def get_symbols(self):
with general_rate_limiter:
return self.exchange._get_symbols()
def format_symbol_bybit(self, symbol):
return f"{symbol[:3]}/{symbol[3:]}:USDT"
def is_valid_symbol_bybit(self, symbol):
valid_symbols = self.get_symbols()
# Check for SYMBOL/USDT:USDT format
if f"{symbol[:3]}/{symbol[3:]}:USDT" in valid_symbols:
return True
# Check for SYMBOL/USD:SYMBOL format
if f"{symbol[:3]}/USD:{symbol[:3]}" in valid_symbols:
return True
# Check for SYMBOL/USDC:USDC format
if f"{symbol}/USDC:USDC" in valid_symbols:
return True
# Check for SYMBOL/USDC:USDC-YYMMDD format
for valid_symbol in valid_symbols:
if valid_symbol.startswith(f"{symbol}/USDC:USDC-"):
return True
# Check for SYMBOL/USDC:USDC-YYMMDD-STRIKE-C/P format
for valid_symbol in valid_symbols:
if valid_symbol.startswith(f"{symbol}/USDC:USDC-") and valid_symbol.endswith(("-C", "-P")):
return True
logging.info(f"Invalid symbol type for some reason according to bybit but is probably valid symbol: {symbol}")
return True
def fetch_open_orders(self, symbol):
with general_rate_limiter:
return self.exchange.retry_api_call(self.exchange.get_open_orders, symbol)
def fetch_open_positions(self):
with general_rate_limiter:
return getattr(manager.exchange, f"get_all_open_positions_{args.exchange.lower()}")()
def generate_l_signals(self, symbol):
with general_rate_limiter:
return self.exchange.generate_l_signals(symbol)
def get_mfirsi_signal(self, symbol):
# Retrieve the MFI/RSI signal
with general_rate_limiter:
return self.exchange.get_mfirsi_ema_secondary_ema(symbol, limit=100, lookback=1, ema_period=5, secondary_ema_period=3)
BALANCE_REFRESH_INTERVAL = 600 # in seconds
orders_canceled = False
def monitor_threads():
"""Function to monitor and restart threads if needed."""
while True:
with thread_management_lock:
for symbol, thread_info in list(active_threads.items()):
for action, thread_data in list(thread_info.items()):
thread, thread_completed = thread_data
if not thread.is_alive() and not thread_completed.is_set():
logging.info(f"Thread for symbol {symbol} with action {action} is not alive and not marked as completed. Restarting the thread.")
try:
thread_completed.set() # Mark the thread as completed
thread.join() # Ensure the thread has terminated
logging.info(f"Successfully joined the thread for symbol {symbol} with action {action}.")
# Restart the thread
with general_rate_limiter:
mfirsi_signal = market_maker.generate_l_signals(symbol)
new_thread_completed = threading.Event()
new_thread = threading.Thread(target=run_bot, args=(
symbol, args, market_maker, manager, args.account_name, symbols_allowed,
latest_rotator_symbols, new_thread_completed, mfirsi_signal, action))
active_threads[symbol][action] = (new_thread, new_thread_completed)
new_thread.start()
logging.info(f"Successfully restarted the thread for symbol {symbol} with action {action}.")
except Exception as e:
logging.error(f"Error while restarting the thread for symbol {symbol} with action {action}: {e}")
logging.debug(traceback.format_exc())
else:
logging.debug(f"Thread for symbol {symbol} with action {action} is alive or already marked as completed.")
time.sleep(10) # Monitor interval
def run_bot(symbol, args, market_maker, manager, account_name, symbols_allowed, rotator_symbols_standardized, thread_completed, mfirsi_signal, action):
global orders_canceled
current_thread = threading.current_thread()
try:
with thread_to_symbol_lock:
thread_to_symbol[current_thread] = symbol
active_symbols.add(symbol) # Add symbol to active_symbols when the thread starts
if not args.config.startswith('configs/'):
config_file_path = Path('configs/' + args.config)
else:
config_file_path = Path(args.config)
logging.info(f"Loading config from: {config_file_path}")
account_file_path = Path('configs/account.json') # Define the account file path
config = load_config(config_file_path, account_file_path) # Pass both file paths to load_config
exchange_name = args.exchange
strategy_name = args.strategy
account_name = args.account_name
logging.info(f"Trading symbol: {symbol}")
logging.info(f"Exchange name: {exchange_name}")
logging.info(f"Strategy name: {strategy_name}")
logging.info(f"Account name: {account_name}")
market_maker.manager = manager
try:
if not orders_canceled and hasattr(market_maker.exchange, 'cancel_all_open_orders_bybit'):
market_maker.exchange.cancel_all_open_orders_bybit()
logging.info(f"Cleared all open orders on the exchange upon initialization.")
orders_canceled = True
except Exception as e:
logging.error(f"Exception caught while cancelling orders: {e}")
logging.info(f"Rotator symbols in run_bot: {rotator_symbols_standardized}")
logging.info(f"Latest rotator symbols in run bot: {latest_rotator_symbols}")
time.sleep(2)
with general_rate_limiter:
future = market_maker.run_strategy(symbol, args.strategy, config, account_name, symbols_to_trade=symbols_allowed, rotator_symbols_standardized=latest_rotator_symbols, mfirsi_signal=mfirsi_signal, action=action)
future.result() # Wait for the strategy to complete
except Exception as e:
logging.error(f"An error occurred in run_bot for symbol {symbol}: {e}")
logging.debug(traceback.format_exc())
finally:
with thread_to_symbol_lock:
if current_thread in thread_to_symbol:
del thread_to_symbol[current_thread]
active_symbols.discard(symbol) # Remove symbol from active_symbols when the thread completes
logging.info(f"Thread for symbol {symbol} with action {action} has completed.")
thread_completed.set()
def bybit_auto_rotation_spot(args, market_maker, manager, symbols_allowed):
global latest_rotator_symbols, active_symbols, last_rotator_update_time
# Set max_workers to the number of CPUs
max_workers_signals = 1
max_workers_trading = 1
signal_executor = ThreadPoolExecutor(max_workers=max_workers_signals)
trading_executor = ThreadPoolExecutor(max_workers=max_workers_trading)
logging.info(f"Initialized signal executor with max workers: {max_workers_signals}")
logging.info(f"Initialized trading executor with max workers: {max_workers_trading}")
config_file_path = Path('configs/' + args.config) if not args.config.startswith('configs/') else Path(args.config)
account_file_path = Path('configs/account.json')
config = load_config(config_file_path, account_file_path)
market_maker.manager = manager
long_mode = config.bot.linear_grid['long_mode']
short_mode = config.bot.linear_grid['short_mode']
logging.info(f"Long mode: {long_mode}")
logging.info(f"Short mode: {short_mode}")
def fetch_open_positions():
with general_rate_limiter:
return getattr(manager.exchange, f"get_all_open_positions_{args.exchange.lower()}")()
def process_futures(futures):
for future in as_completed(futures):
try:
future.result()
except Exception as e:
logging.error(f"Exception in thread: {e}")
logging.debug(traceback.format_exc())
while True:
try:
current_time = time.time()
open_position_data = fetch_open_positions()
open_position_symbols = {standardize_symbol(pos['symbol']) for pos in open_position_data}
logging.info(f"Open position symbols: {open_position_symbols}")
if not latest_rotator_symbols or current_time - last_rotator_update_time >= 60:
with general_rate_limiter:
latest_rotator_symbols = fetch_updated_symbols(args, manager)
last_rotator_update_time = current_time
logging.info(f"Refreshed latest rotator symbols: {latest_rotator_symbols}")
else:
logging.debug(f"No refresh needed yet. Last update was at {last_rotator_update_time}, less than 60 seconds ago.")
update_active_symbols(open_position_symbols)
logging.info(f"Active symbols: {active_symbols}")
logging.info(f"Active symbols updated. Symbols allowed: {symbols_allowed}")
with thread_management_lock:
# update_active_symbols(open_position_symbols)
# logging.info(f"Active symbols updated. Symbols allowed: {symbols_allowed}")
open_position_futures = []
for symbol in open_position_symbols:
with general_rate_limiter:
mfirsi_signal = market_maker.generate_l_signals(symbol)
has_open_long = any(pos['side'].lower() == 'long' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
open_position_futures.append(trading_executor.submit(start_thread_for_open_symbol_spot, symbol, args, manager, mfirsi_signal, has_open_long, long_mode, short_mode))
logging.info(f"Submitted thread for symbol {symbol}. MFIRSI signal: {mfirsi_signal}. Has open long: {has_open_long}.")
signal_futures = [signal_executor.submit(process_signal_for_open_position_spot, symbol, args, manager, symbols_allowed, open_position_data, long_mode, short_mode)
for symbol in open_position_symbols]
logging.info(f"Submitted signal processing for open position symbols: {open_position_symbols}.")
if len(active_symbols) < symbols_allowed:
for symbol in latest_rotator_symbols:
signal_futures.append(signal_executor.submit(process_signal_spot, symbol, args, manager, symbols_allowed, open_position_data, False, long_mode, short_mode))
logging.info(f"Submitted signal processing for new rotator symbol {symbol}.")
time.sleep(2)
process_futures(open_position_futures + signal_futures)
completed_symbols = []
for symbol, (thread, thread_completed) in long_threads.items():
if thread_completed.is_set():
thread.join()
completed_symbols.append(symbol)
for symbol in completed_symbols:
active_symbols.discard(symbol)
if symbol in long_threads:
del long_threads[symbol]
logging.info(f"Thread and symbol management completed for: {symbol}")
except Exception as e:
logging.error(f"Exception caught in bybit_auto_rotation_spot: {str(e)}")
logging.debug(traceback.format_exc())
time.sleep(1)
def bybit_auto_rotation(args, market_maker, manager, symbols_allowed):
global latest_rotator_symbols, long_threads, short_threads, active_symbols, last_rotator_update_time
max_workers_signals = 1
max_workers_trading = 1
signal_executor = ThreadPoolExecutor(max_workers=max_workers_signals)
trading_executor = ThreadPoolExecutor(max_workers=max_workers_trading)
logging.info(f"Initialized signal executor with max workers: {max_workers_signals}")
logging.info(f"Initialized trading executor with max workers: {max_workers_trading}")
config_file_path = Path('configs/' + args.config) if not args.config.startswith('configs/') else Path(args.config)
account_file_path = Path('configs/account.json')
config = load_config(config_file_path, account_file_path)
market_maker.manager = manager
long_mode = config.bot.linear_grid['long_mode']
short_mode = config.bot.linear_grid['short_mode']
logging.info(f"Long mode: {long_mode}")
logging.info(f"Short mode: {short_mode}")
# Get the whitelisted symbol
whitelist = config.bot.whitelist
if not whitelist:
logging.error("No symbol in whitelist. Please add a symbol to the whitelist.")
return
whitelisted_symbol = whitelist[0] # Assume we're using the first symbol in the whitelist
def fetch_open_positions():
with general_rate_limiter:
return getattr(manager.exchange, f"get_all_open_positions_{args.exchange.lower()}")()
# Start threads for open positions at startup
open_position_data = fetch_open_positions()
open_position_symbols = {standardize_symbol(pos['symbol']) for pos in open_position_data}
for symbol in open_position_symbols:
has_open_long = any(pos['side'].lower() == 'long' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
has_open_short = any(pos['side'].lower() == 'short' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
with general_rate_limiter:
mfirsi_signal = market_maker.generate_l_signals(symbol)
if has_open_long:
start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "long")
if has_open_short:
start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "short")
while True:
try:
current_time = time.time()
# Always process the whitelisted symbol
with general_rate_limiter:
mfirsi_signal = market_maker.generate_l_signals(whitelisted_symbol)
logging.info(f"Processing signal for whitelisted symbol {whitelisted_symbol}. MFIRSI signal: {mfirsi_signal}")
# Check if there's an open position for the whitelisted symbol
open_position_data = fetch_open_positions()
has_open_long = any(pos['side'].lower() == 'long' and standardize_symbol(pos['symbol']) == whitelisted_symbol for pos in open_position_data)
has_open_short = any(pos['side'].lower() == 'short' and standardize_symbol(pos['symbol']) == whitelisted_symbol for pos in open_position_data)
# Process the signal for the whitelisted symbol
action_taken = handle_signal(whitelisted_symbol, args, manager, mfirsi_signal, open_position_data, symbols_allowed, True, long_mode, short_mode)
if action_taken:
logging.info(f"Action taken for whitelisted symbol {whitelisted_symbol}.")
else:
logging.info(f"No action taken for whitelisted symbol {whitelisted_symbol}.")
# Manage existing threads
manage_threads(market_maker, args, manager)
time.sleep(1) # Adjust this sleep time as needed to control how often you check for signals
except Exception as e:
logging.error(f"Exception caught in bybit_auto_rotation: {str(e)}")
logging.debug(traceback.format_exc())
def manage_threads(market_maker, args, manager):
"""Function to manage and restart threads if needed."""
try:
logging.info("Starting to manage threads.")
# Fetch open positions at regular intervals
open_position_data = market_maker.fetch_open_positions()
open_position_symbols = {standardize_symbol(pos['symbol']) for pos in open_position_data}
# Logging the open position symbols
logging.info(f"Fetched open position symbols: {open_position_symbols}")
with thread_management_lock:
logging.info(f"Active long threads: {list(long_threads.keys())}")
logging.info(f"Active short threads: {list(short_threads.keys())}")
# Check for missing threads for open positions
for symbol in open_position_symbols:
has_open_long = any(pos['side'].lower() == 'long' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
has_open_short = any(pos['side'].lower() == 'short' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
with general_rate_limiter:
mfirsi_signal = market_maker.generate_l_signals(symbol)
if has_open_long:
if symbol not in long_threads or not long_threads[symbol][0].is_alive():
logging.info(f"Starting missing long thread for symbol {symbol}.")
start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "long")
else:
logging.info(f"Long thread already active for symbol {symbol}.")
if has_open_short:
if symbol not in short_threads or not short_threads[symbol][0].is_alive():
logging.info(f"Starting missing short thread for symbol {symbol}.")
start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "short")
else:
logging.info(f"Short thread already active for symbol {symbol}.")
# Stop threads for symbols that are no longer open
for symbol in list(long_threads.keys()):
if symbol not in open_position_symbols:
logging.info(f"Stopping long thread for symbol {symbol} as it is no longer open.")
stop_thread_for_symbol(symbol, "long")
else:
logging.info(f"Long thread still needed and active for symbol {symbol}.")
for symbol in list(short_threads.keys()):
if symbol not in open_position_symbols:
logging.info(f"Stopping short thread for symbol {symbol} as it is no longer open.")
stop_thread_for_symbol(symbol, "short")
else:
logging.info(f"Short thread still needed and active for symbol {symbol}.")
except Exception as e:
logging.error(f"Exception caught in manage_threads: {e}")
logging.debug(traceback.format_exc())
def stop_thread_for_symbol(symbol, action):
"""Stop the thread for a given symbol and action."""
if action == "long" and symbol in long_threads:
thread, thread_completed = long_threads[symbol]
thread_completed.set()
thread.join()
del long_threads[symbol]
logging.info(f"Stopped and removed long thread for symbol {symbol}.")
elif action == "short" and symbol in short_threads:
thread, thread_completed = short_threads[symbol]
thread_completed.set()
thread.join()
del short_threads[symbol]
logging.info(f"Stopped and removed short thread for symbol {symbol}.")
def process_signal_for_open_position(symbol, args, market_maker, manager, symbols_allowed, open_position_data, long_mode, short_mode):
market_maker.manager = manager
with general_rate_limiter:
mfirsi_signal = market_maker.generate_l_signals(symbol)
logging.info(f"Processing signal for open position symbol {symbol}. MFIRSI signal: {mfirsi_signal}")
action_taken = handle_signal(symbol, args, manager, mfirsi_signal, open_position_data, symbols_allowed, True, long_mode, short_mode)
if action_taken:
logging.info(f"Action taken for open position symbol {symbol}.")
else:
logging.info(f"No action taken for open position symbol {symbol}.")
def process_signal(symbol, args, market_maker, manager, symbols_allowed, open_position_data, is_open_position, long_mode, short_mode):
market_maker.manager = manager
mfirsi_signal = market_maker.generate_l_signals(symbol)
logging.info(f"Processing signal for {'open position' if is_open_position else 'new rotator'} symbol {symbol}. MFIRSI signal: {mfirsi_signal}")
action_taken = handle_signal(symbol, args, manager, mfirsi_signal, open_position_data, symbols_allowed, is_open_position, long_mode, short_mode)
if action_taken:
logging.info(f"Action taken for {'open position' if is_open_position else 'new rotator'} symbol {symbol}.")
else:
logging.info(f"No action taken for {'open position' if is_open_position else 'new rotator'} symbol {symbol}.")
def handle_signal(symbol, args, manager, mfirsi_signal, open_position_data, symbols_allowed, is_open_position, long_mode, short_mode):
open_position_symbols = {standardize_symbol(pos['symbol']) for pos in open_position_data}
logging.info(f"Open position symbols: {open_position_symbols}")
mfi_signal_long = mfirsi_signal.lower() == "long"
mfi_signal_short = mfirsi_signal.lower() == "short"
current_long_positions = sum(1 for pos in open_position_data if pos['side'].lower() == 'long')
current_short_positions = sum(1 for pos in open_position_data if pos['side'].lower() == 'short')
unique_open_symbols = len(open_position_symbols)
logging.info(f"Handling signal for whitelisted symbol {symbol}. Current long positions: {current_long_positions}. Current short positions: {current_short_positions}. Unique open symbols: {unique_open_symbols}")
has_open_long = any(pos['side'].lower() == 'long' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
has_open_short = any(pos['side'].lower() == 'short' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
logging.info(f"Whitelisted symbol {symbol} - Has open long: {has_open_long}, Has open short: {has_open_short}")
logging.info(f"MFIRSI Signal: {mfirsi_signal}, Long Mode: {long_mode}, Short Mode: {short_mode}")
action_taken_long = False
action_taken_short = False
# Always attempt to start a new long position if the signal is long
if mfi_signal_long and long_mode:
logging.info(f"Starting long thread for symbol {symbol}.")
action_taken_long = start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "long")
else:
logging.info(f"Long signal not triggered or long mode not enabled for symbol {symbol}.")
logging.info(f"MFIRSI Signal: {mfirsi_signal}")
logging.info(f"Long mode: {long_mode}")
logging.info(f"Has open long: {has_open_long}")
# Always attempt to start a new short position if the signal is short
if mfi_signal_short and short_mode:
logging.info(f"Starting short thread for symbol {symbol}.")
action_taken_short = start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "short")
else:
logging.info(f"Short signal not triggered or short mode not enabled for symbol {symbol}.")
logging.info(f"MFIRSI Signal: {mfirsi_signal}")
logging.info(f"Short mode: {short_mode}")
logging.info(f"Has open short: {has_open_short}")
if action_taken_long or action_taken_short:
logging.info(f"Action taken for whitelisted symbol {symbol}.")
else:
logging.info(f"No action taken for whitelisted symbol {symbol} due to lack of clear signal.")
return action_taken_long or action_taken_short
def handle_signal_spot(symbol, args, manager, mfirsi_signal, open_position_data, symbols_allowed, is_open_position, long_mode, short_mode):
open_position_symbols = {standardize_symbol(pos['symbol']) for pos in open_position_data}
logging.info(f"Open position symbols: {open_position_symbols}")
mfi_signal_long = mfirsi_signal.lower() == "long"
mfi_signal_short = mfirsi_signal.lower() == "short"
current_long_positions = sum(1 for pos in open_position_data if pos['side'].lower() == 'long')
unique_open_symbols = len(open_position_symbols)
logging.info(f"Handling signal for {'open position' if is_open_position else 'new rotator'} symbol {symbol}. Current long positions: {current_long_positions}. Unique open symbols: {unique_open_symbols}")
has_open_long = any(pos['side'].lower() == 'long' for pos in open_position_data if standardize_symbol(pos['symbol']) == symbol)
logging.info(f"{'Open position' if is_open_position else 'New rotator'} symbol {symbol} - Has open long: {has_open_long}")
action_taken_long = False
if mfi_signal_long and long_mode and not has_open_long:
logging.info(f"Starting long thread for symbol {symbol}.")
action_taken_long = start_thread_for_symbol_spot(symbol, args, manager, mfirsi_signal, "long")
else:
logging.info(f"Long thread already running or long position already open for symbol {symbol}. Skipping.")
if mfi_signal_short and short_mode and has_open_long:
logging.info(f"Starting short (sell) thread for symbol {symbol}.")
action_taken_long = start_thread_for_symbol_spot(symbol, args, manager, mfirsi_signal, "short")
else:
logging.info(f"Short thread (sell order) already running or no long position open for symbol {symbol}. Skipping.")
if action_taken_long:
logging.info(f"Action taken for {'open position' if is_open_position else 'new rotator'} symbol {symbol}.")
else:
logging.info(f"Evaluated action for {'open position' if is_open_position else 'new rotator'} symbol {symbol}: No action due to existing position or lack of clear signal.")
return action_taken_long
def process_signal_for_open_position_spot(symbol, args, market_maker, manager, symbols_allowed, open_position_data, long_mode, short_mode):
market_maker.manager = manager
with general_rate_limiter:
mfirsi_signal = market_maker.generate_l_signals(symbol)
logging.info(f"Processing signal for open position symbol {symbol}. MFIRSI signal: {mfirsi_signal}")
action_taken = handle_signal_spot(symbol, args, manager, mfirsi_signal, open_position_data, symbols_allowed, True, long_mode, short_mode)
if action_taken:
logging.info(f"Action taken for open position symbol {symbol}.")
else:
logging.info(f"No action taken for open position symbol {symbol}.")
def process_signal_spot(symbol, args, market_maker, manager, symbols_allowed, open_position_data, is_open_position, long_mode, short_mode):
market_maker.manager = manager
mfirsi_signal = market_maker.generate_l_signals(symbol)
logging.info(f"Processing signal for {'open position' if is_open_position else 'new rotator'} symbol {symbol}. MFIRSI signal: {mfirsi_signal}")
action_taken = handle_signal_spot(symbol, args, manager, mfirsi_signal, open_position_data, symbols_allowed, is_open_position, long_mode, short_mode)
if action_taken:
logging.info(f"Action taken for {'open position' if is_open_position else 'new rotator'} symbol {symbol}.")
else:
logging.info(f"No action taken for {'open position' if is_open_position else 'new rotator'} symbol {symbol}.")
def start_thread_for_open_symbol_spot(symbol, args, manager, mfirsi_signal, has_open_long, long_mode, short_mode):
action_taken = False
if long_mode and (has_open_long or mfirsi_signal.lower() == "long"):
action_taken |= start_thread_for_symbol_spot(symbol, args, manager, mfirsi_signal, "long")
logging.info(f"[DEBUG] Started long thread for open symbol {symbol}")
if short_mode and (has_open_long and mfirsi_signal.lower() == "short"):
action_taken |= start_thread_for_symbol_spot(symbol, args, manager, mfirsi_signal, "short")
logging.info(f"[DEBUG] Started short (sell) thread for open symbol {symbol}")
return action_taken
def start_thread_for_symbol_spot(symbol, args, manager, mfirsi_signal, action):
if action == "long":
if symbol in long_threads and long_threads[symbol][0].is_alive():
logging.info(f"Long thread already running for symbol {symbol}. Skipping.")
return False
elif action == "short":
if symbol in long_threads and long_threads[symbol][0].is_alive():
logging.info(f"Short thread (sell order) already running for symbol {symbol}. Skipping.")
return False
elif action == "neutral":
logging.info(f"Start thread function hit for {symbol} but signal is {mfirsi_signal}")
thread_completed = threading.Event()
thread = threading.Thread(target=run_bot, args=(symbol, args, market_maker, manager, args.account_name, symbols_allowed, latest_rotator_symbols, thread_completed, mfirsi_signal, action))
if action == "long":
long_threads[symbol] = (thread, thread_completed)
elif action == "short":
long_threads[symbol] = (thread, thread_completed)
thread.start()
logging.info(f"Started thread for symbol {symbol} with action {action} based on MFIRSI signal.")
return True
def update_active_symbols(open_position_symbols):
global active_symbols
active_symbols = open_position_symbols
logging.info(f"Updated active symbols: {active_symbols}")
def manage_rotator_symbols(rotator_symbols, args, manager, symbols_allowed):
global active_symbols, latest_rotator_symbols
logging.info(f"Starting symbol management. Total symbols allowed: {symbols_allowed}. Current active symbols: {len(active_symbols)}")
open_position_data = getattr(manager.exchange, f"get_all_open_positions_{args.exchange.lower()}")()
open_position_symbols = {standardize_symbol(pos['symbol']) for pos in open_position_data}
logging.info(f"Currently open positions: {open_position_symbols}")
current_long_positions = sum(1 for pos in open_position_data if pos['side'].lower() == 'long')
current_short_positions = sum(1 for pos in open_position_data if pos['side'].lower() == 'short')
logging.info(f"Current long positions: {current_long_positions}, Current short positions: {current_short_positions}")
random_rotator_symbols = list(rotator_symbols)
random.shuffle(random_rotator_symbols)
logging.info(f"Shuffled rotator symbols for processing: {random_rotator_symbols}")
for symbol in open_position_symbols:
process_signal(symbol, args, market_maker, manager, symbols_allowed, open_position_data, True)
for symbol in random_rotator_symbols:
if len(open_position_symbols) >= symbols_allowed:
logging.info("Maximum number of open positions reached.")
break
process_signal(symbol, args, manager, market_maker, symbols_allowed, open_position_data, False)
manage_excess_threads(symbols_allowed)
time.sleep(5)
def manage_excess_threads(symbols_allowed):
global active_symbols
long_positions = {symbol for symbol in active_symbols if is_long_position(symbol)}
short_positions = {symbol for symbol in active_symbols if is_short_position(symbol)}
logging.info(f"Managing excess threads. Total long positions: {len(long_positions)}, Total short positions: {len(short_positions)}")
excess_long_count = len(long_positions) - symbols_allowed
excess_short_count = len(short_positions) - symbols_allowed
while excess_long_count > 0:
symbol_to_remove = long_positions.pop()
remove_thread_for_symbol(symbol_to_remove)
logging.info(f"Removed excess long thread for symbol: {symbol_to_remove}")
excess_long_count -= 1
while excess_short_count > 0:
symbol_to_remove = short_positions.pop()
remove_thread_for_symbol(symbol_to_remove)
logging.info(f"Removed excess short thread for symbol: {symbol_to_remove}")
excess_short_count -= 1
def is_long_position(symbol):
pos_data = getattr(manager.exchange, f"get_all_open_positions_{args.exchange.lower()}")()
is_long = any(standardize_symbol(pos['symbol']) == symbol and pos['side'].lower() == 'long' for pos in pos_data)
logging.debug(f"Checked if {symbol} is a long position: {is_long}")
return is_long
def is_short_position(symbol):
pos_data = getattr(manager.exchange, f"get_all_open_positions_{args.exchange.lower()}")()
is_short = any(standardize_symbol(pos['symbol']) == symbol and pos['side'].lower() == 'short' for pos in pos_data)
logging.debug(f"Checked if {symbol} is a short position: {is_short}")
return is_short
def remove_thread_for_symbol(symbol):
if symbol in long_threads:
thread, thread_completed = long_threads[symbol]
elif symbol in short_threads:
thread, thread_completed = short_threads[symbol]
else:
return
if thread:
thread_completed.set()
thread.join()
logging.info(f"Removed thread for symbol {symbol}.")
if symbol in long_threads:
del long_threads[symbol]
if symbol in short_threads:
del short_threads[symbol]
def start_thread_for_open_symbol(symbol, args, manager, mfirsi_signal, has_open_long, has_open_short, long_mode, short_mode):
action_taken = False
if long_mode and (has_open_long or mfirsi_signal.lower() == "long"):
action_taken |= start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "long")
logging.info(f"[DEBUG] Started long thread for open symbol {symbol}")
if short_mode and (has_open_short or mfirsi_signal.lower() == "short"):
action_taken |= start_thread_for_symbol(symbol, args, manager, mfirsi_signal, "short")
logging.info(f"[DEBUG] Started short thread for open symbol {symbol}")
return action_taken
def start_thread_for_symbol(symbol, args, manager, mfirsi_signal, action):
if action == "long":
if symbol in long_threads and long_threads[symbol][0].is_alive():
logging.info(f"Long thread already running for symbol {symbol}. Skipping.")
return False
elif action == "short":
if symbol in short_threads and short_threads[symbol][0].is_alive():
logging.info(f"Short thread already running for symbol {symbol}. Skipping.")
return False
elif action == "neutral":
logging.info(f"Start thread function hit for {symbol} but signal is {mfirsi_signal}")
thread_completed = threading.Event()
thread = threading.Thread(
target=run_bot,
args=(
symbol, args, market_maker, manager, args.account_name,
symbols_allowed, latest_rotator_symbols, thread_completed,
mfirsi_signal, action
)
)
if action == "long":
long_threads[symbol] = (thread, thread_completed)
elif action == "short":
short_threads[symbol] = (thread, thread_completed)
thread.start()
logging.info(f"Started thread for symbol {symbol} with action {action} based on MFIRSI signal.")
return True
def fetch_updated_symbols(args, manager):
strategy = args.strategy.lower()
potential_symbols = []
if strategy == 'basicgrid':
potential_bullish_symbols = manager.get_bullish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_bearish_symbols = manager.get_bearish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_symbols = potential_bullish_symbols + potential_bearish_symbols
elif strategy == 'basicgridmfirsi':
potential_bullish_symbols = manager.get_bullish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_bearish_symbols = manager.get_bearish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_symbols = potential_bullish_symbols + potential_bearish_symbols
elif strategy == 'basicgridmfipersist':
potential_bullish_symbols = manager.get_bullish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_bearish_symbols = manager.get_bearish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_symbols = potential_bullish_symbols + potential_bearish_symbols
elif strategy == 'basicgridpersistnotional':
potential_bullish_symbols = manager.get_bullish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_bearish_symbols = manager.get_bearish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
potential_symbols = potential_bullish_symbols + potential_bearish_symbols
elif strategy == 'qstrendlongonly':
potential_symbols = manager.get_bullish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
elif strategy == 'qstrendshortonly':
potential_symbols = manager.get_bearish_rotator_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
else:
potential_symbols = manager.get_auto_rotate_symbols(min_qty_threshold=None, blacklist=blacklist, whitelist=whitelist, max_usd_value=max_usd_value)
logging.info(f"Potential symbols for {strategy}: {potential_symbols}")
return set(standardize_symbol(sym) for sym in potential_symbols)
def log_symbol_details(strategy, symbols):
logging.info(f"Potential symbols for {strategy}: {symbols}")
def blofin_auto_rotation(args, market_maker, manager, symbols_allowed):