Fair Value Gaps - Significant Displacement

SecondBell · study · 1182 行 · 点赞 172 · TradingView 原页

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

Pine Script

//@version=6
indicator(
     "Fair Value Gaps - Significant Displacement",
     overlay = true,
     max_boxes_count = 500
)
 
// =====================================================
// FAIR VALUE GAP SETTINGS
// =====================================================
 
showFVG = input.bool(
     true,
     "Show FVG",
     group = "Fair Value Gaps"
)
 
showBullishFVG = input.bool(
     true,
     "Show Bullish FVG",
     group = "Fair Value Gaps"
)
 
showBearishFVG = input.bool(
     true,
     "Show Bearish FVG",
     group = "Fair Value Gaps"
)
 
maxFvgHistory = input.int(
     50,
     "Maximum FVG History",
     minval = 10,
     maxval = 200,
     group = "Fair Value Gaps"
)
 
historicalLookbackDays = input.int(
     3,
     "Historical Lookback (Trading Days)",
     options = [1, 3, 5, 10],
     group = "Fair Value Gaps",
     tooltip = "Keeps FVGs visible for this many completed trading days in addition to the current trading day."
)
 
// =====================================================
// VISUALS
// =====================================================
 
bullishFvgColor = input.color(
     color.green,
     "Bullish FVG Color",
     group = "Visuals"
)
 
bearishFvgColor = input.color(
     color.red,
     "Bearish FVG Color",
     group = "Visuals"
)
 
boxTransparency = input.int(
     90,
     "Box Transparency",
     minval = 0,
     maxval = 100,
     group = "Visuals"
)
 
borderTransparency = input.int(
     20,
     "Border Transparency",
     minval = 0,
     maxval = 100,
     group = "Visuals"
)
 
showFvgText = input.bool(
     true,
     "Show FVG Text",
     group = "Visuals"
)
 
showMidpoint = input.bool(
     true,
     "Show 50% Midpoint",
     group = "Visuals",
     tooltip = "Draws the 50% level of each FVG. Off by default to keep the chart clean."
)
 
midpointTransparency = input.int(
     35,
     "Midpoint Transparency",
     minval = 0,
     maxval = 100,
     group = "Visuals"
)
 
// =====================================================
// QUALITY FILTERS
// =====================================================
 
volumeLength = input.int(
     20,
     "Volume Average Length",
     minval = 5,
     maxval = 100,
     group = "Quality Filters"
)
 
minRelativeVolume = input.float(
     1.25,
     "Minimum Relative Volume",
     minval = 1.0,
     maxval = 5.0,
     step = 0.05,
     group = "Quality Filters",
     tooltip = "The middle displacement candle must have at least this multiple of its recent average volume."
)
 
atrLength = input.int(
     14,
     "ATR Length",
     minval = 5,
     maxval = 100,
     group = "Quality Filters"
)
 
minGapAtr = input.float(
     0.10,
     "Minimum FVG Size (ATR)",
     minval = 0.0,
     maxval = 2.0,
     step = 0.05,
     group = "Quality Filters",
     tooltip = "Minimum size of the actual fair value gap as a fraction of ATR."
)
 
minDisplacementBodyAtr = input.float(
     0.70,
     "Minimum Displacement Body (ATR)",
     minval = 0.10,
     maxval = 3.0,
     step = 0.05,
     group = "Quality Filters",
     tooltip = "The middle candle body must be at least this large relative to ATR."
)
 
minBodyPercentOfRange = input.float(
     0.60,
     "Minimum Body % Of Candle Range",
     minval = 0.30,
     maxval = 1.0,
     step = 0.05,
     group = "Quality Filters",
     tooltip = "Requires the displacement candle body to occupy a meaningful percentage of the full candle range."
)
 
requireDirectionalDisplacement = input.bool(
     true,
     "Require Directional Displacement Candle",
     group = "Quality Filters",
     tooltip = "Bullish FVGs require a bullish displacement candle and bearish FVGs require a bearish displacement candle."
)
 
// =====================================================
// CALCULATIONS
// =====================================================
 
float volumeAverage =
     ta.sma(
          volume,
          volumeLength
     )
 
float atrValue =
     ta.atr(
          atrLength
     )
 
