本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Best of N: Selection Premium on One Rule", shorttitle = "BestOfN", overlay = false)
// What this shows
// ONE rule - long while close is above a simple moving average - run at N different
// lengths, nothing else changed. The blue line is the BEST of the N. The grey line is
// the MEDIAN of the N. The gap between them is the selection premium: the part of the
// best curve you get for having looked at N of them, not for having an edge.
// Change nothing but the length and the "best" still beats the median. That is check 9.
minLen = input.int(10, "Shortest length", minval = 2)
stepLen = input.int(2, "Step between lengths", minval = 1)
nVar = input.int(20, "How many variants (N)", minval = 2, maxval = 60)
maxLen = minLen + stepLen * (nVar - 1)
var closeBuf = array.new<float>() // index 0 = this bar, capped at maxLen
var equity = array.new<float>(nVar, 0.0) // cumulative simple return per variant
var position = array.new<int>(nVar, 0) // 1 = long into the next bar, 0 = flat
// --- 1. keep the last maxLen closes, newest first
array.unshift(closeBuf, close)
if array.size(closeBuf) > maxLen
array.pop(closeBuf)
// --- 2. settle yesterday's positions against today's move, before deciding anything new
if bar_index > 0 and close[1] != 0
barRet = (close - close[1]) / close[1]
for k = 0 to nVar - 1
array.set(equity, k, array.get(equity, k) + array.get(position, k) * barRet)
// --- 3. prefix sums of the buffer, so every length reads one subtraction instead of a loop
bufN = array.size(closeBuf)
pre = array.new<float>(bufN + 1, 0.0)
for j = 0 to bufN - 1
array.set(pre, j + 1, array.get(pre, j) + array.get(closeBuf, j))
// --- 4. each variant's own moving average and its own decision for the next bar
for k = 0 to nVar - 1
L = minLen + stepLen * k
if bufN >= L
avg = array.get(pre, L) / L
array.set(position, k, close > avg ? 1 : 0)
else
array.set(position, k, 0)
// --- 5. best and median of the N, recomputed every bar
sorted = array.copy(equity)
array.sort(sorted, order.ascending)
mid = math.floor(nVar / 2)
medEq = nVar % 2 == 0 ? (array.get(sorted, mid - 1) + array.get(sorted, mid)) / 2 : array.get(sorted, mid)
bestEq = array.get(sorted, nVar - 1)
bestLen = minLen
for k = 0 to nVar - 1
if array.get(equity, k) == bestEq
bestLen := minLen + stepLen * k
plot(bestEq, title = "Best of N", color = color.blue, linewidth = 2)
plot(medEq, title = "Median of N", color = color.gray, linewidth = 2)
plot(bestEq - medEq, title = "Selection premium", color = color.red, linewidth = 1)
hline(0, "Flat", color = color.new(color.gray, 60))
// Which length is winning right now, and how often that answer changes, is the other
// half of the lesson: the "best length" is a property of the sample, not of the market.
plotchar(bestLen, title = "Winning length", char = "", location = location.top)
plotchar(nVar, title = "Variants tried (N)", char = "", location = location.top)