本页源码来自 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/
// © harrell.jordon
//@version=6
indicator("Win Rate With Its Error Bar (Wilson 95%)", shorttitle = "WinRateCI", overlay = false)
// What this shows
// One deliberately dull rule, traded bar by bar on this chart: long when the close
// crosses above a simple moving average, out when it crosses back below. Each closed
// trade is a win (exit above entry) or a loss (a flat exit counts as a loss).
// Blue is the running win rate, the teal band is its Wilson 95% score interval, and
// red is the breakeven win rate. The question is not where blue is, but whether the
// BOTTOM of the band has cleared red. That is check 1: count the trades.
smaLen = input.int(50, "SMA length", minval = 2)
useOwn = input.bool(true, "Breakeven from this rule's own payoff (untick for the fixed value)")
beFixed = input.float(50.0, "Fixed breakeven win rate (%)", minval = 0.0, maxval = 100.0)
Z = 1.959964 // two-sided 95% normal quantile
// Wilson 95% lower bound for win rate p over n trades
wilsonLo(float p, float n) =>
z2 = Z * Z
(p + z2 / (2 * n) - Z * math.sqrt(p * (1 - p) / n + z2 / (4 * n * n))) / (1 + z2 / n)
// Smallest trade count at which the lower bound for win rate p clears breakeven b
// (binary search, as the trades-needed arithmetic); na when p does not exceed b
tradesNeeded(float p, float b) =>
int result = na
if not na(p) and not na(b) and p > b
int lowN = 1
int highN = 100000
if wilsonLo(p, highN) > b
while lowN < highN
midN = math.floor((lowN + highN) / 2)
if wilsonLo(p, midN) > b
highN := midN
else
lowN := midN + 1
result := lowN
result
// --- the rule, measured at the signal bar's close, no costs
sma = ta.sma(close, smaLen)
crossUp = ta.crossover(close, sma)
crossDn = ta.crossunder(close, sma)
var bool inPos = false
var float entryPx = na
var int trades = 0
var int wins = 0
var float sumWin = 0.0
var float sumLoss = 0.0
if not inPos and crossUp
inPos := true
entryPx := close
else if inPos and crossDn
inPos := false
trades += 1
r = (close - entryPx) / entryPx
if r > 0
wins += 1
sumWin += r
else
sumLoss -= r
// --- win rate and its Wilson 95% interval, in percent
n = float(trades)
ph = trades > 0 ? float(wins) / n : na
z2 = Z * Z
den = 1 + z2 / n
ctr = trades > 0 ? (ph + z2 / (2 * n)) / den : na
hw = trades > 0 ? Z * math.sqrt(ph * (1 - ph) / n + z2 / (4 * n * n)) / den : na
loPct = 100 * (ctr - hw)
hiPct = 100 * (ctr + hw)
wrPct = 100 * ph
// --- breakeven: 1 / (1 + payoff), payoff = average win / average loss so far
losses = trades - wins
payoff = wins > 0 and losses > 0 and sumLoss > 0 ? (sumWin / wins) / (sumLoss / losses) : na
beOwn = na(payoff) ? na : 100 / (1 + payoff)
bePct = useOwn ? beOwn : beFixed
pHi = plot(hiPct, "Wilson 95% upper", color = color.teal)
pLo = plot(loPct, "Wilson 95% lower", color = color.teal)
fill(pHi, pLo, color = color.new(color.teal, 85), title = "95% interval")
plot(wrPct, "Win rate", color = color.blue, linewidth = 2)
plot(bePct, "Breakeven win rate", color = color.red, style = plot.style_linebr)
plotchar(trades, "Closed trades", char = "", location = location.top)
plotchar(wins, "Wins", char = "", location = location.top)
// --- summary on the last bar
var table tb = table.new(position.top_right, 2, 6, border_width = 0, frame_width = 0)
if barstate.islast
need = tradesNeeded(ph, bePct / 100)
bg = color.new(chart.bg_color, 12)
fg = chart.fg_color
table.cell(tb, 0, 0, " ", bgcolor = bg)
table.cell(tb, 1, 0, " ", bgcolor = bg)
table.cell(tb, 0, 1, "Closed trades", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 1, str.tostring(trades), text_color = fg, bgcolor = bg)
table.cell(tb, 0, 2, "Win rate", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 2, na(wrPct) ? "-" : str.tostring(wrPct, "0.0") + "%", text_color = fg, bgcolor = bg)
table.cell(tb, 0, 3, "Wilson 95% interval", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 3, na(loPct) ? "-" : str.tostring(loPct, "0.0") + " to " + str.tostring(hiPct, "0.0") + "%", text_color = fg, bgcolor = bg)
table.cell(tb, 0, 4, "Breakeven win rate", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 4, na(bePct) ? "-" : str.tostring(bePct, "0.0") + "%", text_color = fg, bgcolor = bg)
table.cell(tb, 0, 5, "Trades for the low end to clear breakeven", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 5, na(ph) or na(bePct) ? "-" : wrPct <= bePct ? "never at this win rate" : na(need) ? "over 100,000" : str.tostring(need), text_color = fg, bgcolor = bg)