파인스크립트(Pine Script v6)로 지지/저항선의 신뢰도를 시간 경과와 돌파 시도에 따라 평가하는 코드를 작성해드리겠습니다.
//@version=6
indicator("동적 지지/저항선 신뢰도 분석", overlay=true)
// 파라미터 설정
lookbackPeriod = input.int(100, "분석 기간", minval=10)
pivotLengthLeft = input.int(5, "피봇 좌측 길이", minval=1)
pivotLengthRight = input.int(5, "피봇 우측 길이", minval=1)
volatilityPeriod = input.int(20, "변동성 계산 기간", minval=5)
volatilityMultiplier = input.float(1.5, "변동성 배수", minval=0.1, step=0.1)
minConfidenceForDisplay = input.int(3, "표시할 최소 신뢰도", minval=1)
maxLines = input.int(5, "최대 표시 라인 수", minval=1, maxval=10)
// 피봇 고점과 저점 계산
highPivot = ta.pivothigh(high, pivotLengthLeft, pivotLengthRight)
lowPivot = ta.pivotlow(low, pivotLengthLeft, pivotLengthRight)
// 변동성 계산 (ATR 사용)
atr = ta.atr(volatilityPeriod)
volatilityThreshold = atr * volatilityMultiplier
// 지지/저항선 저장을 위한 배열
var resistanceLines = array.new_float(0)
var resistanceTimes = array.new_int(0)
var resistanceConfidence = array.new_int(0)
var resistanceLastTest = array.new_int(0)
var supportLines = array.new_float(0)
var supportTimes = array.new_int(0)
var supportConfidence = array.new_int(0)
var supportLastTest = array.new_int(0)
// 새로운 피봇 고점 발견 시 처리
if not na(highPivot)
newResistance = true
// 기존 저항선과 비교하여 '근처' 여부 확인
for i = 0 to array.size(resistanceLines) - 1
existingLevel = array.get(resistanceLines, i)
priceDiff = math.abs(highPivot - existingLevel)
// 기존 저항선 근처에 있는 경우
if priceDiff <= volatilityThreshold
newResistance := false
// 신뢰도 증가 및 마지막 테스트 시간 업데이트
array.set(resistanceConfidence, i, array.get(resistanceConfidence, i) + 1)
array.set(resistanceLastTest, i, bar_index)
break
// 새로운 저항선 추가
if newResistance
array.push(resistanceLines, highPivot)
array.push(resistanceTimes, bar_index)
array.push(resistanceConfidence, 1)
array.push(resistanceLastTest, bar_index)
// 새로운 피봇 저점 발견 시 처리
if not na(lowPivot)
newSupport = true
// 기존 지지선과 비교하여 '근처' 여부 확인
for i = 0 to array.size(supportLines) - 1
existingLevel = array.get(supportLines, i)
priceDiff = math.abs(lowPivot - existingLevel)
// 기존 지지선 근처에 있는 경우
if priceDiff <= volatilityThreshold
newSupport := false
// 신뢰도 증가 및 마지막 테스트 시간 업데이트
array.set(supportConfidence, i, array.get(supportConfidence, i) + 1)
array.set(supportLastTest, i, bar_index)
break
// 새로운 지지선 추가
if newSupport
array.push(supportLines, lowPivot)
array.push(supportTimes, bar_index)
array.push(supportConfidence, 1)
array.push(supportLastTest, bar_index)
// 돌파 확인 및 신뢰도 조정
for i = array.size(resistanceLines) - 1 downto 0
resistanceLevel = array.get(resistanceLines, i)
// 저항선 돌파 확인
if high > resistanceLevel + volatilityThreshold
array.remove(resistanceLines, i)
array.remove(resistanceTimes, i)
array.remove(resistanceConfidence, i)
array.remove(resistanceLastTest, i)
// 저항선 테스트 확인 (근접했지만 돌파하지 못한 경우)
else if high > resistanceLevel - volatilityThreshold and high < resistanceLevel + volatilityThreshold
array.set(resistanceConfidence, i, array.get(resistanceConfidence, i) + 1)
array.set(resistanceLastTest, i, bar_index)
for i = array.size(supportLines) - 1 downto 0
supportLevel = array.get(supportLines, i)
// 지지선 돌파 확인
if low < supportLevel - volatilityThreshold
array.remove(supportLines, i)
array.remove(supportTimes, i)
array.remove(supportConfidence, i)
array.remove(supportLastTest, i)
// 지지선 테스트 확인 (근접했지만 돌파하지 못한 경우)
else if low < supportLevel + volatilityThreshold and low > supportLevel - volatilityThreshold
array.set(supportConfidence, i, array.get(supportConfidence, i) + 1)
array.set(supportLastTest, i, bar_index)
// 시간 경과에 따른 신뢰도 조정 (오래된 라인일수록 신뢰도 증가)
for i = 0 to array.size(resistanceLines) - 1
timeSinceCreation = bar_index - array.get(resistanceTimes, i)
timeBonus = math.floor(timeSinceCreation / 100) // 100봉마다 신뢰도 +1
currentConfidence = array.get(resistanceConfidence, i)
array.set(resistanceConfidence, i, currentConfidence + timeBonus)
for i = 0 to array.size(supportLines) - 1
timeSinceCreation = bar_index - array.get(supportTimes, i)
timeBonus = math.floor(timeSinceCreation / 100) // 100봉마다 신뢰도 +1
currentConfidence = array.get(supportConfidence, i)
array.set(supportConfidence, i, currentConfidence + timeBonus)
// 신뢰도 기준으로 정렬 및 표시할 라인 선택
sortedResistance = array.new_float(0)
sortedResistanceConf = array.new_int(0)
for i = 0 to array.size(resistanceLines) - 1
level = array.get(resistanceLines, i)
conf = array.get(resistanceConfidence, i)
if conf >= minConfidenceForDisplay
// 정렬된 위치 찾기
insertPos = 0
for j = 0 to array.size(sortedResistanceConf) - 1
if conf < array.get(sortedResistanceConf, j)
insertPos := j + 1
// 삽입
array.insert(sortedResistance, insertPos, level)
array.insert(sortedResistanceConf, insertPos, conf)
// 지지선도 동일하게 처리
sortedSupport = array.new_float(0)
sortedSupportConf = array.new_int(0)
for i = 0 to array.size(supportLines) - 1
level = array.get(supportLines, i)
conf = array.get(supportConfidence, i)
if conf >= minConfidenceForDisplay
// 정렬된 위치 찾기
insertPos = 0
for j = 0 to array.size(sortedSupportConf) - 1
if conf < array.get(sortedSupportConf, j)
insertPos := j + 1
// 삽입
array.insert(sortedSupport, insertPos, level)
array.insert(sortedSupportConf, insertPos, conf)
// 최대 표시 라인 수 제한
resistanceCount = math.min(array.size(sortedResistance), maxLines)
supportCount = math.min(array.size(sortedSupport), maxLines)
// 저항선 표시
for i = 0 to resistanceCount - 1
level = array.get(sortedResistance, i)
conf = array.get(sortedResistanceConf, i)
lineColor = color.new(color.red, 100 - math.min(conf * 10, 90))
line.new(bar_index - lookbackPeriod, level, bar_index, level, color=lineColor, width=1 + math.floor(conf / 3))
label.new(bar_index, level, text="R: " + str.tostring(conf), color=lineColor, style=label.style_label_down, textcolor=color.white)
// 지지선 표시
for i = 0 to supportCount - 1
level = array.get(sortedSupport, i)
conf = array.get(sortedSupportConf, i)
lineColor = color.new(color.green, 100 - math.min(conf * 10, 90))
line.new(bar_index - lookbackPeriod, level, bar_index, level, color=lineColor, width=1 + math.floor(conf / 3))
label.new(bar_index, level, text="S: " + str.tostring(conf), color=lineColor, style=label.style_label_up, textcolor=color.white)
이 코드에 대해 설명이나 추가 수정이 필요하신가요?