Level Map from Text

killpips · study · 204 行 · 点赞 7 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © killpips
// Level Map from Text: paste a Daily, Weekly and Monthly string of levels for the instrument
// the script is loaded on, and the three maps are drawn with names, colors, line styles and a table.
// Accepted format: name, value, name, value, ... e.g.
//   $ES1!: vix r1, 6750, vix r2, 6762, vix s1, 6650, ... , range daily max, 6820, range daily min, 6580
 
//@version=6
indicator("Level Map from Text", "Level Map", overlay=true, max_labels_count=100, max_lines_count=100)
 
// ── inputs ────────────────────────────────────────────────────────────────
string G_STR = "Level strings"
string TIP   = "Format: name, value, name, value, ... An optional \"$TICKER:\" at the start is ignored. Order does not matter. Decimals with the dot."
string iStrD = input.text_area("", "Daily string",   group=G_STR, tooltip=TIP)
string iStrW = input.text_area("", "Weekly string",  group=G_STR, tooltip=TIP)
string iStrM = input.text_area("", "Monthly string", group=G_STR, tooltip=TIP)
 
string G_SHOW = "Levels"
bool iShowD  = input.bool(true, "Daily",   inline="d", group=G_SHOW)
int  iWidthD = input.int(2, "Width", minval=1, maxval=4, inline="d", group=G_SHOW)
bool iShowW  = input.bool(true, "Weekly",  inline="w", group=G_SHOW)
int  iWidthW = input.int(2, "Width", minval=1, maxval=4, inline="w", group=G_SHOW)
bool iShowM  = input.bool(true, "Monthly", inline="m", group=G_SHOW, tooltip="Daily lines are solid, weekly dashed, monthly dotted. Each map starts at the beginning of its current period.")
int  iWidthM = input.int(3, "Width", minval=1, maxval=4, inline="m", group=G_SHOW)
 
string G_TXT = "Labels and table"
bool   iLabels   = input.bool(true, "Level names on the chart", group=G_TXT)
string iTxtSize  = input.string("Normal", "Label size", options=["Small", "Normal", "Large"], group=G_TXT)
int    iTxtOff   = input.int(2, "Label offset (bars)", minval=0, group=G_TXT)
bool   iTable    = input.bool(true, "Table", group=G_TXT)
string iTablePos = input.string("Top Right", "Table position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left", "Middle Right", "Middle Left"], group=G_TXT)
string iTableSz  = input.string("Normal", "Table text size", options=["Small", "Normal", "Large"], group=G_TXT)
 
string G_COL = "Colors"
color cMax  = input.color(color.gray,             "Range max", group=G_COL)
color cVAH  = input.color(color.purple,           "VAH",       group=G_COL)
color cExpH = input.color(color.red,              "Exp max",   group=G_COL)
color cVxR2 = input.color(color.fuchsia,          "vix r2",    group=G_COL)
color cVxR1 = input.color(color.fuchsia,          "vix r1",    group=G_COL)
color cR2   = input.color(color.orange,           "R2",        group=G_COL)
color cR1   = input.color(color.yellow,           "R1",        group=G_COL)
color cR0   = input.color(color.green,            "R0",        group=G_COL)
color cHV   = input.color(color.rgb(139, 137, 137), "HV",      group=G_COL)
color cS0   = input.color(color.green,            "S0",        group=G_COL)
color cS1   = input.color(color.yellow,           "S1",        group=G_COL)
color cS2   = input.color(color.orange,           "S2",        group=G_COL)
color cVxS1 = input.color(color.fuchsia,          "vix s1",    group=G_COL)
color cVxS2 = input.color(color.fuchsia,          "vix s2",    group=G_COL)
color cExpL = input.color(color.red,              "Exp min",   group=G_COL)
color cVAL  = input.color(color.purple,           "VAL",       group=G_COL)
color cMin  = input.color(color.gray,             "Range min", group=G_COL)
 
