EdgeFlow Pullback [CHE]

chervolino · study · 240 行 · 点赞 639 · TradingView 原页

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

Pine Script

//@version=6
indicator("EdgeFlow Pullback [CHE]", overlay=true, max_labels_count=500)
 
// ─────────────────────────────────────────────────────────────────────────────
// T3 CALCULATION
// calcT3: Tim Tillson's T3 using an EMA cascade. The "volumeFactor" (0–1) controls curvature.
// Inputs:
//   source: price series to smooth
//   length: EMA period for each stage in the cascade
//   volumeFactor: smoothing coefficient used to combine the stages
// Output:
//   smoothed T3 series
// ─────────────────────────────────────────────────────────────────────────────
calcT3(source, length, volumeFactor) =>
    ema1 = ta.ema(source, length)
    ema2 = ta.ema(ema1, length)
    ema3 = ta.ema(ema2, length)
    ema4 = ta.ema(ema3, length)
    ema5 = ta.ema(ema4, length)
    ema6 = ta.ema(ema5, length)
    coeff1 = volumeFactor
    coeff2 = coeff1 * 3
    coeff3 = coeff1 * 3
    coeff4 = coeff1
    // T3 linear combination of the last 5 cascade stages
    ema6 * coeff4 + ema5 * -coeff3 + ema4 * coeff2 + ema3 * -coeff1 + ema2
 
// ─────────────────────────────────────────────────────────────────────────────
// INPUTS
// typicalPrice (HLC3) and tunables for T3, baseline EMA, windows, display, debug
// ─────────────────────────────────────────────────────────────────────────────
typicalPrice          = (high + low + close) / 3.0
t3Length              = input.int(3,     "T3 length",            minval=1, step=1, inline="t3", group="T3",                 tooltip="Period used for the EMA cascade that forms the T3. Higher = smoother/slower.")
t3VolumeFactor        = input.float(0.7, "T3 volume factor",     minval=0, maxval=1, step=0.1, inline="t3", group="T3",     tooltip="Smoothing factor (0–1) controlling T3 curvature. Typical values: 0.5–0.9.")
emaBaselineLength     = input.int(144,   "EMA length (baseline)", minval=1,                     group="Baseline",            tooltip="EMA period used as the baseline for crossover tests.")
maxBarsForSequence    = input.int(30,    "Max bars for full sequence (signal window)", minval=1, maxval=200, group="Signal logic", tooltip="Maximum number of bars to complete steps 1→4. If exceeded, the sequence resets.")
evaluationBars        = input.int(30,    "Evaluation window after signal (bars)",      minval=1, maxval=200, group="Post-signal evaluation", tooltip="Number of bars after the signal before labeling the outcome as OK/Fail.")
labelsToKeepCount     = input.int(20,    "Show only the last N labels",                minval=1, maxval=200, group="Labels & display", tooltip="Keep only the most recent labels to avoid chart clutter.")
showDebugInfo         = input.bool(false,"Debug: show step/timer",                                      group="Debug",         tooltip="Enable debug info (step index and elapsed bars).")
 
// ─────────────────────────────────────────────────────────────────────────────
// SERIES
// T3 line and baseline EMA computed from typicalPrice
// ─────────────────────────────────────────────────────────────────────────────
t3Line        = calcT3(typicalPrice, t3Length, t3VolumeFactor)
baselineEMA   = ta.ema(typicalPrice, emaBaselineLength)
 
// ─────────────────────────────────────────────────────────────────────────────
// PLOTS
// Color T3 green when above price, red when below. Plot EMA, T3, and HLC3.
// ─────────────────────────────────────────────────────────────────────────────
t3LineColor = t3Line < typicalPrice ? color.new(color.green, 0) : color.new(color.red, 0)
plot(baselineEMA,   "EMA ",     color=color.orange, linewidth=2)
plot(t3Line,        "T3",       color=t3LineColor,  linewidth=2)
plot(typicalPrice,  "HLC3",     color=color.gray)
 
// ─────────────────────────────────────────────────────────────────────────────
// CONDITIONS (LONG SEQUENCE)
// Step logic (1→4) to detect a pullback and continuation above the EMA
// ─────────────────────────────────────────────────────────────────────────────
longCondT3Falling             = t3Line < t3Line[1]                 // 1) T3 falling
longCondCrossunderHLC3EMA     = ta.crossunder(typicalPrice, baselineEMA) // 2) HLC3 below EMA
longCondT3Rising              = t3Line > t3Line[1]                 // 3) T3 rising
longCondCrossoverHLC3EMA      = ta.crossover(typicalPrice, baselineEMA)  // 4) HLC3 above EMA
 
