Walnut Lens Targets by AC Trading

AC_Trades_2k · study · 334 行 · 点赞 35 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0
// © AC Trading
//
// Walnut Lens Targets by AC Trading
// Plots the daily Walnut Lens targets map on ES/MES: buyer targets, seller targets,
// and the two exhaustion/danger zones. Levels anchor at the 18:00 ET open and extend
// to the cash close. Each target tracks HIT / NOT HIT YET for the session; when a
// target is hit, its line turns dashed and faded, and the label moves to the hit
// bar with the ET time.
//
// Daily workflow: paste the new numbers into "Paste levels" (or use the manual inputs).
 
//@version=6
indicator("Walnut Lens Targets by AC Trading", shorttitle = "Walnut Lens", overlay = true, max_lines_count = 60, max_labels_count = 60, max_boxes_count = 10)
 
// ─────────────────────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────────────────────
string GRP_P = "Quick Paste (overrides manual levels)"
string pasteStr = input.string("", "Paste levels", group = GRP_P, tooltip = "Format:  buyers | sellers | buyer danger | seller danger\nExample:  7445 7472 7520 | 7350 7315 | 7560 | 7310\nCommas or spaces both work. Up to 5 targets per side.\nLeave blank to use the manual inputs below.")
 
string GRP_B = "Buyer Targets (0 = off)"
float b1In = input.float(7445.0, "Buyer TGT 1", step = 0.25, group = GRP_B)
float b2In = input.float(7472.0, "Buyer TGT 2", step = 0.25, group = GRP_B)
float b3In = input.float(7520.0, "Buyer TGT 3", step = 0.25, group = GRP_B)
float b4In = input.float(0.0, "Buyer TGT 4", step = 0.25, group = GRP_B)
 
string GRP_S = "Seller Targets (0 = off)"
float s1In = input.float(7350.0, "Seller TGT 1", step = 0.25, group = GRP_S)
float s2In = input.float(7315.0, "Seller TGT 2", step = 0.25, group = GRP_S)
float s3In = input.float(0.0, "Seller TGT 3", step = 0.25, group = GRP_S)
float s4In = input.float(0.0, "Seller TGT 4", step = 0.25, group = GRP_S)
 
string GRP_Z = "Danger Zones (0 = off)"
float bdIn = input.float(7560.0, "Buyer danger zone ≥", step = 0.25, group = GRP_Z)
float sdIn = input.float(7310.0, "Seller danger zone ≤", step = 0.25, group = GRP_Z)
float zoneDepth = input.float(15.0, "Zone box depth (pts)", minval = 1, step = 0.25, group = GRP_Z, tooltip = "Visual height of the shaded zone beyond the danger level.")
 
string GRP_A = "Adjustments"
float offset = input.float(0.0, "Level offset (pts)", step = 0.25, group = GRP_A, tooltip = "Shifts every level. Use for a contract roll basis if the posted levels are on a different contract month.")
int endHr = input.int(16, "Extend levels to (ET hour)", minval = 0, maxval = 23, group = GRP_A)
bool showMarks = input.bool(true, "Show 09:30 open / cash close markers", group = GRP_A)
 
