KP56 Plan Board

kasapop56394 · study · 307 行 · 点赞 730 · 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 at https://mozilla.org/MPL/2.0/
// © kasapop56394
 
//@version=6
// KP56 Plan Board v1.0
// Paste a trading plan as plain text -> the board draws zones, levels, Entry/SL/TP and a plan table.
// The indicator only displays what you paste. It does not create, change or suggest any trade.
// Format: one record per line, fields separated by |   (blank lines and lines starting with # are ignored)
//   H|title|UP/DN/FLAT|note
//   Z|TF|kind|low|high|stars 0-3|label
//   L|price|label
//   T|name|entry or low-high|SL|TP1,TP2|condition
//   N|news / note
//   W|warning
//   F|low|high|note
indicator("KP56 Plan Board", shorttitle="Plan Board", overlay=true, max_boxes_count=200, max_lines_count=300, max_labels_count=200)
 
// ===================== Inputs =====================
string DEF_PLAN = "# Paste your plan here. One record per line, fields separated by |\n# H|title|UP/DN/FLAT|note\n# Z|TF|kind|low|high|stars 0-3|label\n# L|price|label\n# T|name|entry low-high|SL|TP1,TP2|condition\n# N|news\n# W|warning\n# F|low|high|note"
string planTxt  = input.text_area(DEF_PLAN, "Plan text", group="Plan", tooltip="One record per line. Fields separated by |. Lines starting with # are ignored. See the script description for the full format and a template.")
 
int    backBars = input.int(60, "Zones start (bars back)", minval=1, maxval=4000, group="Draw")
int    extBars  = input.int(30, "Extend right (bars)", minval=1, maxval=450, group="Draw")
int    zoneTr   = input.int(85, "Zone transparency", minval=0, maxval=100, group="Draw")
bool   showZTxt = input.bool(true, "Zone labels", group="Draw")
bool   showTL   = input.bool(true, "Draw Entry / SL / TP", group="Draw")
bool   showFr   = input.bool(true, "Draw frame (F line)", group="Draw")
string txtSzS   = input.string("small", "Text size", options=["tiny", "small", "normal"], group="Draw")
 