// ─────────────────────────────────────────────────────────────────────────────
// LATCHED STATE MACHINE (LONG)
// longStepIndex: 0=idle, 1..3=running, 4=done
// longStartBarIndex: bar_index where the sequence started
// longElapsedBars: bars elapsed since start; sequence times out after maxBarsForSequence
// ─────────────────────────────────────────────────────────────────────────────
var int longStepIndex      = 0
var int longStartBarIndex  = na
 
if longStepIndex == 0 and longCondT3Falling
    longStepIndex     := 1
    longStartBarIndex := bar_index
 
longElapsedBars = na(longStartBarIndex) ? na : (bar_index - longStartBarIndex)
 
if longStepIndex > 0 and not na(longElapsedBars) and longElapsedBars > (maxBarsForSequence - 1)
    longStepIndex     := 0
    longStartBarIndex := na
 
if longStepIndex == 1 and longCondCrossunderHLC3EMA
    longStepIndex := 2
if longStepIndex == 2 and longCondT3Rising
    longStepIndex := 3
if longStepIndex == 3 and longCondCrossoverHLC3EMA
    longStepIndex := 4
 
longSignal = (longStepIndex == 4) and not na(longElapsedBars) and longElapsedBars <= (maxBarsForSequence - 1)
 
if longSignal
    longStepIndex     := 0
    longStartBarIndex := na
 
// ─────────────────────────────────────────────────────────────────────────────
// CONDITIONS (SHORT SEQUENCE)
// Mirror of LONG conditions for the short side
// ─────────────────────────────────────────────────────────────────────────────
shortCondT3Rising             = t3Line > t3Line[1]                  // 1) T3 rising
shortCondCrossoverHLC3EMA     = ta.crossover(typicalPrice, baselineEMA)  // 2) HLC3 above EMA
shortCondT3Falling            = t3Line < t3Line[1]                  // 3) T3 falling
shortCondCrossunderHLC3EMA    = ta.crossunder(typicalPrice, baselineEMA) // 4) HLC3 below EMA
 
// ─────────────────────────────────────────────────────────────────────────────
// LATCHED STATE MACHINE (SHORT)
// shortStepIndex: 0=idle, 1..3=running, 4=done
// shortStartBarIndex: bar_index where the sequence started
// shortElapsedBars: bars elapsed since start; sequence times out after maxBarsForSequence
// ─────────────────────────────────────────────────────────────────────────────
var int shortStepIndex      = 0
var int shortStartBarIndex  = na
 
if shortStepIndex == 0 and shortCondT3Rising
    shortStepIndex     := 1
    shortStartBarIndex := bar_index
 
shortElapsedBars = na(shortStartBarIndex) ? na : (bar_index - shortStartBarIndex)
 
if shortStepIndex > 0 and not na(shortElapsedBars) and shortElapsedBars > (maxBarsForSequence - 1)
    shortStepIndex     := 0
    shortStartBarIndex := na
 
if shortStepIndex == 1 and shortCondCrossoverHLC3EMA
    shortStepIndex := 2
if shortStepIndex == 2 and shortCondT3Falling
    shortStepIndex := 3
if shortStepIndex == 3 and shortCondCrossunderHLC3EMA
    shortStepIndex := 4
 
shortSignal = (shortStepIndex == 4) and not na(shortElapsedBars) and shortElapsedBars <= (maxBarsForSequence - 1)
 
if shortSignal
    shortStepIndex     := 0
    shortStartBarIndex := na
 
// ─────────────────────────────────────────────────────────────────────────────
// MARKERS (OPTIONAL)
// Visual markers for completed LONG/SHORT sequences and EMA cross events
// ─────────────────────────────────────────────────────────────────────────────
plotshape(longSignal,                          "Long Marker",  style=shape.triangleup,   location=location.belowbar, size=size.tiny, text="PB Long")
plotshape(shortSignal,                         "Short Marker", style=shape.triangledown, location=location.abovebar, size=size.tiny, text="PB Short", color=color.red)
plotshape(longCondCrossunderHLC3EMA, title="Crossunder HLC3/EMA", style=shape.cross, location=location.abovebar, size=size.tiny, color=color.red)
plotshape(longCondCrossoverHLC3EMA,  title="Crossover HLC3/EMA",  style=shape.cross, location=location.belowbar, size=size.tiny, color=color.lime)
 
// ─────────────────────────────────────────────────────────────────────────────
// ALERTS (OPTIONAL)
// Trigger when the full sequence completes within the allowed window
// ─────────────────────────────────────────────────────────────────────────────
alertcondition(longSignal,  "Pullback LONG complete")
alertcondition(shortSignal, "Pullback SHORT complete")
 