bool enoughBars =
     bar_index >= 2
 
bool validVolumeData =
     enoughBars and
     not na(volume[1]) and
     not na(volumeAverage[1]) and
     volumeAverage[1] > 0
 
float middleRelativeVolume =
     validVolumeData ?
     volume[1] /
     volumeAverage[1] :
     na
 
bool volumeQualified =
     validVolumeData and
     middleRelativeVolume >=
     minRelativeVolume
 
float middleBody =
     enoughBars ?
     math.abs(
          close[1] -
          open[1]
     ) :
     na
 
float middleRange =
     enoughBars ?
     high[1] -
     low[1] :
     na
 
float middleBodyPercent =
     enoughBars and
     middleRange > 0 ?
     middleBody /
     middleRange :
     0.0
 
bool displacementBodyQualified =
     enoughBars and
     not na(atrValue[1]) and
     middleBody >=
     atrValue[1] *
     minDisplacementBodyAtr and
     middleBodyPercent >=
     minBodyPercentOfRange
 
bool bullishDisplacementDirection =
     not requireDirectionalDisplacement or
     close[1] >
     open[1]
 
bool bearishDisplacementDirection =
     not requireDirectionalDisplacement or
     close[1] <
     open[1]
 
float bullishGapSize =
     enoughBars ?
     low -
     high[2] :
     na
 
float bearishGapSize =
     enoughBars ?
     low[2] -
     high :
     na
 
bool bullishGapSizeQualified =
     enoughBars and
     not na(atrValue) and
     bullishGapSize >=
     atrValue *
     minGapAtr
 
bool bearishGapSizeQualified =
     enoughBars and
     not na(atrValue) and
     bearishGapSize >=
     atrValue *
     minGapAtr
 
// =====================================================
// FVG STORAGE
// =====================================================
 
var array<box> fvgBoxes =
     array.new_box()
 
var array<int> fvgDirs =
     array.new_int()
 
var array<float> fvgTops =
     array.new_float()
 
var array<float> fvgBottoms =
     array.new_float()
 
var array<int> fvgCreatedBars =
     array.new_int()
 
var array<int> fvgAgeDays =
     array.new_int()
 
var array<bool> fvgTouched =
     array.new_bool()
 
var array<bool> fvgFinalized =
     array.new_bool()
 
var array<line> fvgMidpointLines =
     array.new_line()
 
var array<label> fvgTextLabels =
     array.new_label()
 
bool newTradingDay =
     ta.change(
          time("D")
     ) != 0
 
// =====================================================
// DAY BOUNDARY DISPLAY MANAGEMENT
//
// Signal logic is untouched.
//
// At the start of each new trading day:
// - current-day boxes from yesterday are capped at
//   yesterday's final bar;
// - historical age advances by one trading day;
// - boxes older than the selected lookback are removed.
// =====================================================
 