color  cBear    = input.color(color.new(#ef5350, 0), "Supply / bearish", group="Colors")
color  cBull    = input.color(color.new(#26a69a, 0), "Demand / bullish", group="Colors")
color  cNeu     = input.color(color.new(#9598a1, 0), "Neutral zone", group="Colors")
color  cLiq     = input.color(color.new(#f9a825, 0), "Level (L line)", group="Colors")
color  cSL      = input.color(color.new(#ff5252, 0), "SL", group="Colors")
color  cTP      = input.color(color.new(#00e676, 0), "TP", group="Colors")
color  cFr      = input.color(color.new(#7e57c2, 0), "Frame", group="Colors")
 
bool   showTbl  = input.bool(true, "Show table", group="Table")
string tblPosS  = input.string("top_right", "Table position", options=["top_right", "top_left", "bottom_right", "bottom_left", "middle_right"], group="Table")
bool   showCond = input.bool(true, "Show condition row", group="Table")
string tblSzS   = input.string("small", "Table text size", options=["tiny", "small", "normal"], group="Table")
 
float  riskAmt  = input.float(0.0, "Max loss per trade (0 = off)", minval=0, group="Size", tooltip="In your account currency. The board shows the total lots that lose at most this amount if the SL is hit (from the entry midpoint).")
float  lotVal   = input.float(100.0, "Value of 1.00 lot per 1.0 price move", minval=0.0000001, group="Size", tooltip="Check your broker's contract size. Examples: XAUUSD 100 oz/lot = 100 · EURUSD 100,000/lot = 100000 · many index CFDs = 1")
float  lotStep  = input.float(0.01, "Lot step", minval=0.0001, group="Size")
 
bool   alertOn  = input.bool(true, "Alert when price touches an entry range", group="Alerts", tooltip="Create an alert on this indicator with condition: Any alert() function call")
 
// ===================== Helpers =====================
f_size() =>
    txtSzS == "tiny" ? size.tiny : txtSzS == "normal" ? size.normal : size.small
 
f_pos() =>
    tblPosS == "top_left" ? position.top_left : tblPosS == "bottom_right" ? position.bottom_right : tblPosS == "bottom_left" ? position.bottom_left : tblPosS == "middle_right" ? position.middle_right : position.top_right
 
f_tsize() =>
    tblSzS == "tiny" ? size.tiny : tblSzS == "normal" ? size.normal : size.small
 
f_get(array<string> a, int i) =>
    i < a.size() ? str.trim(a.get(i)) : ""
 
f_num(string s) =>
    str.tonumber(str.replace_all(str.trim(s), ",", ""))
 
// "4120-4127.5" or "4120" -> [lo, hi]
f_range(string s) =>
    string t = str.replace_all(str.replace_all(s, "–", "-"), " ", "")
    array<string> p = str.split(t, "-")
    float a = p.size() > 0 ? str.tonumber(p.get(0)) : na
    float b = p.size() > 1 ? str.tonumber(p.get(1)) : a
    [math.min(a, b), math.max(a, b)]
 
// -1 bearish, 1 bullish, 0 neutral
f_dir(string kind) =>
    string k = str.lower(kind)
    str.contains(k, "supply") or str.contains(k, "bear") or str.contains(k, "resist") or str.contains(k, "sell") ? -1 : str.contains(k, "demand") or str.contains(k, "bull") or str.contains(k, "support") or str.contains(k, "buy") ? 1 : 0
 
f_stars(int n) =>
    n >= 3 ? "★★★" : n == 2 ? "★★" : n == 1 ? "★" : ""
 
f_px(float v) =>
    na(v) ? "–" : str.tostring(v, format.mintick)
 
// ===================== State =====================
var array<box>   bxs = array.new<box>()
var array<line>  lns = array.new<line>()
var array<label> lbs = array.new<label>()
var table        tb  = na
 
if barstate.islast
    // ---- clear previous drawings
    for b in bxs
        b.delete()
    bxs.clear()
    for l in lns
        l.delete()
    lns.clear()
    for l in lbs
        l.delete()
    lbs.clear()
 
    int    x1   = bar_index - backBars
    int    x2   = bar_index + extBars
    size_t = f_size()
 
    string hTitle = ""
    string hBias  = ""
    string hNote  = ""
    array<string> tNm  = array.new<string>()
    array<string> tEn  = array.new<string>()
    array<float>  tMid = array.new<float>()
    array<float>  tSLv = array.new<float>()
    array<string> tTPs = array.new<string>()
    array<string> tRR  = array.new<string>()
    array<string> tLot = array.new<string>()
    array<string> tCnd = array.new<string>()
    array<int>    tDir = array.new<int>()
    array<float>  tLo  = array.new<float>()
    array<float>  tHi  = array.new<float>()
    array<string> nws  = array.new<string>()
    array<string> wrn  = array.new<string>()
    array<string> err  = array.new<string>()
    string frTxt = ""
    int nRec = 0
 
    array<string> rows = str.split(planTxt, "\n")
    for [i, raw] in rows
        string s = str.trim(raw)
        if s == "" or str.startswith(s, "#")
            continue
        array<string> f = str.split(s, "|")
        string k = str.upper(f_get(f, 0))
        string ln = "Line " + str.tostring(i + 1) + ": "
        nRec += 1
        if k == "H"
            hTitle := f_get(f, 1)
            hBias  := str.upper(f_get(f, 2))
            hNote  := f_get(f, 3)
        else if k == "Z"
            float lo = f_num(f_get(f, 3))
            float hi = f_num(f_get(f, 4))
            if f.size() < 5 or na(lo) or na(hi)
                err.push(ln + "Z needs TF|kind|low|high")
            else
                float zl = math.min(lo, hi)
                float zh = math.max(lo, hi)
                string kind = f_get(f, 2)
                float  stF  = f_num(f_get(f, 5))
                int    st   = na(stF) ? 0 : int(stF)
                int    d    = f_dir(kind)
                color  zc   = d == -1 ? cBear : d == 1 ? cBull : cNeu
                bool   isEntry = st == 0
                string zt = showZTxt ? (f_get(f, 1) + " " + (f_get(f, 6) != "" ? f_get(f, 6) : kind) + " " + f_stars(st)) : ""
                bxs.push(box.new(x1, zh, x2, zl, border_color=color.new(zc, isEntry ? 30 : 50), border_width=1, border_style=isEntry ? line.style_dashed : line.style_solid, bgcolor=color.new(zc, isEntry ? math.min(zoneTr + 5, 100) : zoneTr), text=zt, text_size=size_t, text_color=color.new(zc, 0), text_halign=text.align_right, text_valign=text.align_top))
        else if k == "L"
            float p = f_num(f_get(f, 1))
            if na(p)
                err.push(ln + "L needs a price")
            else
                lns.push(line.new(x1, p, x2, p, color=cLiq, style=line.style_dotted, width=1))
                lbs.push(label.new(x2, p, f_get(f, 2) != "" ? f_get(f, 2) : f_px(p), style=label.style_label_left, color=color.new(color.black, 100), textcolor=cLiq, size=size_t))
        else if k == "T"
            string nm = f_get(f, 1)
            [elo, ehi] = f_range(f_get(f, 2))
            float sl = f_num(f_get(f, 3))
            if f.size() < 4 or na(elo) or na(sl)
                err.push(ln + "T needs name|entry|SL")
            else
                float mid = (elo + ehi) / 2
                string nu = str.upper(nm)
                int dr = str.contains(nu, "SELL") or str.contains(nu, "SHORT") ? -1 : str.contains(nu, "BUY") or str.contains(nu, "LONG") ? 1 : (sl > mid ? -1 : 1)
                if (dr == 1 and sl >= elo) or (dr == -1 and sl <= ehi)
                    err.push(ln + nm + " SL is inside or on the wrong side of the entry")
                float dist = math.abs(mid - sl)
                string tpS = str.replace_all(f_get(f, 4), "/", ",")
                array<string> tpA = tpS == "" ? array.new<string>() : str.split(tpS, ",")
                string rrS = ""
                string tpTxt = ""
                for [j, tpRaw] in tpA
                    float tp = f_num(tpRaw)
                    if not na(tp)
                        tpTxt += (tpTxt == "" ? "" : "\n") + f_px(tp)
                        rrS += (rrS == "" ? "" : "\n") + (dist > 0 ? "1:" + str.tostring(math.abs(tp - mid) / dist, "#.#") : "–")
                        if showTL
                            lns.push(line.new(x1, tp, x2, tp, color=cTP, style=line.style_dotted, width=1))
                            lbs.push(label.new(x2, tp, nm + " TP" + str.tostring(j + 1), style=label.style_label_left, color=color.new(color.black, 100), textcolor=cTP, size=size_t))
                string lotS = "–"
                if riskAmt > 0 and dist > 0
                    float lots = math.floor(riskAmt / (dist * lotVal) / lotStep) * lotStep
                    lotS := lots > 0 ? str.tostring(lots, "#.##") : "too far"
                tNm.push(nm)
                tEn.push(elo == ehi ? f_px(elo) : f_px(ehi) + "\n" + f_px(elo))
                tMid.push(mid)
                tSLv.push(sl)
                tTPs.push(tpTxt == "" ? "–" : tpTxt)
                tRR.push(rrS == "" ? "–" : rrS)
                tLot.push(lotS)
                tCnd.push(f_get(f, 5))
                tDir.push(dr)
                tLo.push(elo)
                tHi.push(ehi)
                if showTL
                    color ec = dr == 1 ? cBull : cBear
                    float pad = elo == ehi ? syminfo.mintick * 2 : 0
                    bxs.push(box.new(x1, ehi + pad, x2, elo - pad, border_color=color.new(ec, 20), border_width=1, bgcolor=color.new(ec, 80)))
                    lbs.push(label.new(x2, mid, nm + " entry", style=label.style_label_left, color=color.new(color.black, 100), textcolor=ec, size=size_t))
                    lns.push(line.new(x1, sl, x2, sl, color=cSL, style=line.style_dashed, width=1))
                    lbs.push(label.new(x2, sl, nm + " SL " + f_px(sl), style=label.style_label_left, color=color.new(color.black, 100), textcolor=cSL, size=size_t))
        else if k == "N"
            nws.push(f_get(f, 1))
        else if k == "W"
            wrn.push(f_get(f, 1))
        else if k == "F"
            float flo = f_num(f_get(f, 1))
            float fhi = f_num(f_get(f, 2))
            if na(flo) or na(fhi) or flo == fhi
                err.push(ln + "F needs low|high")
            else
                float a = math.min(flo, fhi)
                float b = math.max(flo, fhi)
                float pos = (close - a) / (b - a) * 100
                frTxt := f_px(a) + " – " + f_px(b) + " · now " + str.tostring(pos, "#") + "%" + (f_get(f, 3) != "" ? " · " + f_get(f, 3) : "")
                if showFr
                    for q in array.from(0.0, 0.25, 0.5, 0.75, 1.0)
                        float y = a + (b - a) * q
                        lns.push(line.new(x1, y, x2, y, color=color.new(cFr, q == 0.5 ? 0 : 40), style=q == 0.5 ? line.style_dashed : line.style_dotted, width=1))
                        lbs.push(label.new(x2, y, str.tostring(q * 100, "#") + "%", style=label.style_label_right, color=color.new(color.black, 100), textcolor=cFr, size=size.tiny))
        else
            err.push(ln + "unknown record '" + k + "' (use H Z L T N W F)")
            nRec -= 1
 
    // ---- alerts: price touching an entry range
    if alertOn
        for [j, nm] in tNm
            if high >= tLo.get(j) and low <= tHi.get(j)
                alert("Plan Board: " + syminfo.ticker + " touched " + nm + " entry " + str.replace_all(tEn.get(j), "\n", "-"), alert.freq_once_per_bar)
 
    // ---- table
    if not na(tb)
        tb.delete()
    if showTbl
        bool showLot = riskAmt > 0
        int nCol = showLot ? 6 : 5
        int nRow = 4 + tNm.size() * 2 + nws.size() + wrn.size() + err.size()
        color bgH = color.new(#1e222d, 5)
        color bgR = color.new(#131722, 15)
        color bgA = color.new(#1a1e29, 15)
        color tw = color.new(#d1d4dc, 0)
        color tg = color.new(#787b86, 0)
        tsz = f_tsize()
        tb := table.new(f_pos(), nCol, nRow, bgcolor=bgR, frame_color=color.new(#2a2e39, 0), frame_width=1, border_width=0)
        int r = 0
        if nRec == 0
            tb.cell(0, 0, "Paste your plan in Settings > Plan text", text_color=tw, text_size=tsz)
            tb.merge_cells(0, 0, nCol - 1, 0)
            r := 1
        else
            color bc = hBias == "UP" ? cBull : hBias == "DN" or hBias == "DOWN" ? cBear : tg
            tb.cell(0, r, hTitle == "" ? "Plan" : hTitle, text_color=color.white, bgcolor=bgH, text_size=tsz, text_halign=text.align_left)
            tb.merge_cells(0, r, nCol - 2, r)
            tb.cell(nCol - 1, r, hBias == "" ? "–" : hBias, text_color=color.white, bgcolor=color.new(bc, 25), text_size=tsz)
            r += 1
            if hNote != ""
                tb.cell(0, r, hNote, text_color=tg, bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
                tb.merge_cells(0, r, nCol - 1, r)
                r += 1
            if tNm.size() > 0
                array<string> hd = array.from("Trade", "Entry", "SL", "TP", "R:R", "Lots")
                for c = 0 to nCol - 1
                    tb.cell(c, r, hd.get(c), text_color=tg, bgcolor=bgH, text_size=size.tiny)
                r += 1
                for [j, nm] in tNm
                    color sc = tDir.get(j) == 1 ? cBull : cBear
                    color rb = j % 2 == 0 ? bgR : bgA
                    float dd = math.abs(tMid.get(j) - tSLv.get(j))
                    tb.cell(0, r, nm, text_color=sc, bgcolor=rb, text_size=tsz, text_halign=text.align_left)
                    tb.cell(1, r, tEn.get(j), text_color=tw, bgcolor=rb, text_size=tsz)
                    tb.cell(2, r, f_px(tSLv.get(j)) + "\n-" + str.tostring(dd, "#.##"), text_color=cSL, bgcolor=rb, text_size=tsz)
                    tb.cell(3, r, tTPs.get(j), text_color=cTP, bgcolor=rb, text_size=tsz)
                    tb.cell(4, r, tRR.get(j), text_color=tw, bgcolor=rb, text_size=tsz)
                    if showLot
                        tb.cell(5, r, tLot.get(j), text_color=tLot.get(j) == "too far" ? color.orange : tw, bgcolor=rb, text_size=tsz)
                    r += 1
                    if showCond and tCnd.get(j) != ""
                        tb.cell(0, r, "↳ " + tCnd.get(j), text_color=tg, bgcolor=rb, text_size=size.tiny, text_halign=text.align_left)
                        tb.merge_cells(0, r, nCol - 1, r)
                        r += 1
            for n in nws
                tb.cell(0, r, "News", text_color=color.new(#42a5f5, 0), bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
                tb.cell(1, r, n, text_color=tw, bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
                tb.merge_cells(1, r, nCol - 1, r)
                r += 1
            for w in wrn
                tb.cell(0, r, "Warn", text_color=color.orange, bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
                tb.cell(1, r, w, text_color=tw, bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
                tb.merge_cells(1, r, nCol - 1, r)
                r += 1
            if frTxt != ""
                tb.cell(0, r, "Frame", text_color=cFr, bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
                tb.cell(1, r, frTxt, text_color=tw, bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
                tb.merge_cells(1, r, nCol - 1, r)
                r += 1
        for e in err
            tb.cell(0, r, "Format", text_color=color.white, bgcolor=color.new(color.red, 30), text_size=size.tiny, text_halign=text.align_left)
            tb.cell(1, r, e, text_color=color.red, bgcolor=bgH, text_size=size.tiny, text_halign=text.align_left)
            tb.merge_cells(1, r, nCol - 1, r)
            r += 1

← 返回列表