아래는 설명해주신 아이디어를 반영한 파인스크립트(Pine Script) 예제 코드입니다.
- 오래동안 돌파 못한 지지/저항선의 신뢰도(기간)를 유동적으로 관리
- 새로운 고점/저점이 기존 지지/저항선 근처에 있으면 무시 (근처 개념은 변동성(ATR) 기반)
- 근처 판단 시 고점과 저점 간 차이도 고려
//@version=5
indicator("Dynamic Support/Resistance with Volatility Filter", overlay=true)
// === 파라미터 ===
atrLength = input.int(14, "ATR Length")
nearMultiplier = input.float(1.0, "Nearness Multiplier (ATR)")
minDuration = input.int(10, "Minimum Duration for SR line")
maxDuration = input.int(100, "Maximum Duration for SR line")
// === 변동성 계산 ===
atr = ta.atr(atrLength)
// === 고점/저점 탐지 ===
// 고점: 현재 고가가 좌우 n봉보다 높음
pivotLen = 5
isHighPivot = ta.pivothigh(high, pivotLen, pivotLen) != na
isLowPivot = ta.pivotlow(low, pivotLen, pivotLen) != na
var float[] resistanceLevels = array.new_float()
var int[] resistanceDurations = array.new_int()
var float[] supportLevels = array.new_float()
var int[] supportDurations = array.new_int()
// === 근처 판단 함수 ===
// 기존 레벨 배열과 새 레벨이 '근처'인지 판단
// 근처 = |새레벨 - 기존레벨| < nearMultiplier * atr
// 고점 판단 시 저점과의 차이도 고려 (저점 배열과 비교)
f_isNear(level, arr) =>
near = false
for i = 0 to array.size(arr) - 1
if math.abs(level - array.get(arr, i)) < nearMultiplier * atr
near := true
break
near
// === 지지/저항선 추가 함수 ===
f_addResistance(level) =>
if not f_isNear(level, resistanceLevels)
array.push(resistanceLevels, level)
array.push(resistanceDurations, minDuration)
f_addSupport(level) =>
if not f_isNear(level, supportLevels)
array.push(supportLevels, level)
array.push(supportDurations, minDuration)
// === 기존 지지/저항선 기간 업데이트 ===
// 돌파 시 기간 초기화, 돌파 시도 시 기간 증가
f_updateDurations() =>
// 저항선 업데이트
for i = 0 to array.size(resistanceLevels) - 1
level = array.get(resistanceLevels, i)
dur = array.get(resistanceDurations, i)
// 돌파 시도: 현재 종가가 저항선 위로 올라가려는 시도
if close > level
dur := minDuration
else
dur := math.min(dur + 1, maxDuration)
array.set(resistanceDurations, i, dur)
// 지지선 업데이트
for i = 0 to array.size(supportLevels) - 1
level = array.get(supportLevels, i)
dur = array.get(supportDurations, i)
// 돌파 시도: 현재 종가가 지지선 아래로 내려가려는 시도
if close < level
dur := minDuration
else
dur := math.min(dur + 1, maxDuration)
array.set(supportDurations, i, dur)
// === 메인 로직 ===
if isHighPivot
newHigh = high[pivotLen]
// 고점 근처 판단 시 저점과의 차이도 고려
nearToSupport = f_isNear(newHigh, supportLevels)
nearToResistance = f_isNear(newHigh, resistanceLevels)
if not nearToResistance and not nearToSupport
f_addResistance(newHigh)
if isLowPivot
newLow = low[pivotLen]
nearToSupport = f_isNear(newLow, supportLevels)
nearToResistance = f_isNear(newLow, resistanceLevels)
if not nearToSupport and not nearToResistance
f_addSupport(newLow)
f_updateDurations()
// === 차트에 표시 ===
for i = 0 to array.size(resistanceLevels) - 1
level = array.get(resistanceLevels, i)
dur = array.get(resistanceDurations, i)
alphaVal = math.round(255 * (dur / maxDuration))
line.new(bar_index - 1, level, bar_index, level, color=color.new(color.red, 255 - alphaVal), width=2)
for i = 0 to array.size(supportLevels) - 1
level = array.get(supportLevels, i)
dur = array.get(supportDurations, i)
alphaVal = math.round(255 * (dur / maxDuration))
line.new(bar_index - 1, level, bar_index, level, color=color.new(color.green, 255 - alphaVal), width=2)
설명
atr를 이용해 변동성 기반으로 근처(near) 개념을 정의합니다.
- 고점/저점이 기존 지지/저항선 근처에 있으면 무시합니다.
- 고점 근처 판단 시 저점 배열도 같이 체크하여 너무 많은 라인이 생기는 것을 방지합니다.
- 지지/저항선의 기간(duration)은 돌파 시도 여부에 따라 증가하거나 초기화됩니다.
- 기간에 따라 라인의 투명도를 조절해 신뢰도를 시각화합니다.
필요에 따라 파라미터 조정 및 기능 확장 가능합니다.
궁금한 점 있으면 알려주세요!