//@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)

이 소스코드는 너가 수정해준 트레이딩뷰 인디케이터 소스코드야 
여기서 패널 레이블과 프라이스 스케일 라벨을 체크하면 %값이 우측에 나오게끔 바꿔줘