콘텐츠로 이동

오실레이터 (Oscillators)

오실레이터는 가격의 과매수/과매도 상태를 판별하는 지표입니다. 주로 0~100 또는 중앙선 기준으로 진동합니다.

ta.rsi()

상대강도지수 (Relative Strength Index)

일정 기간 동안의 상승폭과 하락폭의 비율로 과매수/과매도를 판단합니다.

ta.rsi(source, period) → TSeries

파라미터:

이름 타입 기본값 설명
source TSeries 입력 시계열
period int 14 계산 기간

반환값: TSeries (0~100 범위)

해석:

  • 70 이상: 과매수 구간 — 매도 고려
  • 30 이하: 과매도 구간 — 매수 고려
  • 50 기준: 50 위면 상승 모멘텀, 아래면 하락 모멘텀

예제:

c = chart("1D")
rsi = ta.rsi(c.close, 14)

c.line("RSI", rsi, color="purple")
c.hline("과매수", 70, color="red")
c.hline("과매도", 30, color="green")

if rsi[0] < 30:
    buy(tag=f"RSI 과매도: {rsi[0]:.1f}")
elif rsi[0] > 70:
    sell(tag=f"RSI 과매수: {rsi[0]:.1f}")
else:
    hold()

RSI 다이버전스 탐지

c = chart("1D")
rsi = ta.rsi(c.close, 14)

# 가격은 저점 갱신, RSI는 저점 미갱신 → 상승 다이버전스
price_lower = c.close[0] < ta.lowest(c.close, 20)[0]
rsi_higher = rsi[0] > ta.lowest(rsi, 20)[0]

if price_lower and rsi_higher and rsi[0] < 40:
    buy(tag="RSI 상승 다이버전스")

ta.macd()

MACD (Moving Average Convergence Divergence)

두 EMA의 차이로 추세와 모멘텀을 동시에 파악합니다.

ta.macd(source, fast_period, slow_period, signal_period) → (TSeries, TSeries, TSeries)

파라미터:

이름 타입 기본값 설명
source TSeries 입력 시계열
fast_period int 12 빠른 EMA 기간
slow_period int 26 느린 EMA 기간
signal_period int 9 시그널 EMA 기간

반환값: (macd_line, signal_line, histogram) — 3개의 TSeries 튜플

  • macd_line: 빠른 EMA - 느린 EMA
  • signal_line: MACD 라인의 EMA
  • histogram: MACD 라인 - 시그널 라인

예제:

c = chart("1D")
macd_line, signal, hist = ta.macd(c.close, 12, 26, 9)

c.line("MACD", macd_line, color="blue")
c.line("Signal", signal, color="orange")
c.histogram("Histogram", hist, color="gray")
c.hline("Zero", 0, color="white")

# MACD 크로스
if macd_line.cross_up(signal):
    buy(tag="MACD 골든크로스")
elif macd_line.cross_down(signal):
    sell(tag="MACD 데드크로스")

MACD 히스토그램 활용

macd_line, signal, hist = ta.macd(c.close, 12, 26, 9)

# 히스토그램 방향 전환
if hist[1] < 0 and hist[0] > 0:
    buy(tag="MACD 히스토그램 양전환")
elif hist[1] > 0 and hist[0] < 0:
    sell(tag="MACD 히스토그램 음전환")

ta.stoch()

스토캐스틱 (Stochastic Oscillator)

현재 종가가 일정 기간의 가격 범위에서 어느 위치에 있는지 나타냅니다.

ta.stoch(high, low, close, k_period=14, d_period=3, smooth_k=3) → (TSeries, TSeries)

파라미터:

이름 타입 기본값 설명
high TSeries 고가 시계열
low TSeries 저가 시계열
close TSeries 종가 시계열
k_period int 14 %K 기간
d_period int 3 %D 기간
smooth_k int 3 %K 평활화 기간

반환값: (k, d) — 2개의 TSeries 튜플 (0~100 범위)

해석:

  • 80 이상: 과매수
  • 20 이하: 과매도
  • %K가 %D를 상향 돌파: 매수 시그널
  • %K가 %D를 하향 돌파: 매도 시그널

예제:

c = chart("1D")
k, d = ta.stoch(c.high, c.low, c.close, 14, 3, 3)

c.line("%K", k, color="blue")
c.line("%D", d, color="orange")
c.hline("과매수", 80, color="red")
c.hline("과매도", 20, color="green")

# 과매도 구간에서 %K가 %D 상향 돌파
if k[0] < 20 and k.cross_up(d):
    buy(tag="스토캐스틱 과매도 골든크로스")
elif k[0] > 80 and k.cross_down(d):
    sell(tag="스토캐스틱 과매수 데드크로스")

ta.cci()

상품채널지수 (Commodity Channel Index)

가격이 통계적 평균에서 얼마나 벗어났는지 측정합니다.

ta.cci(high, low, close, length=20) → TSeries

파라미터:

이름 타입 기본값 설명
high TSeries 고가 시계열
low TSeries 저가 시계열
close TSeries 종가 시계열
length int 20 계산 기간

반환값: TSeries (범위 제한 없음, 일반적으로 -200 ~ +200)

해석:

  • +100 이상: 과매수 / 강한 상승 추세
  • -100 이하: 과매도 / 강한 하락 추세
  • 0 근처: 추세 없음

예제:

c = chart("1D")
cci = ta.cci(c.high, c.low, c.close, 20)

c.line("CCI", cci, color="teal")
c.hline("+100", 100, color="red")
c.hline("-100", -100, color="green")
c.hline("Zero", 0, color="gray")

if cci[0] < -100 and cci[0] > cci[1]:
    buy(tag="CCI 과매도 반등")
elif cci[0] > 100 and cci[0] < cci[1]:
    sell(tag="CCI 과매수 하락")

오실레이터 조합 전략

version("1.0")
description("RSI + 스토캐스틱 이중 확인 전략")

c = chart("1D")

rsi = ta.rsi(c.close, 14)
k, d = ta.stoch(c.high, c.low, c.close, 14, 3, 3)

# 두 오실레이터 모두 과매도일 때만 매수
if rsi[0] < 30 and k[0] < 20:
    buy(tag=f"이중 과매도 — RSI:{rsi[0]:.0f}, K:{k[0]:.0f}")
elif rsi[0] > 70 and k[0] > 80:
    sell(tag=f"이중 과매수 — RSI:{rsi[0]:.0f}, K:{k[0]:.0f}")
else:
    hold()

관련 문서