Apex Trend Ladder + Volume Nodes [Auto TP/SL]

abgthecoder · study · 182 行 · 点赞 120 · TradingView 原页

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

Pine Script

//@version=6
indicator("Apex Trend Ladder + Volume Nodes [Auto TP/SL]", shorttitle="Apex Ladder", overlay=true, max_boxes_count=200, max_lines_count=200, max_labels_count=200, max_bars_back=500)
 
// ───────── Inputs ─────────
grpL = "Trend Ladder"
baseLen = input.int(8, "Base EMA length", minval=2, group=grpL)
stepLen = input.int(6, "Ladder step", minval=1, group=grpL)
bullCol = input.color(#00e676, "Bull color", group=grpL)
bearCol = input.color(#ff1744, "Bear color", group=grpL)
 
grpS = "Signals / Risk"
volMult  = input.float(1.2, "Min volume vs avg", minval=0.0, step=0.1, group=grpS)
anchorLn = input.int(100, "Value anchor (VWMA) length", minval=10, group=grpS)
atrLen   = input.int(14, "ATR length", minval=1, group=grpS)
slMult   = input.float(1.5, "SL (x ATR)", step=0.1, group=grpS)
tp1R     = input.float(1.0, "TP1 (R)", step=0.25, group=grpS)
tp2R     = input.float(2.0, "TP2 (R)", step=0.25, group=grpS)
tp3R     = input.float(3.0, "TP3 (R)", step=0.25, group=grpS)
showTP   = input.bool(true, "Draw trade levels", group=grpS)
 
grpV = "Volume Nodes"
showVP  = input.bool(true, "Show volume nodes", group=grpV)
vpLook  = input.int(200, "Lookback bars", minval=20, maxval=500, group=grpV)
vpBins  = input.int(30, "Rows", minval=10, maxval=60, group=grpV)
hvnMult = input.float(1.5, "HVN threshold (x avg row)", step=0.1, group=grpV)
vpWidth = input.int(40, "Max width (bars)", minval=10, maxval=100, group=grpV)
vpCol   = input.color(color.new(#2962ff, 70), "Node color", group=grpV)
pocCol  = input.color(#ffd600, "POC color", group=grpV)
 
showDash = input.bool(true, "Show dashboard", group="Dashboard")
 
// ───────── Ladder ─────────
e1 = ta.ema(close, baseLen)
e2 = ta.ema(close, baseLen + stepLen)
e3 = ta.ema(close, baseLen + stepLen * 2)
e4 = ta.ema(close, baseLen + stepLen * 3)
e5 = ta.ema(close, baseLen + stepLen * 4)
 
bullStack = e1 > e2 and e2 > e3 and e3 > e4 and e4 > e5
bearStack = e1 < e2 and e2 < e3 and e3 < e4 and e4 < e5
color trendCol = bullStack ? bullCol : bearStack ? bearCol : color.new(color.gray, 20)
 
p1 = plot(e1, "L1", color=trendCol, linewidth=2)
p2 = plot(e2, "L2", color=color.new(trendCol, 30))
p3 = plot(e3, "L3", color=color.new(trendCol, 45))
p4 = plot(e4, "L4", color=color.new(trendCol, 60))
p5 = plot(e5, "L5", color=color.new(trendCol, 75))
fill(p1, p3, color=color.new(trendCol, 85))
fill(p3, p5, color=color.new(trendCol, 92))
barcolor(bullStack ? bullCol : bearStack ? bearCol : na)
 
// ───────── Signals ─────────
float atr = ta.atr(atrLen)
bool volOK = volume > ta.sma(volume, 20) * volMult
float anchor = ta.vwma(close, anchorLn)
 
bool longSig  = bullStack and ta.crossover(close, e2) and close > anchor and volOK and barstate.isconfirmed
bool shortSig = bearStack and ta.crossunder(close, e2) and close < anchor and volOK and barstate.isconfirmed
 
// ───────── Trade tracking ─────────
var int dir = 0
var float entry = na
var float sl = na
var float t1 = na
var float t2 = na
var float t3 = na
var bool hit1 = false
var int wins = 0
var int losses = 0
var int total = 0
var float sumR = 0.0
var line lE = na
var line lS = na
var line lT1 = na
var line lT2 = na
var line lT3 = na
 
// manage open trade
if dir != 0
    bool stopped = dir == 1 ? low <= sl : high >= sl
    bool tp1Hit = dir == 1 ? high >= t1 : low <= t1
    bool tp3Hit = dir == 1 ? high >= t3 : low <= t3
    if not stopped and tp1Hit
        hit1 := true
    if stopped
        if hit1
            wins += 1
            sumR += tp1R
        else
            losses += 1
            sumR -= 1.0
        dir := 0
    else if tp3Hit
        wins += 1
        sumR += tp3R
        dir := 0
    if showTP and not na(lE)
        line.set_x2(lE, bar_index)
        line.set_x2(lS, bar_index)
        line.set_x2(lT1, bar_index)
        line.set_x2(lT2, bar_index)
        line.set_x2(lT3, bar_index)
 
// open new trade
if longSig or shortSig
    dir := longSig ? 1 : -1
    entry := close
    float risk = atr * slMult
    sl := dir == 1 ? entry - risk : entry + risk
    t1 := dir == 1 ? entry + risk * tp1R : entry - risk * tp1R
    t2 := dir == 1 ? entry + risk * tp2R : entry - risk * tp2R
    t3 := dir == 1 ? entry + risk * tp3R : entry - risk * tp3R
    hit1 := false
    total += 1
    label.new(bar_index, dir == 1 ? low : high, dir == 1 ? "BUY" : "SELL", style = dir == 1 ? label.style_label_up : label.style_label_down, color = dir == 1 ? bullCol : bearCol, textcolor = color.white, size = size.small)
    if showTP
        lE := line.new(bar_index, entry, bar_index, entry, color=color.white, style=line.style_dashed)
        lS := line.new(bar_index, sl, bar_index, sl, color=bearCol)
        lT1 := line.new(bar_index, t1, bar_index, t1, color=bullCol)
        lT2 := line.new(bar_index, t2, bar_index, t2, color=bullCol)
        lT3 := line.new(bar_index, t3, bar_index, t3, color=bullCol)
 
// ───────── Volume Nodes (drawn on last bar) ─────────
var array<box> vpBoxes = array.new<box>()
var array<line> vpLines = array.new<line>()
var float pocPrice = na
 
if showVP and barstate.islast
    for b in vpBoxes
        box.delete(b)
    for l in vpLines
        line.delete(l)
    array.clear(vpBoxes)
    array.clear(vpLines)
 
    int n = math.min(vpLook, bar_index + 1)
    float hi = high
    float lo = low
    for i = 0 to n - 1
        hi := math.max(hi, high[i])
        lo := math.min(lo, low[i])
    float rowH = (hi - lo) / vpBins
    if rowH > 0
        array<float> rows = array.new<float>(vpBins, 0.0)
        for i = 0 to n - 1
            float px = hlc3[i]
            int idx = math.min(vpBins - 1, math.max(0, int((px - lo) / rowH)))
            array.set(rows, idx, array.get(rows, idx) + nz(volume[i]))
        float maxV = array.max(rows)
        float avgV = array.avg(rows)
        if maxV > 0
            int pocIdx = array.indexof(rows, maxV)
            pocPrice := lo + rowH * (pocIdx + 0.5)
            for k = 0 to vpBins - 1
                float v = array.get(rows, k)
                int w = math.max(1, int(v / maxV * vpWidth))
                bool isHVN = v >= avgV * hvnMult
                color c = k == pocIdx ? color.new(pocCol, 40) : isHVN ? vpCol : color.new(color.gray, 85)
                array.push(vpBoxes, box.new(bar_index + 3, lo + rowH * (k + 1), bar_index + 3 + w, lo + rowH * k, border_color=na, bgcolor=c))
            array.push(vpLines, line.new(bar_index - n + 1, pocPrice, bar_index + 3 + vpWidth, pocPrice, color=pocCol, width=2, style=line.style_dotted))
 
// ───────── Dashboard ─────────
var table dash = table.new(position.top_right, 2, 6, bgcolor=color.new(#131722, 10), border_width=1, border_color=color.new(color.gray, 60))
if showDash and barstate.islast
    float winRate = (wins + losses) > 0 ? wins / float(wins + losses) * 100 : 0.0
    string trendTxt = bullStack ? "BULLISH" : bearStack ? "BEARISH" : "RANGING"
    table.cell(dash, 0, 0, "APEX LADDER", text_color=color.white, text_size=size.small, bgcolor=color.new(color.blue, 30))
    table.cell(dash, 1, 0, "", bgcolor=color.new(color.blue, 30))
    table.cell(dash, 0, 1, "Trend", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 1, trendTxt, text_color=bullStack ? bullCol : bearStack ? bearCol : color.gray, text_size=size.small)
    table.cell(dash, 0, 2, "Signals", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 2, str.tostring(total), text_color=color.white, text_size=size.small)
    table.cell(dash, 0, 3, "Win rate", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 3, str.tostring(winRate, "#.#") + "%", text_color=winRate >= 50 ? bullCol : bearCol, text_size=size.small)
    table.cell(dash, 0, 4, "Net R", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 4, str.tostring(sumR, "#.##"), text_color=sumR >= 0 ? bullCol : bearCol, text_size=size.small)
    table.cell(dash, 0, 5, "POC", text_color=color.gray, text_size=size.small)
    table.cell(dash, 1, 5, na(pocPrice) ? "-" : str.tostring(pocPrice, format.mintick), text_color=pocCol, text_size=size.small)
 
// ───────── Alerts ─────────
alertcondition(longSig, "Apex BUY", "Apex Ladder: BUY signal on {{ticker}} @ {{close}}")
alertcondition(shortSig, "Apex SELL", "Apex Ladder: SELL signal on {{ticker}} @ {{close}}")

← 返回列表