Indicator · Pine Script v6 · Own pane

Mansfield Relative Strength (Weinstein Method)

by major· code credited to //

This indicator calculates Stan Weinstein's Mansfield Relative Strength (MRS) comparing a security against a chosen benchmark index, such as the S&P 500.

View on GitHub

GitHub stars2on the repository
Pine versionv6current
Lines of code174
Settings8inputs
LicenceApache-2.0
Last update3 months ago29 Jul 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

174 lines of Pine Script v6 from mansfield_relative_strength/mansfield_relative_strength.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

mansfield_relative_strength.pine
// Copyright 2025
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Original version © stageanalysis
// Corrected and improved version
//
// This indicator implements Stan Weinstein's Mansfield Relative Strength
// methodology as described in:
//
//   Weinstein, S. (1988). Secrets for Profiting in Bull and Bear Markets.
//   New York: McGraw-Hill.
//
// The Mansfield RS is a key component of Weinstein's Stage Analysis, which
// identifies four distinct market stages to optimize entry and exit timing.

//@version=6
indicator('Mansfield Relative Strength (Weinstein Method)', shorttitle='Mansfield RS', overlay=false)

// ═══════════════════════════════════════════════════════════════════════════
// INPUTS
// ═══════════════════════════════════════════════════════════════════════════
// Stan Weinstein's Mansfield RS Formula:
// Step 1: RP = (Stock Close / Index Close) × 100
// Step 2: MRS = ((RP / SMA(RP, n)) - 1) × 100
//
// Where n = 52 for weekly charts (Weinstein's primary timeframe)
//       n = 200 for daily charts

sym = input.symbol(title='Benchmark Symbol', defval='SPCFD:SPX', tooltip='Index to compare against (default: S&P 500)')
resolution = input.timeframe('W', title='Timeframe', tooltip='Weinstein used Weekly charts with 52-period MA')
maLength = input.int(52, minval=1, title='MA Length', tooltip='52 for weekly, 200 for daily charts')
stageSmoothingPeriod = input.int(3, minval=1, maxval=10, title='Stage Smoothing', tooltip='Bars required to confirm trend change (higher = less flipping)', group='Stage Detection')

// Display Options
showColoredLine = input.bool(true, title='Color-Coded MRS Line', tooltip='Change line color based on Weinstein stage', group='Display')
showMarkers = input.bool(false, title='Stage Transition Markers', tooltip='Show labels when entering Stage 2/4', group='Display')
showTable = input.bool(true, title='Show Info Table', tooltip='Display current stage information', group='Display')
tablePosition = input.string('top_center', title='Table Position', options=['top_left', 'top_center', 'top_right', 'middle_left', 'middle_center', 'middle_right', 'bottom_left', 'bottom_center', 'bottom_right'], tooltip='Position of the info table', group='Display')

// ═══════════════════════════════════════════════════════════════════════════
// CALCULATIONS
// ═══════════════════════════════════════════════════════════════════════════

// Retrieve data for BOTH symbols at the same timeframe with repainting protection
// This ensures accurate comparison - both stock and benchmark use the same resolution
benchmarkClose = request.security(sym, resolution, close, lookahead=barmerge.lookahead_off)
stockClose = request.security(syminfo.tickerid, resolution, close, lookahead=barmerge.lookahead_off)

// Step 1: Calculate Relative Performance (RP)
relativePerformance = stockClose / benchmarkClose * 100

// Step 2: Calculate 52-week (or n-period) moving average of RP
rpMovingAverage = ta.sma(relativePerformance, maLength)

// Step 3: Calculate Mansfield Relative Strength
// MRS = ((RP / SMA(RP, n)) - 1) × 100
mansfieldRS = ((relativePerformance / rpMovingAverage) - 1) * 100

// ═══════════════════════════════════════════════════════════════════════════
// STAGE DETECTION
// ═══════════════════════════════════════════════════════════════════════════

