M-Alpha-Fair Value Gaps - Does Price Really Fill the FVG?

MAlphaCore · study · 98 行 · 点赞 27 · 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/
// © MAlphaCore
 
//@version=6
// M·Alpha·Fair Value Gaps — "price always comes back to fill the gap": does it? A fair value gap is a three-bar gap:
// the low of a bar sits above the high two bars earlier (bullish gap) or the high sits below the low two bars earlier
// (bearish gap). Open gaps are drawn as zones and shrink while price eats into them. Every gap is then judged on THIS
// chart: fully filled within the chosen number of bars, or not. The table shows the share and the average time it took.
// Closed bars only, nothing repaints.
indicator("M·Alpha·Fair Value Gaps", shorttitle = "MA FVG", overlay = true, max_boxes_count = 500)
 
// ───────────────────────────── Inputs ─────────────────────────────
grSig    = "Gaps"
minAtr   = input.float(0.25, "Minimum gap size (× ATR 14)", minval = 0, maxval = 5, step = 0.05, group = grSig, display = display.none)
maxBars  = input.int(100, "Bars to fill", minval = 5, maxval = 1000, group = grSig, display = display.none)
 
grStyle  = "Style"
showFilled = input.bool(false, "Keep filled gaps on the chart", group = grStyle, display = display.none)
colBull  = input.color(#ffeb3b, "Bullish gap (below price)", group = grStyle, display = display.none)
colBear  = input.color(#b2b5be, "Bearish gap (above price)", group = grStyle, display = display.none)
colText  = input.color(#e8eaf0, "Text", group = grStyle, display = display.none)
textSizeIn = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grStyle, display = display.none)
textSize = textSizeIn == "Tiny" ? size.tiny : textSizeIn == "Normal" ? size.normal : size.small
 
// ───────────────────────────── Gaps ─────────────────────────────
type Gap
    float top
    float bot
    int   dir      // 1 = bullish gap (filled from above), -1 = bearish gap (filled from below)
    int   bi
    box   bx
 
var array<Gap> gaps = array.new<Gap>()
var array<int> judged = array.from(0, 0)    // [bullish, bearish]
var array<int> filled = array.from(0, 0)
var array<int> fillSum = array.from(0, 0)   // bars it took, summed over filled gaps
 
atr = ta.atr(14)
bullGap = barstate.isconfirmed and low > high[2] and low - high[2] >= minAtr * atr
bearGap = barstate.isconfirmed and high < low[2] and low[2] - high >= minAtr * atr
 
if barstate.isconfirmed
    // Judge older gaps first, so a gap is never judged on the bar that created it.
    if gaps.size() > 0
        for i = gaps.size() - 1 to 0
            g = gaps.get(i)
            idx = g.dir == 1 ? 0 : 1
            isFilled = g.dir == 1 ? low <= g.bot : high >= g.top
            if isFilled
                judged.set(idx, judged.get(idx) + 1)
                filled.set(idx, filled.get(idx) + 1)
                fillSum.set(idx, fillSum.get(idx) + bar_index - g.bi)
                if showFilled
                    g.bx.set_right(bar_index)
                    g.bx.set_bgcolor(color.new(g.dir == 1 ? colBull : colBear, 92))
                    g.bx.set_border_color(color.new(g.dir == 1 ? colBull : colBear, 80))
                else
                    g.bx.delete()
                gaps.remove(i)
            else if bar_index - g.bi >= maxBars
                judged.set(idx, judged.get(idx) + 1)
                g.bx.delete()
                gaps.remove(i)
            else
                // Partly filled: the zone shrinks to what is still open.
                if g.dir == 1 and low < g.top
                    g.top := low
                    g.bx.set_top(low)
                if g.dir == -1 and high > g.bot
                    g.bot := high
                    g.bx.set_bottom(high)
                g.bx.set_right(bar_index + 1)
 
    if bullGap
        gaps.push(Gap.new(low, high[2], 1, bar_index - 1, box.new(bar_index - 1, low, bar_index + 1, high[2], border_color = color.new(colBull, 50), bgcolor = color.new(colBull, 78))))
    if bearGap
        gaps.push(Gap.new(low[2], high, -1, bar_index - 1, box.new(bar_index - 1, low[2], bar_index + 1, high, border_color = color.new(colBear, 50), bgcolor = color.new(colBear, 78))))
 
share(int idx) => judged.get(idx) > 0 ? str.tostring(100.0 * filled.get(idx) / judged.get(idx), "#") + " % of " + str.tostring(judged.get(idx)) : "no history yet"
avgBars(int idx) => filled.get(idx) > 0 ? str.tostring(fillSum.get(idx) / filled.get(idx), "#") + " bars" : "–"
 
// ───────────────────────────── Table ─────────────────────────────
var table stats = table.new(position.top_right, 3, 4, bgcolor = color.new(#131722, 15), border_color = color.new(#787b86, 60), border_width = 1, frame_color = color.new(#787b86, 40), frame_width = 1)
if barstate.islast
    table.cell(stats, 0, 0, "M-Alpha-Fair Value Gaps", text_color = colText, text_size = textSize)
    table.cell(stats, 1, 0, "Filled", text_color = colText, text_size = textSize)
    table.cell(stats, 2, 0, "Avg. time", text_color = colText, text_size = textSize)
    table.cell(stats, 0, 1, "Bullish gaps", text_color = colBull, text_size = textSize, text_halign = text.align_left)
    table.cell(stats, 1, 1, share(0), text_color = colBull, text_size = textSize)
    table.cell(stats, 2, 1, avgBars(0), text_color = colBull, text_size = textSize)
    table.cell(stats, 0, 2, "Bearish gaps", text_color = colBear, text_size = textSize, text_halign = text.align_left)
    table.cell(stats, 1, 2, share(1), text_color = colBear, text_size = textSize)
    table.cell(stats, 2, 2, avgBars(1), text_color = colBear, text_size = textSize)
    table.cell(stats, 0, 3, "Fully filled within " + str.tostring(maxBars) + " bars, gaps ≥ " + str.tostring(minAtr) + " ATR", text_color = color.new(colText, 30), text_size = size.tiny, text_halign = text.align_left)
    table.merge_cells(stats, 0, 3, 2, 3)
 
alertcondition(bullGap, "Bullish fair value gap", "M·Alpha·Fair Value Gaps: new bullish gap")
alertcondition(bearGap, "Bearish fair value gap", "M·Alpha·Fair Value Gaps: new bearish gap")

← 返回列表