Pattern-Adam & Eve [KG]

kingshukghosh71 · study · 390 行 · 点赞 458 · TradingView 原页

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

Pine Script

//@version=6
indicator('Pattern-Adam & Eve [KG]', shorttitle = 'Adam & Eve Pattern', overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500, max_polylines_count = 100)
 
// =====================================================================
//  ADAM & EVE PATTERN  
//  ---------------------------------------------------------------
//  BULLISH : prior bearish trend -> Adam (sharp V bottom) -> neckline peak
//            -> Eve (rounded U bottom) -> neckline breakout -> bullish trend
//  BEARISH : mirror image (Adam = sharp V top, Eve = rounded U top)
//
//  Pivots, neckline, V/U shape, shading, labels and
//  arrows are all computed from live price data (ATR-scaled).
// =====================================================================
 
// ------------------------------ INPUTS ------------------------------
grpD = 'Detection'
pivLen = input.int(5, 'Pivot Length (bars each side)', minval = 2, group = grpD)
atrLen = input.int(14, 'ATR Length', minval = 1, group = grpD)
tolMult = input.float(0.8, 'Max difference between Adam & Eve bottoms (x ATR)', minval = 0.05, step = 0.05, group = grpD)
minDepth = input.float(1.5, 'Min pattern depth: neckline to bottoms (x ATR)', minval = 0.2, step = 0.1, group = grpD)
minSep = input.int(6, 'Min bars between Adam & Eve', minval = 2, group = grpD)
maxSep = input.int(60, 'Max bars between Adam & Eve', minval = 5, group = grpD)
trendMult = input.float(2.0, 'Min prior trend move into Adam (x ATR)', minval = 0.0, step = 0.25, group = grpD)
trendLook = input.int(25, 'Prior trend lookback (bars)', minval = 3, group = grpD)
maxBreak = input.int(30, 'Max bars to wait for neckline breakout', minval = 1, group = grpD)
 
grpS = 'Shape (V vs U)'
bandMult = input.float(0.35, 'Bottom band height (x ATR)', minval = 0.05, step = 0.05, group = grpS)
adamMaxW = input.int(4, 'Adam (V): MAX bars inside bottom band', minval = 1, group = grpS)
eveMinW = input.int(6, 'Eve (U): MIN bars inside bottom band', minval = 2, group = grpS)
 
grpV = 'Visuals'
showBull = input.bool(true, 'Show Bullish Pattern', group = grpV)
showBear = input.bool(true, 'Show Bearish Pattern', group = grpV)
showShade = input.bool(true, 'Shade Adam / Eve areas', group = grpV)
showTrend = input.bool(true, 'Show trend arrows', group = grpV)
showLbl = input.bool(true, 'Show labels (Adam / Eve / V + U / Breakout)', group = grpV)
 
