Fractal Candle Lens

EkamVerma · study · 217 行 · 点赞 13 · TradingView 原页

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

Pine Script

//@version=6
indicator("Fractal Candle Lens", shorttitle="FCL", overlay=true, max_boxes_count=100, max_lines_count=100, max_labels_count=100)
 
// ── Cluster inputs ──────────────────────────────────
count     = input.int(10, "Number of 1m candles", minval=2, maxval=40)
gapBars   = input.int(6,  "Gap from last chart candle (bars)", minval=1, maxval=100)
halfWidth = input.int(1,  "Candle half-width (bars)", minval=1, maxval=5)
upColor   = input.color(color.teal, "Bullish candle color")
dnColor   = input.color(color.red,  "Bearish candle color")
candleGap = input.int(1, "Gap between 1m candles (bars)", minval=0, maxval=5)
 
// ── Reversal inputs (same as your script) ───────────
gWick    = "Wick Reversal"
wickMult = input.float(2.5,  "Wick Multiplier", minval=0.5, step=0.5,  group=gWick)
bodyPct  = input.float(0.25, "Body Percentage", minval=0.1, step=0.05, group=gWick)
 
gExt        = "Extreme Reversal"
extBodySize = input.float(0.525, "Body Size",       minval=0.05, step=0.025, group=gExt)
extBarsBack = input.int(50,      "Bars Back",       minval=1, maxval=55,     group=gExt)
extBodyMult = input.float(2.0,   "Body Multiplier", minval=0.25, step=0.25,  group=gExt)
 
gOut        = "Outside Reversal"
outBarMult  = input.float(1.25, "Bar Multiplier", minval=0.05, step=0.05, group=gOut)
outBarsBack = input.int(50,     "Bars Back",      minval=1, maxval=55,    group=gOut)
 
gDoji   = "Doji Reversal"
dojiPct = input.float(0.10, "Percentage", minval=0.01, step=0.01, group=gDoji)
 
gShow       = "Display"
showWick    = input.bool(true, "Wick Reversal",    group=gShow)
showExtreme = input.bool(true, "Extreme Reversal", group=gShow)
showOutside = input.bool(true, "Outside Reversal", group=gShow)
showDoji    = input.bool(true, "Doji Reversal",    group=gShow)
bullCol     = input.color(color.lime, "Bullish circle color", group=gShow)
bearCol     = input.color(color.red,  "Bearish circle color", group=gShow)
 
// Extra completed candles fetched only so the averages (SMA 50 etc.) have history
warm  = 60
total = count + warm
 
// ── Completed 1m candles (index 0 = newest completed) ──
f_done(int n) =>
    array<float> a = array.new<float>()
    for i = 1 to n
        a.push(open[i])
        a.push(high[i])
        a.push(low[i])
        a.push(close[i])
    a
 
[sO, sH, sL, sC] = request.security(syminfo.tickerid, "1", [open, high, low, close], lookahead=barmerge.lookahead_on)
done = request.security(syminfo.tickerid, "1", f_done(total - 1), lookahead=barmerge.lookahead_off)
 
// ── Live 1m candle built tick by tick ───────────────
varip float lO = na
varip float lH = na
varip float lL = na
varip float lC = na
varip int   lastMin = na
 
int curMin = timenow - timenow % 60000
 
if barstate.isrealtime
    if na(lastMin)
        lastMin := curMin
        lO := sO
        lH := sH
        lL := sL
        lC := close
    else if curMin != lastMin
        lastMin := curMin
        lO := close
        lH := close
        lL := close
        lC := close
    else
        lH := math.max(lH, close)
        lL := math.min(lL, close)
        lC := close
 
// ── Helpers ─────────────────────────────────────────
f_avg(array<float> a, int idx, int n) =>
    float s = 0.0
    for k = idx - n + 1 to idx
        s += a.get(k)
    s / n
 
