Trding R-Multiple Trade Log

Trding_AI · study · 338 行 · 点赞 3 · 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/
 
// ═══════════════════════════════════════════════════════════════
// Trding R-Multiple Trade Log · v2.0.0
// Educational trade-journaling tool. Plots your closed trades as
// W/L markers on the chart with R-multiple labels, plus a four-block
// v3 dashboard scorecard (Total R hero · win rate · best / worst).
// Enter your log once in the settings · the script parses, computes R,
// and paints markers at each trade's date.
// No buy/sell signals. Not financial advice.
// ═══════════════════════════════════════════════════════════════
//
// v2.0.0 (2026-10-06) — FOUR-BLOCK DASHBOARD REDESIGN · four-block dashboard layout.
//   • Brand ≡ R-LOG · v3 cyan palette
//   • STATE pill (▲ WINNING / ▼ LOSING / ─ BREAK EVEN)
//   • HERO · Total R as the big "scoreboard" number · color-coded
//   • SYNOPSIS · plain-English session summary
//   • BREAKDOWN · trade count · win rate · avg / best / worst R · errors
// v1.2.0 (2026-09-05) — Marker anchoring polish (yloc.abovebar/belowbar).
// v1.1.0 (2026-09-05) — Auto-fit markers + vertical trade lines.
// v1.0.0 (2026-09-05) — Initial Wave 3 release.
//
// ═══════════════════════════════════════════════════════════════
 
//@version=6
indicator("Trding R-Multiple Trade Log", shorttitle="TrdingRLog",
     overlay=true, max_labels_count=100, max_lines_count=50)
 