cBullAdam = input.color(color.new(#4f86f7, 72), 'Bullish: Adam fill', group = grpV)
cBullEve = input.color(color.new(#ff8aa1, 62), 'Bullish: Eve fill', group = grpV)
cBearAdam = input.color(color.new(#5bd68a, 65), 'Bearish: Adam fill', group = grpV)
cBearEve = input.color(color.new(#5bd68a, 65), 'Bearish: Eve fill', group = grpV)
cLblBg = input.color(#2f5fd0, 'Adam / Eve label background', group = grpV)
cUp = input.color(#1fb25a, 'Up-trend / bullish arrow color', group = grpV)
cDn = input.color(#e0302f, 'Down-trend / bearish arrow color', group = grpV)
 
grpG = 'GALLERY'
showGallery = input.bool(true, 'Show Reference Gallery (ON / OFF)', group = grpG)
galOff = input.int(30, 'Gallery X offset from last bar', minval = 1, maxval = 300, group = grpG)
galBg = input.bool(true, 'Gallery panel background', group = grpG)
galBgCol = input.color(#000000, 'Panel background', group = grpG)
galTxt = input.color(color.white, 'Gallery text colour', group = grpG)
galW = input.int(40, 'Gallery width (bars)', minval = 40, maxval = 200, group = grpG)
galLook = input.int(90, 'Gallery height = price range of last N bars', minval = 20, maxval = 500, group = grpG)
galSz = input.string('small', 'Gallery text size', options = ['tiny', 'small', 'normal', 'large'], group = grpG)
 
// ------------------------------ TYPES -------------------------------
type Pat
	float lowP = na // last pivot extreme (signed space)  -> Adam candidate
	int lowB = na
	bool pend = false // pattern found, waiting for breakout
	float neck = na
	float aP = na
	int aB = na
	float eP = na
	int eB = na
	int hB = na // bar of the neckline peak between Adam and Eve
	int sB = na // left start of Adam
	float prP = na // start price of prior trend
	int prB = na
	int pendBar = na
 
// ---------------- Signed-space helpers (dir = +1 bull, -1 bear) ------
 
sLow(int off, int dir) =>
    int o = math.max(off, 0)
    float v = dir == 1 ? low[o] : -high[o]
    off < 0 ? na : v
sHigh(int off, int dir) =>
    int o = math.max(off, 0)
    float v = dir == 1 ? high[o] : -low[o]
    off < 0 ? na : v
sClose(int off, int dir) =>
    int o = math.max(off, 0)
    float v = dir == 1 ? close[o] : -close[o]
    off < 0 ? na : v
 
// Number of contiguous bars around a bottom that stay within a band -> measures V (few) vs U (many)
bandWidth(int off, float level, int dir, float band) =>
    int cnt = 1
    int k = 1
    while k < 100 and sLow(off + k, dir) <= level + band
        cnt := cnt + 1
        k := k + 1
        k
    k := 1
    while k < 100 and off - k >= 0 and sLow(off - k, dir) <= level + band
        cnt := cnt + 1
        k := k + 1
        k
    cnt
 
atrG = ta.atr(atrLen)
 
// ------------------------------ DRAWING -----------------------------
drawPattern(Pat p, int dir, color cAdam, color cEve) =>
    int bb = bar_index
    float nk = p.neck
    float rn = dir * nk
    float dA = nk - p.aP
    float dE = nk - p.eP
    float dM = math.max(dA, dE)
 
    // Neckline
    line.new(p.sB, rn, bb + 3, rn, color = chart.fg_color, width = 2)
 
    if showShade
        // ADAM - V shape (triangle between neckline and the V bottom)
        array<chart.point> ap = array.new<chart.point>()
        ap.push(chart.point.from_index(p.sB, rn))
        ap.push(chart.point.from_index(p.aB, dir * p.aP))
        ap.push(chart.point.from_index(p.hB, rn))
        polyline.new(ap, closed = true, line_color = color.new(cAdam, 0), fill_color = cAdam, line_width = 1)
 
        // EVE - U shape (smooth rounded bowl, vertex placed at the real Eve bottom)
        int N = 24
        array<chart.point> ep = array.new<chart.point>()
        for j = 0 to N by 1
            int x = p.hB + math.round((bb - p.hB) * j / N)
            float y = nk
            if x <= p.eB
                float s = (x - p.hB) / math.max(1, p.eB - p.hB)
                y := nk - dE * (1.0 - (1.0 - s) * (1.0 - s))
                y
            else
                float s2 = (x - p.eB) / math.max(1, bb - p.eB)
                y := nk - dE * (1.0 - s2 * s2)
                y
            ep.push(chart.point.from_index(x, dir * y))
        polyline.new(ep, closed = true, line_color = color.new(cEve, 0), fill_color = cEve, line_width = 1)
 
    if showTrend
        // Prior trend arrow into Adam
        color cPre = dir == 1 ? cDn : cUp
        line.new(p.prB, dir * p.prP, p.aB, dir * p.aP, color = cPre, width = 2, style = line.style_arrow_right)
        // Post-breakout projected move (measured move = pattern height)
        color cPost = dir == 1 ? cUp : cDn
        int x2 = bb + math.max(6, p.eB - p.aB)
        float y2 = dir * (nk + dM)
        line.new(bb, rn, x2, y2, color = cPost, width = 2, style = line.style_arrow_right)
        if showLbl
            label.new(math.round((p.prB + p.aB) / 2.0), dir * (p.prP + p.aP) / 2.0, dir == 1 ? 'Bearish Trend' : 'Bullish Trend', style = label.style_none, textcolor = cPre, size = size.small)
            label.new(math.round((bb + x2) / 2.0), dir * (nk + dM / 2.0), dir == 1 ? 'Bullish Trend' : 'Bearish Trend', style = label.style_none, textcolor = cPost, size = size.small)
 
    if showLbl
        label.new(p.aB, dir * p.aP, 'Adam', style = dir == 1 ? label.style_label_up : label.style_label_down, color = cLblBg, textcolor = color.white, size = size.small)
        label.new(p.eB, rn + dir * 0.55 * dM, 'Eve', style = dir == 1 ? label.style_label_down : label.style_label_up, color = cLblBg, textcolor = color.white, size = size.small)
        label.new(math.round((p.sB + bb) / 2.0), rn + dir * 1.5 * dM, 'V + U', style = label.style_none, textcolor = chart.fg_color, size = size.large)
        label.new(bb, dir == 1 ? low : high, 'Neckline Breakout', style = dir == 1 ? label.style_label_up : label.style_label_down, color = dir == 1 ? cUp : cDn, textcolor = color.white, size = size.small)
    true
 
// ------------------------------ DETECTION ---------------------------
detect(Pat p, int dir, float pivRaw, bool on, color cAdam, color cEve) =>
    int result = 0
    float atrV = atrG
 
    // 1) Pending pattern: invalidate or confirm neckline breakout
    if p.pend
        if bar_index - p.pendBar > maxBreak or sLow(0, dir) < p.eP - tolMult * atrV
            p.pend := false
            p.pend
        else if sClose(0, dir) > p.neck and bar_index > p.pendBar
            if on
                drawPattern(p, dir, cAdam, cEve)
                result := 1
                result
            p.pend := false
            p.pend
 
    // 2) New confirmed pivot -> test (previous pivot = Adam, this pivot = Eve)
    if not na(pivRaw) and not na(atrV)
        float pv = dir == 1 ? pivRaw : -pivRaw
        int pb = bar_index - pivLen
        int offE = pivLen
        if not na(p.lowB)
            int sep = pb - p.lowB
            if sep >= minSep and sep <= maxSep
                int offA = bar_index - p.lowB
 
                // neckline = highest point between Adam and Eve
                float nh = sHigh(offA, dir)
                int nhOff = offA
                for i = offE to offA by 1
                    if sHigh(i, dir) > nh
                        nh := sHigh(i, dir)
                        nhOff := i
                        nhOff
 
                // prior trend into Adam
                float prH = sHigh(offA + 1, dir)
                int prOff = offA + 1
                for i = offA + 1 to offA + trendLook by 1
                    if sHigh(i, dir) > prH
                        prH := sHigh(i, dir)
                        prOff := i
                        prOff
 
                float band = bandMult * atrV
                bool equal = math.abs(pv - p.lowP) <= tolMult * atrV
                bool deep = nh - math.max(pv, p.lowP) >= minDepth * atrV
                bool trendOk = prH - p.lowP >= trendMult * atrV
                int wA = bandWidth(offA, p.lowP, dir, band)
                int wE = bandWidth(offE, pv, dir, band)
                bool isV = wA <= adamMaxW
                bool isU = wE >= eveMinW
 
                if equal and deep and trendOk and isV and isU
                    // left start of Adam: nearest bar left of Adam that touched the neckline
                    int sOff = offA
                    bool found = false
                    for i = offA + 1 to offA + 40 by 1
                        if not found and sHigh(i, dir) >= nh
                            sOff := i
                            found := true
                            found
                    int sBar = found ? bar_index - sOff : p.lowB - (bar_index - nhOff - p.lowB)
 
                    p.pend := true
                    p.neck := nh
                    p.aP := p.lowP
                    p.aB := p.lowB
                    p.eP := pv
                    p.eB := pb
                    p.hB := bar_index - nhOff
                    p.sB := sBar
                    p.prP := prH
                    p.prB := bar_index - prOff
                    p.pendBar := bar_index
                    p.pendBar
 
        p.lowP := pv
        p.lowB := pb
        p.lowB
    result
 
// ------------------------------ MAIN --------------------------------
pl = ta.pivotlow(low, pivLen, pivLen)
ph = ta.pivothigh(high, pivLen, pivLen)
 
var Pat bull = Pat.new()
var Pat bear = Pat.new()
 
bullEv = detect(bull, 1, pl, showBull, cBullAdam, cBullEve)
bearEv = detect(bear, -1, ph, showBear, cBearAdam, cBearEve)
 
plotshape(bullEv == 1, 'Bullish Breakout', shape.triangleup, location.belowbar, cUp, size = size.small)
plotshape(bearEv == 1, 'Bearish Breakout', shape.triangledown, location.abovebar, cDn, size = size.small)
 
alertcondition(bullEv == 1, 'Bullish Adam & Eve Breakout', 'Bullish Adam and Eve pattern - neckline breakout')
alertcondition(bearEv == 1, 'Bearish Adam & Eve Breakout', 'Bearish Adam and Eve pattern - neckline breakdown')
 
// =====================================================================
//  GALLERY  -  "Types of Adam and Eve chart Pattern" (bearish + bullish)
// =====================================================================
var array<polyline> gPoly = array.new<polyline>()
var array<label> gLbl = array.new<label>()
 
gSz = galSz == 'tiny' ? size.tiny : galSz == 'normal' ? size.normal : galSz == 'large' ? size.large : size.small
gYlo = ta.lowest(low, galLook)
gYhi = ta.highest(high, galLook)
gX0 = bar_index + math.min(galOff, 499 - galW)
gH = math.max(nz(gYhi - gYlo), syminfo.mintick * 20)
 
gx(float px) =>
    int(gX0 + math.round(px / 720.0 * galW))
gy(float py) =>
    gYlo + (1.0 - (py - 560.0) / 720.0) * gH
 
gClear() =>
    while gPoly.size() > 0
        polyline.delete(gPoly.pop())
    while gLbl.size() > 0
        label.delete(gLbl.pop())
    true
 
gRect(float x1, float y1, float x2, float y2, color fill, color ln) =>
    array<chart.point> cp = array.new<chart.point>()
    cp.push(chart.point.from_index(gx(x1), gy(y1)))
    cp.push(chart.point.from_index(gx(x2), gy(y1)))
    cp.push(chart.point.from_index(gx(x2), gy(y2)))
    cp.push(chart.point.from_index(gx(x1), gy(y2)))
    gPoly.push(polyline.new(cp, closed = true, line_color = ln, fill_color = fill, line_width = 2))
    true
 
gTxt(float xc, float yc, string txt, color bg, color tc, string sz) =>
    gLbl.push(label.new(gx(xc), gy(yc), txt, xloc = xloc.bar_index, yloc = yloc.price, color = bg, style = label.style_label_center, textcolor = tc, size = sz))
    true
 
gShape(array<float> pts, color fill, color ln) =>
    array<chart.point> cp = array.new<chart.point>()
    int n = int(pts.size() / 2)
    for i = 0 to n - 1 by 1
        cp.push(chart.point.from_index(gx(pts.get(2 * i)), gy(pts.get(2 * i + 1))))
    gPoly.push(polyline.new(cp, closed = true, line_color = ln, fill_color = fill, line_width = 1))
    true
 
// open poly-line stroke
gStroke(array<float> pts, color c, int w) =>
    array<chart.point> cp = array.new<chart.point>()
    int n = int(pts.size() / 2)
    for i = 0 to n - 1 by 1
        cp.push(chart.point.from_index(gx(pts.get(2 * i)), gy(pts.get(2 * i + 1))))
    gPoly.push(polyline.new(cp, closed = false, line_color = c, line_width = w))
    true
 
// filled arrow head at (x2,y2) pointing away from (x1,y1) - computed in picture pixels
gHead(float x1, float y1, float x2, float y2, color c) =>
    float dx = x2 - x1
    float dy = y2 - y1
    float ln = math.max(math.sqrt(dx * dx + dy * dy), 0.0001)
    float ux = dx / ln
    float uy = dy / ln
    float hx = x2 - ux * 20.0
    float hy = y2 - uy * 20.0
    float qx = -uy * 9.0
    float qy = ux * 9.0
    gShape(array.from(x2, y2, hx + qx, hy + qy, hx - qx, hy - qy), c, c)
 
// zig-zag trend line with an arrow head on the last segment
gZig(array<float> pts, color c, int w) =>
    gStroke(pts, c, w)
    int n = int(pts.size() / 2)
    gHead(pts.get(2 * n - 4), pts.get(2 * n - 3), pts.get(2 * n - 2), pts.get(2 * n - 1), c)
 
// rounded U / dome between x0p and x1p anchored on yNeck, vertex at yPeak
gBowl(float x0p, float x1p, float yNeck, float yPeak, color fill, color ln) =>
    array<float> pts = array.new<float>()
    int N = 24
    for j = 0 to N by 1
        float t = j * 1.0 / N
        pts.push(x0p + t * (x1p - x0p))
        pts.push(yNeck + (yPeak - yNeck) * (1.0 - math.pow(2.0 * t - 1.0, 2)))
    gShape(pts, fill, ln)
 
gBuild() =>
    color none = color.new(color.black, 100)
    color yel = #f2c230
    color dark = #1b1b1b
    color bluLn = color.new(#3b7de0, 0)
    color pnkLn = color.new(#e0657a, 0)
    color grnLn = color.new(#3fae5a, 0)
 
    // ---- panel background (created first = underneath everything)
    if galBg
        gRect(0, 560, 720, 1280, galBgCol, galBgCol)
 
    // ---- header
    gRect(58, 568, 657, 680, yel, yel)
    gTxt(357, 624, 'Types of Adam and Eve chart\nPattern', yel, dark, size.large)
 
    // ---- BEARISH panel (bullish trend -> double top -> bearish trend)
    gRect(10, 685, 475, 985, none, yel)
    gTxt(215, 705, 'Bearish Adam and Eve Pattern', none, galTxt, gSz)
    gShape(array.from(135.0, 803.0, 172.0, 733.0, 205.0, 803.0), cBearAdam, grnLn) // Adam (V top)
    gBowl(205.0, 310.0, 803.0, 730.0, cBearEve, bluLn) // Eve  (U top)
    gStroke(array.from(97.0, 803.0, 335.0, 803.0), galTxt, 2) // neckline
    gTxt(217, 816, 'Neckline Breakout', none, galTxt, gSz)
    gTxt(378, 816, 'Bearish Trend', none, galTxt, gSz)
    gTxt(83, 852, 'Bullish Trend', none, galTxt, gSz)
    gZig(array.from(73.0, 980.0, 92.0, 912.0, 104.0, 935.0, 120.0, 868.0, 131.0, 890.0, 137.0, 808.0), cUp, 2)
    gZig(array.from(318.0, 806.0, 335.0, 865.0, 352.0, 840.0, 372.0, 905.0, 388.0, 880.0, 420.0, 952.0), cDn, 2)
 
    // ---- BULLISH panel (bearish trend -> double bottom -> bullish trend)
    gRect(240, 995, 705, 1280, none, yel)
    gShape(array.from(375.0, 1172.0, 412.0, 1224.0, 450.0, 1172.0), cBullAdam, pnkLn) // Adam (V bottom)
    gBowl(450.0, 570.0, 1172.0, 1232.0, cBullEve, pnkLn) // Eve  (U bottom)
    gStroke(array.from(345.0, 1172.0, 608.0, 1172.0), galTxt, 2) // neckline
    gTxt(303, 1120, 'Bearish Trend', none, galTxt, gSz)
    gTxt(478, 1158, 'Neckline Breakout', none, galTxt, gSz)
    gTxt(628, 1138, 'Bullish Trend', none, galTxt, gSz)
    gTxt(489, 1259, 'Bullish Adam and Eve Pattern', none, galTxt, gSz)
    gZig(array.from(258.0, 1022.0, 280.0, 1080.0, 290.0, 1065.0, 318.0, 1118.0, 328.0, 1108.0, 372.0, 1168.0), cDn, 2)
    gZig(array.from(572.0, 1163.0, 590.0, 1105.0, 602.0, 1118.0, 620.0, 1065.0, 632.0, 1075.0, 645.0, 1000.0), cUp, 2)
    true
 
if barstate.islast
    gClear()
    if showGallery
        gBuild()
//-------------------End of Script-----------------------------

← 返回列表