string GRP_V = "Style"
color buyCol = input.color(#26A69A, "Buyer color", group = GRP_V)
color sellCol = input.color(#F0714F, "Seller color", group = GRP_V)
color bdCol = input.color(#7E57C2, "Buyer danger zone", group = GRP_V)
color sdCol = input.color(#E91E63, "Seller danger zone", group = GRP_V)
int lineW = input.int(2, "Line width", minval = 1, maxval = 4, group = GRP_V)
int hitFade = input.int(55, "Hit line transparency", minval = 0, maxval = 90, group = GRP_V)
string lblSizeIn = input.string("normal", "Label size", options = ["tiny", "small", "normal", "large"], group = GRP_V)
bool showScale = input.bool(true, "Show levels on price scale", group = GRP_V)
float lblOff = input.float(2.0, "Label vertical offset (pts)", step = 0.25, group = GRP_V, tooltip = "Lifts the label text off its line. Positive = above the line, negative = below.")
 
string GRP_T = "Status Table"
bool showTable = input.bool(true, "Show status table", group = GRP_T)
string tblPosIn = input.string("top_right", "Position", options = ["top_right", "top_left", "bottom_right", "bottom_left"], group = GRP_T)
 
// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────
f_size(string s) =>
    s == "tiny" ? size.tiny : s == "small" ? size.small : s == "large" ? size.large : size.normal
 
f_pos(string s) =>
    s == "top_left" ? position.top_left : s == "bottom_right" ? position.bottom_right : s == "bottom_left" ? position.bottom_left : position.top_right
 
// Pull every number out of a text group, applying the offset.
f_nums(string s, float off) =>
    array<float> out = array.new<float>()
    string clean = str.replace_all(s, ",", " ")
    array<string> parts = str.split(clean, " ")
    for p in parts
        float v = str.tonumber(p)
        if not na(v) and v > 0
            out.push(v + off)
    out
 
f_fmt(float v) =>
    str.tostring(v, format.mintick)
 
f_txt(bool isBuy, int num, float px, bool hit, int ht) =>
    string side = isBuy ? "BUYER" : "SELLER"
    string st = hit ? "HIT " + str.format_time(ht, "HH:mm", "America/New_York") : "NOT HIT YET"
    side + " TGT " + str.tostring(num) + "  " + f_fmt(px) + "  ·  " + st
 
string LBL_SIZE = f_size(lblSizeIn)
 
// ─────────────────────────────────────────────────────────────────────────────
// Build level set once
// ─────────────────────────────────────────────────────────────────────────────
var array<float> lvPx = array.new<float>()
var array<bool> lvBuy = array.new<bool>()
var array<int> lvNum = array.new<int>()
var float bdLv = na
var float sdLv = na
 
if barstate.isfirst
    array<float> buys = array.new<float>()
    array<float> sells = array.new<float>()
    if str.length(pasteStr) > 0
        array<string> grp = str.split(pasteStr, "|")
        if grp.size() > 0
            buys := f_nums(grp.get(0), offset)
        if grp.size() > 1
            sells := f_nums(grp.get(1), offset)
        if grp.size() > 2
            array<float> t1 = f_nums(grp.get(2), offset)
            bdLv := t1.size() > 0 ? t1.get(0) : na
        if grp.size() > 3
            array<float> t2 = f_nums(grp.get(3), offset)
            sdLv := t2.size() > 0 ? t2.get(0) : na
    else
        for v in array.from(b1In, b2In, b3In, b4In)
            if v > 0
                buys.push(v + offset)
        for v in array.from(s1In, s2In, s3In, s4In)
            if v > 0
                sells.push(v + offset)
        bdLv := bdIn > 0 ? bdIn + offset : na
        sdLv := sdIn > 0 ? sdIn + offset : na
    int bCount = math.min(buys.size(), 5)
    int sCount = math.min(sells.size(), 5)
    if bCount > 0
        for i = 0 to bCount - 1
            lvPx.push(buys.get(i))
            lvBuy.push(true)
            lvNum.push(i + 1)
    if sCount > 0
        for i = 0 to sCount - 1
            lvPx.push(sells.get(i))
            lvBuy.push(false)
            lvNum.push(i + 1)
 
int N = lvPx.size()
 
// ─────────────────────────────────────────────────────────────────────────────
// Session tracking (18:00 ET open → trading day)
// ─────────────────────────────────────────────────────────────────────────────
bool newSess = barstate.isfirst or ta.change(time_tradingday) != 0
 
int tdY = year(time_tradingday, "UTC")
int tdM = month(time_tradingday, "UTC")
int tdD = dayofmonth(time_tradingday, "UTC")
int endT = timestamp("America/New_York", tdY, tdM, tdD, endHr, 0)
int rthT = timestamp("America/New_York", tdY, tdM, tdD, 9, 30)
int cashT = timestamp("America/New_York", tdY, tdM, tdD, 16, 0)
 
var float sHi = na
var float sLo = na
var array<bool> lvHit = array.new<bool>()
var array<int> lvHitT = array.new<int>()
var array<line> lvLine = array.new<line>()
var array<label> lvLbl = array.new<label>()
var box bdBox = na
var box sdBox = na
var line rthLn = na
var line cashLn = na
var bool bdTouched = false
var bool sdTouched = false
 
if newSess
    sHi := high
    sLo := low
    bdTouched := false
    sdTouched := false
    // clear the prior session's drawings
    for ln in lvLine
        ln.delete()
    for lb in lvLbl
        lb.delete()
    lvLine.clear()
    lvLbl.clear()
    lvHit.clear()
    lvHitT.clear()
    bdBox.delete()
    sdBox.delete()
    rthLn.delete()
    cashLn.delete()
    // fresh drawings for this session
    if N > 0
        for i = 0 to N - 1
            float px = lvPx.get(i)
            bool isB = lvBuy.get(i)
            color c = isB ? buyCol : sellCol
            lvHit.push(false)
            lvHitT.push(0)
            lvLine.push(line.new(time, px, endT, px, xloc.bar_time, extend.none, c, line.style_solid, lineW))
            lvLbl.push(label.new(endT, px + lblOff, f_txt(isB, lvNum.get(i), px, false, 0), xloc.bar_time, yloc.price, color.new(c, 85), label.style_label_right, c, LBL_SIZE))
    if not na(bdLv)
        bdBox := box.new(time, bdLv + zoneDepth, endT, bdLv, color.new(bdCol, 70), 1, line.style_solid, extend.none, xloc.bar_time, color.new(bdCol, 85), "BUYER EXHAUSTION & DANGER ZONE  |  ≥ " + f_fmt(bdLv), size.small, bdCol)
    if not na(sdLv)
        sdBox := box.new(time, sdLv, endT, sdLv - zoneDepth, color.new(sdCol, 70), 1, line.style_solid, extend.none, xloc.bar_time, color.new(sdCol, 85), "SELLER EXHAUSTION & DANGER ZONE  |  ≤ " + f_fmt(sdLv), size.small, sdCol)
    if showMarks
        rthLn := line.new(rthT, close, rthT, close + syminfo.mintick, xloc.bar_time, extend.both, color.new(#5B8DEF, 40), line.style_solid, 1)
        cashLn := line.new(cashT, close, cashT, close + syminfo.mintick, xloc.bar_time, extend.both, color.new(#5B8DEF, 40), line.style_dashed, 1)
else
    sHi := math.max(sHi, high)
    sLo := math.min(sLo, low)
 
// ─────────────────────────────────────────────────────────────────────────────
// Hit detection
// ─────────────────────────────────────────────────────────────────────────────
bool buyHitNow = false
bool sellHitNow = false
string hitMsg = ""
 
if N > 0
    for i = 0 to N - 1
        if not lvHit.get(i)
            float px = lvPx.get(i)
            bool isB = lvBuy.get(i)
            bool touched = isB ? high >= px : low <= px
            if touched
                color c = isB ? buyCol : sellCol
                lvHit.set(i, true)
                lvHitT.set(i, time)
                // level stays on the chart to the close, now dashed and faded
                line ln = lvLine.get(i)
                ln.set_style(line.style_dashed)
                ln.set_color(color.new(c, hitFade))
                // label moves to the hit bar and stamps the time
                label lb = lvLbl.get(i)
                lb.set_x(time)
                lb.set_text(f_txt(isB, lvNum.get(i), px, true, time))
                lb.set_color(color.new(c, 92))
                lb.set_textcolor(color.new(c, 20))
                if isB
                    buyHitNow := true
                else
                    sellHitNow := true
                hitMsg += (isB ? "BUYER" : "SELLER") + " TGT " + str.tostring(lvNum.get(i)) + " " + f_fmt(px) + " hit. "
 
bool bdHitNow = false
bool sdHitNow = false
if not na(bdLv) and not bdTouched and high >= bdLv
    bdTouched := true
    bdHitNow := true
if not na(sdLv) and not sdTouched and low <= sdLv
    sdTouched := true
    sdHitNow := true
 
// ─────────────────────────────────────────────────────────────────────────────
// Alerts
// ─────────────────────────────────────────────────────────────────────────────
if str.length(hitMsg) > 0
    alert("Walnut Lens " + syminfo.ticker + ": " + hitMsg + "Last " + f_fmt(close), alert.freq_once_per_bar)
if bdHitNow
    alert("Walnut Lens " + syminfo.ticker + ": entered BUYER DANGER ZONE ≥ " + f_fmt(bdLv), alert.freq_once_per_bar)
if sdHitNow
    alert("Walnut Lens " + syminfo.ticker + ": entered SELLER DANGER ZONE ≤ " + f_fmt(sdLv), alert.freq_once_per_bar)
 
alertcondition(buyHitNow, "Walnut Lens buyer target hit", "Walnut Lens buyer target hit on {{ticker}} at {{close}}")
alertcondition(sellHitNow, "Walnut Lens seller target hit", "Walnut Lens seller target hit on {{ticker}} at {{close}}")
alertcondition(bdHitNow, "Walnut Lens buyer danger zone", "{{ticker}} entered the Walnut Lens buyer danger zone")
alertcondition(sdHitNow, "Walnut Lens seller danger zone", "{{ticker}} entered the Walnut Lens seller danger zone")
 
// ─────────────────────────────────────────────────────────────────────────────
// Price scale tags (one hidden plot per slot, shown only on the price column)
// ─────────────────────────────────────────────────────────────────────────────
f_get(bool wantBuy, int num) =>
    float px = na
    bool hit = false
    if N > 0
        for i = 0 to N - 1
            if lvBuy.get(i) == wantBuy and lvNum.get(i) == num
                px := lvPx.get(i)
                hit := lvHit.size() > i ? lvHit.get(i) : false
    [px, hit]
 
f_tagCol(color c, bool hit) =>
    hit ? color.new(c, hitFade) : c
 
[b1p, b1h] = f_get(true, 1)
[b2p, b2h] = f_get(true, 2)
[b3p, b3h] = f_get(true, 3)
[b4p, b4h] = f_get(true, 4)
[b5p, b5h] = f_get(true, 5)
[s1p, s1h] = f_get(false, 1)
[s2p, s2h] = f_get(false, 2)
[s3p, s3h] = f_get(false, 3)
[s4p, s4h] = f_get(false, 4)
[s5p, s5h] = f_get(false, 5)
 
plot(showScale ? b1p : na, "Buyer TGT 1", f_tagCol(buyCol, b1h), display = display.price_scale)
plot(showScale ? b2p : na, "Buyer TGT 2", f_tagCol(buyCol, b2h), display = display.price_scale)
plot(showScale ? b3p : na, "Buyer TGT 3", f_tagCol(buyCol, b3h), display = display.price_scale)
plot(showScale ? b4p : na, "Buyer TGT 4", f_tagCol(buyCol, b4h), display = display.price_scale)
plot(showScale ? b5p : na, "Buyer TGT 5", f_tagCol(buyCol, b5h), display = display.price_scale)
plot(showScale ? s1p : na, "Seller TGT 1", f_tagCol(sellCol, s1h), display = display.price_scale)
plot(showScale ? s2p : na, "Seller TGT 2", f_tagCol(sellCol, s2h), display = display.price_scale)
plot(showScale ? s3p : na, "Seller TGT 3", f_tagCol(sellCol, s3h), display = display.price_scale)
plot(showScale ? s4p : na, "Seller TGT 4", f_tagCol(sellCol, s4h), display = display.price_scale)
plot(showScale ? s5p : na, "Seller TGT 5", f_tagCol(sellCol, s5h), display = display.price_scale)
plot(showScale ? bdLv : na, "Buyer Danger", bdCol, display = display.price_scale)
plot(showScale ? sdLv : na, "Seller Danger", sdCol, display = display.price_scale)
 
// ─────────────────────────────────────────────────────────────────────────────
// Status table
// ─────────────────────────────────────────────────────────────────────────────
var table tbl = table.new(f_pos(tblPosIn), 2, 6, color.new(#132238, 0), color.new(#5B6B80, 0), 1, color.new(#5B6B80, 0), 1)
 
if showTable and barstate.islast
    float nextB = na
    float nextS = na
    int hits = 0
    if N > 0
        for i = 0 to N - 1
            if lvHit.get(i)
                hits += 1
            else
                float px = lvPx.get(i)
                if lvBuy.get(i)
                    nextB := na(nextB) ? px : math.min(nextB, px)
                else
                    nextS := na(nextS) ? px : math.max(nextS, px)
    string zoneTxt = not na(bdLv) and close >= bdLv ? "BUYER DANGER" : not na(sdLv) and close <= sdLv ? "SELLER DANGER" : "NEUTRAL"
    color zoneCol = zoneTxt == "BUYER DANGER" ? bdCol : zoneTxt == "SELLER DANGER" ? sdCol : color.gray
    string nbTxt = na(nextB) ? "ALL HIT" : f_fmt(nextB) + "  (+" + f_fmt(nextB - close) + ")"
    string nsTxt = na(nextS) ? "ALL HIT" : f_fmt(nextS) + "  (−" + f_fmt(close - nextS) + ")"
    color wht = color.white
    color dim = color.new(color.white, 35)
    tbl.cell(0, 0, "WALNUT LENS TARGETS", text_color = wht, text_size = size.small)
    tbl.merge_cells(0, 0, 1, 0)
    tbl.cell(0, 1, "Session H / L", text_color = dim, text_size = size.small, text_halign = text.align_left)
    tbl.cell(1, 1, f_fmt(sHi) + " / " + f_fmt(sLo), text_color = wht, text_size = size.small)
    tbl.cell(0, 2, "Next buyer", text_color = dim, text_size = size.small, text_halign = text.align_left)
    tbl.cell(1, 2, nbTxt, text_color = buyCol, text_size = size.small)
    tbl.cell(0, 3, "Next seller", text_color = dim, text_size = size.small, text_halign = text.align_left)
    tbl.cell(1, 3, nsTxt, text_color = sellCol, text_size = size.small)
    tbl.cell(0, 4, "Targets hit", text_color = dim, text_size = size.small, text_halign = text.align_left)
    tbl.cell(1, 4, str.tostring(hits) + " / " + str.tostring(N), text_color = wht, text_size = size.small)
    tbl.cell(0, 5, "Zone", text_color = dim, text_size = size.small, text_halign = text.align_left)
    tbl.cell(1, 5, zoneTxt, text_color = zoneCol, text_size = size.small)

← 返回列表