// Evaluates all reversal rules on candle number i of the arrays
f_sig(array<float> O, array<float> H, array<float> L, array<float> C, array<float> R, array<float> B, int i) =>
    float o  = O.get(i)
    float h  = H.get(i)
    float l  = L.get(i)
    float c  = C.get(i)
    float rg = R.get(i)
 
    float o1 = O.get(i - 1)
    float h1 = H.get(i - 1)
    float l1 = L.get(i - 1)
    float c1 = C.get(i - 1)
    float h2 = H.get(i - 2)
    float l2 = L.get(i - 2)
 
    float rg1   = R.get(i - 1)
    float body1 = B.get(i - 1)
    float rg2   = R.get(i - 2)
    float body2 = B.get(i - 2)
 
    float avgR50    = f_avg(R, i, 50)
    float avgBody   = f_avg(B, i, extBarsBack)
    float avgRngEx  = f_avg(R, i, extBarsBack)
    float avgRngOut = f_avg(R, i, outBarsBack)
    float sma10     = f_avg(C, i, 10)
 
    // 1) Wick
    bool wl1 = c > o and (o - l) >= (c - o) * wickMult and (h - c) <= rg * bodyPct
    bool wl2 = c < o and (c - l) >= (o - c) * wickMult and (h - o) <= rg * bodyPct
    bool wl3 = c == o and c != h and rg >= (h - c) * wickMult and (h - c) <= rg * bodyPct
    bool wl4 = o == h and c == h and rg >= avgR50
    bool wickLong = wl1 or wl2 or wl3 or wl4
 
    bool ws1 = c < o and (h - o) >= (o - c) * wickMult and (c - l) <= rg * bodyPct
    bool ws2 = c > o and (h - c) >= (c - o) * wickMult and (o - l) <= rg * bodyPct
    bool ws3 = c == o and c != l and rg >= (c - l) * wickMult and (c - l) <= rg * bodyPct
    bool ws4 = o == l and c == l and rg >= avgR50
    bool wickShort = ws1 or ws2 or ws3 or ws4
 
    // 2) Extreme
    bool extLong  = (o1 - c1) >= extBodySize * rg1 and rg1 > avgRngEx * extBodyMult and (o1 - c1) > avgBody and c > o
    bool extShort = (c1 - o1) >= extBodySize * rg1 and rg1 > avgRngEx * extBodyMult and (c1 - o1) > avgBody and o > c
 
    // 3) Outside
    bool outLong  = l < l1 and c > h1 and rg >= avgRngOut * outBarMult
    bool outShort = h > h1 and c < l1 and rg >= avgRngOut * outBarMult
 
    // 4) Doji
    bool ds1 = body1 <= rg1 * dojiPct and c < l1 and l1 > sma10 and c < o
    bool ds2 = c < l2 and not (c1 < l2) and body2 <= rg2 * dojiPct and c < o and l2 > sma10
    bool dojiShort = ds1 or ds2
 
    bool dl1 = body1 <= rg1 * dojiPct and c > h1 and h1 < sma10 and c > o
    bool dl2 = c > h2 and not (c1 > h2) and body2 <= rg2 * dojiPct and c > o and h2 < sma10
    bool dojiLong = dl1 or dl2
 
    bool bull = (showWick and wickLong)  or (showExtreme and extLong)  or (showOutside and outLong)  or (showDoji and dojiLong)
    bool bear = (showWick and wickShort) or (showExtreme and extShort) or (showOutside and outShort) or (showDoji and dojiShort)
    [bull, bear]
 
// ── Drawing storage ─────────────────────────────────
var array<box>   bodies = array.new<box>()
var array<line>  wicks  = array.new<line>()
var array<label> dots   = array.new<label>()
 
if barstate.islast
    // Wipe everything; circles vanish together with the candle that drops off
    while bodies.size() > 0
        box.delete(bodies.pop())
    while wicks.size() > 0
        line.delete(wicks.pop())
    while dots.size() > 0
        label.delete(dots.pop())
 
    // Build one continuous series, oldest = 0, live candle = total - 1
    array<float> O = array.new<float>(total, na)
    array<float> H = array.new<float>(total, na)
    array<float> L = array.new<float>(total, na)
    array<float> C = array.new<float>(total, na)
 
    for j = 0 to total - 2
        int src = (total - 2 - j) * 4
        if done.size() >= src + 4
            O.set(j, done.get(src))
            H.set(j, done.get(src + 1))
            L.set(j, done.get(src + 2))
            C.set(j, done.get(src + 3))
 
    O.set(total - 1, barstate.isrealtime ? lO : sO)
    H.set(total - 1, barstate.isrealtime ? lH : sH)
    L.set(total - 1, barstate.isrealtime ? lL : sL)
    C.set(total - 1, barstate.isrealtime ? lC : sC)
 
    array<float> R = array.new<float>(total, na)
    array<float> B = array.new<float>(total, na)
    for j = 0 to total - 1
        R.set(j, H.get(j) - L.get(j))
        B.set(j, math.abs(C.get(j) - O.get(j)))
 
    float dotOffset = nz(f_avg(R, total - 1, 10)) * 0.6
 
    int spacing = 2 * halfWidth + candleGap
    int startX  = bar_index + gapBars
 
    // k = 0 oldest shown (left) ... k = count-1 live candle (right)
    for k = 0 to count - 1
        int idx = total - count + k
        float o = O.get(idx)
        float h = H.get(idx)
        float l = L.get(idx)
        float c = C.get(idx)
        bool live = k == count - 1
 
        if not na(o) and not na(h) and not na(l) and not na(c)
            color col = c >= o ? upColor : dnColor
            int cx    = startX + k * spacing
 
            wicks.push(line.new(cx, h, cx, l, color=col, width=live ? 2 : 1))
            bodies.push(box.new(cx - halfWidth, math.max(o, c),
                                cx + halfWidth, math.min(o, c),
                                border_color=live ? color.white : col,
                                bgcolor=col))
 
            [bull, bear] = f_sig(O, H, L, C, R, B, idx)
            if bull
                dots.push(label.new(cx, l - dotOffset, "⦿", style=label.style_none,
                                    textcolor=bullCol, size=size.tiny))
            if bear
                dots.push(label.new(cx, h + dotOffset, "⦿", style=label.style_none,
                                    textcolor=bearCol, size=size.tiny))

← 返回列表