Volume Bubbles & Trap Indicator

TradeWiseWithEase · study · 237 行 · 点赞 309 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradeWiseWithEase
 
//@version=6
indicator("Volume Bubbles & Trap Indicator", shorttitle = "BE_VFT0.1", max_labels_count = 500, max_boxes_count = 500, overlay = true, behind_chart = false)
 
useCustomTF = input.bool(true,    "Use Custom Lower Timeframe")
customTF    = input.timeframe("15S", "Custom Timeframe", active = useCustomTF)
window_bar  = input.int(20, "Trap Pair Window", minval = 3, maxval = 50,step = 1)
 
var int chart_sec                   = timeframe.in_seconds(timeframe.period)
var string lowerTimeframe = switch
    useCustomTF          => customTF
    timeframe.isseconds  => "1S"
    timeframe.isintraday => "1"
    timeframe.isdaily    => "5"
    => "60"
 
var int ltm = timeframe.in_seconds(lowerTimeframe)
 
var effortArray                     = array.new_float(3, 0)
 
type vInfo
    float v = volume
    float o = open
    float h = high
    float l = low
    float c = close
    int t = time
    float pc = na
 
type vfpData
    float v = na
    float l = na
    float r = na
    int t = na
    bool isBullish = false
    int b_type = na 
    int bi = na
    label b = na
    box z = na
    float zt = na
    float zb = na
    bool isTrapped = false
    color c = na
    bool isTested = false
    bool isTrapAlertFired = false
    bool isTestAlertFired = false
    color zc = na
 
var vfDataArray = array.new<vfpData>(15 * (chart_sec / ltm),  na)
var volArray = array.new<float>(vfDataArray.size(),  0)
var rangeArray = array.new<float>(vfDataArray.size(),  0)
var weightArray = array.new<float>(vfDataArray.size(),  0)
var volDeltaArray = array.new<float>(vfDataArray.size(),  0)
var bArray = array.new<vfpData>(450,  na)
varip bubbleTracker = array.new_int(1, -2)
varip vfIndexTracker = array.new_int(1, -2)
varip lastVolEffort = array.new_int(1, 0)
varip hasLoadedData = array.new_bool(1, false)
var int lblTransp = 65
var int bxUnTestedTransp = 70
var int bxTestedTransp = 90
 
if barstate.isfirst
    if na(volume)
        runtime.error("No Volume Detected!  Current Chart dose not have any volume Information to analyse")
    for [c, d] in vfDataArray
        vfDataArray.set(c, vfpData.new())
 
method trackTrapBubbles(array<vfpData> id)=>
    for [index, data] in id
        if na(data) or na(data.v)
            continue
        if data.b_type >= 1 and bar_index > data.bi and bar_index <= data.bi + window_bar
            if data.isBullish
                if not data.isTrapped and close <= data.zb - data.r
                    box.set_text(data.z, "T")
                    box.set_bgcolor(data.z, color.new(data.zc, bxTestedTransp))
                    box.set_text_color(data.z, data.zc)
                    data.isTrapped := true
                    if not data.isTrapAlertFired and data.b_type >= 2
                        data.isTrapAlertFired := true
                        string alrtTxt = str.format("Buy Trap Detected\nVolume Chunk: {0}\nLevel: {1}\nLTP: {2}", data.b_type == 2 ? "Medium" : "Large", data.l, close)
                        alert(alrtTxt, alert.freq_once_per_bar)
                else if data.isTrapped and close > data.zt
                    box.set_text(data.z, "")
                    box.set_bgcolor(data.z, color.new(data.zc, bxUnTestedTransp))
                    data.isTrapped := false
            else
                if not data.isTrapped and close >= data.zt + data.r
                    box.set_text(data.z, "T")
                    box.set_bgcolor(data.z, color.new(data.zc, bxTestedTransp))
                    box.set_text_color(data.z, data.zc)
                    data.isTrapped := true
                    if not data.isTrapAlertFired and data.b_type >= 2
                        data.isTrapAlertFired := true
                        string alrtTxt = str.format("Sell Trap Detected\nVolume Chunk: {0}\nLevel: {1}\nLTP: {2}", data.b_type == 2 ? "Medium" : "Large", data.l, close)
                        alert(alrtTxt, alert.freq_once_per_bar)                    
                else if data.isTrapped and close < data.zb
                    box.set_text(data.z, "")
                    box.set_bgcolor(data.z, color.new(data.zc, bxUnTestedTransp))
                    data.isTrapped := false
        if data.b_type == 3 and bar_index > data.bi
            if data.isBullish
                if low <= data.zt
                    data.isTested := true
                    if not data.isTestAlertFired
                        data.isTestAlertFired := true
                        string alrtTxt = str.format("Price Tested the Major Buyers Level: {0}", data.zt)
                        alert(alrtTxt, alert.freq_once_per_bar)
                if data.isTested and bar_index >= data.bi + window_bar
                    if close < data.zb
                        box.set_extend(data.z, extend.none)
                    else if low > data.zt
                        box.set_bgcolor(data.z, color.new(data.zc, bxTestedTransp))
            else
                if high >= data.zb
                    data.isTested := true
                    if not data.isTestAlertFired
                        data.isTestAlertFired := true
                        string alrtTxt = str.format("Price Tested the Major Sellers Level: {0}", data.zb)
                        alert(alrtTxt, alert.freq_once_per_bar)                    
                if data.isTested and bar_index >= data.bi + window_bar
                    if close > data.zt
                        box.set_extend(data.z, extend.none)
                    else if high < data.zb
                        box.set_bgcolor(data.z, color.new(data.zc, bxTestedTransp))
    id
 