// ─────────────────────────────────────────────────────────────────────────────
// LABEL MANAGEMENT VIA ARRAYS
// We create a label for each signal, keep it "pending" for `evaluationBars`,
// then stamp it as OK/Fail based on the position of HLC3 vs EMA at evaluation time.
// type: +1 = LONG, -1 = SHORT
// ─────────────────────────────────────────────────────────────────────────────
var label[] labelArray                 = array.new_label()
var int[]   labelBornBarIndexArray     = array.new_int()
var int[]   signalTypeArray            = array.new_int()
var int[]   signalStateArray           = array.new_int()   // 0=pending, 1=ok, -1=fail
var float[] yAnchorPriceArray          = array.new_float() // preserve original y position
 
// createSignalLabel: builds the initial pending label
createSignalLabel(signalType, xBarIndex, yPrice) =>
    string title = signalType == 1 ? "Pullback LONG" : "Pullback SHORT"
    label newLbl = label.new(
     xBarIndex, yPrice,
     title + "\n⏳ 0/" + str.tostring(evaluationBars),
     xloc=xloc.bar_index,
     yloc=signalType == 1 ? yloc.belowbar : yloc.abovebar,
     style=signalType == 1 ? label.style_label_up : label.style_label_down,
     textcolor=color.white,
     color=color.new(color.gray, 0),
     size=size.small)
    newLbl
 
// pushLabelToArrays: registers label + metadata, and trims older labels to avoid clutter
pushLabelToArrays(labelObj, signalType, bornBarIndex, yAnchorPrice) =>
    array.push(labelArray, labelObj)
    array.push(labelBornBarIndexArray, bornBarIndex)
    array.push(signalTypeArray, signalType)
    array.push(signalStateArray, 0)
    array.push(yAnchorPriceArray, yAnchorPrice)
    // Keep only the last N visible
    while array.size(labelArray) > labelsToKeepCount
        label toDelete = array.shift(labelArray)
        array.shift(labelBornBarIndexArray)
        array.shift(signalTypeArray)
        array.shift(signalStateArray)
        array.shift(yAnchorPriceArray)
        label.delete(toDelete)
 
// On signal: create label and register it
if longSignal
    newLabelObj = createSignalLabel(1, bar_index, low)
    pushLabelToArrays(newLabelObj, 1, bar_index, low)
 
if shortSignal
    newLabelObj = createSignalLabel(-1, bar_index, high)
    pushLabelToArrays(newLabelObj, -1, bar_index, high)
 
// Per bar: update all existing labels (guard against empty array)
labelCount = array.size(labelArray)
if labelCount > 0
    for i = 0 to labelCount - 1
        labelObj       = array.get(labelArray, i)
        bornBarIndex   = array.get(labelBornBarIndexArray, i)
        signalType     = array.get(signalTypeArray, i)
        signalState    = array.get(signalStateArray, i)
        yAnchorPrice   = array.get(yAnchorPriceArray, i)
        ageBars        = bar_index - bornBarIndex
 
        baseTitle  = signalType == 1 ? "Pullback LONG" : "Pullback SHORT"
 
        if signalState == 0
            // Pending phase with countdown
            label.set_text(labelObj, baseTitle + "\n⏳ " + str.tostring(math.min(ageBars, evaluationBars)) + "/" + str.tostring(evaluationBars))
            label.set_color(labelObj, color.new(color.gray, 0))
            label.set_textcolor(labelObj, color.white)
            // Keep position anchored at origin bar/price
            label.set_x(labelObj, bornBarIndex)
            label.set_y(labelObj, yAnchorPrice)
 
            // Evaluate once the countdown reaches `evaluationBars`
            if ageBars >= evaluationBars
                bool isOk      = signalType == 1 ? (typicalPrice >= baselineEMA) : (typicalPrice <= baselineEMA)
                array.set(signalStateArray, i, isOk ? 1 : -1)
                statusSymbol   = isOk ? "✔" : "✖"
                statusColor    = isOk ? color.new(color.green, 0) : color.new(color.red, 0)
                label.set_text(labelObj, statusSymbol + " " + baseTitle + "\n" + (isOk ? "Condition met" : "Condition failed"))
                label.set_color(labelObj, statusColor)
                label.set_textcolor(labelObj, color.white)
        else
            // Keep final status and original position
            label.set_x(labelObj, bornBarIndex)
            label.set_y(labelObj, yAnchorPrice)

← 返回列表