The Axelerator: FVG

AxelJ95 · study · 123 行 · 点赞 14 · TradingView 原页

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

Pine Script

// © AxelJ95
//
// The Axelerator: FVG
// Fair value gaps (FVG) and their midlines (CE, consequent encroachment).
//
// How it works
//   • A gap is three candles that leave empty space between the first and the
//     third candle. It is drawn once the third candle has closed.
//   • The box covers the full range of the gap and extends until the gap is
//     mitigated. The CE is the line at 50% of the gap.
//   • Mitigation threshold: price reaching the CE, or price filling the whole
//     gap. Measured with wicks or with candle bodies.
//   • A mitigated gap stops extending and stays on the chart, or is deleted.
 
//@version=6
indicator("The Axelerator: FVG", "The Axelerator: FVG", overlay = true, max_boxes_count = 500, max_lines_count = 500)
 
//------------------------------------------------------------------------------
// 1. Settings
//------------------------------------------------------------------------------
maxGaps = input.int(200, 'Maximum Gaps', minval = 1, maxval = 200, tooltip = 'Unmitigated gaps kept per direction. When the limit is passed the oldest one is removed.')
 
showBull = input.bool(true, 'Bullish FVG', inline = 'bull', group = 'Style')
bullCss  = input.color(#4CAF50, '', inline = 'bull', group = 'Style')
showBear = input.bool(true, 'Bearish FVG', inline = 'bear', group = 'Style')
bearCss  = input.color(#F23645, '', inline = 'bear', group = 'Style')
showCE   = input.bool(true, 'Show CE', inline = 'ce', group = 'Style')
ceCss    = input.color(#787B86, '', inline = 'ce', group = 'Style')
ceStyle  = input.string('Dotted', '', options = ['Dotted', 'Dashed', 'Solid'], inline = 'ce', group = 'Style')
deleteFilled = input.bool(false, 'Delete Mitigated Gaps', group = 'Style')
 
useCE     = input.bool(true, 'Mitigate at CE (50%)', group = 'Conditions', tooltip = 'On: a gap is mitigated when price reaches its CE (50%). Off: only when the whole gap is filled. The mitigation alerts use the same threshold.')
useBodies = input.bool(false, 'Measure with Candle Bodies', group = 'Conditions', tooltip = 'On: only the candle body (open and close) can mitigate a gap. Off: wicks count.')
 
//------------------------------------------------------------------------------
// 2. Constants & types
//------------------------------------------------------------------------------
GAP_BARS   = 2  //Bars between the first and the third candle of a gap
BOX_TRANSP = 86 //Transparency of the gap box
 
type Fvg
    float top
    float bot
    int   born           //Bar of the third candle of the gap
    bool  entered = false //Price has already traded into the gap
    box   bx      = na
    line  ln      = na
 
ceLineStyle = ceStyle == 'Dotted' ? line.style_dotted : ceStyle == 'Dashed' ? line.style_dashed : line.style_solid
 
//------------------------------------------------------------------------------
// 3. Engine
//------------------------------------------------------------------------------
//One call handles one direction and keeps its own list of unmitigated gaps.
//Returns [formed, entered, mitigated] for this bar.
fvg_side(bool isBull, bool show, color css) =>
    var gaps = array.new<Fvg>()
    formed    = false
    entered   = false
    mitigated = false
 
    //A bullish gap is revisited from above, a bearish gap from below
    reach = isBull ? (useBodies ? math.min(open, close) : low) : (useBodies ? math.max(open, close) : high)
 
    //Unmitigated gaps, newest first so removal keeps the remaining indexes valid
    sz = array.size(gaps)
    if sz > 0
        for i = sz - 1 to 0
            g = array.get(gaps, i)
            if bar_index > g.born
                box.set_right(g.bx, bar_index)
                line.set_x2(g.ln, bar_index)
 
                edge = isBull ? g.top : g.bot
                far  = isBull ? g.bot : g.top
                threshold = useCE ? math.avg(g.top, g.bot) : far
 
                if not g.entered and (isBull ? reach <= edge : reach >= edge)
                    g.entered := true
                    entered := true
 
                if isBull ? reach <= threshold : reach >= threshold
                    mitigated := true
                    if deleteFilled
                        box.delete(g.bx)
                        line.delete(g.ln)
                    array.remove(gaps, i)
 
    //New gap, once its third candle has closed
    if show and barstate.isconfirmed
        isGap = isBull ? low > high[GAP_BARS] : high < low[GAP_BARS]
        if isGap
            top = isBull ? low : low[GAP_BARS]
            bot = isBull ? high[GAP_BARS] : high
            mid = math.avg(top, bot)
            bx = box.new(bar_index - GAP_BARS, top, bar_index, bot, border_color = na, bgcolor = color.new(css, BOX_TRANSP))
            line ln = na
            if showCE
                ln := line.new(bar_index - GAP_BARS, mid, bar_index, mid, color = ceCss, style = ceLineStyle)
            array.push(gaps, Fvg.new(top = top, bot = bot, born = bar_index, bx = bx, ln = ln))
            formed := true
            if array.size(gaps) > maxGaps
                old = array.shift(gaps)
                box.delete(old.bx)
                line.delete(old.ln)
 
    [formed, entered, mitigated]
 
//------------------------------------------------------------------------------
// 4. Gaps
//------------------------------------------------------------------------------
[bullFormed, bullEntered, bullMitigated] = fvg_side(true,  showBull, bullCss)
[bearFormed, bearEntered, bearMitigated] = fvg_side(false, showBear, bearCss)
 
//------------------------------------------------------------------------------
// 5. Alerts
//------------------------------------------------------------------------------
alertcondition(bullFormed, 'New Bullish FVG', 'A bullish FVG has formed')
alertcondition(bearFormed, 'New Bearish FVG', 'A bearish FVG has formed')
alertcondition(bullEntered, 'Price Entered a Bullish FVG', 'Price has entered a bullish FVG')
alertcondition(bearEntered, 'Price Entered a Bearish FVG', 'Price has entered a bearish FVG')
alertcondition(bullMitigated, 'Bullish FVG Mitigated', 'Price has reached the threshold (CE or full fill) of a bullish FVG')
alertcondition(bearMitigated, 'Bearish FVG Mitigated', 'Price has reached the threshold (CE or full fill) of a bearish FVG')

← 返回列表