콘텐츠로 이동

유틸리티 (Utility)

유틸리티 함수는 시계열 데이터의 범용 분석과 조건 감지를 위한 보조 함수입니다.

ta.highest()

N기간 동안의 최고값을 반환합니다.

ta.highest(source, period) → TSeries

파라미터:

이름 타입 설명
source TSeries 입력 시계열
period int 조회 기간

예제:

c = chart("1D")
highest20 = ta.highest(c.high, 20)

c.line("20일 최고가", highest20, color="red")

# 신고가 돌파
if c.close[0] >= highest20[0]:
    buy(tag="20일 신고가 돌파")

ta.lowest()

N기간 동안의 최저값을 반환합니다.

ta.lowest(source, period) → TSeries

파라미터:

이름 타입 설명
source TSeries 입력 시계열
period int 조회 기간

예제:

c = chart("1D")
lowest20 = ta.lowest(c.low, 20)

c.line("20일 최저가", lowest20, color="green")

# 신저가 이탈
if c.close[0] <= lowest20[0]:
    sell(tag="20일 신저가 이탈")

채널 돌파 전략 (Donchian)

c = chart("1D")
upper_ch = ta.highest(c.high, 20)
lower_ch = ta.lowest(c.low, 20)

c.line("Upper Channel", upper_ch, color="red")
c.line("Lower Channel", lower_ch, color="green")

if c.close[0] > upper_ch[1]:
    buy(tag="상단 채널 돌파")
elif c.close[0] < lower_ch[1]:
    sell(tag="하단 채널 이탈")

ta.change()

현재 값과 N기간 전 값의 차이를 반환합니다.

ta.change(source, period=1) → TSeries

파라미터:

이름 타입 기본값 설명
source TSeries 입력 시계열
period int 1 비교 기간

예제:

c = chart("1D")
daily_change = ta.change(c.close)       # 전일 대비 변화량
weekly_change = ta.change(c.close, 5)   # 5일 전 대비 변화량

log(f"일간 변화: {daily_change[0]:.0f}")
log(f"5일 변화: {weekly_change[0]:.0f}")

ta.crossover()

두 시계열의 상향 교차를 감지합니다.

ta.crossover(s1, s2) → bool

파라미터:

이름 타입 설명
s1 TSeries 첫 번째 시계열
s2 TSeries 두 번째 시계열

예제:

c = chart("1D")
sma5 = ta.sma(c.close, 5)
sma20 = ta.sma(c.close, 20)

# TSeries.cross_up()과 유사
if ta.crossover(sma5, sma20):
    buy(tag="이평선 골든크로스")

crossover vs cross_up

ta.crossover(s1, s2)s1.cross_up(s2)는 동일한 교차 개념을 다룹니다.


ta.crossunder()

두 시계열의 하향 교차를 감지합니다.

ta.crossunder(s1, s2) → bool

파라미터:

이름 타입 설명
s1 TSeries 첫 번째 시계열
s2 TSeries 두 번째 시계열

예제:

c = chart("1D")
sma5 = ta.sma(c.close, 5)
sma20 = ta.sma(c.close, 20)

if ta.crossunder(sma5, sma20):
    sell(tag="이평선 데드크로스")

ta.sum()

N기간 동안의 합계를 반환합니다.

ta.sum(source, period) → TSeries

파라미터:

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

예제:

c = chart("1D")

# 20일 누적 거래량
cum_vol = ta.sum(c.volume, 20)
log(f"20일 누적 거래량: {cum_vol[0]:.0f}")

ta.valuewhen()

조건이 마지막으로 충족된 시점의 값을 반환합니다.

ta.valuewhen(condition, source) → TSeries

파라미터:

이름 타입 기본값 설명
condition TSeries/bool 시계열 조건 시계열
source TSeries 값을 가져올 시계열

예제:

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

# RSI가 마지막으로 30 이하였을 때의 종가
oversold_price = ta.valuewhen(rsi < 30, c.close)
log(f"마지막 과매도 시점 가격: {oversold_price[0]}")

ta.barssince()

조건이 마지막으로 충족된 후 경과한 바 수를 반환합니다.

ta.barssince(condition) → TSeries

파라미터:

이름 타입 설명
condition TSeries/bool 시계열 조건 시계열

예제:

c = chart("1D")
sma5 = ta.sma(c.close, 5)
sma20 = ta.sma(c.close, 20)

bars = ta.barssince(sma5 > sma20)
log(f"마지막 조건 충족 이후 {bars[0]}봉 경과")

# 골든크로스 후 5봉 이내에만 매수
if bars[0] <= 5 and c.close > sma20:
    buy(tag=f"조건 충족 후 {bars[0]}봉 — 초기 진입")

유틸리티 조합 예제

version("1.0")
description("채널 돌파 + 타이밍 전략")

c = chart("1D")

# 채널
high_ch = ta.highest(c.high, 20)
low_ch = ta.lowest(c.low, 10)

# 변화량
change5 = ta.change(c.close, 5)

# 마지막 돌파 이후 경과
bars_since_high = ta.barssince(c.close[0] > high_ch[1])

c.line("20일 고점", high_ch, color="red")
c.line("10일 저점", low_ch, color="green")

# 돌파 직후 + 5일간 양의 변화
if bars_since_high <= 2 and change5[0] > 0:
    buy(tag="채널 돌파 직후 상승 모멘텀")
elif c.close[0] < low_ch[1]:
    sell(tag="10일 저점 이탈")
else:
    hold()

관련 문서