고급 전략¶
LLM 통합, 멀티 조건 스코어링 등 고급 기법을 활용한 전략 예제입니다.
1. 스코어 기반 멀티팩터 전략¶
여러 지표의 시그널에 점수를 부여하고, 총점으로 매매 결정합니다.
version("1.0")
description("멀티팩터 스코어링 전략")
param("buy_threshold", "buy threshold", 3)
param("sell_threshold", "sell threshold", -3)
c = chart("1D")
# 지표 계산
rsi = ta.rsi(c.close, 14)
macd_line, signal, hist = ta.macd(c.close, 12, 26, 9)
sma20 = ta.sma(c.close, 20)
sma60 = ta.sma(c.close, 60)
adx = ta.adx(c.high, c.low, c.close, 14)
upper, mid, lower = ta.bbands(c.close, 20, 2.0)
# 스코어 계산
score = 0
reasons = []
# 1. RSI
if rsi[0] < 30:
score = score + 2
reasons.append(f"RSI과매도({rsi[0]:.0f})")
elif rsi[0] > 70:
score = score - 2
reasons.append(f"RSI과매수({rsi[0]:.0f})")
# 2. MACD
if hist[0] > 0 and hist[0] > hist[1]:
score = score + 1
reasons.append("MACD상승")
elif hist[0] < 0 and hist[0] < hist[1]:
score = score - 1
reasons.append("MACD하락")
# 3. 이평선 배열
if sma20 > sma60:
score = score + 1
reasons.append("정배열")
elif sma20 < sma60:
score = score - 1
reasons.append("역배열")
# 4. 가격 위치
if c.close > sma20:
score = score + 1
reasons.append("이평위")
elif c.close < sma20:
score = score - 1
reasons.append("이평아래")
# 5. 볼린저밴드
if c.close[0] < lower[0]:
score = score + 1
reasons.append("BB하단")
elif c.close[0] > upper[0]:
score = score - 1
reasons.append("BB상단")
# 결정
reason_str = ", ".join(reasons)
log(f"스코어: {score} [{reason_str}]")
if score >= script_params["buy_threshold"]:
buy(tag=f"스코어 {score}: {reason_str}")
elif score <= script_params["sell_threshold"]:
sell(tag=f"스코어 {score}: {reason_str}")
else:
hold(tag=f"스코어 {score} — 중립")
2. LLM 보조 의사결정 전략 — Postponed¶
[Postponed]
adviser.session()기능이 스펙아웃되어 이 예제는 현재 실행할 수 없습니다.
3. LLM 시장 분석 보고서 전략 — Postponed¶
[Postponed]
adviser.session()기능이 스펙아웃되어 이 예제는 현재 실행할 수 없습니다.
2. 적응형 파라미터 전략¶
변동성에 따라 지표 파라미터를 동적으로 조정합니다.
version("1.0")
description("적응형 파라미터 전략 — 변동성 기반 자동 조정")
c = chart("1D")
# 변동성 측정
atr = ta.atr(c.high, c.low, c.close, 14)
atr_sma = ta.sma(atr, 50)
# 변동성 비율 (현재 ATR / 평균 ATR)
if atr_sma[0] > 0:
vol_ratio = atr[0] / atr_sma[0]
else:
vol_ratio = 1.0
# 변동성에 따라 RSI 임계값 조정
if vol_ratio > 1.5:
# 높은 변동성 → 더 극단적인 값에서만 진입
rsi_buy_level = 20
rsi_sell_level = 80
log(f"높은 변동성({vol_ratio:.1f}x) — RSI 임계: {rsi_buy_level}/{rsi_sell_level}")
elif vol_ratio < 0.7:
# 낮은 변동성 → 덜 극단적인 값에서도 진입
rsi_buy_level = 35
rsi_sell_level = 65
log(f"낮은 변동성({vol_ratio:.1f}x) — RSI 임계: {rsi_buy_level}/{rsi_sell_level}")
else:
rsi_buy_level = 30
rsi_sell_level = 70
rsi = ta.rsi(c.close, 14)
c.line("RSI", rsi, color="purple")
c.hline("Buy Level", rsi_buy_level, color="green")
c.hline("Sell Level", rsi_sell_level, color="red")
if rsi[0] < rsi_buy_level:
buy(tag=f"적응형 매수 — RSI={rsi[0]:.0f} < {rsi_buy_level} (vol={vol_ratio:.1f}x)")
elif rsi[0] > rsi_sell_level:
sell(tag=f"적응형 매도 — RSI={rsi[0]:.0f} > {rsi_sell_level} (vol={vol_ratio:.1f}x)")
else:
hold()