Files
upbit-trader/main.py
joungmin 9fe3ce488e fix: auto-restart scanner on crash with Telegram notification
Wrap run_scanner() in while True loop so main thread recovers from
unhandled exceptions. Sends Telegram alert on crash before restarting.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-28 10:42:05 +09:00

71 lines
2.0 KiB
Python

"""Upbit 자동 트레이딩 봇 진입점."""
import logging
import threading
import time
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[
logging.StreamHandler(),
logging.FileHandler("trading.log", encoding="utf-8"),
],
)
from core.monitor import run_monitor
from core.notify import notify_error, notify_status
from core.trader import get_positions, restore_positions
from daemon.runner import run_scanner
STATUS_INTERVAL = 3600 # 1시간마다 요약 전송
def run_status_reporter(interval: int = STATUS_INTERVAL) -> None:
"""주기적으로 포지션 현황을 Telegram으로 전송."""
logger = logging.getLogger("status")
logger.info(f"상태 리포터 시작 (주기={interval//60}분)")
time.sleep(interval) # 첫 전송은 1시간 후
while True:
try:
notify_status(dict(get_positions()))
except Exception as e:
logger.error(f"상태 리포트 오류: {e}")
time.sleep(interval)
def main() -> None:
logger = logging.getLogger("main")
# 재시작 시 기존 잔고 복원 (이중 매수 방지)
restore_positions()
# 트레일링 스탑 감시 스레드 (10초 주기)
monitor_thread = threading.Thread(
target=run_monitor, args=(10,), daemon=True, name="monitor"
)
monitor_thread.start()
# 1시간 주기 상태 리포트 스레드
status_thread = threading.Thread(
target=run_status_reporter, daemon=True, name="status"
)
status_thread.start()
# 매수 스캔 루프 (60초 주기, 메인 스레드) — 예외 발생 시 Telegram 알림 후 재시작
while True:
try:
run_scanner()
except Exception as e:
logger.error(f"스캐너 비정상 종료: {e}", exc_info=True)
notify_error(f"스캐너 비정상 종료: {e}\n5초 후 재시작합니다.")
time.sleep(5)
if __name__ == "__main__":
main()