- restore_positions(): read Upbit balances on startup to prevent double-buying after restart - notify.py: read TOKEN/CHAT_ID inside _send() to avoid empty values when module is imported before load_dotenv() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
"""Telegram 매매 알림."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
|
|
import requests
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_API = "https://api.telegram.org/bot{token}/sendMessage"
|
|
|
|
|
|
def _send(text: str) -> None:
|
|
token = os.getenv("TELEGRAM_TRADE_TOKEN", "")
|
|
chat_id = os.getenv("TELEGRAM_CHAT_ID", "")
|
|
if not token or not chat_id:
|
|
logger.warning("Telegram 설정 없음, 알림 스킵")
|
|
return
|
|
try:
|
|
requests.post(
|
|
_API.format(token=token),
|
|
json={"chat_id": chat_id, "text": text, "parse_mode": "HTML"},
|
|
timeout=5,
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"Telegram 알림 실패: {e}")
|
|
|
|
|
|
def notify_buy(ticker: str, price: float, amount: float, invested_krw: int) -> None:
|
|
_send(
|
|
f"📈 <b>[매수]</b> {ticker}\n"
|
|
f"가격: {price:,.0f}원\n"
|
|
f"수량: {amount}\n"
|
|
f"투자금: {invested_krw:,}원"
|
|
)
|
|
|
|
|
|
def notify_sell(ticker: str, price: float, pnl_pct: float, reason: str) -> None:
|
|
emoji = "✅" if pnl_pct >= 0 else "🔴"
|
|
_send(
|
|
f"{emoji} <b>[매도]</b> {ticker}\n"
|
|
f"가격: {price:,.0f}원\n"
|
|
f"수익률: {pnl_pct:+.1f}%\n"
|
|
f"사유: {reason}"
|
|
)
|
|
|
|
|
|
def notify_error(message: str) -> None:
|
|
_send(f"⚠️ <b>[오류]</b>\n{message}")
|