Flat Candle + True Gap v2

jazzijazzo · study · 369 行 · 点赞 17 · TradingView 原页

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

Pine Script

//@version=6
indicator(
     "Flat Candle + True Gap v2",
     overlay = true,
     max_lines_count = 500,
     max_labels_count = 500,
     max_boxes_count = 500)
 
// =====================================================
// SETTINGS — GENERAL
// =====================================================
 
groupHistory = "Historique"
 
keepAfterTouch = input.bool(
     true,
     "Conserver après croisement",
     tooltip = "Activé : la ligne/bande reste visible et s'arrête à la chandelle qui la touche. Désactivé : elle disparaît au toucher.",
     group = groupHistory)
 
maxFlatObjects = input.int(
     50,
     "Nombre de lignes Flat Candle à conserver",
     minval = 1,
     maxval = 500,
     tooltip = "Nombre maximal de lignes Flat Candle visibles sur le graphique, actives et historiques combinées.",
     group = groupHistory)
 
maxGapObjects = input.int(
     50,
     "Nombre de bandes True Gap à conserver",
     minval = 1,
     maxval = 500,
     tooltip = "Nombre maximal de bandes True Gap visibles sur le graphique, actives et historiques combinées.",
     group = groupHistory)
 
// =====================================================
// SETTINGS — FLAT CANDLE
// =====================================================
 
groupFlat = "Flat Candle"
 
showFlat = input.bool(true, "Afficher Flat Candles", group = groupFlat)
flatColor = input.color(color.yellow, "Couleur", group = groupFlat)
flatWidth = input.int(2, "Épaisseur", minval = 1, maxval = 5, group = groupFlat)
showFlatText = input.bool(true, "Texte FLAT CANDLE", group = groupFlat)
 
// =====================================================
// SETTINGS — TRUE GAP
// =====================================================
 
groupGap = "True Gap"
 
showTrueGap = input.bool(true, "Afficher True Gaps", group = groupGap)
gapColor = input.color(color.new(color.aqua, 80), "Couleur bande", group = groupGap)
gapBorderColor = input.color(color.aqua, "Couleur bordure", group = groupGap)
showGapText = input.bool(true, "Texte TRUE GAP", group = groupGap)
 
// =====================================================
// ARRAYS — FLAT CANDLES
// =====================================================
 
var line[] flatLines = array.new_line()
var label[] flatLabels = array.new_label()
var float[] flatPrices = array.new_float()
var int[] flatCreatedBars = array.new_int()
var bool[] flatActive = array.new_bool()
 
// =====================================================
// ARRAYS — TRUE GAPS
// =====================================================
 
var box[] gapBoxes = array.new_box()
var float[] gapTops = array.new_float()
var float[] gapBottoms = array.new_float()
var int[] gapCreatedBars = array.new_int()
var bool[] gapActive = array.new_bool()
 
// =====================================================
// MANAGE FLAT CANDLES
// =====================================================
 
if array.size(flatLines) > 0
    int i = array.size(flatLines) - 1
 
    while i >= 0
        line ln = array.get(flatLines, i)
        label lbl = array.get(flatLabels, i)
        float price = array.get(flatPrices, i)
        int createdBar = array.get(flatCreatedBars, i)
        bool isActive = array.get(flatActive, i)
 
        if isActive
            bool futureBar = bar_index > createdBar
 
            // First future candle whose wick/body reaches the level.
            bool touched =
                 futureBar and
                 high >= price and
                 low <= price
 
            if touched
                if keepAfterTouch
                    // Keep the historical line, but stop it exactly
                    // at the candle that first touches/crosses it.
                    line.set_extend(ln, extend.none)
                    line.set_x2(ln, bar_index)
                    line.set_y2(ln, price)
 
                    if not na(lbl)
                        label.set_x(lbl, bar_index)
                        label.set_y(lbl, price)
 
                    array.set(flatActive, i, false)
 
                else
                    line.delete(ln)
 
                    if not na(lbl)
                        label.delete(lbl)
 
                    array.remove(flatLines, i)
                    array.remove(flatLabels, i)
                    array.remove(flatPrices, i)
                    array.remove(flatCreatedBars, i)
                    array.remove(flatActive, i)
 
            else
                // Active line continues to the right.
                if not na(lbl)
                    label.set_x(lbl, bar_index)
                    label.set_y(lbl, price)
 
        i -= 1
 
// =====================================================
// MANAGE TRUE GAPS
// =====================================================
 
if array.size(gapBoxes) > 0
    int i = array.size(gapBoxes) - 1
 
    while i >= 0
        box gap = array.get(gapBoxes, i)
        float gapTop = array.get(gapTops, i)
        float gapBottom = array.get(gapBottoms, i)
        int createdBar = array.get(gapCreatedBars, i)
        bool isActive = array.get(gapActive, i)
 
        if isActive
            bool futureBar = bar_index > createdBar
 
            // Any wick/body contact with any part of the band.
            bool touched =
                 futureBar and
                 high >= gapBottom and
                 low <= gapTop
 
            if touched
                if keepAfterTouch
                    // Keep the historical band, but stop it exactly
                    // at the candle that first touches the gap.
                    box.set_extend(gap, extend.none)
                    box.set_right(gap, bar_index)
                    array.set(gapActive, i, false)
 
                else
                    box.delete(gap)
 
                    array.remove(gapBoxes, i)
                    array.remove(gapTops, i)
                    array.remove(gapBottoms, i)
                    array.remove(gapCreatedBars, i)
                    array.remove(gapActive, i)
 
        i -= 1
 