if newTradingDay
 
    int dayIndex =
         array.size(
              fvgBoxes
         ) -
         1
 
    while dayIndex >= 0
 
        int ageDays =
             array.get(
                  fvgAgeDays,
                  dayIndex
             ) +
             1
 
        array.set(
             fvgAgeDays,
             dayIndex,
             ageDays
        )
 
        bool removeForAge =
             ageDays >
             historicalLookbackDays
 
        if removeForAge
 
            box expiredHistoryBox =
                 array.get(
                      fvgBoxes,
                      dayIndex
                 )
 
            if not na(
                 expiredHistoryBox
            )
 
                box.delete(
                     expiredHistoryBox
                )
 
            line expiredMidpointLine =
                 array.get(
                      fvgMidpointLines,
                      dayIndex
                 )
 
            if not na(
                 expiredMidpointLine
            )
 
                line.delete(
                     expiredMidpointLine
                )
 
            label expiredTextLabel =
                 array.get(
                      fvgTextLabels,
                      dayIndex
                 )
 
            if not na(
                 expiredTextLabel
            )
 
                label.delete(
                     expiredTextLabel
                )
 
            array.remove(
                 fvgBoxes,
                 dayIndex
            )
 
            array.remove(
                 fvgDirs,
                 dayIndex
            )
 
            array.remove(
                 fvgTops,
                 dayIndex
            )
 
            array.remove(
                 fvgBottoms,
                 dayIndex
            )
 
            array.remove(
                 fvgCreatedBars,
                 dayIndex
            )
 
            array.remove(
                 fvgAgeDays,
                 dayIndex
            )
 
            array.remove(
                 fvgTouched,
                 dayIndex
            )
 
            array.remove(
                 fvgFinalized,
                 dayIndex
            )
 
            array.remove(
                 fvgMidpointLines,
                 dayIndex
            )
 
            array.remove(
                 fvgTextLabels,
                 dayIndex
            )
 
        else
 
            bool finalized =
                 array.get(
                      fvgFinalized,
                      dayIndex
                 )
 
            if not finalized
 
                box dayBox =
                     array.get(
                          fvgBoxes,
                          dayIndex
                     )
 
                if not na(
                     dayBox
                )
 
                    box.set_extend(
                         dayBox,
                         extend.none
                    )
 
                    box.set_right(
                         dayBox,
                         bar_index -
                         1
                    )
 
                line dayMidpointLine =
                     array.get(
                          fvgMidpointLines,
                          dayIndex
                     )
 
                if not na(
                     dayMidpointLine
                )
 
                    line.set_extend(
                         dayMidpointLine,
                         extend.none
                    )
 
                    line.set_x2(
                         dayMidpointLine,
                         bar_index -
                         1
                    )
 
                array.set(
                     fvgFinalized,
                     dayIndex,
                     true
                )
 
        dayIndex -=
             1
 
// =====================================================
// ALERT FLAGS
// =====================================================
 
bool newBullishFVG =
     false
 
bool newBearishFVG =
     false
 
bool bullishFvgFilled =
     false
 
bool bearishFvgFilled =
     false
 
// =====================================================
// SIGNIFICANT FVG DETECTION
// =====================================================
 
bool bullishFVG =
     enoughBars and
     barstate.isconfirmed and
     low >
     high[2] and
     volumeQualified and
     bullishGapSizeQualified and
     displacementBodyQualified and
     bullishDisplacementDirection
 
bool bearishFVG =
     enoughBars and
     barstate.isconfirmed and
     high <
     low[2] and
     volumeQualified and
     bearishGapSizeQualified and
     displacementBodyQualified and
     bearishDisplacementDirection
 
// =====================================================
// CREATE BULLISH FVG
// =====================================================
 
if bullishFVG
 
    float fvgTop =
         low
 
    float fvgBottom =
         high[2]
 
    box newFvgBox =
         na
 
    if showFVG and
       showBullishFVG
 
        newFvgBox :=
             box.new(
                  left =
                       bar_index -
                       1,
                  top =
                       fvgTop,
                  right =
                       bar_index,
                  bottom =
                       fvgBottom,
                  xloc =
                       xloc.bar_index,
                  extend =
                       extend.right,
                  border_color =
                       color.new(
                            bullishFvgColor,
                            borderTransparency
                       ),
                  border_width =
                       1,
                  bgcolor =
                       color.new(
                            bullishFvgColor,
                            boxTransparency
                       )
             )
 
    array.push(
         fvgBoxes,
         newFvgBox
    )
 
    array.push(
         fvgDirs,
         1
    )
 
    array.push(
         fvgTops,
         fvgTop
    )
 
    array.push(
         fvgBottoms,
         fvgBottom
    )
 
    array.push(
         fvgCreatedBars,
         bar_index
    )
 
    array.push(
         fvgAgeDays,
         0
    )
 
    array.push(
         fvgTouched,
         false
    )
 
    array.push(
         fvgFinalized,
         false
    )
 
    line newBullishMidpoint =
         na
 
    if showFVG and
       showBullishFVG and
       showMidpoint
 
        float midpoint =
             (
                  fvgTop +
                  fvgBottom
             ) /
             2.0
 
        newBullishMidpoint :=
             line.new(
                  x1 =
                       bar_index -
                       1,
                  y1 =
                       midpoint,
                  x2 =
                       bar_index,
                  y2 =
                       midpoint,
                  xloc =
                       xloc.bar_index,
                  extend =
                       extend.right,
                  color =
                       color.new(
                            bullishFvgColor,
                            midpointTransparency
                       ),
                  width =
                       1,
                  style =
                       line.style_dashed
             )
 
    array.push(
         fvgMidpointLines,
         newBullishMidpoint
    )
 
    label newBullishTextLabel =
         na
 
    if showFVG and
       showBullishFVG and
       showFvgText
 
        float bullishTextY =
             (
                  fvgTop +
                  fvgBottom
             ) /
             2.0
 
        newBullishTextLabel :=
             label.new(
                  x =
                       bar_index +
                       1,
                  y =
                       bullishTextY,
                  text =
                       "FVG",
                  xloc =
                       xloc.bar_index,
                  yloc =
                       yloc.price,
                  style =
                       label.style_none,
                  textcolor =
                       color.new(
                            color.white,
                            25
                       ),
                  size =
                       size.tiny
             )
 
    array.push(
         fvgTextLabels,
         newBullishTextLabel
    )
 
    newBullishFVG :=
         true
 