// Determine if MRS is rising or falling (with smoothing to reduce flipping)
// Using stageSmoothingPeriod bars instead of just 1 for more stable stage detection
mrsRising = ta.rising(mansfieldRS, stageSmoothingPeriod)
mrsFalling = ta.falling(mansfieldRS, stageSmoothingPeriod)

// Determine current Weinstein stage
// Stage 1: MRS < 0, rising (Basing/Accumulation)
// Stage 2: MRS > 0, rising (Advancing/Markup) ✓ BUY
// Stage 3: MRS > 0, falling (Topping/Distribution)
// Stage 4: MRS < 0, falling (Declining/Markdown) ✗ AVOID

var int currentStage = na
if mansfieldRS > 0 and mrsRising
    currentStage := 2
else if mansfieldRS < 0 and mrsRising
    currentStage := 1
else if mansfieldRS > 0 and mrsFalling
    currentStage := 3
else if mansfieldRS < 0 and mrsFalling
    currentStage := 4
else
    // Flat/unchanged - maintain previous stage
    currentStage := currentStage[1]

// Detect stage transitions (for markers)
stage2Entry = currentStage == 2 and currentStage[1] != 2
stage4Entry = currentStage == 4 and currentStage[1] != 4

// Stage colors
stageColor = switch currentStage
    1 => color.new(color.blue, 0)      // Stage 1: Light Blue
    2 => color.new(color.green, 0)     // Stage 2: Green (BUY)
    3 => color.new(color.orange, 0)    // Stage 3: Orange
    4 => color.new(color.red, 0)       // Stage 4: Red (AVOID)
    => color.black

// Stage names for table
stageName = switch currentStage
    1 => "Stage 1: Basing"
    2 => "Stage 2: Advancing ✓"
    3 => "Stage 3: Topping"
    4 => "Stage 4: Declining ✗"
    => "Unknown"

// Trend direction for table
trendText = mrsRising ? "Rising ↗" : mrsFalling ? "Falling ↘" : "Flat →"

// ═══════════════════════════════════════════════════════════════════════════
// PLOTS
// ═══════════════════════════════════════════════════════════════════════════

// Plot the Mansfield RS line (color-coded if enabled)
// Simplified: Green above zero (outperforming), Red below zero (underperforming)
simpleColor = mansfieldRS >= 0 ? color.new(color.green, 0) : color.new(color.red, 0)
plotColor = showColoredLine ? simpleColor : color.black
plot(mansfieldRS, title='Mansfield RS', color=plotColor, linewidth=2)

// Plot zero line (key reference for Weinstein Stage Analysis)
// MRS > 0: Outperforming vs 52-week average (Stage 2/3)
// MRS < 0: Underperforming vs 52-week average (Stage 1/4)
hline(0, title='Zero Line', color=color.white, linestyle=hline.style_dotted, linewidth=1)

// Plot stage transition markers
if showMarkers and stage2Entry
    label.new(bar_index, mansfieldRS, text='Stage 2 ✓', style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.small)

if showMarkers and stage4Entry
    label.new(bar_index, mansfieldRS, text='Stage 4 ✗', style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.small)

// ═══════════════════════════════════════════════════════════════════════════
// INFO TABLE
// ═══════════════════════════════════════════════════════════════════════════

// Convert table position string to position constant
tPos = switch tablePosition
    'top_left' => position.top_left
    'top_center' => position.top_center
    'top_right' => position.top_right
    'middle_left' => position.middle_left
    'middle_center' => position.middle_center
    'middle_right' => position.middle_right
    'bottom_left' => position.bottom_left
    'bottom_center' => position.bottom_center
    'bottom_right' => position.bottom_right
    => position.top_center