// =====================================================
// FLAT CANDLE DETECTION
// =====================================================
 
float bodyHigh = math.max(open, close)
float bodyLow = math.min(open, close)
 
// Exact definition:
// No upper wick.
bool flatHigh = high == bodyHigh
 
// No lower wick.
bool flatLow = low == bodyLow
 
// =====================================================
// CREATE FLAT CANDLES
// =====================================================
 
if barstate.isconfirmed and showFlat
 
    // FLAT HIGH
    if flatHigh
        float price = high
 
        line ln = line.new(
             x1 = bar_index,
             y1 = price,
             x2 = bar_index + 1,
             y2 = price,
             xloc = xloc.bar_index,
             extend = extend.right,
             color = flatColor,
             width = flatWidth)
 
        label lbl = na
 
        if showFlatText
            lbl := label.new(
                 x = bar_index,
                 y = price,
                 text = "FLAT CANDLE",
                 xloc = xloc.bar_index,
                 style = label.style_none,
                 textcolor = flatColor,
                 size = size.tiny)
 
        array.push(flatLines, ln)
        array.push(flatLabels, lbl)
        array.push(flatPrices, price)
        array.push(flatCreatedBars, bar_index)
        array.push(flatActive, true)
 
    // FLAT LOW
    if flatLow
        float price = low
 
        line ln = line.new(
             x1 = bar_index,
             y1 = price,
             x2 = bar_index + 1,
             y2 = price,
             xloc = xloc.bar_index,
             extend = extend.right,
             color = flatColor,
             width = flatWidth)
 
        label lbl = na
 
        if showFlatText
            lbl := label.new(
                 x = bar_index,
                 y = price,
                 text = "FLAT CANDLE",
                 xloc = xloc.bar_index,
                 style = label.style_none,
                 textcolor = flatColor,
                 size = size.tiny)
 
        array.push(flatLines, ln)
        array.push(flatLabels, lbl)
        array.push(flatPrices, price)
        array.push(flatCreatedBars, bar_index)
        array.push(flatActive, true)
 
// =====================================================
// TRUE GAP DETECTION
// =====================================================
 
bool bullishTrueGap =
     bar_index > 0 and
     low > high[1]
 
bool bearishTrueGap =
     bar_index > 0 and
     high < low[1]
 
// =====================================================
// CREATE TRUE GAPS
// =====================================================
 
if barstate.isconfirmed and showTrueGap
 
    // BULLISH TRUE GAP
    // Previous High -> Current Low
    if bullishTrueGap
        float top = low
        float bottom = high[1]
 
        box gap = box.new(
             left = bar_index - 1,
             top = top,
             right = bar_index,
             bottom = bottom,
             xloc = xloc.bar_index,
             extend = extend.right,
             bgcolor = gapColor,
             border_color = gapBorderColor,
             border_width = 1,
             text = showGapText ? "TRUE GAP" : "",
             text_color = gapBorderColor,
             text_size = size.tiny,
             text_halign = text.align_left,
             text_valign = text.align_center)
 
        array.push(gapBoxes, gap)
        array.push(gapTops, top)
        array.push(gapBottoms, bottom)
        array.push(gapCreatedBars, bar_index)
        array.push(gapActive, true)
 
    // BEARISH TRUE GAP
    // Current High -> Previous Low
    if bearishTrueGap
        float top = low[1]
        float bottom = high
 
        box gap = box.new(
             left = bar_index - 1,
             top = top,
             right = bar_index,
             bottom = bottom,
             xloc = xloc.bar_index,
             extend = extend.right,
             bgcolor = gapColor,
             border_color = gapBorderColor,
             border_width = 1,
             text = showGapText ? "TRUE GAP" : "",
             text_color = gapBorderColor,
             text_size = size.tiny,
             text_halign = text.align_left,
             text_valign = text.align_center)
 
        array.push(gapBoxes, gap)
        array.push(gapTops, top)
        array.push(gapBottoms, bottom)
        array.push(gapCreatedBars, bar_index)
        array.push(gapActive, true)
 
// =====================================================
// HISTORY LIMIT — FLAT CANDLES
// =====================================================
 
// Keep only the most recent N Flat Candle lines.
// This includes active lines and historical stopped lines.
while array.size(flatLines) > maxFlatObjects
    line oldestLine = array.shift(flatLines)
    label oldestLabel = array.shift(flatLabels)
 
    line.delete(oldestLine)
 
    if not na(oldestLabel)
        label.delete(oldestLabel)
 
    array.shift(flatPrices)
    array.shift(flatCreatedBars)
    array.shift(flatActive)
 
// =====================================================
// HISTORY LIMIT — TRUE GAPS
// =====================================================
 
// Keep only the most recent N True Gap bands.
// This includes active bands and historical stopped bands.
while array.size(gapBoxes) > maxGapObjects
    box oldestGap = array.shift(gapBoxes)
 
    box.delete(oldestGap)
 
    array.shift(gapTops)
    array.shift(gapBottoms)
    array.shift(gapCreatedBars)
    array.shift(gapActive)

← 返回列表