method fxCreateVolBubble(array<vfpData> id, array<int> tracker, vfpData newVal, color clr, int sz, color zClr)=>
    int where2Load = tracker.first() + 1
    if where2Load == -1 or where2Load == id.size()
        tracker.set(0, 0)
        where2Load := 0
    else
        tracker.set(0, where2Load)
    vfpData newChartInfo = vfpData.new(newVal.v, newVal.l, newVal.r, newVal.t, newVal.isBullish, newVal.b_type, newVal.bi, zt = newVal.zt, zb = newVal.zb, isTrapped = false, zc = zClr, c = clr)
    newChartInfo.b := label.new(newVal.t, newVal.l, "", xloc.bar_time, yloc.price, color.new(newChartInfo.c, lblTransp), label.style_circle, color.white, sz, text.align_center, text_formatting = text.format_bold)
    newChartInfo.z := box.new(newVal.bi, newVal.zt, newVal.bi + window_bar, newVal.zb, color.new(color.white, 100), 1, line.style_solid, newVal.b_type < 3 ? extend.none : extend.right, xloc.bar_index, color.new(newChartInfo.zc, bxUnTestedTransp), "", size.small, color.white, text.align_right, text.align_center, text_formatting = text.format_bold)
    id.set(where2Load, newChartInfo)
    id
 
method loadData(array<vfpData> id, vfpData newVal, array<float>vArr, array<float>rArr, array<float>wArr, array<int> tracker, array<bool> isFull, array<int> prEffect, array<float>vDelArr)=>
    int where2Load = vfIndexTracker.first() + 1
    if where2Load == -1 or where2Load == id.size()
        vfIndexTracker.set(0, 0)
        where2Load := 0
    else
        vfIndexTracker.set(0, where2Load)
    bool isDataLoaded  = array.every(isFull)
    if not isDataLoaded and bar_index > 0 and where2Load == 0
        isFull.set(0, true)
        isDataLoaded := true
    if isDataLoaded
        float WA = array.sum(wArr) / array.sum(rArr)
        float rangeMax = rArr.max()
        bool isMaxVol = newVal.v > vArr.max()
        bool isMaxEfrt = newVal.r > rangeMax
        bool isGWA = newVal.v > WA * 1.75
 
        if isMaxVol and isMaxEfrt
            // log.info("Volume with Impact")
            prEffect.set(0, 3)
            newVal.b_type := 3
            bArray.fxCreateVolBubble(bubbleTracker, newVal, newVal.isBullish ? color.green : color.red, 18, newVal.isBullish ? color.blue : color.fuchsia)
        else if isMaxEfrt and isGWA
            // log.info("Moderate Volume with Impact")
            prEffect.set(0, 2)
            newVal.b_type := 2
            bArray.fxCreateVolBubble(bubbleTracker, newVal, newVal.isBullish ? color.green : color.red, 12, newVal.isBullish ? color.blue : color.fuchsia)
        else if isGWA and newVal.r >= rangeMax * 0.75 
            // log.info("Moderate Volume with Moderate Impact")
            prEffect.set(0, 1)
            newVal.b_type := 1
            bArray.fxCreateVolBubble(bubbleTracker, newVal, newVal.isBullish ? color.green : color.red, 7, newVal.isBullish ? color.blue : color.fuchsia)
        else if (isMaxVol or newVal.v >= WA * 1.6) and newVal.r <= rArr.avg() * 0.4 and prEffect.first() >= 1
            // log.info("Volume Absorbtion")
            prEffect.set(0, 0)
            newVal.b_type := 0
            bArray.fxCreateVolBubble(bubbleTracker, newVal, color.yellow, 10, color.yellow)
        else if newVal.v < WA * 0.75 and newVal.r > rArr.avg()
            // log.error("Volume Exhaution")
            prEffect.set(0, 0)
            newVal.b_type := 0
            bArray.fxCreateVolBubble(bubbleTracker, newVal, color.white, 10, color.white)
        else
            prEffect.set(0, 0)
    float weightVolue = (newVal.v / 1000) * newVal.r
    vArr.set(where2Load, newVal.v)
    rArr.set(where2Load, newVal.r)
    wArr.set(where2Load, weightVolue )
    vDelArr.set(where2Load, newVal.isBullish ? weightVolue : weightVolue * -1 )
    id.set(where2Load, newVal)
 