// ═══════════════════════════════════════════════════════════════
// v3 BRAND PALETTE
// ═══════════════════════════════════════════════════════════════
color V3_CYAN     = #00D4FF
color V3_BG_DEEP  = #0A0E1A
color V3_CARD     = #141D3A
color V3_HEAD     = #1A2240
color V3_WHITE    = #E8EDF5
color V3_MUTED    = #6B7A8D
color V3_GREEN    = #5EE33E
color V3_AMBER    = #FFD166
color V3_RED      = #3B82F6
color V3_BORDER   = color.new(#00D4FF, 50)
 
// ═══════════════════════════════════════════════════════════════
// INPUTS
// ═══════════════════════════════════════════════════════════════
grpL = "Trade Log"
trade_log = input.string(
     "2026-08-01,LONG,703.50,715.00,700.00|2026-08-05,SHORT,731.00,720.00,733.50|2026-08-12,LONG,731.00,728.00,728.00",
     "Trades", group=grpL,
     tooltip="Format per trade: DATE,DIR,ENTRY,EXIT,STOP\n" +
             "  DATE  = YYYY-MM-DD\n" +
             "  DIR   = LONG or SHORT\n" +
             "  ENTRY = fill price\n" +
             "  EXIT  = close price\n" +
             "  STOP  = original stop price\n" +
             "Separate trades with pipe |  ·  cap 20 trades  ·  no spaces needed.")
 
grpD = "Display"
show_wins       = input.bool(true,  "Show wins",   group=grpD, inline="wl")
show_losses     = input.bool(true,  "Show losses", group=grpD, inline="wl")
show_r_labels   = input.bool(true,  "Show R multiple labels on markers", group=grpD)
show_trade_nums = input.bool(true,  "Show trade numbers (#1, #2, …)",     group=grpD)
showDash        = input.bool(true,  "Show v3 dashboard", group=grpD)
auto_fit        = input.bool(true,  "Auto-fit markers to bar high/low", group=grpD)
show_v_lines    = input.bool(true,  "Show vertical trade lines", group=grpD)
marker_size     = input.string("Normal", "Marker size",
     options=["Tiny", "Small", "Normal", "Large"], group=grpD)
 
grpC = "Colors"
col_win       = input.color(#5EE33E,     "Win",  group=grpC, inline="wl")
col_loss      = input.color(#3B82F6,     "Loss", group=grpC, inline="wl")
 
// ═══════════════════════════════════════════════════════════════
// STORAGE
// ═══════════════════════════════════════════════════════════════
var arr_ts    = array.new<int>()
var arr_dir   = array.new<string>()
var arr_entry = array.new<float>()
var arr_exit  = array.new<float>()
var arr_stop  = array.new<float>()
var arr_r     = array.new<float>()
var arr_win   = array.new<bool>()
var arr_err   = array.new<string>()
 
// ═══════════════════════════════════════════════════════════════
// HELPERS
// ═══════════════════════════════════════════════════════════════
clean(string s) => str.replace_all(s, " ", "")
 
parseDate(string s) =>
    parts = str.split(s, "-")
    int result = na
    if array.size(parts) == 3
        y = str.tonumber(array.get(parts, 0))
        m = str.tonumber(array.get(parts, 1))
        d = str.tonumber(array.get(parts, 2))
        if not na(y) and not na(m) and not na(d)
            result := timestamp(int(y), int(m), int(d), 12, 0)
    result
 
f_r(float r, string fmt) =>
    (r > 0 ? "+" : "") + str.tostring(r, fmt) + "R"
 
// ═══════════════════════════════════════════════════════════════
// PARSE TRADE LOG
// ═══════════════════════════════════════════════════════════════
if barstate.isfirst
    trades = str.split(trade_log, "|")
    tcount = math.min(array.size(trades), 20)
    if tcount > 0
        for i = 0 to tcount - 1
            raw = clean(array.get(trades, i))
            if str.length(raw) > 0
                fields = str.split(raw, ",")
                if array.size(fields) == 5
                    dstr = array.get(fields, 0)
                    dir  = str.upper(array.get(fields, 1))
                    en   = str.tonumber(array.get(fields, 2))
                    ex   = str.tonumber(array.get(fields, 3))
                    st   = str.tonumber(array.get(fields, 4))
                    ts   = parseDate(dstr)
                    bad_parse = na(en) or na(ex) or na(st) or na(ts) or
                         (dir != "LONG" and dir != "SHORT")
                    if bad_parse
                        array.push(arr_err, "Trade " + str.tostring(i + 1) + ": parse error")
                    else
                        float r = na
                        bool valid = false
                        if dir == "LONG"
                            if en > st
                                r := (ex - en) / (en - st)
                                valid := true
                        else
                            if st > en
                                r := (en - ex) / (st - en)
                                valid := true
                        if valid
                            r := math.round(r * 10) / 10.0
                            array.push(arr_ts,    ts)
                            array.push(arr_dir,   dir)
                            array.push(arr_entry, en)
                            array.push(arr_exit,  ex)
                            array.push(arr_stop,  st)
                            array.push(arr_r,     r)
                            array.push(arr_win,   r > 0)
                        else
                            array.push(arr_err, "Trade " + str.tostring(i + 1) + ": stop on wrong side of entry")
                else
                    array.push(arr_err, "Trade " + str.tostring(i + 1) + ": expected 5 fields")
 
// ═══════════════════════════════════════════════════════════════
// STYLE MAPPING
// ═══════════════════════════════════════════════════════════════
sizeVal = marker_size == "Tiny"  ? size.tiny  :
         marker_size == "Small" ? size.small :
         marker_size == "Large" ? size.large : size.normal
 
// ═══════════════════════════════════════════════════════════════
// AUTO-FIT ANCHORS
// ═══════════════════════════════════════════════════════════════
var float chart_high = na
var float chart_low  = na
chart_high := na(chart_high) ? high : math.max(chart_high, high)
chart_low  := na(chart_low)  ? low  : math.min(chart_low,  low)
 
// ═══════════════════════════════════════════════════════════════
// DRAW TRADE MARKERS + VERTICAL LINES
// ═══════════════════════════════════════════════════════════════
var array<label> label_refs = array.new<label>()
var array<line>  line_refs  = array.new<line>()
 
if barstate.islast
    if array.size(label_refs) > 0
        for lbl in label_refs
            label.delete(lbl)
        array.clear(label_refs)
    if array.size(line_refs) > 0
        for ln in line_refs
            line.delete(ln)
        array.clear(line_refs)
 
    float line_y_top = chart_high * 10
    float line_y_bot = chart_low  * 0.1
 
    n = array.size(arr_ts)
    if n > 0
        for i = 0 to n - 1
            isWin = array.get(arr_win, i)
            if (isWin and show_wins) or (not isWin and show_losses)
                ts     = array.get(arr_ts,    i)
                entry  = array.get(arr_entry, i)
                exitP  = array.get(arr_exit,  i)
                stopP  = array.get(arr_stop,  i)
                dir    = array.get(arr_dir,   i)
                r      = array.get(arr_r,     i)
                col    = isWin ? col_win : col_loss
                sty    = isWin ? label.style_triangleup : label.style_triangledown
                num_str = show_trade_nums ? "#" + str.tostring(i + 1) : ""
                r_str   = show_r_labels   ? f_r(r, "#.#") : ""
                txt     = num_str + (str.length(num_str) > 0 and str.length(r_str) > 0 ? "  " : "") + r_str
                txtcol  = isWin ? color.black : color.white
                float marker_y = auto_fit ? na : entry
                y_loc = auto_fit ? (isWin ? yloc.abovebar : yloc.belowbar) : yloc.price
                tt = "Trade #" + str.tostring(i + 1) + "  ·  " + dir + "  ·  " + f_r(r, "#.#") +
                     "\nEntry: " + str.tostring(entry, format.mintick) +
                     "\nExit:  " + str.tostring(exitP, format.mintick) +
                     "\nStop:  " + str.tostring(stopP, format.mintick)
                lbl = label.new(x=ts, y=marker_y, text=txt,
                     xloc=xloc.bar_time, yloc=y_loc,
                     style=sty, color=col, textcolor=txtcol, size=sizeVal, tooltip=tt)
                array.push(label_refs, lbl)
 
                if show_v_lines
                    ln = line.new(x1=ts, y1=line_y_bot, x2=ts, y2=line_y_top,
                         xloc=xloc.bar_time, style=line.style_dashed,
                         color=color.new(col, 65), width=1)
                    array.push(line_refs, ln)
 
// ═══════════════════════════════════════════════════════════════
// AGGREGATE
// ═══════════════════════════════════════════════════════════════
int    n_trades = array.size(arr_ts)
int    wins     = 0
int    losses   = 0
float  totalR   = 0.0
float  bestR    = -1e10
float  worstR   =  1e10
 
if n_trades > 0
    for i = 0 to n_trades - 1
        r = array.get(arr_r, i)
        w = array.get(arr_win, i)
        if w
            wins += 1
        else
            losses += 1
        totalR += r
        bestR  := math.max(bestR, r)
        worstR := math.min(worstR, r)
 
float winRate = n_trades > 0 ? wins / float(n_trades) * 100.0 : 0.0
float avgR    = n_trades > 0 ? totalR / n_trades : 0.0
int   errCount = array.size(arr_err)
 
// ═══════════════════════════════════════════════════════════════
// v3 FOUR-BLOCK DASHBOARD
// ═══════════════════════════════════════════════════════════════
string stateLbl = n_trades == 0 ? "─ NO LOG" : totalR > 0 ? "▲ WINNING" : totalR < 0 ? "▼ LOSING" : "─ BREAK EVEN"
color  stateCol = n_trades == 0 ? V3_MUTED : totalR > 0 ? V3_GREEN : totalR < 0 ? V3_RED : V3_AMBER
color  heroCol  = n_trades == 0 ? V3_MUTED : totalR > 0 ? V3_GREEN : V3_RED
string heroVal  = n_trades == 0 ? "—" : f_r(totalR, "#.#")
string synopsis = n_trades == 0 ? "Enter trades in settings to see your R scoreboard"
     : totalR > 0 ? str.tostring(n_trades) + " trades · " + f_r(totalR, "#.#") + " total · " + str.tostring(winRate, "#") + "% win rate · profitable"
     : totalR < 0 ? str.tostring(n_trades) + " trades · " + f_r(totalR, "#.#") + " total · review losses in #" + str.tostring(wins + 1) + "+"
     : str.tostring(n_trades) + " trades · break-even · tighten entries or sizing"
 
var table v3Dash = table.new(position.top_right, 2, 13,
     bgcolor=V3_BG_DEEP, border_width=0, frame_width=2, frame_color=V3_BORDER)
 
if showDash and barstate.islast
    table.clear(v3Dash, 0, 0, 1, 12)
    int r = 0
 
    // Brand
    table.cell(v3Dash, 0, r, "≡ R-LOG", text_color=V3_CYAN, bgcolor=V3_BG_DEEP, text_size=size.small, text_halign=text.align_center)
    table.cell(v3Dash, 1, r, "", bgcolor=V3_BG_DEEP)
    table.merge_cells(v3Dash, 0, r, 1, r)
    r += 1
 
    // State pill + trades count
    table.cell(v3Dash, 0, r, stateLbl, text_color=stateCol, bgcolor=color.new(stateCol, 85), text_size=size.small, text_halign=text.align_left)
    table.cell(v3Dash, 1, r, str.tostring(n_trades) + " TRADES", text_color=V3_CYAN, bgcolor=color.new(V3_CYAN, 85), text_size=size.small, text_halign=text.align_right)
    r += 1
 
    // Hero label
    table.cell(v3Dash, 0, r, "TOTAL R", text_color=V3_MUTED, bgcolor=V3_CARD, text_size=size.tiny, text_halign=text.align_center)
    table.cell(v3Dash, 1, r, "", bgcolor=V3_CARD)
    table.merge_cells(v3Dash, 0, r, 1, r)
    r += 1
 
    // Hero value (Total R)
    table.cell(v3Dash, 0, r, heroVal, text_color=heroCol, bgcolor=V3_CARD, text_size=size.huge, text_halign=text.align_center)
    table.cell(v3Dash, 1, r, "", bgcolor=V3_CARD)
    table.merge_cells(v3Dash, 0, r, 1, r)
    r += 1
 
    // Hero sub (win rate)
    table.cell(v3Dash, 0, r, n_trades > 0 ? str.tostring(winRate, "#.#") + "% win rate · " + f_r(avgR, "#.##") + " avg" : "no trades logged", text_color=V3_MUTED, bgcolor=V3_CARD, text_size=size.tiny, text_halign=text.align_center)
    table.cell(v3Dash, 1, r, "", bgcolor=V3_CARD)
    table.merge_cells(v3Dash, 0, r, 1, r)
    r += 1
 
    // Synopsis
    table.cell(v3Dash, 0, r, synopsis, text_color=V3_WHITE, bgcolor=color.new(V3_CYAN, 92), text_size=size.tiny, text_halign=text.align_center)
    table.cell(v3Dash, 1, r, "", bgcolor=color.new(V3_CYAN, 92))
    table.merge_cells(v3Dash, 0, r, 1, r)
    r += 1
 
    // Breakdown header
    table.cell(v3Dash, 0, r, "SCORECARD", text_color=V3_CYAN, bgcolor=color.new(V3_CYAN, 85), text_size=size.tiny, text_halign=text.align_center)
    table.cell(v3Dash, 1, r, "", bgcolor=color.new(V3_CYAN, 85))
    table.merge_cells(v3Dash, 0, r, 1, r)
    r += 1
 
    // Breakdown rows
    table.cell(v3Dash, 0, r, "Wins", text_color=V3_MUTED, bgcolor=V3_BG_DEEP, text_size=size.small, text_halign=text.align_left)
    table.cell(v3Dash, 1, r, str.tostring(wins), text_color=col_win, bgcolor=V3_BG_DEEP, text_size=size.small, text_halign=text.align_right)
    r += 1
    table.cell(v3Dash, 0, r, "Losses", text_color=V3_MUTED, bgcolor=V3_CARD, text_size=size.small, text_halign=text.align_left)
    table.cell(v3Dash, 1, r, str.tostring(losses), text_color=col_loss, bgcolor=V3_CARD, text_size=size.small, text_halign=text.align_right)
    r += 1
    table.cell(v3Dash, 0, r, "Best", text_color=V3_MUTED, bgcolor=V3_BG_DEEP, text_size=size.small, text_halign=text.align_left)
    table.cell(v3Dash, 1, r, n_trades > 0 ? f_r(bestR, "#.#") : "—", text_color=col_win, bgcolor=V3_BG_DEEP, text_size=size.small, text_halign=text.align_right)
    r += 1
    table.cell(v3Dash, 0, r, "Worst", text_color=V3_MUTED, bgcolor=V3_CARD, text_size=size.small, text_halign=text.align_left)
    table.cell(v3Dash, 1, r, n_trades > 0 ? f_r(worstR, "#.#") : "—", text_color=col_loss, bgcolor=V3_CARD, text_size=size.small, text_halign=text.align_right)
    r += 1
    table.cell(v3Dash, 0, r, "Errors", text_color=V3_MUTED, bgcolor=V3_BG_DEEP, text_size=size.small, text_halign=text.align_left)
    table.cell(v3Dash, 1, r, errCount > 0 ? str.tostring(errCount) + " skipped" : "0", text_color=errCount > 0 ? col_loss : V3_MUTED, bgcolor=V3_BG_DEEP, text_size=size.small, text_halign=text.align_right)
 
// ═══════════════════════════════════════════════════════════════
// ERROR DETAIL LABEL (only when there are parse errors)
// ═══════════════════════════════════════════════════════════════
var label err_lbl = na
 
if barstate.islast
    if not na(err_lbl)
        label.delete(err_lbl)
        err_lbl := na
    if array.size(arr_err) > 0
        string msg = "⚠ R-LOG parse errors:"
        for j = 0 to array.size(arr_err) - 1
            msg := msg + "\n• " + array.get(arr_err, j)
        err_lbl := label.new(x=bar_index, y=high, text=msg,
             xloc=xloc.bar_index, yloc=yloc.price,
             style=label.style_label_down, color=color.new(V3_RED, 20),
             textcolor=color.white, size=size.small)

← 返回列表