Volume Profile & LVN Zones

Jan80Kenneth · study · 242 行 · 点赞 571 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// © jan80hansen
//@version=6
indicator("Volume Profile & LVN Zones", overlay=true, max_boxes_count=500, max_lines_count=100)
 
// -----------------------------------------------------------------------------
// 1. INTERACTIVE INPUTS (CLICK & DRAG)
// -----------------------------------------------------------------------------
gp_time = "Select Area on Chart (Click & Drag)"
start_time_input = input.time(timestamp("2026-01-01 00:00"), title="Start Point", confirm=true, group=gp_time)
end_time_input   = input.time(timestamp("2026-01-02 00:00"), title="End Point", confirm=true, group=gp_time)
 
gp_set = "Profile Settings"
bin_count   = input.int(40, title="Number of Rows", minval=10, maxval=150, group=gp_set, tooltip="Lower number = thicker bars.")
hist_width  = input.int(30, title="Histogram Width (%)", minval=5, maxval=100, group=gp_set)
row_spacing = input.int(15, title="Row Spacing Margin (%)", minval=0, maxval=40, group=gp_set, tooltip="Creates the visual gap between bars.")
 
gp_va = "Value Area Settings"
show_va     = input.bool(true, title="Show Value Area (VAH / VAL)", group=gp_va)
va_pct      = input.float(70.0, title="Value Area Volume %", minval=10, maxval=100, group=gp_va)
 
gp_lvn = "LVN Settings"
show_lvn      = input.bool(true, title="Highlight Low Volume Nodes (LVN)", group=gp_lvn)
lvn_threshold = input.int(15, title="LVN Threshold (% of POC)", minval=1, maxval=100, group=gp_lvn)
 
