本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © harrell.jordon
//@version=6
strategy("Cost Reality Check: EMA Cross Before and After Fees", shorttitle = "CostCheck", overlay = true,
initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10,
commission_type = strategy.commission.percent, commission_value = 0.1, slippage = 2,
pyramiding = 0, process_orders_on_close = false, calc_on_every_tick = true)
// ── Inputs ──────────────────────────────────────────────────────────────
// The entry rule is deliberately plain. The point of this script is the table:
// it splits every closed trade into what the rule made before fees and what the fees took.
fastLen = input.int(20, "Fast EMA", minval = 1)
slowLen = input.int(50, "Slow EMA", minval = 2)
allowShort = input.bool(false, "Allow shorts")
showTable = input.bool(true, "Show cost table")
// ── Entry rule (chart's own bars only; no request.security, nothing repaints) ──
fastE = ta.ema(close, fastLen)
slowE = ta.ema(close, slowLen)
upX = ta.crossover(fastE, slowE)
dnX = ta.crossunder(fastE, slowE)
if upX
strategy.entry("Long", strategy.long)
if dnX
if allowShort
strategy.entry("Short", strategy.short)
else
strategy.close("Long")
plot(fastE, "Fast EMA", color = color.new(color.teal, 0), linewidth = 2)
plot(slowE, "Slow EMA", color = color.new(color.orange, 0), linewidth = 2)
// ── Split each closed trade into gross (before commission) and commission ──
// Gross = net profit + commission paid. Slippage is already inside the fill prices,
// so "gross" here means "after slippage, before commission".
var int seen = 0
var float grossSum = 0.0 // currency
var float commSum = 0.0 // currency
var float grossBpsSum = 0.0 // per-trade gross, in bps of entry notional
var float commBpsSum = 0.0 // per-trade commission, in bps of entry notional
var int grossWins = 0
var int netWins = 0
nClosed = strategy.closedtrades
if nClosed > seen
for i = seen to nClosed - 1
p = strategy.closedtrades.profit(i)
c = strategy.closedtrades.commission(i)
ntl = strategy.closedtrades.entry_price(i) * math.abs(strategy.closedtrades.size(i))
g = p + c
grossSum += g
commSum += c
grossBpsSum += ntl > 0 ? g / ntl * 10000.0 : 0.0
commBpsSum += ntl > 0 ? c / ntl * 10000.0 : 0.0
grossWins += g > 0 ? 1 : 0
netWins += p > 0 ? 1 : 0
seen := nClosed
n = math.max(seen, 1)
avgGross = grossBpsSum / n // average gross result per trade, bps
avgComm = commBpsSum / n // average commission per round trip, bps
breakEven = avgGross / 2.0 // commission per side that would zero the average trade
netSum = grossSum - commSum
cap = strategy.initial_capital
// Data Window copies of every headline number (readable without the table).
plot(seen, "Closed trades", display = display.data_window, format = format.price, precision = 0)
plot(grossSum / cap * 100.0, "Before commission, % of capital", display = display.data_window, format = format.price, precision = 2)
plot(commSum / cap * 100.0, "Commission paid, % of capital", display = display.data_window, format = format.price, precision = 2)
plot(netSum / cap * 100.0, "After commission, % of capital", display = display.data_window, format = format.price, precision = 2)
plot(avgGross, "Avg trade before commission, bps", display = display.data_window, format = format.price, precision = 2)
plot(avgComm, "Avg commission per round trip, bps", display = display.data_window, format = format.price, precision = 2)
plot(breakEven, "Break-even commission per side, bps", display = display.data_window, format = format.price, precision = 2)
// ── Table ───────────────────────────────────────────────────────────────
var table t = table.new(position.top_right, 2, 10, border_width = 0, frame_width = 0)
// A dark panel with light text reads the same on light and dark chart themes.
PANEL = color.new(#131722, 10)
TXT = #D1D4DC
GOOD = #26A69A
BAD = #FF6B6B
WARN = #FFB74D
cell(int r, string a, string b, color tc) =>
table.cell(t, 0, r, a, text_color = TXT, bgcolor = PANEL, text_halign = text.align_left, text_size = size.small)
table.cell(t, 1, r, b, text_color = tc, bgcolor = PANEL, text_halign = text.align_right, text_size = size.small)
// calc_on_every_tick = true only changes how often the realtime bar is recalculated; fills on
// history are unchanged. It lets the table draw on the chart's last bar.
if barstate.islast
table.clear(t, 0, 0, 1, 9)
if showTable
// Row 0 is a spacer: TradingView's symbol legend covers a table's top row.
table.cell(t, 0, 0, " ")
table.cell(t, 1, 0, " ")
fg = TXT
verdict = switch
seen < 30 => "Too few trades to judge (" + str.tostring(seen) + ")"
avgGross <= 0 => "No edge even before fees"
avgGross <= avgComm => "Fees take the whole edge"
avgGross <= 2 * avgComm => "Fees take over half the edge"
=> "Edge survives these fees"
vcol = switch
seen < 30 => fg
avgGross <= avgComm => BAD
avgGross <= 2 * avgComm => WARN
=> GOOD
cell(1, "Closed trades", str.tostring(seen), fg)
cell(2, "Before commission", str.tostring(grossSum / cap * 100.0, "0.00") + " %", grossSum >= 0 ? GOOD : BAD)
cell(3, "Commission paid", "-" + str.tostring(commSum / cap * 100.0, "0.00") + " %", BAD)
cell(4, "After commission", str.tostring(netSum / cap * 100.0, "0.00") + " %", netSum >= 0 ? GOOD : BAD)
cell(5, "Avg trade before commission", str.tostring(avgGross, "0.0") + " bps", fg)
cell(6, "Avg commission per round trip", str.tostring(avgComm, "0.0") + " bps", fg)
cell(7, "Break-even commission per side", avgGross > 0 ? str.tostring(breakEven, "0.0") + " bps" : "none (negative before fees)", fg)
cell(8, "Winners before / after commission", str.tostring(grossWins) + " / " + str.tostring(netWins), fg)
cell(9, "Verdict", verdict, vcol)