本页源码来自 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/
// © Market_Logic_India
// ══════════════════════════════════════════════════════════════════════════════
// ml_paramscan — Stable-plateau parameter selection (Batch-6 foundation library)
// The overfit cure: when you scan a parameter, don't pick the single BEST-scoring value (that
// cell is almost always overfit) — pick the robust PLATEAU that scores well AND consistently.
// Score each variant by mean − λ·std and choose the argmax. Turns any parametric script self-
// tuning and overfit-resistant.
// import Market_Logic_India/ml_paramscan/1 as ps
//
// DESIGN Host runs the variants in parallel (a small state array per candidate parameter) and
// keeps each variant's running performance mean and std; this library scores and selects.
// Pure functions of the arrays you pass; nothing repaints.
//
// CONCEPT CREDIT Stability-over-optimum (plateau) parameter selection is a standard walk-
// forward / robustness practice. Original, dependency-free Pine v6 packaging.
// ══════════════════════════════════════════════════════════════════════════════
//@version=6
library("ml_paramscan", overlay = false)
// Plateau score for ONE variant: mean − λ·std. Rewards consistent performance over a fragile
// single-best peak (high mean but high variance = overfit). Larger λ = more risk-averse.
export plateauScore(series float mean, series float std, simple float lambda) =>
(na(mean) ? na : mean) - lambda * (na(std) ? 0.0 : std)
// Index of the most STABLE variant: argmax over candidates of (mean − λ·std). Host holds
// per-variant running mean and std arrays. Returns the chosen index (-1 if empty).
export stablePlateau(array<float> means, array<float> stds, simple float lambda) =>
int best = -1
float bestScore = -1e308
int n = array.size(means)
if n > 0
for i = 0 to n - 1
float m = nz(array.get(means, i))
float sd = i < array.size(stds) ? nz(array.get(stds, i)) : 0.0
float sc = m - lambda * sd
if sc > bestScore
bestScore := sc
best := i
best
// The plateau score of the chosen variant (companion to stablePlateau, for a dashboard).
export bestPlateauScore(array<float> means, array<float> stds, simple float lambda) =>
float bestScore = na
int n = array.size(means)
if n > 0
bestScore := -1e308
for i = 0 to n - 1
float m = nz(array.get(means, i))
float sd = i < array.size(stds) ? nz(array.get(stds, i)) : 0.0
float sc = m - lambda * sd
if sc > bestScore
bestScore := sc
bestScore
// ── demo output (library preview only) ──
plot(plateauScore(ta.sma(close, 20), ta.stdev(close, 20), 0.5), "plateauScore (demo)", color = color.new(#e8a33d, 0))