๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
pip install python-telegram-bot yfinance
=========================
A-1. ํ
๋ ๊ทธ๋จ ์ฃผ์ ์๋๋ด ์ฝ๋
import time
import yfinance as yf
from telegram import Bot
TOKEN = "์ฌ๊ธฐ์_๋ดํ ํฐ"
CHAT_ID = "์ฌ๊ธฐ์_๋ณธ์ธ_CHAT_ID"
bot = Bot(token=TOKEN)
# ๊ฐ์ํ ์ข
๋ชฉ
stocks = {
"005930.KS": 70000, # ์ผ์ฑ์ ์ ๋ชฉํ๊ฐ
"035420.KS": 200000 # ๋ค์ด๋ฒ ๋ชฉํ๊ฐ
}
def send_message(text):
bot.send_message(chat_id=CHAT_ID, text=text)
def check_price():
for ticker, target in stocks.items():
stock = yf.Ticker(ticker)
price = stock.history(period="1d")["Close"].iloc[-1]
if price >= target:
msg = f"๐จ {ticker} ๋ชฉํ๊ฐ ๋๋ฌ!\nํ์ฌ๊ฐ: {int(price):,}์\n๋ชฉํ๊ฐ: {target:,}์"
send_message(msg)
print(msg)
while True:
try:
check_price()
time.sleep(300) # 5๋ถ๋ง๋ค ์ฒดํฌ
except Exception as e:
print("์๋ฌ:", e)
time.sleep(60)
===============================
======================
๋ํ ์๋ฆผ ์ ์ฉ ํ
๋ ๊ทธ๋จ ๋ด
pip install python-telegram-bot yfinance
A-2 .๋ํ ์๋ฆผ๋ด ์ฝ๋
import time
import yfinance as yf
from telegram import Bot
TOKEN = "์ฌ๊ธฐ์_ํ
๋ ๊ทธ๋จ_๋ด_ํ ํฐ"
CHAT_ID = "์ฌ๊ธฐ์_๋ณธ์ธ_CHAT_ID"
bot = Bot(token=TOKEN)
# ์ข
๋ชฉ : ๋ชฉํ๊ฐ
targets = {
"005930.KS": 70000, # ์ผ์ฑ์ ์
"000660.KS": 150000, # SKํ์ด๋์ค
"AAPL": 200 # ์ ํ
}
sent = set() # ์ด๋ฏธ ์๋ฆผ ๋ณด๋ธ ์ข
๋ชฉ ์ค๋ณต ๋ฐฉ์ง
def check_targets():
for code, target_price in targets.items():
stock = yf.Ticker(code)
price = stock.history(period="1d")["Close"].iloc[-1]
if price >= target_price and code not in sent:
message = f"""
๐ ๋ชฉํ๊ฐ ๋ํ ์๋ฆผ!
์ข
๋ชฉ: {code}
ํ์ฌ๊ฐ: {int(price):,}
๋ชฉํ๊ฐ: {int(target_price):,}
์ํ: ๋ชฉํ๊ฐ ๋ํ ์ฑ๊ณต ๐
"""
bot.send_message(chat_id=CHAT_ID, text=message)
sent.add(code)
while True:
try:
check_targets()
time.sleep(180) # 3๋ถ๋ง๋ค ์ฒดํฌ
except Exception as e:
print("์ค๋ฅ:", e)
time.sleep(60)
-===============
A-3.๊ธ๋ฑ ๊ธ๋ฝ ํผ์ผํธ ์๋ฆผ๋ด
pip install python-telegram-bot yfinance
import time
import yfinance as yf
from telegram import Bot
TOKEN = "์ฌ๊ธฐ์_๋ดํ ํฐ"
CHAT_ID = "์ฌ๊ธฐ์_CHAT_ID"
bot = Bot(token=TOKEN)
# ๊ฐ์ ์ข
๋ชฉ
watch_list = ["005930.KS", "000660.KS", "AAPL", "TSLA"]
UP_ALERT = 5 # +5% ์ด์ ๊ธ๋ฑ
DOWN_ALERT = -5 # -5% ์ดํ ๊ธ๋ฝ
alerted = set()
def check_change():
for code in watch_list:
stock = yf.Ticker(code)
data = stock.history(period="2d")
yesterday = data["Close"].iloc[-2]
today = data["Close"].iloc[-1]
change = (today - yesterday) / yesterday * 100
key = f"{code}_{int(change)}"
if (change >= UP_ALERT or change <= DOWN_ALERT) and key not in alerted:
arrow = "๐ ๊ธ๋ฑ" if change >= UP_ALERT else "๐ป ๊ธ๋ฝ"
msg = f"""
{arrow} ํผ์ผํธ ์๋ฆผ
์ข
๋ชฉ: {code}
์ ์ผ์ข
๊ฐ: {int(yesterday):,}
ํ์ฌ๊ฐ: {int(today):,}
๋ฑ๋ฝ๋ฅ : {change:.2f}%
"""
bot.send_message(chat_id=CHAT_ID, text=msg)
alerted.add(key)
while True:
try:
check_change()
time.sleep(180) # 3๋ถ๋ง๋ค ์ฒดํฌ
except Exception as e:
print("์๋ฌ:", e)
time.sleep(60)
======================================
์ด๋ํ๊ท ์ ๊ณจ๋ ํฌ๋ก์ค ๋ฐ์ ์ ํ
๋ ๊ทธ๋จ์ผ๋ก ์๋ฆผ ์ค๋ ๋ด
pip install python-telegram-bot yfinance pandas
A-4.๊ณจ๋ ํฌ๋ก์ค ์๋ฆผ ๋ด ์ฝ๋ (5์ผ์ / 20์ผ์ ์์)
import time
import yfinance as yf
import pandas as pd
from telegram import Bot
TOKEN = "์ฌ๊ธฐ์_๋ดํ ํฐ"
CHAT_ID = "์ฌ๊ธฐ์_CHAT_ID"
bot = Bot(token=TOKEN)
watch_list = ["005930.KS", "000660.KS", "AAPL", "TSLA"]
SHORT_MA = 5
LONG_MA = 20
alerted = set()
def check_golden_cross():
for code in watch_list:
df = yf.download(code, period="3mo", interval="1d")
df["MA_S"] = df["Close"].rolling(SHORT_MA).mean()
df["MA_L"] = df["Close"].rolling(LONG_MA).mean()
prev = df.iloc[-2]
now = df.iloc[-1]
# ๊ณจ๋ ํฌ๋ก์ค ์กฐ๊ฑด: ์ด์ ๋ ์๋, ์ค๋์ ์
if prev["MA_S"] < prev["MA_L"] and now["MA_S"] > now["MA_L"]:
if code not in alerted:
msg = f"""
๐ ๊ณจ๋ ํฌ๋ก์ค ๋ฐ์!
์ข
๋ชฉ: {code}
๋จ๊ธฐ์ ({SHORT_MA}์ผ): {now["MA_S"]:.2f}
์ฅ๊ธฐ์ ({LONG_MA}์ผ): {now["MA_L"]:.2f}
ํ์ฌ๊ฐ: {now["Close"]:.2f}
์ ํธ: ์์น ์ถ์ธ ์ ํ ๐
"""
bot.send_message(chat_id=CHAT_ID, text=msg)
alerted.add(code)
while True:
try:
check_golden_cross()
time.sleep(300) # 5๋ถ๋ง๋ค ์ฒดํฌ
except Exception as e:
print("์๋ฌ:", e)
time.sleep(60)
=========
ํ
๋ ๊ทธ๋จ ์๋ฆผ → ์ฆ๊ถ์ฌ API ์ฐ๋ → ์๋๋งค์/๋งค๋๊น์ง ํ๋ ์์ ์๋๋งค๋งค ๋ด ๊ตฌ์กฐ
[์ค์๊ฐ ์์ธ ์์ง]
↓
[์กฐ๊ฑด ํ๋จ (๊ณจ๋ ํฌ๋ก์ค, ๋ชฉํ๊ฐ, ํผ์ผํธ)]
↓
[๋งค์/๋งค๋ ์๊ทธ๋ ๋ฐ์]
↓
[์ฆ๊ถ์ฌ REST API ์ฃผ๋ฌธ ์ ์ก]
↓
[์ฒด๊ฒฐ ํ์ธ]
↓
[ํ
๋ ๊ทธ๋จ์ผ๋ก ๊ฒฐ๊ณผ ํต๋ณด]
==================
pip install requests python-telegram-bot yfinance pandas
A-5. ์๋๋งค๋งค ๊ธฐ๋ณธ์์ ์ฝ๋
import requests
import time
import yfinance as yf
import pandas as pd
from telegram import Bot
# ===== ํ
๋ ๊ทธ๋จ =====
TG_TOKEN = "ํ
๋ ๊ทธ๋จ_๋ดํ ํฐ"
CHAT_ID = "๋ณธ์ธ_CHAT_ID"
bot = Bot(TG_TOKEN)
# ===== ์ฆ๊ถ์ฌ API (์: ํ๊ตญํฌ์) =====
APP_KEY = "์ฆ๊ถ์ฌ_APP_KEY"
APP_SECRET = "์ฆ๊ถ์ฌ_APP_SECRET"
ACCESS_TOKEN = "๋ฐ๊ธ๋ฐ์_ACCESS_TOKEN"
ACCOUNT = "๊ณ์ข๋ฒํธ"
BASE_URL = "https://openapi.koreainvestment.com:9443"
headers = {
"authorization": f"Bearer {ACCESS_TOKEN}",
"appkey": APP_KEY,
"appsecret": APP_SECRET,
"tr_id": "TTTC0802U", # ์ฃผ์ ์์ฅ๊ฐ ๋งค์
"content-type": "application/json"
}
def send_telegram(msg):
bot.send_message(chat_id=CHAT_ID, text=msg)
# ===== ๋งค์ ํจ์ =====
def buy(stock_code, qty):
url = f"{BASE_URL}/uapi/domestic-stock/v1/trading/order-cash"
data = {
"CANO": ACCOUNT[:8],
"ACNT_PRDT_CD": ACCOUNT[8:],
"PDNO": stock_code,
"ORD_DVSN": "01", # ์์ฅ๊ฐ
"ORD_QTY": str(qty),
"ORD_UNPR": "0"
}
res = requests.post(url, headers=headers, json=data)
return res.json()
# ===== ๊ณจ๋ ํฌ๋ก์ค ์กฐ๊ฑด =====
def golden_cross(code):
df = yf.download(code, period="3mo")
df["MA5"] = df["Close"].rolling(5).mean()
df["MA20"] = df["Close"].rolling(20).mean()
prev = df.iloc[-2]
now = df.iloc[-1]
return prev["MA5"] < prev["MA20"] and now["MA5"] > now["MA20"]
# ===== ๋ฉ์ธ ๋ฃจํ =====
watch = ["005930.KS", "000660.KS"]
bought = set()
while True:
try:
for stock in watch:
code = stock.replace(".KS","")
if golden_cross(stock) and code not in bought:
result = buy(code, 1) # 1์ฃผ ๋งค์
send_telegram(f"๐ ์๋๋งค์ ์คํ\n์ข
๋ชฉ: {code}\n๊ฒฐ๊ณผ: {result}")
bought.add(code)
time.sleep(60)
except Exception as e:
sen. d_telegram(f"⚠ ์ค๋ฅ ๋ฐ์: {e}")
time.sleep(30)
====================
A-6.ํ
๋ ๊ทธ๋จ์์ ๋ช
๋ น์ด๋ก ์ข
๋ชฉ ์ถ๊ฐ/์ญ์ ํ๋ ์ฃผ์ ์๋ฆผ ๋ด
pip install python-telegram-bot yfinance pandas
ํ
๋ ๊ทธ๋จ ๋ช
๋ น์ดํ ์ฃผ์ ๊ด๋ฆฌ ๋ด
๊ธฐ๋ฅ
-
/add 005930 → ์ข
๋ชฉ ์ถ๊ฐ
-
/del 005930 → ์ข
๋ชฉ ์ญ์
-
/list → ํ์ฌ ๊ฐ์ ์ข
๋ชฉ ๋ณด๊ธฐ
from telegram.ext import Application, CommandHandler
import yfinance as yf
import pandas as pd
TOKEN = "์ฌ๊ธฐ์_๋ดํ ํฐ"
watch_list = set()
async def start(update, context):
await update.message.reply_text(
"๐ ์ฃผ์ ์๋ฆผ ๋ด ์์!\n\n"
"/add ์ข
๋ชฉ์ฝ๋\n"
"/del ์ข
๋ชฉ์ฝ๋\n"
"/list"
)
async def add_stock(update, context):
code = context.args[0].upper()
watch_list.add(code)
await update.message.reply_text(f"✅ {code} ๊ฐ์์ข
๋ชฉ์ ์ถ๊ฐ๋จ")
async def del_stock(update, context):
code = context.args[0].upper()
if code in watch_list:
watch_list.remove(code)
await update.message.reply_text(f"❌ {code} ๊ฐ์์ข
๋ชฉ์์ ์ญ์ ๋จ")
else:
await update.message.reply_text("⚠ ๋ชฉ๋ก์ ์๋ ์ข
๋ชฉ์
๋๋ค.")
async def list_stock(update, context):
if not watch_list:
await update.message.reply_text("ํ์ฌ ๊ฐ์ ์ข
๋ชฉ์ด ์์ต๋๋ค.")
else:
msg = "๐ ํ์ฌ ๊ฐ์ ์ข
๋ชฉ\n\n"
msg += "\n".join(watch_list)
await update.message.reply_text(msg)
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("add", add_stock))
app.add_handler(CommandHandler("del", del_stock))
app.add_handler(CommandHandler("list", list_stock))
app.run_polling()
====================
์๋๋งค๋งค ์ปจํธ๋กค๊ธฐ๋ฅ ์ถ๊ฐ์
/target 005930 72000 → ๋ชฉํ๊ฐ ์ค์
/ma 5 20 → ์ด๋ํ๊ท ๋ณ๊ฒฝ
/buy 005930 10 → ์ค๋งค์
/sell 005930 10 → ์ค๋งค๋
/on → ์๋๋งค๋งค ์์
/off → ์๋๋งค๋งค ์ค์ง