반응형
✅ 10단계 개요: 왜 AI가 전략을 결정해야 할까?
기존 시스템은 전략을 사람이 "선택"했습니다.
하지만 시장은 매일 바뀌는 생물입니다.
✅ 이제는 AI가 스스로:
- 전략을 선택하고
- 수익률을 학습하여
- 강화학습을 통해 더 나은 전략을 탐색하고
- 자동으로 조정하는 지능형 시스템을 구성할 수 있습니다.
🧠 STEP 1. 전략 추천 AI 기본 구조
🔹 학습 데이터 구성
python
# 전략별 성능 기록 예시
data = pd.DataFrame([
{"code": "005930", "strategy": "MA", "return": 0.13},
{"code": "005930", "strategy": "RSI", "return": 0.08},
{"code": "005930", "strategy": "BB", "return": 0.11},
{"code": "000660", "strategy": "MA", "return": 0.07},
...
])
🔹 추천 모델: LightGBM, XGBoost, 또는 sklearn 사용
python
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
X = data[["code", "strategy"]]
y = (data["return"] > 0.10).astype(int)
X["code"] = LabelEncoder().fit_transform(X["code"])
X["strategy"] = LabelEncoder().fit_transform(X["strategy"])
model = RandomForestClassifier()
model.fit(X, y)
🔹 전략 추천 예시
python
predict_input = pd.DataFrame([{"code": "005930", "strategy": "RSI"}])
predict_input["code"] = 0 # 라벨 변환 필요
predict_input["strategy"] = 1
prob = model.predict_proba(predict_input)[0][1]
if prob > 0.6:
print("✅ 추천: RSI 전략 사용")
🧪 STEP 2. 실시간 강화학습 기반 자동매매 구조 (RL 기반)
🔹 DQN / PPO 기반 강화학습 사용 가능 (stable-baselines3)
bash
pip install stable-baselines3
🔹 환경 클래스 구성 (OpenAI Gym 스타일)
python
import gym
from gym import spaces
import numpy as np
class TradingEnv(gym.Env):
def __init__(self, df):
super().__init__()
self.df = df
self.current_step = 0
self.balance = 1000000
self.shares = 0
self.action_space = spaces.Discrete(3) # 0: 매도, 1: 보유, 2: 매수
self.observation_space = spaces.Box(low=0, high=1, shape=(5,), dtype=np.float32)
def reset(self):
self.current_step = 0
self.balance = 1000000
self.shares = 0
return self._next_observation()
def _next_observation(self):
row = self.df.iloc[self.current_step]
return np.array([
row['open'], row['high'], row['low'], row['close'], row['volume']
]) / 100000 # 정규화
def step(self, action):
reward = 0
...
self.current_step += 1
done = self.current_step >= len(self.df) - 1
return self._next_observation(), reward, done, {}
🧠 STEP 3. 전략 실행 → 결과 반영 → 재학습 루프
🔁 자동화 루프 구조
- AI가 전략 선택 (model.predict)
- 선택된 전략 실행 (run_strategy)
- 수익률 저장 및 X, y 업데이트
- 모델 재학습
- 다음 날 새로운 전략 추천
python
# 간단한 루프 예시
for day in range(N):
strategy = ai_select_strategy(code)
df = run_strategy(code, strategy)
result = evaluate(df)
update_model(code, strategy, result)
📡 STEP 4. Streamlit + AI 연동
python
st.header("🧠 AI 전략 추천")
if st.button("전략 추천받기"):
rec = ai_select_strategy("005930")
st.success(f"추천 전략: {rec}")
📷 AI 추천 UI 예시:
🧾 STEP 5. 전략 추천 기록 + 성능 트래킹 + 보고서
- AI 추천 전략 → Google Sheet 자동 기록
- 추천 전략별 누적 수익률 비교
- 전략 성공률 로그로 추적하여 AI 학습 성능 확인
✅ 전체 구조 요약: AI 자동매매 통합 구조
📷 AI 자동매매 시스템 흐름도
구성요소 | 역활 |
전략 추천 AI | 종목/날짜/상황에 맞는 전략 자동 선택 |
전략 실행 모듈 | 추천 전략 기반 자동매매 |
결과 기록 | 수익률, 위험도 저장 |
AI 재학습 | 성능을 반영하여 전략 추천 정확도 향상 |
반응형