gp_col = "Colors"
col_buy  = input.color(color.new(#00e676, 40), title="Buy Volume", group=gp_col)
col_sell = input.color(color.new(#ff1744, 40), title="Sell Volume", group=gp_col)
col_poc  = input.color(color.new(color.yellow, 0), title="POC Line", group=gp_col)
col_va   = input.color(color.new(color.blue, 0), title="VAH / VAL Lines", group=gp_col)
col_lvn  = input.color(color.new(color.gray, 85), title="LVN Zone Background", group=gp_col)
 
// -----------------------------------------------------------------------------
// 2. TRUE VOLUME ARRAYS
// -----------------------------------------------------------------------------
is_buy   = close > open
is_sell  = close < open
vol_buy  = is_buy ? volume : (close == open ? volume / 2 : 0)
vol_sell = is_sell ? volume : (close == open ? volume / 2 : 0)
 
var float[] range_h      = array.new_float()
var float[] range_l      = array.new_float()
var float[] range_v_buy  = array.new_float()
var float[] range_v_sell = array.new_float()
 
var float zone_high = na
var float zone_low  = na
var int   start_bar_index = na
var int   end_bar_index   = na
 
bool in_range = time >= start_time_input and time <= end_time_input
 
if in_range
    array.push(range_h, high)
    array.push(range_l, low)
    array.push(range_v_buy, vol_buy)
    array.push(range_v_sell, vol_sell)
    
    zone_high := na(zone_high) ? high : math.max(zone_high, high)
    zone_low  := na(zone_low) ? low : math.min(zone_low, low)
    start_bar_index := na(start_bar_index) ? bar_index : start_bar_index
    end_bar_index   := bar_index
 
var box[] hist_boxes = array.new_box()
var box[] lvn_boxes  = array.new_box()
var line  poc_line   = na
var label poc_label  = na
var line  vah_line   = na
var label vah_label  = na
var line  val_line   = na
var label val_label  = na
 
// -----------------------------------------------------------------------------
// 3. GENERATE PROFILE & LVN ON LAST BAR
// -----------------------------------------------------------------------------
if barstate.islast
    if array.size(hist_boxes) > 0
        for i = 0 to array.size(hist_boxes) - 1
            box.delete(array.get(hist_boxes, i))
        array.clear(hist_boxes)
    if array.size(lvn_boxes) > 0
        for i = 0 to array.size(lvn_boxes) - 1
            box.delete(array.get(lvn_boxes, i))
        array.clear(lvn_boxes)
    if not na(poc_line)
        line.delete(poc_line)
    if not na(poc_label)
        label.delete(poc_label)
    if not na(vah_line)
        line.delete(vah_line)
    if not na(vah_label)
        label.delete(vah_label)
    if not na(val_line)
        line.delete(val_line)
    if not na(val_label)
        label.delete(val_label)
 
    int arr_size = array.size(range_h)
 
    if arr_size > 0 and not na(zone_high) and not na(zone_low)
        a_buy  = array.new_float(bin_count, 0)
        a_sell = array.new_float(bin_count, 0)
        a_tot  = array.new_float(bin_count, 0)
        
        float bin_size = (zone_high - zone_low) / bin_count
        if bin_size == 0
            bin_size := syminfo.mintick
            
        float max_vol_in_any_bin = 0.0
        float total_profile_vol  = 0.0
        
        for i = 0 to arr_size - 1
            float c_h = array.get(range_h, i)
            float c_l = array.get(range_l, i)
            float v_b = array.get(range_v_buy, i)
            float v_s = array.get(range_v_sell, i)
            
            if c_h == c_l
                c_h := c_h + syminfo.mintick
            
            int idx_bot = math.floor((c_l - zone_low) / bin_size)
            int idx_top = math.floor((c_h - zone_low) / bin_size)
            idx_bot := math.max(0, math.min(bin_count - 1, idx_bot))
            idx_top := math.max(0, math.min(bin_count - 1, idx_top))
            
            int span = (idx_top - idx_bot) + 1
            float vb_per_bin = v_b / span
            float vs_per_bin = v_s / span
            
            for k = idx_bot to idx_top
                array.set(a_buy, k, array.get(a_buy, k) + vb_per_bin)
                array.set(a_sell, k, array.get(a_sell, k) + vs_per_bin)
                
        float poc_vol = 0.0
        int   poc_idx = 0
        for j = 0 to bin_count - 1
            float tot_bin_vol = array.get(a_buy, j) + array.get(a_sell, j)
            array.set(a_tot, j, tot_bin_vol)
            total_profile_vol += tot_bin_vol
            
            if tot_bin_vol > max_vol_in_any_bin
                max_vol_in_any_bin := tot_bin_vol
                
            if tot_bin_vol > poc_vol
                poc_vol := tot_bin_vol
                poc_idx := j
 
        // 4. VALUE AREA ALGORITHM
        float target_va_vol = total_profile_vol * (va_pct / 100.0)
        float current_va_vol = array.get(a_tot, poc_idx)
        int high_idx = poc_idx
        int low_idx  = poc_idx
 
        while current_va_vol < target_va_vol
            float vol_above = (high_idx < bin_count - 1) ? array.get(a_tot, high_idx + 1) : -1.0
            float vol_below = (low_idx > 0) ? array.get(a_tot, low_idx - 1) : -1.0
 
            if vol_above == -1.0 and vol_below == -1.0
                break 
 
            if vol_above >= vol_below
                current_va_vol += vol_above
                high_idx += 1
            else
                current_va_vol += vol_below
                low_idx -= 1
 
        // 5. TEGN LVN SONER (Filtrert for støy)
        if show_lvn
            float lvn_limit = max_vol_in_any_bin * (lvn_threshold / 100.0)
            bool  in_lvn = false
            float current_lvn_bot = na
            float current_lvn_top = na
            
            for j = 0 to bin_count - 1
                float bin_vol = array.get(a_tot, j)
                
                bool is_lvn = bin_vol <= lvn_limit
                
                if is_lvn
                    if not in_lvn
                        in_lvn := true
                        current_lvn_bot := zone_low + (j * bin_size)
                    current_lvn_top := zone_low + ((j + 1) * bin_size)
                else
                    if in_lvn
                        box lvn_b = box.new(left=start_bar_index, top=current_lvn_top, right=end_bar_index, bottom=current_lvn_bot, bgcolor=col_lvn, border_width=0)
                        array.push(lvn_boxes, lvn_b)
                        in_lvn := false
            
            if in_lvn
                box lvn_b = box.new(left=start_bar_index, top=current_lvn_top, right=end_bar_index, bottom=current_lvn_bot, bgcolor=col_lvn, border_width=0)
                array.push(lvn_boxes, lvn_b)
 
        // 6. TEGN DET VISUELLE HISTOGRAMMET MED MARGIN (SPACING)
        int bars_in_range = end_bar_index - start_bar_index
        float width_multiplier = (bars_in_range * (hist_width / 100.0)) / (max_vol_in_any_bin == 0 ? 1 : max_vol_in_any_bin)
        
        float margin = bin_size * (row_spacing / 100.0)
 
        for j = 0 to bin_count - 1
            float b_vol = array.get(a_buy, j)
            float s_vol = array.get(a_sell, j)
            
            if (b_vol + s_vol) > 0
                float bin_bottom = zone_low + (j * bin_size)
                float bin_top    = zone_low + ((j + 1) * bin_size)
                
                float box_top = bin_top - margin
                float box_bot = bin_bottom + margin
                
                if box_top <= box_bot
                    box_top := bin_top
                    box_bot := bin_bottom
                
                int end_buy_box = start_bar_index + math.round(b_vol * width_multiplier)
                int end_sell_box = end_buy_box + math.round(s_vol * width_multiplier)
                
                box bb = box.new(left=start_bar_index, top=box_top, right=end_buy_box, bottom=box_bot, bgcolor=col_buy, border_width=0)
                array.push(hist_boxes, bb)
                
                box bs = box.new(left=end_buy_box, top=box_top, right=end_sell_box, bottom=box_bot, bgcolor=col_sell, border_width=0)
                array.push(hist_boxes, bs)
 
        // 7. TEGN POC, VAH OG VAL
        float poc_price = zone_low + (poc_idx * bin_size) + (bin_size / 2)
        poc_line := line.new(x1=start_bar_index, y1=poc_price, x2=end_bar_index, y2=poc_price, color=col_poc, width=2, style=line.style_solid)
        string poc_txt = "POC: " + str.tostring(poc_price, format.mintick)
        poc_label := label.new(x=end_bar_index, y=poc_price, text=poc_txt, color=color(na), textcolor=col_poc, style=label.style_label_left, size=size.small)
 
        if show_va
            float vah_price = zone_low + (high_idx * bin_size) + bin_size
            float val_price = zone_low + (low_idx * bin_size)
            
            vah_line := line.new(x1=start_bar_index, y1=vah_price, x2=end_bar_index, y2=vah_price, color=col_va, width=1, style=line.style_dashed)
            string vah_txt = "VAH: " + str.tostring(vah_price, format.mintick)
            vah_label := label.new(x=end_bar_index, y=vah_price, text=vah_txt, color=color(na), textcolor=col_va, style=label.style_label_left, size=size.small)
            
            val_line := line.new(x1=start_bar_index, y1=val_price, x2=end_bar_index, y2=val_price, color=col_va, width=1, style=line.style_dashed)
            string val_txt = "VAL: " + str.tostring(val_price, format.mintick)
            val_label := label.new(x=end_bar_index, y=val_price, text=val_txt, color=color(na), textcolor=col_va, style=label.style_label_left, size=size.small)
 
plot(close, title="Reference", display=display.none)

← 返回列表