Fair value indicator

Jan80Kenneth · study · 113 行 · 点赞 10 · TradingView 原页

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

Pine Script

// © jan80hansen
//@version=6
indicator("Fair value indicator", overlay=true)
 
// --- 1. SETTINGS ---
g_dash = "Dashboard Options"
show_dash = input.bool(true, title="Show Volume Dashboard", group=g_dash)
 
g_vol = "ICL Volume Filter Settings"
use_vol_filter = input.bool(true, title="Enable Volume Filter (Institutional Only)", group=g_vol)
// Fjernet 'step' for å gjøre inputen helt åpen og fleksibel for brukeren
vol_mult       = input.float(1.3, title="Volume Strength Multiplier", group=g_vol, tooltip="Filters out retail noise. Only sums volume from bars that exceed average volume multiplied by this factor.")
vol_length     = input.int(20, title="Volume SMA Period", minval=1, group=g_vol)
 
g_look1 = "Equilibrium Period 1"
useEq1     = input.bool(true, title="Enable Period 1", group=g_look1)
lookback1  = input.int(24, title="Lookback Period 1", minval=1, group=g_look1)
 
g_look2 = "Equilibrium Period 2"
useEq2     = input.bool(false, title="Enable Period 2", group=g_look2)
lookback2  = input.int(48, title="Lookback Period 2", minval=1, group=g_look2)
 
g_look3 = "Equilibrium Period 3"
useEq3     = input.bool(false, title="Enable Period 3", group=g_look3)
lookback3  = input.int(120, title="Lookback Period 3", minval=1, group=g_look3)
 
// Format numbers for clean display (e.g., 1.2M, 450K)
format_vol(float v) =>
    if v >= 1000000
        str.format("{0,number,#.##}M", v / 1000000)
    else if v >= 1000
        str.format("{0,number,#.##}K", v / 1000)
    else
        str.format("{0,number,#}", v)
 
// --- 2. CALCULATION ENGINE FUNCTION ---
calcEquilibriumAndVolume(bool enabled, int lookbackBars, bool filterOn, float multiplier, int smaLen) =>
    float eqMid = na
    float sumBuyVol = 0.0
    float sumSellVol = 0.0
    
    if enabled
        hi = ta.highest(high, lookbackBars)
        lo = ta.lowest(low, lookbackBars)
        eqMid := (hi + lo) / 2
        
        volSma = ta.sma(volume, smaLen)
        
        for i = 0 to lookbackBars - 1
            bool is_strong_volume = not filterOn or (volume[i] > volSma[i] * multiplier)
            
            if is_strong_volume
                if close[i] > open[i]
                    sumBuyVol += volume[i]
                if close[i] < open[i]
                    sumSellVol += volume[i]
                
    [eqMid, sumBuyVol, sumSellVol]
 
// Gather data for all 3 periods
[eq1, buyVol1, sellVol1] = calcEquilibriumAndVolume(useEq1, lookback1, use_vol_filter, vol_mult, vol_length)
[eq2, buyVol2, sellVol2] = calcEquilibriumAndVolume(useEq2, lookback2, use_vol_filter, vol_mult, vol_length)
[eq3, buyVol3, sellVol3] = calcEquilibriumAndVolume(useEq3, lookback3, use_vol_filter, vol_mult, vol_length)
 
// --- 3. PLOT THE EQUILIBRIUM LINES ---
plot(eq1, title="Equilibrium Line 1", color=color.new(color.green, 50), linewidth=1)
plot(eq2, title="Equilibrium Line 2", color=color.new(color.yellow, 50), linewidth=1)
plot(eq3, title="Equilibrium Line 3", color=color.new(color.red, 50), linewidth=1)
 
// --- 4. ELEGANT VOLUME DASHBOARD ---
var table tabStats = table.new(position.top_right, 3, 4, bgcolor=color.new(#131722, 10), border_width=1, border_color=color.new(#363a45, 50))
 
if barstate.islast
    if show_dash
        string buy_header  = use_vol_filter ? "FILT. BUY" : "RAW BUY"
        string sell_header = use_vol_filter ? "FILT. SELL" : "RAW SELL"
        
        // Headers
        table.cell(tabStats, 0, 0, "PERIOD", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
        table.cell(tabStats, 1, 0, buy_header, text_color=color.gray, text_size=size.small, text_halign=text.align_right)
        table.cell(tabStats, 2, 0, sell_header, text_color=color.gray, text_size=size.small, text_halign=text.align_right)
        
        // Row 1
        if useEq1
            table.cell(tabStats, 0, 1, "Period 1 (" + str.tostring(lookback1) + ")", text_color=color.white, text_size=size.small, text_halign=text.align_left)
            table.cell(tabStats, 1, 1, format_vol(buyVol1), text_color=color.new(#00e676, 0), text_size=size.small, text_halign=text.align_right)
            table.cell(tabStats, 2, 1, format_vol(sellVol1), text_color=color.new(#ff1744, 0), text_size=size.small, text_halign=text.align_right)
        else
            table.cell(tabStats, 0, 1, "Period 1", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
            table.cell(tabStats, 1, 1, "-", text_color=color.gray, text_size=size.small, text_halign=text.align_right)
            table.cell(tabStats, 2, 1, "-", text_color=color.gray, text_size=size.small, text_halign=text.align_right)
            
        // Row 2
        if useEq2
            table.cell(tabStats, 0, 2, "Period 2 (" + str.tostring(lookback2) + ")", text_color=color.white, text_size=size.small, text_halign=text.align_left)
            table.cell(tabStats, 1, 2, format_vol(buyVol2), text_color=color.new(#00e676, 0), text_size=size.small, text_halign=text.align_right)
            table.cell(tabStats, 2, 2, format_vol(sellVol2), text_color=color.new(#ff1744, 0), text_size=size.small, text_halign=text.align_right)
        else
            table.cell(tabStats, 0, 2, "Period 2", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
            table.cell(tabStats, 1, 2, "-", text_color=color.gray, text_size=size.small, text_halign=text.align_right)
            table.cell(tabStats, 2, 2, "-", text_color=color.gray, text_size=size.small, text_halign=text.align_right)
            
        // Row 3
        if useEq3
            table.cell(tabStats, 0, 3, "Period 3 (" + str.tostring(lookback3) + ")", text_color=color.white, text_size=size.small, text_halign=text.align_left)
            table.cell(tabStats, 1, 3, format_vol(buyVol3), text_color=color.new(#00e676, 0), text_size=size.small, text_halign=text.align_right)
            table.cell(tabStats, 2, 3, format_vol(sellVol3), text_color=color.new(#ff1744, 0), text_size=size.small, text_halign=text.align_right)
        else
            table.cell(tabStats, 0, 3, "Period 3", text_color=color.gray, text_size=size.small, text_halign=text.align_left)
            table.cell(tabStats, 1, 3, "-", text_color=color.gray, text_size=size.small, text_halign=text.align_right)
            table.cell(tabStats, 2, 3, "-", text_color=color.gray, text_size=size.small, text_halign=text.align_right)
    else
        table.clear(tabStats, 0, 0, 2, 3)

← 返回列表