Trade Tracker (with Trailing Stop)

asg1994 · study · 188 行 · 点赞 98 · 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/
// © asg1994
 
//@version=6
indicator("Trade Tracker", overlay = true)
 
// ── Settings ──────────────────────────────────────────
dir             = input.string("Long", "Direction", options = ["Long", "Short"])
startTime       = input.time(timestamp("2026-09-20 00:00"), "Trade start time (candle you entered on)")
entryPrice      = input.price(100000, "Entry price")
tpPrice         = input.price(101000, "Take profit (TP) price")
slPrice         = input.price(99500,  "Stop loss (SL) price")
activationPrice = input.price(100500, "Trailing stop activation price")
callbackPct     = input.float(0.5, "Trailing callback (% pullback from best price)", minval = 0.01, step = 0.05)
showTable       = input.bool(true, "Show status box")
showLabels      = input.bool(true, "Show labels on the lines")
showZones       = input.bool(true, "Show shaded zones (profit / risk)")
labelOffset     = input.int(3, "Label distance from last candle (bars)", minval = 1, maxval = 50)
 
isLong = dir == "Long"
 
// ── Tracking the trade ────────────────────────────────
var bool   activated  = false
var bool   closed     = false
var float  bestPrice  = na
var string exitReason = ""
var float  exitPrice  = na
var int    startBar   = na
 
bool justActivated = false
bool justClosed    = false
 
// Remember which candle the trade started on (used for the shaded zones)
if na(startBar) and time >= startTime
    startBar := bar_index
 
// Trailing stop level as it stood BEFORE this candle (based on earlier candles only)
float trailBefore = na(bestPrice) ? na : isLong ? bestPrice * (1 - callbackPct / 100) : bestPrice * (1 + callbackPct / 100)
 
if time >= startTime and not closed
    // Step 1: did the trade end on this candle?
    // The SL is the floor; the trailing stop takes over once it is closer to price than the SL
    float  stopLevel = slPrice
    string stopName  = "SL"
    if not na(trailBefore) and (isLong ? trailBefore > slPrice : trailBefore < slPrice)
        stopLevel := trailBefore
        stopName  := "Trailing stop"
 
    bool stopHit = isLong ? low <= stopLevel : high >= stopLevel
    bool tpHit   = isLong ? high >= tpPrice  : low <= tpPrice
 
    // If both could have happened in one candle, assume the stop came first (cautious)
    if stopHit
        closed     := true
        justClosed := true
        exitReason := stopName
        exitPrice  := stopLevel
    else if tpHit
        closed     := true
        justClosed := true
        exitReason := "TP"
        exitPrice  := tpPrice
 
    // Step 2: if still open, check activation and track the best price
    if not closed
        if not activated
            if isLong ? high >= activationPrice : low <= activationPrice
                activated     := true
                justActivated := true
        if activated
            bestPrice := na(bestPrice) ? (isLong ? high : low) : (isLong ? math.max(bestPrice, high) : math.min(bestPrice, low))
 
// ── Trailing stop level (after this candle's update) ──
float trailStop = (closed or na(bestPrice)) ? na : isLong ? bestPrice * (1 - callbackPct / 100) : bestPrice * (1 + callbackPct / 100)
 
// ── Lines on the chart (only while the trade is live) ─
bool live = time >= startTime and (not closed or justClosed)
 
plot(live ? entryPrice      : na, "Entry",               color = color.gray,   linewidth = 2, style = plot.style_linebr)
plot(live ? tpPrice         : na, "TP",                  color = color.green,  linewidth = 2, style = plot.style_linebr)
plot(live ? slPrice         : na, "SL",                  color = color.red,    linewidth = 2, style = plot.style_linebr)
plot(live ? activationPrice : na, "Trailing activation", color = color.orange, linewidth = 1, style = plot.style_linebr)
plot(closed ? na : bestPrice,     "Best price since activation", color = color.new(color.aqua, 50), linewidth = 1, style = plot.style_linebr)
plot(trailStop,                   "Trailing stop",       color = color.yellow, linewidth = 2, style = plot.style_linebr)
 
// ── Extending the lines to the right, with labels and zones ──
// Plot lines can't run past the current candle, so we draw a separate line from the
// current candle to the right, plus a name label and shaded zones.
var array<line>  extLines  = array.new<line>()
var array<label> extLabels = array.new<label>()
var array<box>   extBoxes  = array.new<box>()
 
f_level(float price, string name, color c, color tc = color.white, int w = 2) =>
    array.push(extLines, line.new(bar_index, price, bar_index + 1, price, extend = extend.right, color = c, width = w))
    if showLabels
        array.push(extLabels, label.new(bar_index + labelOffset, price, name, style = label.style_label_left, color = color.new(c, 15), textcolor = tc, size = size.small))
 
f_zone(float p1, float p2, color c) =>
    array.push(extBoxes, box.new(startBar, math.max(p1, p2), bar_index + 1, math.min(p1, p2), border_color = na, extend = extend.right, bgcolor = color.new(c, 88)))
 
