OrderFlow Volume Support and Resistance [BigBeluga]

BigBeluga · study · 251 行 · 点赞 2,137 · TradingView 原页

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

Pine Script

// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International  
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga
 
//@version=6
indicator("OrderFlow Volume Support and Resistance [BigBeluga]", "OrderFlow Volume S/R Zones [BigBeluga]", overlay = true)
 
// INPUTS ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
length = input.int(5, title = "Pivot Length")
vol_filter_res = input.float(50.0, title = "Resistance Volume Threshold %", minval = 0.0, maxval = 100.0, step = 5.0) / 100.0
vol_filter_sup = input.float(50.0, title = "Support Volume Threshold %", minval = 0.0, maxval = 100.0, step = 5.0) / 100.0
 
box_text_size_input = input.string("small", title = "Box Text Size", options = ["auto", "tiny", "small", "normal", "large", "huge"], group = "Text Settings")
label_text_size_input = input.string("large", title = "Label Text Size", options = ["auto", "tiny", "small", "normal", "large", "huge"], group = "Text Settings")
 
show_mid_lines = input.bool(true, title = "Show Midlines", group = "Visibility Settings")
 
color_res = input.color(color.rgb(82, 151, 255), title = "Resistance Color", group = "Color Settings")
color_sup = input.color(color.green, title = "Support Color", group = "Color Settings")
// }
 
// CALCULATIONS――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
ph = ta.pivothigh(length, length)
pl = ta.pivotlow(length, length)
 
var topH = float(na)
var lowH = float(na)
var topL = float(na)
var lowL = float(na)
 
type pivotVol 
    float top 
    float bot 
    float price 
    float vol
    float buyVol
    float sellVol
 
var topFlow = array.new<pivotVol>()
var botFlow = array.new<pivotVol>()
 
dist = ta.sma(high - low, 100)
max_Vol = ta.highest(volume, 100)
 
if not na(ph)
    n_vol = volume[length] / max_Vol[length]
    buyV = 0.
    sellV = 0.
 
    for i = 0 to length * 2
        if close[i] > open[i]
            buyV += volume[i]
        else if close[i] < open[i]
            sellV += volume[i] 
 
    topFlow.push(pivotVol.new(ph + dist, ph - dist, ph, n_vol, buyV, sellV))
 
if topFlow.size() > 100
    topFlow.shift()
 
for p in topFlow
    if close > p.top 
        topFlow.remove(array.indexof(topFlow, p))
        
for p in topFlow
    if p.vol > vol_filter_res
        topH := p.top
        lowH := p.bot
 
if not na(pl)
    n_vol = volume[length] / max_Vol[length]
    buyV = 0.
    sellV = 0.
 
    for i = 0 to length * 2
        if close[i] > open[i]
            buyV += volume[i] 
        else if close[i] < open[i]
            sellV += volume[i] 
 
    botFlow.push(pivotVol.new(pl + dist, pl - dist, pl, n_vol, buyV, sellV))
 
if botFlow.size() > 50
    botFlow.shift()
 
for p in botFlow
    if close < p.bot
        botFlow.remove(array.indexof(botFlow, p))
 
for p in botFlow
    if p.vol > vol_filter_sup
        topL := p.top
        lowL := p.bot
 
topPH = topH != topH[1] ? na : topH
botPH = lowH != lowH[1] ? na : lowH
midH  = math.avg(topPH, botPH)
 
if high > botPH
    botPH := high
 
if close > topPH
    topPH := na
    botPH := na
    midH := na
    topH := na 
    lowH := na
 
topPL = topL != topL[1] ? na : topL
botPL = lowL != lowL[1] ? na : lowL
midL  = math.avg(topPL, botPL)
 
if low < topPL
    topPL := low
 
if close < botPL
    topPL := na
    botPL := na
    midL := na
    topL := na 
    lowL := na
 
// Custom Clamp & Linear Interpolation Helpers
clamp(val, min_val, max_val) =>
    math.max(min_val, math.min(max_val, val))
 
mix(x, y, a) =>
    x * (1.0 - a) + y * a
 
// Track zone updates for smooth 50-bar fade-in
var int bars_since_res_change = 100
var int bars_since_sup_change = 100
 
if topH != topH[1] or lowH != lowH[1]
    bars_since_res_change := 0
else
    bars_since_res_change += 1
 
if topL != topL[1] or lowL != lowL[1]
    bars_since_sup_change := 0
else
    bars_since_sup_change += 1
 
float res_fade_factor = clamp(bars_since_res_change / 50.0, 0.0, 1.0)
float sup_fade_factor = clamp(bars_since_sup_change / 50.0, 0.0, 1.0)
 
// Dynamic Opacity Logic based on Price Proximity to Fill Zone
dist_to_res = not na(botPH) ? math.max(0.0, botPH - high) : na
res_proximity = not na(dist_to_res) and dist > 0 ? clamp(1.0 - (dist_to_res / dist), 0.0, 1.0) : 0.0
float target_trans_res = mix(85.0, 60.0, res_proximity)
int trans_res = int(mix(100.0, target_trans_res, res_fade_factor))
 
dist_to_sup = not na(topPL) ? math.max(0.0, low - topPL) : na
sup_proximity = not na(dist_to_sup) and dist > 0 ? clamp(1.0 - (dist_to_sup / dist), 0.0, 1.0) : 0.0
float target_trans_sup = mix(85.0, 60.0, sup_proximity)
int trans_sup = int(mix(100.0, target_trans_sup, sup_fade_factor))
 
// Midline transparency fades from 100% (hidden) down to 0% (fully solid) over 50 bars
int trans_mid_res = int(mix(100.0, 0.0, res_fade_factor))
int trans_mid_sup = int(mix(100.0, 0.0, sup_fade_factor))
 
