오랬동안 특정 지지/저항선을 돌파하지 못했다면 그 지지 저항선의 신뢰도는 높다. 따라서 시간의 흐름에 따라 돌파 또는 돌파시도 여부에 따라 특정 지지/저항선의…
- User
오랬동안 특정 지지/저항선을 돌파하지 못했다면 그 지지 저항선의 신뢰도는 높다. 따라서 시간의 흐름에 따라 돌파 또는 돌파시도 여부에 따라 특정 지지/저항선의 기간을 유동적으로 늘려야한다.
또한 이미 형성된 지지/저항선 근처에 새로운(지지보다 높고 저항보다 낮은)저점/고점이 생길 경우에는 무시해야 한다. 무시 안할 경우 지나치게 많은 지지/저항선이 생긴다. 즉 이때 '근처'라는 개념을 변동성을 통해서 도출해야 한다.
이때 근처의 개념을 확정짓기 위해서는 고점의 경우 고점만 판단하면 안되고 저점과 저점과 고점과의 차이도 고려해야 한다.
위의 아이디어를 실현시켜주는 파인스크립트 코드를 작성해줘(version 6 기준)
- Assistant
말씀하신 아이디어를 구현한 Pine Script v6 코드입니다:
//@version=6 indicator("동적 지지/저항선", overlay=true, max_lines_count=500) // 입력 파라미터 lookback = input.int(50, "피벗 확인 기간", minval=5) atr_period = input.int(14, "ATR 기간") atr_multiplier = input.float(0.5, "근처 판단 ATR 배수", step=0.1) min_touches = input.int(2, "최소 터치 횟수") touch_multiplier = input.float(0.3, "터치 판단 ATR 배수", step=0.1) max_lines = input.int(10, "최대 표시 라인 수") // ATR 계산 atr = ta.atr(atr_period) // 피벗 포인트 찾기 pivotHigh = ta.pivothigh(high, lookback, lookback) pivotLow = ta.pivotlow(low, lookback, lookback) // 지지/저항선 저장을 위한 배열 var array<float> resistance_levels = array.new<float>() var array<int> resistance_touches = array.new<int>() var array<int> resistance_start = array.new<int>() var array<line> resistance_lines = array.new<line>() var array<float> support_levels = array.new<float>() var array<int> support_touches = array.new<int>() var array<int> support_start = array.new<int>() var array<line> support_lines = array.new<line>() // 근처 레벨 확인 함수 isNearLevel(price, levels, threshold) => near = false for i = 0 to array.size(levels) - 1 if math.abs(price - array.get(levels, i)) < threshold near = true break near // 레벨 업데이트 함수 updateLevel(price, levels, touches, starts, isResistance) => threshold = atr * atr_multiplier touch_threshold = atr * touch_multiplier updated = false // 기존 레벨 확인 및 업데이트 for i = 0 to array.size(levels) - 1 level = array.get(levels, i) if math.abs(price - level) < touch_threshold // 터치 횟수 증가 array.set(touches, i, array.get(touches, i) + 1) updated = true break // 새로운 레벨 추가 (근처에 기존 레벨이 없을 때만) if not updated and not isNearLevel(price, levels, threshold) // 고점의 경우 저점과의 차이도 고려 if isResistance recent_low = ta.lowest(low, lookback) price_range = price - recent_low if price_range > atr * 2 // 충분한 가격 차이가 있을 때만 추가 array.push(levels, price) array.push(touches, 1) array.push(starts, bar_index) else recent_high = ta.highest(high, lookback) price_range = recent_high - price if price_range > atr * 2 // 충분한 가격 차이가 있을 때만 추가 array.push(levels, price) array.push(touches, 1) array.push(starts, bar_index) // 피벗 포인트 발견 시 레벨 업데이트 if not na(pivotHigh) updateLevel(pivotHigh, resistance_levels, resistance_touches, resistance_start, true) if not na(pivotLow) updateLevel(pivotLow, support_levels, support_touches, support_start, false) // 현재 가격이 레벨 근처에 있는지 확인하고 터치 횟수 업데이트 touch_threshold = atr * touch_multiplier // 저항선 터치 확인 for i = 0 to array.size(resistance_levels) - 1 if array.size(resistance_levels) > i level = array.get(resistance_levels, i) if high >= level - touch_threshold and high <= level + touch_threshold array.set(resistance_touches, i, array.get(resistance_touches, i) + 1) // 지지선 터치 확인 for i = 0 to array.size(support_levels) - 1 if array.size(support_levels) > i level = array.get(support_levels, i) if low <= level + touch_threshold and low >= level - touch_threshold array.set(support_touches, i, array.get(support_touches, i) + 1) // 라인 그리기 함수 drawLines() => // 기존 라인 삭제 for line in resistance_lines line.delete(line) for line in support_lines line.delete(line) array.clear(resistance_lines) array.clear(support_lines) // 저항선 그리기 (터치 횟수가 많은 순으로 정렬) if array.size(resistance_levels) > 0 sorted_indices = array.new<int>() for i = 0 to array.size(resistance_levels) - 1 array.push(sorted_indices, i) // 터치 횟수로 정렬 for i = 0 to array.size(sorted_indices) - 2 for j = 0 to array.size(sorted_indices) - i - 2 idx1 = array.get(sorted_indices, j) idx2 = array.get(sorted_indices, j + 1) if array.get(resistance_touches, idx1) < array.get(resistance_touches, idx2) array.set(sorted_indices, j, idx2) array.set(sorted_indices, j + 1, idx1) // 상위 라인만 그리기 for i = 0 to math.min(max_lines, array.size(sorted_indices)) - 1 idx = array.get(sorted_indices, i) if array.get(resistance_touches, idx) >= min_touches level = array.get(resistance_levels, idx) touches = array.get(resistance_touches, idx) start = array.get(resistance_start, idx) // 시간에 따른 신뢰도 (라인 굵기로 표현) time_factor = (bar_index - start) / 100 width = math.min(4, 1 + touches / 5 + time_factor) l = line.new(start, level, bar_index, level, color=color.red, width=int(width), style=line.style_solid) array.push(resistance_lines, l) // 지지선 그리기 if array.size(support_levels) > 0 sorted_indices = array.new<int>() for i = 0 to array.size(support_levels) - 1 array.push(sorted_indices, i) // 터치 횟수로 정렬 for i = 0 to array.size(sorted_indices) - 2 for j = 0 to array.size(sorted_indices) - i - 2 idx1 = array.get(sorted_indices, j) idx2 = array.get(sorted_indices, j + 1) if array.get(support_touches, idx1) < array.get(support_touches, idx2) array.set(sorted_indices, j, idx2) array.set(sorted_indices, j + 1, idx1) // 상위 라인만 그리기 for i = 0 to math.min(max_lines, array.size(sorted_indices)) - 1 idx = array.get(sorted_indices, i) if array.get(support_touches, idx) >= min_touches level = array.get(support_levels, idx) touches = array.get(support_touches, idx) start = array.get(support_start, idx) // 시간에 따른 신뢰도 (라인 굵기로 표현) time_factor = (bar_index - start) / 100 width = math.min(4, 1 + touches / 5 + time_factor) l = line.new(start, level, bar_index, level, color=color.green, width=int(width), style=line.style_solid) array.push(support_lines, l) // 매 바마다 라인 업데이트 if barstate.islast drawLines() // 현재 레벨 정보 표시 if array.size(resistance_levels) > 0 strongest_res_idx = 0 max_touches = 0 for i = 0 to array.size(resistance_levels) - 1 if array.get(resistance_touches, i) > max_touches max_touches = array.get(resistance_touches, i) strongest_res_idx = i if max_touches >= min_touches label.new(bar_index, array.get(resistance_levels, strongest_res_idx), "R: " + str.tostring(array.get(resistance_levels, strongest_res_idx), "#.##") + " (" + str.tostring(max_touches) + ")", style=label.style_label_down, color=color.red, textcolor=color.white) if array.size(support_levels) > 0 strongest_sup_idx = 0 max_touches = 0 for i = 0 to array.size(support_levels) - 1 if array.get(support_touches, i) > max_touches max_touches = array.get(support_touches, i) strongest_sup_idx = i if max_touches >= min_touches label.new(bar_index, array.get(support_levels, strongest_sup_idx), "S: " + str.tostring(array.get(support_levels, strongest_sup_idx), "#.##") + " (" + str.tostring(max_touches) + ")", style=label.style_label_up, color=color.green, textcolor=color.white)이 코드의 주요 특징:
-
동적 지지/저항선 기간: 터치 횟수와 시간 경과에 따라 라인의 굵기가 증가하여 신뢰도를 시각적으로 표현
-
근처 레벨 무시: ATR 기반으로 '근처' 개념을 정의하여 너무 가까운 새로운 피벗은 무시
-
고점/저점 차이 고려: 고점 저항선 추가 시 최근 저점과의 차이를, 저점 지지선 추가 시 최근 고점과의 차이를 확인
-
변동성 기반 판단: ATR을 사용하여 시장 변동성에 따라 '근처' 거리를 동적으로 조정
-
터치 횟수 추적: 각 레벨이 얼마나 자주 테스트되었는지 추적하여 신뢰도 평가
-
시각적 표현: 가장 강한 지지/저항선을 라벨로 표시하고, 터치 횟수와 시간에 따라 라인 굵기 조정
파라미터를 조정하여 원하는 민감도로 설정할 수 있습니다.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.