🖥️ 영웅문4 OpenAPI + Python 완전 정복 - 6단계: GUI + 전략 설정 + 알림 연동

반응형

OpenAPI + Python 완전 정복 - 6단계: GUI + 전략 설정 + 알림 연동

✅ 6단계 개요: 전략을 눈으로 보고, 버튼으로 실행!

지금까지는 코드를 직접 실행해야 했다면,
이제는 클릭 한 번으로 전략을 선택 → 실행 → 결과 확인 → 실시간 알림까지 가능한 구조로 확장합니다.


🛠️ STEP 1. GUI 도구 선택 – Streamlit (다운로드)

Streamlit은 Python 기반의 초간단 웹 GUI 프레임워크입니다.

bash

pip install streamlit

🖼️ STEP 2. 자동매매 GUI 템플릿 만들기

python

# streamlit_app.py
import streamlit as st
from datetime import date

st.title("🔰 키움증권 자동매매 시스템")

st.sidebar.header("전략 설정")
strategy = st.sidebar.selectbox("전략 선택", ["MA 골든크로스", "RSI 30 이하", "볼린저밴드"])

start_date = st.sidebar.date_input("백테스트 시작일", value=date(2024, 1, 1))
code = st.sidebar.text_input("종목코드", value="005930")

if st.button("🚀 전략 실행"):
    st.write(f"▶ 전략: {strategy}, 종목: {code}")
    st.write("📊 백테스트 중...")
    # 전략 함수 호출 예시
    # result = run_backtest(code, strategy, start_date)
    # st.line_chart(result['cumulative'])
    # send_discord_alert(...)

📷 Streamlit 실행

bash

streamlit run streamlit_app.py

실행 후 http://localhost:8501에서 자동매매 UI 확인 가능!


💾 STEP 3. 자동 저장 기능 (CSV/JSON 로그 저장)

🔹 백테스트 결과 CSV 저장

python

df.to_csv(f"logs/{code}_{strategy}_{start_date}.csv", index=True)

🔹 실행 로그 기록 (JSON)

python

import json
log = {
    "code": code,
    "strategy": strategy,
    "start_date": str(start_date),
    "end_date": str(df.index[-1]),
    "cumulative_return": float(df['cumulative'].iloc[-1]),
}
with open(f"logs/{code}_{strategy}.json", "w") as f:
    json.dump(log, f, indent=2)

📢 STEP 4. 디스코드 알림 연동

🔹 Webhook 설정 (서버에서 Webhook 생성 후 URL 복사)

python

import requests

def send_discord_alert(message, webhook_url):
    data = {"content": message}
    requests.post(webhook_url, json=data)

🔹 실행 예시

python

send_discord_alert(f"✅ 전략 실행 완료: {strategy}\n📈 누적 수익률: {round(df['cumulative'].iloc[-1]*100 - 100, 2)}%", 
                   webhook_url="https://discord.com/api/webhooks/xxx")

📷 디스코드 알림 예시:

matlab

✅ 전략 실행 완료: MA 골든크로스
📈 누적 수익률: +15.72%

📊 STEP 5. 실시간 실행 결과 Streamlit 시각화

python

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 4))
ax.plot(df['cumulative'], label="누적 수익률")
ax.set_title("전략 수익률 시각화")
ax.grid()
st.pyplot(fig)

 

반응형