if barstate.islast
    // Clear last update's drawings, then redraw
    for l in extLines
        line.delete(l)
    for lb in extLabels
        label.delete(lb)
    for b in extBoxes
        box.delete(b)
    array.clear(extLines)
    array.clear(extLabels)
    array.clear(extBoxes)
 
    // Only while the trade is live (started and not yet closed)
    if time >= startTime and not closed
        if showZones
            f_zone(entryPrice, tpPrice, color.green)
            f_zone(entryPrice, slPrice, color.red)
        f_level(entryPrice,      "▶ Entry",               color.gray)
        f_level(tpPrice,         "🎯 TP",                  color.green)
        f_level(slPrice,         "🛑 SL",                  color.red)
        f_level(activationPrice, "⚡ Trailing activation", color.orange, color.black, 1)
        if activated and not na(trailStop)
            f_level(trailStop,   "🔒 Trailing stop",       color.yellow, color.black)
 
// ── Markers ───────────────────────────────────────────
plotshape(justActivated ? activationPrice : na, "Trailing activated", style = shape.circle,  location = location.absolute, color = color.orange, size = size.small)
plotshape(justClosed and exitReason == "TP"            ? exitPrice : na, "TP hit",            style = shape.diamond, location = location.absolute, color = color.green,  size = size.small)
plotshape(justClosed and exitReason == "SL"            ? exitPrice : na, "SL hit",            style = shape.xcross,  location = location.absolute, color = color.red,    size = size.small)
plotshape(justClosed and exitReason == "Trailing stop" ? exitPrice : na, "Trailing stop hit", style = shape.xcross,  location = location.absolute, color = color.yellow, size = size.small)
 
// ── Alerts ────────────────────────────────────────────
if justActivated
    alert("Trailing stop activated - now tracking the best price", alert.freq_once_per_bar)
if justClosed
    alert("Trade closed: " + exitReason + " hit at " + str.tostring(exitPrice, "#.##"), alert.freq_once_per_bar)
 
// ── Status box ────────────────────────────────────────
// Rows: 0 title | 1 status | 2 price | 3 move | 4 trailing stop | 5 to TP | 6 to SL | 7 progress bar
var table info = table.new(position.top_right, 2, 8, border_width = 1)
 
if barstate.isfirst
    // Make the title row and the progress bar row span both columns
    table.cell(info, 0, 0, "")
    table.cell(info, 0, 7, "")
    table.merge_cells(info, 0, 0, 1, 0)
    table.merge_cells(info, 0, 7, 1, 7)
 
f_row(int row, string label, string value, color bg = na, color tc = color.white) =>
    table.cell(info, 0, row, label, text_color = color.white, text_halign = text.align_left,  bgcolor = color.new(color.black, 20))
    table.cell(info, 1, row, value, text_color = tc,          text_halign = text.align_right, bgcolor = na(bg) ? color.new(color.black, 20) : bg)
 
if showTable and barstate.islast
    string statusIcon = closed ? (exitReason == "TP" ? "🎯 " : exitReason == "SL" ? "🛑 " : "🔒 ") : activated ? "🔒 " : "⏳ "
    string status = statusIcon + (closed ? "Closed - " + exitReason : activated ? "Trailing stop active" : "Waiting for activation")
    color  statusColor = closed ? (exitReason == "TP" ? color.green : exitReason == "SL" ? color.red : color.orange) : activated ? color.teal : color.gray
 
    float ref  = closed ? exitPrice : close
    float move = isLong ? ref - entryPrice : entryPrice - ref
    float toTp = isLong ? tpPrice - close : close - tpPrice
    float toSl = isLong ? close - slPrice : slPrice - close
    float toAct = math.max(0, isLong ? activationPrice - close : close - activationPrice)
 
    string trailText = closed ? "-" : activated ? str.tostring(trailStop, "#.##") : "Not active ($" + str.tostring(toAct, "#") + " to go)"
 
    // Green when the trade is in profit, red when it is losing
    color pnlColor = move >= 0 ? #00e676 : #ff5252
 
    // Progress bar: left end = SL, right end = TP (works the same for long and short)
    float rawProgress = (ref - slPrice) / (tpPrice - slPrice)
    float progress    = nz(math.max(0.0, math.min(1.0, rawProgress)))
    int   barLen      = 12
    int   filled      = int(math.round(progress * barLen))
    string barText    = ""
    for i = 0 to barLen - 1
        barText += i < filled ? "█" : "░"
 
    // Title bar (green for long, red for short)
    table.cell(info, 0, 0, "TRADE TRACKER   " + (isLong ? "▲ LONG" : "▼ SHORT"), text_color = color.white, text_halign = text.align_center, bgcolor = isLong ? #00897b : #c62828)
 
    f_row(1, "Status",             status, statusColor)
    f_row(2, "Price now",          str.tostring(close, "#.##"))
    f_row(3, "Move vs entry ($)",  (move >= 0 ? "+" : "") + str.tostring(move, "#.##"), color(na), pnlColor)
    f_row(4, "Trailing stop",      trailText)
    f_row(5, "Distance to TP ($)", closed ? "-" : str.tostring(toTp, "#.##"))
    f_row(6, "Distance to SL ($)", closed ? "-" : str.tostring(toSl, "#.##"))
 
    // Progress bar row
    table.cell(info, 0, 7, "SL  " + barText + "  TP", text_color = pnlColor, text_halign = text.align_center, bgcolor = color.new(color.black, 20))

← 返回列表