// =====================================================
// CREATE BEARISH FVG
// =====================================================
 
if bearishFVG
 
    float fvgTop =
         low[2]
 
    float fvgBottom =
         high
 
    box newFvgBox =
         na
 
    if showFVG and
       showBearishFVG
 
        newFvgBox :=
             box.new(
                  left =
                       bar_index -
                       1,
                  top =
                       fvgTop,
                  right =
                       bar_index,
                  bottom =
                       fvgBottom,
                  xloc =
                       xloc.bar_index,
                  extend =
                       extend.right,
                  border_color =
                       color.new(
                            bearishFvgColor,
                            borderTransparency
                       ),
                  border_width =
                       1,
                  bgcolor =
                       color.new(
                            bearishFvgColor,
                            boxTransparency
                       )
             )
 
    array.push(
         fvgBoxes,
         newFvgBox
    )
 
    array.push(
         fvgDirs,
         -1
    )
 
    array.push(
         fvgTops,
         fvgTop
    )
 
    array.push(
         fvgBottoms,
         fvgBottom
    )
 
    array.push(
         fvgCreatedBars,
         bar_index
    )
 
    array.push(
         fvgAgeDays,
         0
    )
 
    array.push(
         fvgTouched,
         false
    )
 
    array.push(
         fvgFinalized,
         false
    )
 
    line newBearishMidpoint =
         na
 
    if showFVG and
       showBearishFVG and
       showMidpoint
 
        float midpoint =
             (
                  fvgTop +
                  fvgBottom
             ) /
             2.0
 
        newBearishMidpoint :=
             line.new(
                  x1 =
                       bar_index -
                       1,
                  y1 =
                       midpoint,
                  x2 =
                       bar_index,
                  y2 =
                       midpoint,
                  xloc =
                       xloc.bar_index,
                  extend =
                       extend.right,
                  color =
                       color.new(
                            bearishFvgColor,
                            midpointTransparency
                       ),
                  width =
                       1,
                  style =
                       line.style_dashed
             )
 
    array.push(
         fvgMidpointLines,
         newBearishMidpoint
    )
 
    label newBearishTextLabel =
         na
 
    if showFVG and
       showBearishFVG and
       showFvgText
 
        float bearishTextY =
             (
                  fvgTop +
                  fvgBottom
             ) /
             2.0
 
        newBearishTextLabel :=
             label.new(
                  x =
                       bar_index +
                       1,
                  y =
                       bearishTextY,
                  text =
                       "FVG",
                  xloc =
                       xloc.bar_index,
                  yloc =
                       yloc.price,
                  style =
                       label.style_none,
                  textcolor =
                       color.new(
                            color.white,
                            25
                       ),
                  size =
                       size.tiny
             )
 
    array.push(
         fvgTextLabels,
         newBearishTextLabel
    )
 
    newBearishFVG :=
         true
 
// =====================================================
// MANAGE FVG BOXES
//
// - Every independently significant FVG may print.
// - The box remains extended through the trading day.
// - Once the gap is FULLY FILLED, the box is deleted.
// - At the next trading day, any still-open box is capped
//   at the prior day's final bar.
// =====================================================
 