// ── constants (17 slots, top to bottom) ───────────────────────────────────
var array<color>  COLS = array.from(cMax, cVAH, cExpH, cVxR2, cVxR1, cR2, cR1, cR0, cHV, cS0, cS1, cS2, cVxS1, cVxS2, cExpL, cVAL, cMin)
var array<string> NAMES_D = array.from("RangeDmax", "VAH", "1DexpMAX", "vix r2", "vixr1", "RD2", "RD1", "RD0", "HV", "SD0", "SD1", "SD2", "vix s1", "vix s2", "1DexpMIN", "VAL", "RangeDmin")
var array<string> NAMES_W = array.from("RangeWmax", "WVAH", "1WexpMAX", "Wvix r2", "Wvixr1", "WR2", "WR1", "WR0", "WHV", "WS0", "WS1", "WS2", "Wvix s1", "Wvix s2", "1WexpMIN", "WVAL", "RangeWmin")
var array<string> NAMES_M = array.from("RangeMmax", "MVAH", "1MexpMAX", "Mvix r2", "Mvixr1", "MR2", "MR1", "MR0", "MHV", "MS0", "MS1", "MS2", "Mvix s1", "Mvix s2", "1MexpMIN", "MVAL", "RangeMmin")
 
txtSz = iTxtSize == "Small" ? size.small : iTxtSize == "Large" ? size.large : size.normal
tabSz = iTableSz == "Small" ? size.small : iTableSz == "Large" ? size.large : size.normal
tabPos = switch iTablePos
    "Top Right"    => position.top_right
    "Top Left"     => position.top_left
    "Bottom Right" => position.bottom_right
    "Bottom Left"  => position.bottom_left
    "Middle Right" => position.middle_right
    =>                position.middle_left
 
// ── parser ────────────────────────────────────────────────────────────────
// Maps a level name (any period) to its slot 0..16; -1 if unknown.
f_slot(string name) =>
    string k = str.lower(str.replace_all(name, " ", ""))
    int s = -1
    if str.contains(k, "range")
        s := str.contains(k, "max") ? 0 : str.contains(k, "min") ? 16 : -1
    else if str.contains(k, "exp")
        s := str.contains(k, "max") ? 2 : str.contains(k, "min") ? 14 : -1
    else if str.contains(k, "vix")
        s := str.endswith(k, "r2") ? 3 : str.endswith(k, "r1") ? 4 : str.endswith(k, "s1") ? 12 : str.endswith(k, "s2") ? 13 : -1
    else if str.endswith(k, "vah")
        s := 1
    else if str.endswith(k, "val")
        s := 15
    else if str.endswith(k, "hv")
        s := 8
    else
        // RD0..RD2 / SD0..SD2 (daily) and WR0 / MS1 ... (weekly, monthly)
        k := str.replace_all(str.replace_all(k, "rd", "r"), "sd", "s")
        s := str.endswith(k, "r2") ? 5 : str.endswith(k, "r1") ? 6 : str.endswith(k, "r0") ? 7 : str.endswith(k, "s0") ? 9 : str.endswith(k, "s1") ? 10 : str.endswith(k, "s2") ? 11 : -1
    s
 
f_parse(string raw) =>
    array<float> out = array.new_float(17, na)
    string s = raw
    // drop the "$TICKER:" header (also "EXCHANGE:TICKER:")
    for _ = 0 to 2
        int p = str.pos(s, ":")
        if not na(p)
            if not str.contains(str.substring(s, 0, p), ",")
                s := str.substring(s, p + 1)
    s := str.replace_all(s, "\r", ",")
    s := str.replace_all(s, "\n", ",")
    s := str.replace_all(s, ";", ",")
    s := str.replace_all(s, "\t", " ")
    string pend = ""
    for tok0 in str.split(s, ",")
        string tok = str.trim(tok0)
        if tok != ""
            float v = str.tonumber(tok)
            if not na(v)
                if pend != ""
                    int k = f_slot(pend)
                    if k >= 0
                        out.set(k, v)
                pend := ""
            else
                // also accepts "VAH 6750" in a single token
                array<string> ws = str.split(tok, " ")
                float lv = str.tonumber(ws.last())
                if ws.size() > 1 and not na(lv)
                    int k = f_slot(array.join(array.slice(ws, 0, ws.size() - 1), " "))
                    if k >= 0
                        out.set(k, lv)
                    pend := ""
                else
                    pend := tok
    out
 