method fxVolumefootPrint(array<vInfo> id, array<float> eArr)=>
    for [index, vData] in id
        float dBody = math.abs(vData.o - vData.c)
        float dl2C = math.abs(vData.l - vData.c)
        float dh2C = math.abs(vData.h - vData.c)
        eArr.set(0, dBody)
        eArr.set(1, dl2C)
        eArr.set(2, dh2C)
        bool isGreen = vData.o < vData.c
        bool isRed = vData.c < vData.o
        float mxEffort = effortArray.max()
        if isGreen or (not isRed and vData.c > vData.pc)
            if mxEffort == dBody
                vfpData newVal = vfpData.new(vData.v, vData.o, mxEffort, vData.t, true, bi = bar_index, zt = vData.o + (mxEffort * 0.5), zb = vData.o)
                vfDataArray.loadData(newVal, volArray, rangeArray, weightArray, vfIndexTracker, hasLoadedData, lastVolEffort, volDeltaArray)
            else if mxEffort == dl2C
                vfpData newVal = vfpData.new(vData.v, vData.l, mxEffort, vData.t, true, bi = bar_index, zt = math.max(vData.o, vData.l + (mxEffort * 0.5)), zb = vData.l)
                vfDataArray.loadData(newVal, volArray, rangeArray, weightArray, vfIndexTracker, hasLoadedData, lastVolEffort, volDeltaArray)
            else if mxEffort == dh2C
                vfpData newVal = vfpData.new(vData.v, vData.h, mxEffort, vData.t, false, bi = bar_index, zb = math.min(vData.o, vData.h - (mxEffort * 0.5)), zt = vData.h)
                vfDataArray.loadData(newVal, volArray, rangeArray, weightArray, vfIndexTracker, hasLoadedData, lastVolEffort, volDeltaArray)
        else if isRed or (not isGreen and vData.c < vData.pc)
            if mxEffort == dBody
                vfpData newVal = vfpData.new(vData.v, vData.o, mxEffort, vData.t, false, bi = bar_index, zb = vData.o - (mxEffort * 0.5), zt = vData.o)
                vfDataArray.loadData(newVal, volArray, rangeArray, weightArray, vfIndexTracker, hasLoadedData, lastVolEffort, volDeltaArray)
            else if mxEffort == dl2C
                vfpData newVal = vfpData.new(vData.v, vData.l, mxEffort, vData.t, true, bi = bar_index, zt = math.max(vData.o, vData.l + (mxEffort * 0.5)), zb = vData.l)
                vfDataArray.loadData(newVal, volArray, rangeArray, weightArray, vfIndexTracker, hasLoadedData, lastVolEffort, volDeltaArray)
            else if mxEffort == dh2C
                vfpData newVal = vfpData.new(vData.v, vData.h, mxEffort, vData.t, false, bi = bar_index, zb = math.min(vData.o, vData.h - (mxEffort * 0.5)), zt = vData.h)
                vfDataArray.loadData(newVal, volArray, rangeArray, weightArray, vfIndexTracker, hasLoadedData, lastVolEffort, volDeltaArray)
        mxEffort
    
if barstate.isconfirmed
    vArray = request.security_lower_tf(syminfo.tickerid, lowerTimeframe, vInfo.new(pc = nz(close[1], close) ))
    vArray.fxVolumefootPrint(effortArray)
    bArray.trackTrapBubbles()
 
float volTrend = volDeltaArray.sum()
color barColor = volTrend > 0 ? color.green : color.red
plot(volTrend, "VolDelta", color = barColor, linewidth = 1, display = display.status_line)
plotcandle(open, high, low, close, "VolTrendCandles", barColor, barColor, bordercolor = barColor, display = display.pane )

← 返回列表