ml_portfolio Signal diversification & redundancy audit

Market_Logic_India · library · 59 行 · 点赞 1 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// 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_portfolio — Signal diversification & redundancy audit  (Batch-5 foundation library)
// Turns "I have 180 indicators" into "which of them actually add anything." Convert each
// signal to its own return STREAM, correlate the streams, and grade how redundant vs
// complementary a set is — so you weight the diversifiers (feeds ml_weights / U3) and prune
// the duplicates.
//   import Market_Logic_India/ml_portfolio/1 as pf
//
// DESIGN — host-fed  Pairwise correlation needs two full series with history, so the host
// computes each pair's rollingCorr and collects them into an array; this library standardizes
// the return-stream construction, the pair test, and the set-level diversification grade.
// Pure functions of what you pass; nothing repaints.
//
// CONCEPT CREDIT  Return-stream correlation and diversification grading are standard
// portfolio-construction ideas. Original, dependency-free Pine v6 packaging.
// ══════════════════════════════════════════════════════════════════════════════
//@version=6
library("ml_portfolio", overlay = false)
 
// Per-bar strategy RETURN of a signal: +ret when long, −ret when short, else 0. This is the
// return STREAM you correlate across indicators to find overlap. Pass `ret` = the bar return
// (e.g. close/close[1] − 1), and the signal's own long/short booleans.
export signalReturn(series bool longSig, series bool shortSig, series float ret) =>
    longSig ? ret : shortSig ? -ret : 0.0
 
// Rolling correlation of two return streams over `len` (standardized wrapper over ta.correlation).
export rollingCorr(series float a, series float b, simple int len) =>
    ta.correlation(a, b, len)
 
// Are two signals REDUNDANT? |correlation| ≥ thr over `len` = they largely repeat each other,
// so keeping both adds cost without diversification.
export redundantPair(series float a, series float b, simple int len, simple float thr) =>
    float c = ta.correlation(a, b, len)
    not na(c) and math.abs(c) >= thr
 
// Average |correlation| across a set of pairwise correlations (host pushes each rollingCorr
// into the array). Low = the set is diversified; high = the set is redundant.
export avgAbsCorr(array<float> corrs) =>
    int n = array.size(corrs)
    float s = 0.0
    int cnt = 0
    if n > 0
        for i = 0 to n - 1
            float c = array.get(corrs, i)
            if not na(c)
                s   += math.abs(c)
                cnt += 1
    cnt > 0 ? s / cnt : na
 
// DIVERSIFICATION score (0..100): 100·(1 − avg|corr|). Higher = the set behaves more
// independently (more genuine breadth); lower = the indicators mostly echo each other.
export diversificationScore(array<float> corrs) =>
    float a = avgAbsCorr(corrs)
    na(a) ? na : math.max(0.0, math.min(100.0, (1.0 - a) * 100.0))
 
// ── demo output (library preview only) ──
plot(rollingCorr(close - close[1], close[1] - close[2], 50), "corr(ret, lagged ret) (demo)", color = color.new(#e8a33d, 0))

← 返回列表