f_count(array<float> lv) =>
    int n = 0
    for v in lv
        if not na(v)
            n += 1
    n
 
// Inputs are constant: parse once.
var array<float> lvD = f_parse(iStrD)
var array<float> lvW = f_parse(iStrW)
var array<float> lvM = f_parse(iStrM)
 
// ── period starts ─────────────────────────────────────────────────────────
var int tD = time
var int tW = time
var int tM = time
if timeframe.change("D")
    tD := time
if timeframe.change("W")
    tW := time
if timeframe.change("M")
    tM := time
 
// ── lines and labels ──────────────────────────────────────────────────────
var array<line>  gLines  = array.new<line>()
var array<label> gLabels = array.new<label>()
 
f_draw(array<float> lv, array<string> names, int t0, int w, string st, bool show) =>
    if show
        for i = 0 to 16
            float y = lv.get(i)
            if not na(y)
                gLines.push(line.new(t0, y, time, y, xloc.bar_time, color=COLS.get(i), width=w, style=st))
                if iLabels
                    gLabels.push(label.new(bar_index + iTxtOff, y, names.get(i), style=label.style_none, textcolor=COLS.get(i), size=txtSz))
 
if barstate.islast
    for ln in gLines
        ln.delete()
    for lb in gLabels
        lb.delete()
    gLines.clear()
    gLabels.clear()
    f_draw(lvD, NAMES_D, tD, iWidthD, line.style_solid,  iShowD)
    f_draw(lvW, NAMES_W, tW, iWidthW, line.style_dashed, iShowW)
    f_draw(lvM, NAMES_M, tM, iWidthM, line.style_dotted, iShowM)
 
// ── table ─────────────────────────────────────────────────────────────────
var table tb = table.new(tabPos, 6, 19, bgcolor=color.new(color.gray, 80), frame_color=color.gray, frame_width=1, border_color=color.gray, border_width=1)
 
f_column(int c, string title, array<float> lv, array<string> names, string raw) =>
    table.cell(tb, c,     0, title,   bgcolor=color.new(color.gray, 70), text_color=color.white, text_size=tabSz)
    table.cell(tb, c + 1, 0, "Value", bgcolor=color.new(color.gray, 70), text_color=color.white, text_size=tabSz)
    for i = 0 to 16
        color bg = i % 2 == 0 ? color.new(color.gray, 95) : color.new(color.gray, 90)
        float v  = lv.get(i)
        table.cell(tb, c,     i + 1, names.get(i), bgcolor=bg, text_color=COLS.get(i), text_size=tabSz, text_halign=text.align_left)
        table.cell(tb, c + 1, i + 1, na(v) ? "—" : str.tostring(v, format.mintick), bgcolor=bg, text_color=COLS.get(i), text_size=tabSz, text_halign=text.align_right)
    // status row: how many of the 17 levels were read
    int n = f_count(lv)
    string st = str.trim(raw) == "" ? "no string" : str.tostring(n) + "/17 read"
    color sc  = str.trim(raw) == "" ? color.gray : n == 17 ? color.green : color.orange
    table.cell(tb, c,     18, "Status", bgcolor=color.new(color.gray, 70), text_color=color.white, text_size=tabSz, text_halign=text.align_left)
    table.cell(tb, c + 1, 18, st,       bgcolor=color.new(color.gray, 70), text_color=sc, text_size=tabSz, text_halign=text.align_right)
    c + 2
 
if barstate.islast
    table.clear(tb, 0, 0, 5, 18)
    if iTable
        int c = 0
        if iShowD
            c := f_column(c, "Daily", lvD, NAMES_D, iStrD)
        if iShowW
            c := f_column(c, "Weekly", lvW, NAMES_W, iStrW)
        if iShowM
            c := f_column(c, "Monthly", lvM, NAMES_M, iStrM)

← 返回列表