// Retest Conditions with 5-Bar Gap
var int last_res_retest = 0
var int last_sup_retest = 0
 
raw_res_retest = not na(botPH) and high >= midH and high <= topPH
raw_sup_retest = not na(topPL) and low <= midL and low >= botPL
 
bool retest_res = raw_res_retest and (bar_index - last_res_retest >= 10)
bool retest_sup = raw_sup_retest and (bar_index - last_sup_retest >= 10)
 
if retest_res
    last_res_retest := bar_index
 
if retest_sup
    last_sup_retest := bar_index
 
// Text size resolution helper
getTextSize(size_str) =>
    switch size_str
        "auto"   => size.auto
        "tiny"   => size.tiny
        "small"  => size.small
        "normal" => size.normal
        "large"  => size.large
        "huge"   => size.huge
        => size.auto
 
user_box_text_size   = getTextSize(box_text_size_input)
user_label_text_size = getTextSize(label_text_size_input)
// }
 
// PLOT ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
if barstate.islast
    pivotVol activeTop = na
    for p in topFlow
        if p.vol > vol_filter_res
            activeTop := p
 
    var box boxTop = na
    var box boxBot = na
 
    var label labelTop = na
    var label labelTop1 = na
    
    var label labelBot = na
    var label labelBot1 = na
 
    box.delete(boxTop)
    box.delete(boxBot)
    label.delete(labelTop)
    label.delete(labelTop1)
    label.delete(labelBot)
    label.delete(labelBot1)
 
    if not na(activeTop)
        float deltaTop = activeTop.buyVol - activeTop.sellVol
        string textTop = "B: " + str.tostring(activeTop.buyVol, format.volume) + "\n" +
                         "S: " + str.tostring(activeTop.sellVol, format.volume) + "\n" +
                         "D: " + str.tostring(deltaTop, format.volume)
        boxTop := box.new(left = bar_index, top = activeTop.top, right = bar_index + 20, bottom = activeTop.bot, border_color = color.new(color_res, trans_mid_res), bgcolor = color.new(color_res, trans_res), text = textTop, text_color = chart.fg_color, text_size = user_box_text_size, text_halign = text.align_center, text_valign = text.align_center)
        labelTop := label.new(bar_index + 20, math.avg(activeTop.top, activeTop.bot), str.tostring(activeTop.vol * 100, format.percent), color = na, textcolor = color_res, style = label.style_label_left, size = user_label_text_size, tooltip = "Zone Volume Strength 0-100%")
        labelTop1 := label.new(bar_index + 10, math.avg(activeTop.top, activeTop.bot), color = color.rgb(54, 58, 69, 100), textcolor = na, style = label.style_label_center, size = size.huge, tooltip = "B: Buy Volume\nS: Sell Volume\nD: Delta Volume", force_overlay = true)
 
    pivotVol activeBot = na
    for p in botFlow
        if p.vol > vol_filter_sup
            activeBot := p
 
    if not na(activeBot)
        float deltaBot = activeBot.buyVol - activeBot.sellVol
        string textBot = "B: " + str.tostring(activeBot.buyVol, format.volume) + "\n" +
                         "S: " + str.tostring(activeBot.sellVol, format.volume) + "\n" +
                         "D: " + str.tostring(deltaBot, format.volume)
        boxBot := box.new(left = bar_index, top = activeBot.top, right = bar_index + 20, bottom = activeBot.bot, border_color = color.new(color_sup, trans_mid_sup), bgcolor = color.new(color_sup, trans_sup), text = textBot, text_color = chart.fg_color, text_size = user_box_text_size, text_halign = text.align_center, text_valign = text.align_center)
        labelBot := label.new(bar_index + 20, math.avg(activeBot.top, activeBot.bot), str.tostring(activeBot.vol * 100, format.percent), color = na, textcolor = color_sup, style = label.style_label_left, size = user_label_text_size, tooltip = "Zone Volume Strength 0-100%")
        labelBot1 := label.new(bar_index + 10, math.avg(activeBot.top, activeBot.bot), color = color.rgb(54, 58, 69, 100), textcolor = na, style = label.style_label_center, size = size.huge, tooltip = "B: Buy Volume\nS: Sell Volume\nD: Delta Volume", force_overlay = true)
 
ptt = plot(topPH, title = "Resistance Top Boundary", style = plot.style_linebr, color = color.new(color_res, trans_mid_res), display = display.none)
ptb = plot(botPH, title = "Resistance Bottom Boundary", style = plot.style_linebr, color = color.new(color_res, trans_mid_res), display = display.none)
plot(show_mid_lines ? midH : na, title = "Resistance Midline", style = plot.style_linebr, color = color.new(color_res, trans_mid_res))
fill(ptt, ptb, color.new(color_res, trans_res), title = "Resistance Zone Fill")
 
pbt = plot(topPL, title = "Support Top Boundary", style = plot.style_linebr, color = color.new(color_sup, trans_mid_sup), display = display.none)
pbb = plot(botPL, title = "Support Bottom Boundary", style = plot.style_linebr, color = color.new(color_sup, trans_mid_sup), display = display.none)
plot(show_mid_lines ? midL : na, title = "Support Midline", style = plot.style_linebr, color = color.new(color_sup, trans_mid_sup))
fill(pbt, pbb, color.new(color_sup, trans_sup), title = "Support Zone Fill")
 
plotshape(retest_res, title = "Top Retest Signal", style = shape.diamond, location = location.abovebar, color = color.orange, size = size.tiny)
plotshape(retest_sup, title = "Bottom Retest Signal", style = shape.diamond, location = location.belowbar, color = color.yellow, size = size.tiny)
// }

← 返回列表