本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Break-Even Trading Cost: The Cost That Zeroes It", shorttitle = "CostToZero", 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, measured at
// the signal bar's close. Each closed trade's gross return is recorded in basis points
// (1 bp = 0.01%). The four lines are the running total of those returns, in percent,
// after subtracting a round-trip cost from every trade. Returns are added, not
// compounded, so a round-trip cost equal to the average gross trade ends the total at
// exactly zero. That average is in the table: it is the cost that zeroes the rule.
// That is check 3: find the cost that zeroes it.
smaLen = input.int(50, "SMA length", minval = 2)
costA = input.float(0.0, "Cost A, round trip (bps)", minval = 0.0)
costB = input.float(10.0, "Cost B, round trip (bps)", minval = 0.0)
costC = input.float(20.0, "Cost C, round trip (bps)", minval = 0.0)
costD = input.float(40.0, "Cost D, round trip (bps)", minval = 0.0)
Z = 1.959964 // two-sided 95% normal quantile
// --- the rule, measured at the signal bar's close
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 float sumBps = 0.0
var float sumSq = 0.0
if not inPos and crossUp
inPos := true
entryPx := close
else if inPos and crossDn
inPos := false
trades += 1
g = 10000 * (close - entryPx) / entryPx
sumBps += g
sumSq += g * g
// --- running totals in percent: gross trade returns added, minus one round-trip cost per trade
n = float(trades)
netA = trades > 0 ? (sumBps - n * costA) / 100 : na
netB = trades > 0 ? (sumBps - n * costB) / 100 : na
netC = trades > 0 ? (sumBps - n * costC) / 100 : na
netD = trades > 0 ? (sumBps - n * costD) / 100 : na
// --- the average gross trade is the round-trip cost that zeroes the total
avgBps = trades > 0 ? sumBps / n : na
varBps = trades > 1 ? math.max(sumSq - n * avgBps * avgBps, 0.0) / (n - 1) : na
seBps = trades > 1 ? math.sqrt(varBps / n) : na
loBps = avgBps - Z * seBps
hiBps = avgBps + Z * seBps
hline(0, "Zero", color = color.gray, linestyle = hline.style_dotted)
plot(netA, "Net total at cost A (%)", color = color.blue, linewidth = 2, style = plot.style_stepline)
plot(netB, "Net total at cost B (%)", color = color.teal, style = plot.style_stepline)
plot(netC, "Net total at cost C (%)", color = color.orange, style = plot.style_stepline)
plot(netD, "Net total at cost D (%)", color = color.red, style = plot.style_stepline)
plot(trades, "Closed trades", display = display.data_window)
plot(avgBps, "Average gross trade (bps)", display = display.data_window)
plot(loBps, "Rough 95% low (bps)", display = display.data_window)
plot(hiBps, "Rough 95% high (bps)", display = display.data_window)
// --- summary on the last bar
fmt(float x) =>
na(x) ? "-" : str.tostring(x, "0.0")
var table tb = table.new(position.top_right, 2, 8, border_width = 1)
if barstate.islast
bg = color.new(chart.bg_color, 12)
fg = chart.fg_color
zeroText = na(avgBps) ? "-" : avgBps <= 0 ? "none, gross is at or below zero already" : fmt(avgBps) + " bps (" + fmt(avgBps / 2) + " a side)"
table.cell(tb, 0, 0, "Closed trades", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 0, str.tostring(trades), text_color = fg, bgcolor = bg)
table.cell(tb, 0, 1, "Average gross trade", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 1, na(avgBps) ? "-" : fmt(avgBps) + " bps", text_color = fg, bgcolor = bg)
table.cell(tb, 0, 2, "Rough 95% range of that average", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 2, na(loBps) ? "-" : fmt(loBps) + " to " + fmt(hiBps) + " bps", text_color = fg, bgcolor = bg)
table.cell(tb, 0, 3, "Round-trip cost that zeroes the total", text_color = fg, bgcolor = bg)
table.cell(tb, 1, 3, zeroText, text_color = fg, bgcolor = bg)
table.cell(tb, 0, 4, "Net total at " + fmt(costA) + " bps", text_color = color.blue, bgcolor = bg)
table.cell(tb, 1, 4, na(netA) ? "-" : fmt(netA) + "%", text_color = color.blue, bgcolor = bg)
table.cell(tb, 0, 5, "Net total at " + fmt(costB) + " bps", text_color = color.teal, bgcolor = bg)
table.cell(tb, 1, 5, na(netB) ? "-" : fmt(netB) + "%", text_color = color.teal, bgcolor = bg)
table.cell(tb, 0, 6, "Net total at " + fmt(costC) + " bps", text_color = color.orange, bgcolor = bg)
table.cell(tb, 1, 6, na(netC) ? "-" : fmt(netC) + "%", text_color = color.orange, bgcolor = bg)
table.cell(tb, 0, 7, "Net total at " + fmt(costD) + " bps", text_color = color.red, bgcolor = bg)
table.cell(tb, 1, 7, na(netD) ? "-" : fmt(netD) + "%", text_color = color.red, bgcolor = bg)