From b423197dffc149ef5e4b61561f602a1861cf285f Mon Sep 17 00:00:00 2001 From: dsyoon Date: Sat, 26 Apr 2025 20:10:14 +0900 Subject: [PATCH] init --- stock_monitor.py | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/stock_monitor.py b/stock_monitor.py index 5b8a84a..cc1b8d1 100644 --- a/stock_monitor.py +++ b/stock_monitor.py @@ -10,15 +10,18 @@ from multiprocessing import Pool import schedule from config import * + def send(text): client = telegram.Bot(token=TELEGRAM_BOT_TOKEN) asyncio.run(client.send_message(chat_id=TELEGRAM_CHAT_ID, text=text)) return + def send_telegram_message(message): pool = Pool(12) pool.map(send, [message]) + def calculate_bollinger_bands(data): data['MA'] = data['Close'].rolling(window=BOLLINGER_PERIOD).mean() data['STD'] = data['Close'].rolling(window=BOLLINGER_PERIOD).std() @@ -26,6 +29,7 @@ def calculate_bollinger_bands(data): data['Lower'] = data['MA'] - (BOLLINGER_STD * data['STD']) return data + def check_bollinger_bands(symbol, data): if len(data) < BOLLINGER_PERIOD: return None @@ -46,6 +50,7 @@ def check_bollinger_bands(symbol, data): 'distance': distance } + def get_coin_data(symbol, retries=3): for attempt in range(retries): try: @@ -82,6 +87,7 @@ def get_coin_data(symbol, retries=3): continue return None + def get_stock_data(symbol, retries=3): for attempt in range(retries): try: @@ -106,11 +112,14 @@ def get_stock_data(symbol, retries=3): continue return None + def sendAlertMsg(info, market="US"): if market == "US": - message = "๐Ÿ”” [US] {} ({}) ํ˜„์žฌ๊ฐ€: ${:.2f}, ๊ทผ์ ‘๋„: {:.2f}%".format(info['name'], info['symbol'], info['price'], info['distance']) + message = "๐Ÿ”” [US] {} ({}) ํ˜„์žฌ๊ฐ€: ${:.2f}, ๊ทผ์ ‘๋„: {:.2f}%".format(info['name'], info['symbol'], info['price'], + info['distance']) else: - message = "๐Ÿ”” [KR] {} ({}) ํ˜„์žฌ๊ฐ€: โ‚ฉ{:.0f}, ๊ทผ์ ‘๋„: {:.2f}%".format(info['name'], info['symbol'].replace('.KS', ''), info['price'], info['distance']) + message = "๐Ÿ”” [KR] {} ({}) ํ˜„์žฌ๊ฐ€: โ‚ฉ{:.0f}, ๊ทผ์ ‘๋„: {:.2f}%".format(info['name'], info['symbol'].replace('.KS', ''), + info['price'], info['distance']) try: send_telegram_message(message) @@ -118,6 +127,7 @@ def sendAlertMsg(info, market="US"): print(f"Error sending Telegram message: {str(e)}") return + def monitor_us_stocks(): # ๋ฏธ๊ตญ ์ฃผ์‹ ๋ชจ๋‹ˆํ„ฐ๋ง print("Monitoring US stocks...") @@ -141,6 +151,7 @@ def monitor_us_stocks(): return + def monitor_kr_stocks(): # ํ•œ๊ตญ ETF ๋ชจ๋‹ˆํ„ฐ๋ง print("\nMonitoring Korean ETFs...") @@ -164,8 +175,9 @@ def monitor_kr_stocks(): return + def monitor_coins(): - # ๋ฏธ๊ตญ ์ฃผ์‹ ๋ชจ๋‹ˆํ„ฐ๋ง + # ์ฝ”์ธ ๋ชจ๋‹ˆํ„ฐ๋ง print("Monitoring KR Coins...") for symbol in KR_COINS: data = get_coin_data(symbol) @@ -187,25 +199,23 @@ def monitor_coins(): return + def run_schedule(): + # ์ฝ”์ธ ๋ชจ๋‹ˆํ„ฐ๋ง ์Šค์ผ€์ค„ (๋งค์‹œ๊ฐ„ 1๋ถ„, 11๋ถ„, 21๋ถ„, 31๋ถ„, 41๋ถ„, 51๋ถ„) + for minute in [1, 11, 21, 31, 41, 51]: + schedule.every().hour.at(f":{minute:02d}").do(monitor_coins) + + # ๋ฏธ๊ตญ ์ฃผ์‹ ๋ชจ๋‹ˆํ„ฐ๋ง ์Šค์ผ€์ค„ (๋งค์ผ ์ €๋… 5์‹œ 20๋ถ„) + schedule.every().day.at("17:20").do(monitor_us_stocks) + + # ํ•œ๊ตญ ETF ๋ชจ๋‹ˆํ„ฐ๋ง ์Šค์ผ€์ค„ (๋งค์ผ ์˜ค์ „ 8์‹œ) + schedule.every().day.at("18:20").do(monitor_kr_stocks) + + print("Scheduler started. Monitoring will run at specified times.") while True: schedule.run_pending() time.sleep(1) + if __name__ == "__main__": - print("Starting monitoring system...") - - # ์Šค์ผ€์ค„ ์„ค์ • - schedule.every().hour.at(":01").do(monitor_coins) # ๋งค์‹œ 1๋ถ„์— ์‹คํ–‰ - schedule.every().day.at("17:20").do(monitor_us_stocks) # ๋งค์ผ ์ €๋… 5์‹œ 20๋ถ„์— ์‹คํ–‰ - schedule.every().day.at("08:00").do(monitor_kr_stocks) # ๋งค์ผ ์˜ค์ „ 8์‹œ์— ์‹คํ–‰ - - # ์ดˆ๊ธฐ ์‹คํ–‰ - print("Running initial checks...") - monitor_coins() - monitor_us_stocks() - monitor_kr_stocks() - - # ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰ - print("Starting scheduler...") run_schedule()