ml_rs Relative Strength vs a benchmark

Market_Logic_India · library · 56 行 · 点赞 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_rs — Relative Strength vs a benchmark  (Batch-5 foundation library)
// The leadership lens the suite lacked: how a name performs RELATIVE to NIFTY (or any
// benchmark). Raw Dorsey RS, zero-centered Mansfield RS, a leadership state, a percentile
// rank for scanning a basket, and an RS slope. One shared definition so "outperforming",
// "leading", and "accelerating vs the index" mean the same thing across every scan.
//   import Market_Logic_India/ml_rs/1 as rsx
//
// DESIGN — host-fed  You fetch the benchmark (e.g. NSE:NIFTY) with request.security /
// input.source and pass its close in, so the library carries no hard-wired tickers and works
// on any instrument/benchmark. Every read is a pure function of the two series you pass;
// nothing repaints. For a cross-session-consistent read, fetch the benchmark on the same
// timeframe you chart (or a higher one, your choice) and pass it straight through.
//
// CONCEPT CREDIT  Relative Strength (Dorsey's RS line) and Mansfield's zero-centered RS
// (Stan Weinstein / Mansfield) are public technical-analysis techniques. Original,
// dependency-free Pine v6 packaging; not affiliated with any originator.
// ══════════════════════════════════════════════════════════════════════════════
//@version=6
library("ml_rs", overlay = false)
 
// Raw (Dorsey) RELATIVE STRENGTH: price ÷ benchmark × 100. Rising = the name is
// outperforming the index; falling = lagging. The base ratio everything else builds on.
export rs(series float price, series float indexPrice) =>
    (not na(indexPrice) and indexPrice != 0.0) ? price / indexPrice * 100.0 : na
 
// MANSFIELD Relative Strength (zero-centered): (RS ÷ SMA(RS, len) − 1) × 100. Above 0 =
// outperforming its OWN recent trend (accelerating leadership); below 0 = lagging. A zero
// cross is a leadership change — the more tradeable form of RS.
export mansfield(series float price, series float indexPrice, simple int len) =>
    float r  = (not na(indexPrice) and indexPrice != 0.0) ? price / indexPrice * 100.0 : na
    float ma = ta.sma(r, len)
    (not na(ma) and ma != 0.0) ? (r / ma - 1.0) * 100.0 : na
 
// LEADERSHIP state from Mansfield RS: +1 leading (accelerating vs the index), −1 lagging, 0 flat.
export rsLeading(series float price, series float indexPrice, simple int len) =>
    float m = mansfield(price, indexPrice, len)
    na(m) ? 0 : m > 0.0 ? 1 : m < 0.0 ? -1 : 0
 
// RS PERCENTILE RANK (0..100) over `len` — where this name's RS sits in its own recent range.
// Rank a basket of constituents against NIFTY: the highest ranks are the leaders to trade with.
export rsRank(series float price, series float indexPrice, simple int len) =>
    float r = (not na(indexPrice) and indexPrice != 0.0) ? price / indexPrice * 100.0 : na
    ta.percentrank(r, len)
 
// RS SLOPE: change in RS over `slopeLen` bars — the momentum of relative strength (leadership
// building vs fading), independent of absolute price direction.
export rsSlope(series float price, series float indexPrice, simple int slopeLen) =>
    float r = (not na(indexPrice) and indexPrice != 0.0) ? price / indexPrice * 100.0 : na
    (not na(r) and not na(r[slopeLen])) ? r - r[slopeLen] : na
 
// ── demo output (library preview only) ──
// Mansfield RS of the charted symbol vs NIFTY (plots na on feeds without NSE:NIFTY).
plot(mansfield(close, request.security("NSE:NIFTY", timeframe.period, close, lookahead = barmerge.lookahead_off), 50), "Mansfield RS vs NIFTY (demo)", color = color.new(#6f9bd8, 0))

← 返回列表