if barstate.isconfirmed
 
    int f =
         array.size(
              fvgBoxes
         ) -
         1
 
    while f >= 0
 
        box currentBox =
             array.get(
                  fvgBoxes,
                  f
             )
 
        int fvgDir =
             array.get(
                  fvgDirs,
                  f
             )
 
        float fvgTop =
             array.get(
                  fvgTops,
                  f
             )
 
        float fvgBottom =
             array.get(
                  fvgBottoms,
                  f
             )
 
        int createdBar =
             array.get(
                  fvgCreatedBars,
                  f
             )
 
        bool alreadyTouched =
             array.get(
                  fvgTouched,
                  f
             )
 
        bool laterBar =
             bar_index >
             createdBar
 
        if laterBar
 
            bool touchesFvg =
                 high >=
                 fvgBottom and
                 low <=
                 fvgTop
 
            if not alreadyTouched and
               touchesFvg
 
                array.set(
                     fvgTouched,
                     f,
                     true
                )
 
            bool fullyFilled =
                 fvgDir == 1 ?
                 low <=
                 fvgBottom :
                 high >=
                 fvgTop
 
            if fullyFilled
 
                if fvgDir == 1
 
                    bullishFvgFilled :=
                         true
 
                else
 
                    bearishFvgFilled :=
                         true
 
                if not na(
                     currentBox
                )
 
                    box.delete(
                         currentBox
                    )
 
                line currentMidpointLine =
                     array.get(
                          fvgMidpointLines,
                          f
                     )
 
                if not na(
                     currentMidpointLine
                )
 
                    line.delete(
                         currentMidpointLine
                    )
 
                label currentTextLabel =
                     array.get(
                          fvgTextLabels,
                          f
                     )
 
                if not na(
                     currentTextLabel
                )
 
                    label.delete(
                         currentTextLabel
                    )
 
                array.remove(
                     fvgBoxes,
                     f
                )
 
                array.remove(
                     fvgDirs,
                     f
                )
 
                array.remove(
                     fvgTops,
                     f
                )
 
                array.remove(
                     fvgBottoms,
                     f
                )
 
                array.remove(
                     fvgCreatedBars,
                     f
                )
 
                array.remove(
                     fvgAgeDays,
                     f
                )
 
                array.remove(
                     fvgTouched,
                     f
                )
 
                array.remove(
                     fvgFinalized,
                     f
                )
 
                array.remove(
                     fvgMidpointLines,
                     f
                )
 
                array.remove(
                     fvgTextLabels,
                     f
                )
 
        f -=
             1
 
// =====================================================
// LIMIT FVG HISTORY
// =====================================================
 
while array.size(
     fvgBoxes
) > maxFvgHistory
 
    box oldBox =
         array.shift(
              fvgBoxes
         )
 
    array.shift(
         fvgDirs
    )
 
    array.shift(
         fvgCreatedBars
    )
 
    if not na(
         oldBox
    )
 
        box.delete(
             oldBox
        )
 
    line oldMidpointLine =
         array.shift(
              fvgMidpointLines
         )
 
    if not na(
         oldMidpointLine
    )
 
        line.delete(
             oldMidpointLine
        )
 
    label oldTextLabel =
         array.shift(
              fvgTextLabels
         )
 
    if not na(
         oldTextLabel
    )
 
        label.delete(
             oldTextLabel
        )
 
    array.shift(
         fvgTops
    )
 
    array.shift(
         fvgBottoms
    )
 
    array.shift(
         fvgAgeDays
    )
 
    array.shift(
         fvgTouched
    )
 
    array.shift(
         fvgFinalized
    )
 
// =====================================================
// ALERTS
// =====================================================
 
alertcondition(
     newBullishFVG,
     "Bullish FVG",
     "Significant bullish FVG confirmed on {{ticker}}."
)
 
alertcondition(
     newBearishFVG,
     "Bearish FVG",
     "Significant bearish FVG confirmed on {{ticker}}."
)
 
alertcondition(
     bullishFvgFilled,
     "Bullish FVG Filled",
     "Bullish FVG fully filled on {{ticker}}."
)
 
alertcondition(
     bearishFvgFilled,
     "Bearish FVG Filled",
     "Bearish FVG fully filled on {{ticker}}."
)

← 返回列表