이 아이디어를 구현하기 위한 파인스크립트 코드를 작성해보겠습니다. 주요 로직은 다음과 같습니다:
- 지지/저항선의 유효성을 시간에 따라 평가
- 변동성을 기반으로 '근처' 개념 정의
- 새로운 고점/저점의 유효성 검증
//@version=5
indicator("Dynamic Support/Resistance Lines", overlay=true)
// 설정값
lookbackPeriod = input.int(20, "Lookback Period")
volatilityPeriod = input.int(14, "Volatility Period")
volatilityMultiplier = input.float(1.5, "Volatility Multiplier")
// 피봇 포인트 계산
pivotHighs = ta.pivothigh(high, lookbackPeriod, lookbackPeriod)
pivotLows = ta.pivotlow(low, lookbackPeriod, lookbackPeriod)
// 변동성 계산 (ATR 사용)
atr = ta.atr(volatilityPeriod)
// 지지/저항선 구조체
type SRLine
float price
int firstBar
int lastBar
bool isValid
bool isResistance
var array<SRLine> srLines = array.new<SRLine>()
// 새로운 피봇 발생 시 처리
if not na(pivotHighs)
isNearExisting = false
// 기존 저항선과의 거리 확인
for i = 0 to array.size(srLines) - 1
line = array.get(srLines, i)
if line.isResistance and math.abs(pivotHighs - line.price) <= atr * volatilityMultiplier
isNearExisting := true
line.lastBar := bar_index
break
if not isNearExisting
// 새로운 저항선 추가
newLine = SRLine.new(
price = pivotHighs,
firstBar = bar_index,
lastBar = bar_index,
isValid = true,
isResistance = true
)
array.push(srLines, newLine)
// 지지선에 대해서도 동일한 로직 적용
if not na(pivotLows)
isNearExisting = false
for i = 0 to array.size(srLines) - 1
line = array.get(srLines, i)
if not line.isResistance and math.abs(pivotLows - line.price) <= atr * volatilityMultiplier
isNearExisting := true
line.lastBar := bar_index
break
if not isNearExisting
newLine = SRLine.new(
price = pivotLows,
firstBar = bar_index,
lastBar = bar_index,
isValid = true,
isResistance = false
)
array.push(srLines, newLine)
// 라인 유효성 검사 및 그리기
for i = 0 to array.size(srLines) - 1
line = array.get(srLines, i)
if line.isValid
lineColor = line.isResistance ? color.red : color.green
line.new(
x1 = line.firstBar,
y1 = line.price,
x2 = line.lastBar,
y2 = line.price,
color = lineColor,
width = 2
)
// 오래된 라인 제거
maxLines = 10
if array.size(srLines) > maxLines
array.shift(srLines)
이 코드의 주요 특징:
- ATR을 사용하여 변동성 기반의 '근처' 개념을 정의합니다.
- 지지/저항선을 구조체로 관리하여 각 라인의 생성 시점과 마지막 터치 시점을 추적합니다.
- 새로운 피봇 포인트가 기존 라인 근처에 형성될 경우, 새 라인을 생성하지 않고 기존 라인을 업데이트합니다.
- 라인의 유효기간을 마지막 터치 시점까지로 설정합니다.
사용자 설정 가능한 파라미터:
lookbackPeriod: 피봇 포인트 탐지를 위한 룩백 기간
volatilityPeriod: ATR 계산 기간
volatilityMultiplier: 변동성 배수 (근처 판단 기준)
이 코드를 기반으로 필요에 따라 추가적인 기능이나 수정이 필요한 부분이 있다면 말씀해 주세요.