-
Notifications
You must be signed in to change notification settings - Fork 5
/
wednesday.py
68 lines (55 loc) · 2.49 KB
/
wednesday.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
import time
import sys
from multiprocessing import Queue, Process
from Auto_Trader import is_Market_Open, run_ticker, create_master, Apply_Rules, Updater, logging, traceback
from Auto_Trader.TelegramLink import telegram_main
logger = logging.getLogger("Auto_Trade_Logger")
def monitor_market():
processes = []
q = Queue() # Queue for Orders Placements
message_queue = Queue() # Queue for Telegram Messages
def start_processes():
"""Starts all necessary processes."""
logger.info("Market is open. Starting processes.")
message_queue.put("Market is open. Starting processes.")
# Start the worker processes
p1 = Process(target=run_ticker, args=(create_master(), q))
p2 = Process(target=Apply_Rules, args=(q, message_queue))
p3 = Process(target=Updater)
p4 = Process(target=telegram_main, args=(message_queue,))
p1.start()
p2.start()
p3.start()
p4.start()
return [p1, p2, p3, p4]
def stop_processes(processes):
"""Stops all running processes."""
logger.info("Market is closed. Stopping processes.")
message_queue.put("Market is closed. Stopping processes.")
for p in processes:
p.terminate() # Gracefully terminate the process
p.join() # Ensure the process has finished
return []
while True:
try:
market_status = is_Market_Open() # Check market status
if market_status and not processes:
# Start processes if market is open and none are running
processes = start_processes()
elif not market_status and processes:
# Stop processes and exit the program when the market closes
processes = stop_processes(processes)
sys.exit(0) # Exit the script cleanly; systemd will restart it
time.sleep(60) # Sleep for 60 seconds before checking again
except Exception as e:
logger.error(f"Error occurred: {e}, Traceback: {traceback.format_exc()}")
message_queue.put(f"Error occurred: {e}, Traceback: {traceback.format_exc()}")
if processes:
processes = stop_processes(processes)
sys.exit(1) # Exit with an error code to indicate failure
if __name__ == '__main__':
try:
monitor_market()
except KeyboardInterrupt:
logger.error("Monitor stopped by user.")
sys.exit(0) # Exit cleanly if interrupted by the user