Mansfield Relative Strength (Original Version) by stageanalysis
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © stageanalysis

//@version=5
indicator('Mansfield Relative Strength (Original Version) by stageanalysis', shorttitle='Mansfield RS', overlay=false)

// Global Variables
resolution = '1W'
sym = input.symbol(title='Symbol', defval='SPCFD:SPX')
maLength = input.int(52, minval = 1, title='MA Length')
mansfieldRSInput = input(defval=true, title="Original Style")

// Retrieve the other symbol's data
spx = request.security(sym, resolution, close)
stock = close

stockDividedBySpx = stock / spx * 100
zeroLineMA = ta.sma(stockDividedBySpx, maLength)
mansfieldRS = ((( stockDividedBySpx / zeroLineMA ) - 1 ) * 100)

v = if mansfieldRSInput  
    mansfieldRS
else
    stockDividedBySpx

x = if not mansfieldRSInput  
    zeroLineMA

plot(v, title='Mansfield RS Line', color=color.new(color.black, 0))
hline(0, title="Zero Line", color=color.blue, linestyle=hline.style_solid, linewidth=1)
plot(x, title='MA', color=color.blue)

// Create inputs to configure the background colour signals
positiveColor = color.new(#53b987, 85)
neutralColorRisingMA = color.new(color.blue, 85)
neutralColorFallingMA = color.new(color.gray, 95)
negativeColor = color.new(color.red, 85)

zeroLineMaisRising = ta.rising(zeroLineMA, 1) 
zeroLineMaisFalling = ta.falling(zeroLineMA, 1) 

// Compare price and averages to determine background colour
backgroundColour = if stockDividedBySpx > zeroLineMA and zeroLineMaisRising
    positiveColor
else if stockDividedBySpx < zeroLineMA and zeroLineMaisRising
    neutralColorRisingMA
else if stockDividedBySpx > zeroLineMA and zeroLineMaisFalling 
    neutralColorFallingMA
else
    negativeColor

// Apply chosen background to the chart
bgcolor(backgroundColour, title="Background Colors")