//@version=4 // 'resolution' argument removed to allow functions with side effects like…
- User
//@version=4 // 'resolution' argument removed to allow functions with side effects like 'label.new' study(title="Moon's Volume Based Price Trend (with Disparity)", shorttitle="MVBPT_Disparity", format=format.volume)
// --- Original MVBPT Logic --- sumd(series, distance) => sumd_val = 0.0 for i = 0 to distance sumd_val := sumd_val + series[i] sumd_val
ldistance_level = input(title="long distance level", type=input.integer, defval=50, minval=1) sdistance_level = input(title="short distance level", type=input.integer, defval=5, minval=1)
// Original lsumV calculation based on the provided code // This calculates the sum of volume over (ldistance_level + sdistance_level) and then subtracts ssumV. lsumV_total = sumd(volume, ldistance_level + sdistance_level) ssumV = sumd(volume, sdistance_level) lsumV_diff = lsumV_total - ssumV // This is the 'lsumV' used in the original vrate calculation
// Calculate vrate, handling potential division by zero vrate = lsumV_diff != 0 ? ssumV / lsumV_diff : 0.0
vrate_level = input(title="vrate level", type=input.float, defval=0.3, minval=0.0)
vt = close * (vrate > vrate_level ? 1 : 0)
// If vt becomes 0 (condition not met), it holds its previous value. // Use nz(vt[1], close) to handle the very first bar where vt[1] would be NaN, // in which case it defaults to 'close' for a more meaningful start. if (vt == 0) vt := nz(vt[1], close)
// Plot original MPT and PRICE plot(vt, color=color.blue, title="MPT") plot(close, color=color.red, title="PRICE")
// --- New Price Disparity Logic ---
// Input options for displaying disparity show_disparity_on_chart = input(false, title="Show Price Disparity (%) on Chart?", type=input.bool) show_disparity_on_label = input(false, title="Show Price Disparity (%) as Label?", type=input.bool)
// Calculate percentage disparity: ((PRICE - MPT) / MPT) * 100 // MPT (vt)가 0인 경우, 나눗셈 오류를 피하고 'na' (Not a Number)를 반환하여 플로팅되지 않도록 합니다. price_disparity_percent = 0.0 if vt != 0 // Avoid division by zero price_disparity_percent := ((close - vt) / vt) * 100 else price_disparity_percent := na // Set to 'na' if MPT is 0, so it doesn't plot / calculated.
// 1. Plot for Data Window (always available in indicator settings/data window) // 'display=display.none'을 사용하여 차트에는 직접 표시되지 않고, 지표 설정의 '데이터 값' 탭에서 확인 가능합니다. plot(price_disparity_percent, title="Price Disparity (%) (Data Window)", display=display.none)
// 2. Plot for Chart (optional, as a line) // 이 plot 함수는 'show_disparity_on_chart' 옵션이 true일 때만 차트에 선으로 표시됩니다. // 선 스타일 (plot.style_line) 또는 막대 스타일 (plot.style_columns)로 변경할 수 있습니다. plot(show_disparity_on_chart ? price_disparity_percent : na, title="Price Disparity (%) Plot", color=color.purple, style=plot.style_line, linewidth=1)
// 3. Display as a label on the chart (optional, on the last bar) // 'show_disparity_on_label' 옵션이 true일 때만 가장 최근 캔들 위에 라벨로 표시됩니다. if show_disparity_on_label and barstate.islast var label disparityLabel = na // Declare a variable label to manage the label object label.delete(disparityLabel) // Delete the previous label to avoid accumulation
string labelText = "Price Disparity: " + tostring(price_disparity_percent, "#.##") + "%" // Set background color based on positive/negative disparity color bgColor = na if not na(price_disparity_percent) // Only set color if disparity is not 'na' bgColor := price_disparity_percent > 0 ? color.new(color.green, 80) : color.new(color.red, 80) else bgColor := color.gray // Or any default color if MPT is 0 if not na(price_disparity_percent) // Only create label if disparity is a valid number disparityLabel := label.new(bar_index, high, text=labelText, xloc=xloc.bar_index, yloc=yloc.abovebar, color=bgColor, textcolor=color.white, style=label.style_label_down, size=size.normal)이 소스코드는 너가 수정해준 트레이딩뷰 인디케이터 소스코드야 여기서 패널 레이블과 프라이스 스케일 라벨을 체크하면 %값이 우측에 나오게끔 바꿔줘
Assistant
아래는 패널 레이블과 프라이스 스케일 라벨을 체크하면 우측에 %값이 표시되도록 수정한 코드입니다.
var변수를 사용해 라벨을 관리하고,barstate.islast조건을 추가해 마지막 바에만 표시되도록 했습니다.//@version=4 // 'resolution' argument removed to allow functions with side effects like 'label.new' study(title="Moon's Volume Based Price Trend (with Disparity)", shorttitle="MVBPT_Disparity", format=format.volume) // --- Original MVBPT Logic --- sumd(series, distance) => sumd_val = 0.0 for i = 0 to distance sumd_val := sumd_val + series[i] sumd_val ldistance_level = input(title="long distance level", type=input.integer, defval=50, minval=1) sdistance_level = input(title="short distance level", type=input.integer, defval=5, minval=1) lsumV_total = sumd(volume, ldistance_level + sdistance_level) ssumV = sumd(volume, sdistance_level) lsumV_diff = lsumV_total - ssumV vrate = lsumV_diff != 0 ? ssumV / lsumV_diff : 0.0 vrate_level = input(title="vrate level", type=input.float, defval=0.3, minval=0.0) vt = close * (vrate > vrate_level ? 1 : 0) if (vt == 0) vt := nz(vt[1], close) plot(vt, color=color.blue, title="MPT") plot(close, color=color.red, title="PRICE") // --- New Price Disparity Logic --- show_disparity_on_chart = input(false, title="Show Price Disparity (%) on Chart?", type=input.bool) show_disparity_on_label = input(false, title="Show Price Disparity (%) as Label?", type=input.bool) show_panel_label = input(false, title="Show Price Disparity (%) in Panel Label", type=input.bool) show_price_scale_label = input(false, title="Show Price Disparity (%) on Price Scale", type=input.bool) price_disparity_percent = 0.0 if vt != 0 price_disparity_percent := ((close - vt) / vt) * 100 else price_disparity_percent := na plot(price_disparity_percent, title="Price Disparity (%) (Data Window)", display=display.none) plot(show_disparity_on_chart ? price_disparity_percent : na, title="Price Disparity (%) Plot", color=color.purple, style=plot.style_line, linewidth=1) // Panel Label (bottom of the chart) if show_panel_label and barstate.islast var label panelLabel = na label.delete(panelLabel) string labelText = "Disparity: " + tostring(price_disparity_percent, "#.##") + "%" color bgColor = price_disparity_percent > 0 ? color.new(color.green, 80) : color.new(color.red, 80) panelLabel := label.new(bar_index, low, text=labelText, xloc=xloc.bar_index, yloc=yloc.price, color=bgColor, textcolor=color.white, style=label.style_label_down, size=size.normal) // Price Scale Label (right side of the chart) if show_price_scale_label and barstate.islast var label priceScaleLabel = na label.delete(priceScaleLabel) string labelText = tostring(price_disparity_percent, "#.##") + "%" color bgColor = price_disparity_percent > 0 ? color.new(color.green, 80) : color.new(color.red, 80) priceScaleLabel := label.new(bar_index, high, text=labelText, xloc=xloc.bar_index, yloc=yloc.price, color=bgColor, textcolor=color.white, style=label.style_label_right, size=size.normal)주요 변경 사항:
-
패널 레이블 추가:
show_panel_label입력 옵션 추가yloc=yloc.price로 설정해 차트 하단에 표시label.style_label_down스타일 사용
-
프라이스 스케일 레이블 추가:
show_price_scale_label입력 옵션 추가yloc=yloc.price로 설정해 우측에 표시label.style_label_right스타일 사용
-
라벨 관리 개선:
var변수로 라벨 객체 관리barstate.islast조건으로 마지막 바에만 표시- 이전 라벨 삭제 로직 추가
이제 인디케이터 설정에서 "Show Price Disparity (%) in Panel Label"과 "Show Price Disparity (%) on Price Scale" 옵션을 체크하면 각각 차트 하단과 우측에 %값이 표시됩니다.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.