-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (70 loc) · 2.52 KB
/
main.py
File metadata and controls
79 lines (70 loc) · 2.52 KB
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
import asyncio
import logging
import time
import pandas as pd
import sys
from config_setup import SYMBOL_CHECK_INTERVAL, SELECTED_CSV, LOG_FILE
from symbol_selector import select_latest_symbols
from exchange_setup import init_exchange
from entry_manager import EntryManager
from position_manager import PositionManager
# Setup logging
logging.basicConfig(
filename=LOG_FILE,
level=logging.INFO,
format='%(asctime)s %(levelname)s:%(message)s'
)
logger = logging.getLogger(__name__)
# Initialize exchange and managers
exchange = init_exchange()
entry_mgr = EntryManager(exchange)
pos_mgr = PositionManager(exchange)
async def symbol_updater():
"""Refresh the list of trading symbols periodically."""
while True:
try:
select_latest_symbols()
logger.info("Symbol list refreshed.")
except Exception as e:
logger.error(f"Symbol update error: {e}")
await asyncio.sleep(SYMBOL_CHECK_INTERVAL)
async def entry_loop():
"""Check and place new entries for selected symbols."""
while True:
symbols = pd.read_csv(SELECTED_CSV)['symbol'].tolist()
for symbol in symbols:
try:
await entry_mgr.check_and_place(symbol)
except Exception as e:
logger.error(f"EntryManager error for {symbol}: {e}")
await asyncio.sleep(5)
def management_loop():
"""Synchronous loop to manage open positions continuously."""
while True:
try:
pos_mgr.update_positions()
except Exception as e:
logger.error(f"PositionManager error: {e}")
time.sleep(2)
async def main():
# Initial cleanup run before starting loops
try:
logger.info("Running initial position cleanup before starting loops...")
pos_mgr.update_positions()
except Exception as e:
logger.error(f"Initial cleanup error: {e}")
# Schedule management loop as background task first
management_task = asyncio.create_task(asyncio.to_thread(management_loop))
# Then start symbol and entry loops
symbol_task = asyncio.create_task(symbol_updater())
entry_task = asyncio.create_task(entry_loop())
# Await all tasks
await asyncio.gather(management_task, symbol_task, entry_task)
if __name__ == '__main__':
try:
asyncio.run(main())
except (KeyboardInterrupt, SystemExit):
logger.info('Recieved shutdown signal, closing all positions...')
pos_mgr.close_all_positions()
logger.info('All positions closed. Exiting.')
sys.exit(0)