本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ProofCharts
//@version=6
indicator("Hindsight vs Live: Why Perfect Signals Repaint", shorttitle = "Hindsight vs Live", overlay = true, max_labels_count = 500, max_lines_count = 500)
// One signal, two records. The signal buys at a swing low and sells at a swing high. A swing is known only some
// candles after it happened, so a script that draws the arrow at the turn shows a record nobody could trade live.
// Hindsight record: in and out at the close of the swing candle (where the arrow is drawn).
// Live record: the same trades, in and out at the first close where the swing could be known.
// Numbers are attached to the candle where they became known and never change after it closes.
// The hindsight arrows are drawn back in time on purpose, to show what a repainting chart looks like.
G_RULE = "Signal"
G_LOOK = "Display"
iN = input.int(5, "Turning point: candles on each side", minval = 2, maxval = 20, group = G_RULE, tooltip = "A swing low is the lowest low with this many candles on each side of it. So you only know it this many candles later. Swing highs work the same way.")
iHind = input.bool(true, "Hindsight arrows (drawn at the turn)", group = G_LOOK, tooltip = "Arrows on the turning candle itself, the way a repainting indicator shows them. Repainting means a signal changes or moves after it was shown. These arrows appear only once the turn is confirmed.")
iLive = input.bool(true, "Live markers (when it could be known)", group = G_LOOK, tooltip = "Markers on the candle where the turn was first confirmed. That is the first moment anyone could act on it.")
iDelay = input.bool(true, "Delay lines", group = G_LOOK, tooltip = "Dotted line from the hindsight price (at the turn) to the live price (when you could first know) of the same signal.")
iCand = input.bool(true, "Possible new turns on the newest candles", group = G_LOOK, tooltip = "Question marks on the newest candles that are a new low or high so far. They may still disappear. They are not part of any record.")
iPanel = input.bool(true, "Panel", group = G_LOOK, tooltip = "Box with both records side by side: hindsight and live.")
iPos = input.string("Top right", "Panel position", options = ["Top right", "Bottom right", "Bottom left", "Top left"], group = G_LOOK, tooltip = "Where the panel sits on the chart.", active = iPanel)
// ---------- Calculation ----------
float pl = ta.pivotlow(low, iN, iN)
float ph = ta.pivothigh(high, iN, iN)
bool candL = bar_index >= iN and math.sign(low - ta.lowest(low, iN)[1]) <= 0
bool candH = bar_index >= iN and math.sign(high - ta.highest(high, iN)[1]) >= 0
var int arrows = 0
var int vanished = 0
if bar_index - iN >= iN
if candL[iN]
arrows += 1
vanished += na(pl) ? 1 : 0
if candH[iN]
arrows += 1
vanished += na(ph) ? 1 : 0
var int pos = 0
var float hEntry = na
var float lEntry = na
var int trades = 0
var int hWins = 0
var int lWins = 0
var float hSum = 0.0
var float lSum = 0.0
var float hWorst = na
var float lWorst = na
int sig = 0
if not na(pl) and not na(ph)
sig := pos == 1 ? -1 : pos == -1 ? 1 : 0
else if not na(pl)
sig := pos != 1 ? 1 : 0
else if not na(ph)
sig := pos != -1 ? -1 : 0
float hClose = close[iN]
if sig != 0
if pos != 0
trades += 1
float rh = pos * (hClose / hEntry - 1) * 100
float rl = pos * (close / lEntry - 1) * 100
hWins += math.sign(pos * (hClose - hEntry)) > 0 ? 1 : 0
lWins += math.sign(pos * (close - lEntry)) > 0 ? 1 : 0
hSum += rh
lSum += rl
hWorst := na(hWorst) ? rh : math.min(hWorst, rh)
lWorst := na(lWorst) ? rl : math.min(lWorst, rl)
pos := sig
hEntry := hClose
lEntry := close
float hWinPct = trades > 0 ? 100.0 * hWins / trades : na
float lWinPct = trades > 0 ? 100.0 * lWins / trades : na
float hAvg = trades > 0 ? hSum / trades : na
float lAvg = trades > 0 ? lSum / trades : na
float vanPct = arrows > 0 ? 100.0 * vanished / arrows : na
// ---------- Look ----------
color C_PANEL = #0f172a
color C_HEAD = #1e293b
color C_TEXT = #e2e8f0
color C_MUTED = #94a3b8
color C_GREEN = #22c55e
color C_RED = #ef4444
color C_AMBER = #f59e0b
color C_SKY = #38bdf8
if sig != 0 and barstate.isconfirmed
if iHind
label.new(bar_index - iN, sig == 1 ? low[iN] : high[iN], "", yloc = sig == 1 ? yloc.belowbar : yloc.abovebar, style = sig == 1 ? label.style_triangleup : label.style_triangledown, color = color.new(sig == 1 ? C_GREEN : C_RED, 0), size = size.small, tooltip = "Hindsight " + (sig == 1 ? "buy" : "sell") + ": drawn here " + str.tostring(iN) + " candles later, when the swing was confirmed.")
if iDelay
line.new(bar_index - iN, hClose, bar_index, close, color = color.new(C_AMBER, 20), style = line.style_dotted, width = 1)
plotshape(iLive and sig == 1 and barstate.isconfirmed, "Live buy", shape.labelup, location.belowbar, color.new(C_SKY, 10), text = "live", textcolor = C_PANEL, size = size.tiny, display = display.pane)
plotshape(iLive and sig == -1 and barstate.isconfirmed, "Live sell", shape.labeldown, location.abovebar, color.new(C_AMBER, 10), text = "live", textcolor = C_PANEL, size = size.tiny, display = display.pane)
// ---------- Values in the Data Window (also used by the tests) ----------
plot(sig == 1 ? 1 : 0, "Buy confirmed", display = display.data_window)
plot(sig == -1 ? 1 : 0, "Sell confirmed", display = display.data_window)
plot(pos, "Position (1 long, -1 short, 0 none)", display = display.data_window)
plot(trades, "Trades (both records)", display = display.data_window)
plot(hWins, "Hindsight wins", display = display.data_window)
plot(hWinPct, "Hindsight win %", display = display.data_window)
plot(hAvg, "Hindsight average %", display = display.data_window)
plot(hSum, "Hindsight total %", display = display.data_window)
plot(hWorst, "Hindsight worst %", display = display.data_window)
plot(lWins, "Live wins", display = display.data_window)
plot(lWinPct, "Live win %", display = display.data_window)
plot(lAvg, "Live average %", display = display.data_window)
plot(lSum, "Live total %", display = display.data_window)
plot(lWorst, "Live worst %", display = display.data_window)
plot(trades > 0 ? hAvg - lAvg : na, "Gap per trade (points)", display = display.data_window)
plot(arrows, "Arrows shown live", display = display.data_window)
plot(vanished, "Arrows that vanished", display = display.data_window)
plot(vanPct, "Vanished %", display = display.data_window)
// ---------- Candidates and panel ----------
pos2 = iPos == "Bottom right" ? position.bottom_right : iPos == "Bottom left" ? position.bottom_left : iPos == "Top left" ? position.top_left : position.top_right
// Breaks a text into lines of at most n characters, at spaces only (no word is changed), so the panel fits the chart
// of a phone held upright (compliance/phone_fit.py; owner's Android screenshot 2026-11-03: wide panels were cut off).
wrapText(string s, int n) =>
string out = ""
int len = -1
for w in str.split(s, " ")
int wl = str.length(w)
if len < 0
out := w
len := wl
else if len + 1 + wl > n
out := out + "\n" + w
len := wl
else
out := out + " " + w
len := len + 1 + wl
out
var table tb = table.new(pos2, 1, 7, bgcolor = color.new(C_PANEL, 5), frame_color = color.new(C_MUTED, 70), frame_width = 1)
var label[] cands = array.new_label(0)
sp(float x) => (x > 0 ? "+" : "") + str.tostring(x, "0.00")
if barstate.islast
while array.size(cands) > 0
label.delete(array.pop(cands))
if iCand
for k = 0 to iN - 1
if candL[k]
array.push(cands, label.new(bar_index - k, low[k], "?", yloc = yloc.belowbar, style = label.style_none, textcolor = C_MUTED, size = size.normal, tooltip = "New " + str.tostring(iN) + "-candle low so far. It becomes a swing only if the next " + str.tostring(iN) + " candles stay above it. Not in any record."))
if candH[k]
array.push(cands, label.new(bar_index - k, high[k], "?", yloc = yloc.abovebar, style = label.style_none, textcolor = C_MUTED, size = size.normal, tooltip = "New " + str.tostring(iN) + "-candle high so far. It becomes a swing only if the next " + str.tostring(iN) + " candles stay below it. Not in any record."))
if iPanel
for r = 0 to 6
table.cell(tb, 0, r, "")
table.cell(tb, 0, 0, wrapText("HINDSIGHT VS LIVE" + (barstate.isconfirmed ? "" : " · live"), 30), text_color = color.white, bgcolor = C_HEAD, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 0, 1, wrapText("Same signal: buy at a swing low, sell at a swing high (" + str.tostring(2 * iN + 1) + "-candle swings).", 30), text_color = C_TEXT, text_size = size.small, text_halign = text.align_left)
int row = 2
if trades == 0
table.cell(tb, 0, row, wrapText("No completed trade yet (n = 0).", 30), text_color = C_TEXT, text_size = size.small, text_halign = text.align_left)
row += 1
else
string k = str.tostring(trades)
table.cell(tb, 0, row, wrapText("Hindsight (arrow at the turn): trades " + k + ", wins " + str.tostring(hWins) + " (" + str.tostring(hWinPct, "0") + "%), average " + sp(hAvg) + "%, worst " + sp(hWorst) + "%.", 30), text_color = C_GREEN, text_size = size.small, text_halign = text.align_left, tooltip = "In and out at the close of the swing candle. Nobody could trade this live.")
table.cell(tb, 0, row + 1, wrapText("Live (when it could be known, " + str.tostring(iN) + " candles later): trades " + k + ", wins " + str.tostring(lWins) + " (" + str.tostring(lWinPct, "0") + "%), average " + sp(lAvg) + "%, worst " + sp(lWorst) + "%.", 30), text_color = C_SKY, text_size = size.small, text_halign = text.align_left, tooltip = "The same trades, in and out at the first close where the swing was confirmed.")
table.cell(tb, 0, row + 2, wrapText("Gap: " + str.tostring(hAvg - lAvg, "0.00") + " points per trade. The only difference is time.", 30), text_color = C_AMBER, text_size = size.small, text_halign = text.align_left)
row += 3
if arrows > 0
table.cell(tb, 0, row, wrapText("A script that marks every new " + str.tostring(iN) + "-candle low or high would have shown " + str.tostring(arrows) + " arrows live; " + str.tostring(vanished) + " (" + str.tostring(vanPct, "0") + "%) vanished later.", 30), text_color = C_TEXT, text_size = size.small, text_halign = text.align_left)
row += 1
table.cell(tb, 0, row, wrapText("No costs included. Past behaviour, not a forecast.", 36), text_color = C_MUTED, text_size = size.tiny, text_halign = text.align_left)