// Create and populate table - compact single-line format
if showTable and barstate.islast
    var table infoTable = table.new(tPos, 3, 1, border_width=1, border_color=color.new(color.gray, 50), frame_color=color.new(color.gray, 50), frame_width=1)

    // Compact single row: Stage name (colored) | Trend
    table.cell(infoTable, 0, 0, text=stageName, text_color=color.white, bgcolor=stageColor, text_size=size.small)
    table.cell(infoTable, 2, 0, text=trendText, text_color=color.gray, bgcolor=color.new(color.white, 0), text_size=size.small)

// ═══════════════════════════════════════════════════════════════════════════
// WEINSTEIN STAGE ANALYSIS REFERENCE
// ═══════════════════════════════════════════════════════════════════════════
// Stage 1 (Basing): MRS < 0, starting to stabilize
// Stage 2 (Advancing): MRS > 0 and rising ✓ Best time to buy
// Stage 3 (Topping): MRS > 0 but starting to decline
// Stage 4 (Declining): MRS < 0 and falling ✗ Avoid or short

Overview

Mansfield Relative Strength (Weinstein Method) measures outperformance and underperformance relative to a zero line using a moving average baseline. The script identifies market stages, offering customizable trend smoothing, color-coded lines, optional stage transition markers, and an information table. It plots in a separate pane below the price chart.

Details

Published by

major on GitHub

Code credit

//, as written in the script's header

Draws

Separate pane below the chart

Markets

Any symbol

Timeframes

H4 and above

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Benchmark SymbolsymbolSPCFD:SPXsym
TimeframetimeframeWresolution
MA Lengthint52maLength
Stage Smoothingint3stageSmoothingPeriod
Color-Coded MRS LinebooltrueshowColoredLine
Stage Transition MarkersboolfalseshowMarkers
Show Info TablebooltrueshowTable
Table Positionstringtop_centertablePosition
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

228 stars

Multi-Timeframe Analysis Suite calculates trend direction and momentum conditions across five user-selected timeframes using moving averages, RSI, and Stochastic oscillators. It evaluates moving average slope and oscillator levels to compute trend alignment, m

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Suite

KamaFama

by Mastaaa1987
141 stars

KamaFama is an indicator that translates a Freqtrade trading strategy into Pine Script for visualization. It combines Kaufman Adaptive Moving Average (KAMA), MAMA/FAMA calculations derived via Hilbert Transform, Williams %R, and correlation trend filters to ev

Indicator v6 Moving averagesOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of KamaFama

The Adaptive Cycle Detector estimates the dominant market cycle from WaveTrend oscillator zero-crossing frequency and applies smoothing to recommend adaptive indicator lengths. Fast markets with frequent crossings receive shorter lengths while slow swing marke

Indicator v6 Oscillators & momentumVolatility & bands
Updated 3 days ago · MIT GitHub repository of Adaptive Cycle Detector [WavesUnchained]

Manias, Panics and Crashes

by MeridianAlgo
9 stars

Manias, Panics and Crashes is a separate-pane market regime indicator that quantifies market sentiment and systemic risk. It processes technical metrics including RSI, moving averages, and volume alongside optional intermarket data such as VIX volatility and c

Indicator v6 Oscillators & momentumVolatility & bands
Updated 4 months ago · MIT GitHub repository of Manias, Panics and Crashes

Nexural Pressure Pro

by JasonTeixeira
9 stars

Nexural Pressure Pro is a composite pressure indicator that measures directional market momentum using volume-weighted directional movement, order flow delta, aggression, and efficiency ratio metrics. It filters out low-activity bars using configurable volume

Indicator v6 Oscillators & momentumVolume
Updated 2 weeks ago · Custom licence GitHub repository of Nexural Pressure Pro

Elton's Screener

by eltonaguiar
4 stars

Elton's Screener is an indicator that runs in a separate pane and scores one of twenty selectable strategies, or auto-selects a route from the symbol type. It filters signals by a minimum confidence level and a higher-timeframe EMA trend alignment. ATR multipl

Indicator v6 Screeners & dashboardsOscillators & momentum Repaints
Updated 2 days ago · None stated GitHub repository of Elton's Screener

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers