제시하신 아이디어를 구현한 Pine Script 코드를 작성해드리겠습니다. 이 코드는 동적 지지/저항선 관리와 변동성 기반 필터링을 포함합니다.
pinescript
//@version=5
indicator("Dynamic Support/Resistance", overlay=true, max_lines_count=500)
// 입력 매개변수
lookback = input.int(20, "Pivot Lookback", minval=5, maxval=50)
max_levels = input.int(10, "Maximum S/R Levels", minval=3, maxval=20)
volatility_period = input.int(14, "Volatility Period", minval=5, maxval=50)
volatility_multiplier = input.float(2.0, "Volatility Multiplier", minval=0.5, maxval=5.0)
min_touches = input.int(2, "Minimum Touches", minval=2, maxval=10)
extend_period = input.int(50, "Extension Period", minval=10, maxval=200)
// 변동성 계산 (ATR 기반)
atr_value = ta.atr(volatility_period)
volatility_threshold = atr_value * volatility_multiplier
// 피벗 포인트 감지
pivot_high = ta.pivothigh(high, lookback, lookback)
pivot_low = ta.pivotlow(low, lookback, lookback)
// 지지/저항 레벨을 저장할 배열
var array<float> support_levels = array.new<float>()
var array<int> support_touches = array.new<int>()
var array<int> support_bars = array.new<int>()
var array<line> support_lines = array.new<line>()
var array<float> resistance_levels = array.new<float>()
var array<int> resistance_touches = array.new<int>()
var array<int> resistance_bars = array.new<int>()
var array<line> resistance_lines = array.new<line>()
// 레벨이 기존 레벨과 너무 가까운지 확인하는 함수
is_too_close(level, levels, threshold) =>
result = false
if array.size(levels) > 0
for i = 0 to array.size(levels) - 1
existing_level = array.get(levels, i)
if math.abs(level - existing_level) < threshold
result := true
break
result
// 레벨 근처에서 터치 확인하는 함수
check_touch(price, level, threshold) =>
math.abs(price - level) <= threshold
// 지지/저항 레벨 추가 함수
add_support_level(level) =>
if not is_too_close(level, support_levels, volatility_threshold)
if array.size(support_levels) >= max_levels
// 가장 오래된 레벨 제거
oldest_index = 0
oldest_bar = array.get(support_bars, 0)
for i = 1 to array.size(support_bars) - 1
if array.get(support_bars, i) < oldest_bar
oldest_bar := array.get(support_bars, i)
oldest_index := i
array.remove(support_levels, oldest_index)
array.remove(support_touches, oldest_index)
array.remove(support_bars, oldest_index)
line.delete(array.get(support_lines, oldest_index))
array.remove(support_lines, oldest_index)
array.push(support_levels, level)
array.push(support_touches, 1)
array.push(support_bars, bar_index)
new_line = line.new(bar_index, level, bar_index + extend_period, level,
color=color.green, width=1, style=line.style_solid)
array.push(support_lines, new_line)
add_resistance_level(level) =>
if not is_too_close(level, resistance_levels, volatility_threshold)
if array.size(resistance_levels) >= max_levels
// 가장 오래된 레벨 제거
oldest_index = 0
oldest_bar = array.get(resistance_bars, 0)
for i = 1 to array.size(resistance_bars) - 1
if array.get(resistance_bars, i) < oldest_bar
oldest_bar := array.get(resistance_bars, i)
oldest_index := i
array.remove(resistance_levels, oldest_index)
array.remove(resistance_touches, oldest_index)
array.remove(resistance_bars, oldest_index)
line.delete(array.get(resistance_lines, oldest_index))
array.remove(resistance_lines, oldest_index)
array.push(resistance_levels, level)
array.push(resistance_touches, 1)
array.push(resistance_bars, bar_index)
new_line = line.new(bar_index, level, bar_index + extend_period, level,
color=color.red, width=1, style=line.style_solid)
array.push(resistance_lines, new_line)
// 새로운 피벗이 감지되면 레벨 추가
if not na(pivot_high)
add_resistance_level(pivot_high)
if not na(pivot_low)
add_support_level(pivot_low)
// 기존 레벨들의 터치 횟수 업데이트 및 라인 연장
update_levels() =>
// 지지선 업데이트
if array.size(support_levels) > 0
for i = 0 to array.size(support_levels) - 1
level = array.get(support_levels, i)
touches = array.get(support_touches, i)
level_bar = array.get(support_bars, i)
current_line = array.get(support_lines, i)
// 터치 확인
if check_touch(low, level, volatility_threshold)
array.set(support_touches, i, touches + 1)
// 돌파 확인
if close < level - volatility_threshold
// 돌파되면 레벨 제거
array.remove(support_levels, i)
array.remove(support_touches, i)
array.remove(support_bars, i)
line.delete(current_line)
array.remove(support_lines, i)
break
else
// 라인 연장 (시간이 지날수록 신뢰도 증가)
time_factor = math.min((bar_index - level_bar) / 100, 2.0)
line_width = math.max(1, math.round(touches * time_factor))
line.set_x2(current_line, bar_index + extend_period)
line.set_width(current_line, line_width)
// 저항선 업데이트
if array.size(resistance_levels) > 0
for i = 0 to array.size(resistance_levels) - 1
level = array.get(resistance_levels, i)
touches = array.get(resistance_touches, i)
level_bar = array.get(resistance_bars, i)
current_line = array.get(resistance_lines, i)
// 터치 확인
if check_touch(high, level, volatility_threshold)
array.set(resistance_touches, i, touches + 1)
// 돌파 확인
if close > level + volatility_threshold
// 돌파되면 레벨 제거
array.remove(resistance_levels, i)
array.remove(resistance_touches, i)
array.remove(resistance_bars, i)
line.delete(current_line)
array.remove(resistance_lines, i)
break
else
// 라인 연장 (시간이 지날수록 신뢰도 증가)
time_factor = math.min((bar_index - level_bar) / 100, 2.0)
line_width = math.max(1, math.round(touches * time_factor))
line.set_x2(current_line, bar_index + extend_period)
line.set_width(current_line, line_width)
// 매 바마다 레벨 업데이트
if barstate.isconfirmed
update_levels()
// 정보 테이블 표시
if barstate.islast
var table info_table = table.new(position.top_right, 3, 10, bgcolor=color.white, border_width=1)
table.cell(info_table, 0, 0, "Level", text_color=color.black, bgcolor=color.gray)
table.cell(info_table, 1, 0, "Price", text_color=color.black, bgcolor=color.gray)
table.cell(info_table, 2, 0, "Touches", text_color=color.black, bgcolor=color.gray)
row = 1
// 저항선 정보
if array.size(resistance_levels) > 0
for i = 0 to math.min(array.size(resistance_levels) - 1, 4)
level = array.get(resistance_levels, i)
touches = array.get(resistance_touches, i)
table.cell(info_table, 0, row, "R" + str.tostring(i+1), text_color=color.red)
table.cell(info_table, 1, row, str.tostring(level, "#.##"), text_color=color.black)
table.cell(info_table, 2, row, str.tostring(touches), text_color=color.black)
row := row + 1
// 지지선 정보
if array.size(support_levels) > 0
for i = 0 to math.min(array.size(support_levels) - 1, 4)
level = array.get(support_levels, i)
touches = array.get(support_touches, i)
table.cell(info_table, 0, row, "S" + str.tostring(i+1), text_color=color.green)
table.cell(info_table, 1, row, str.tostring(level, "#.##"), text_color=color.black)
table.cell(info_table, 2, row, str.tostring(touches), text_color=color.black)
row := row + 1
// 현재 변동성 임계값 표시
plot(volatility_threshold, "Volatility Threshold", color=color.yellow, display=display.data_window)
매개변수를 조정하여 다양한 시장 조건과 거래 스타일에 맞게 최적화할 수 있습니다.