RS
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bharatTrader

//@version=5

// @description Utility methods for Relative Strength analysis
library("RS")

// @function Simple ratio of symbol vs benchmark
// @param symbol to be compared
// @param benchmark to be compared
// @returns ratio of symbol to benchmark
export ratio(float symbol, float benchmark) =>
	symbol / benchmark
	
// @function Simple spread of symbol vs benchmark
// @param symbol to be compared
// @param benchmark to be compared
// @returns spread of symbol to benchmark
export spread(float symbol, float benchmark) =>
	symbol - benchmark
	
// @function Simple add of symbol and benchmark
// @param symbol to be compared
// @param benchmark to be compared
// @returns join of symbol to benchmark
export join(float symbol, float benchmark) =>
	symbol + benchmark

// @function Relative strength comparison between two offsets
// @param symbol to be compared
// @param benchmark to be compared
// @param offset1 starting offset, must be greater than offset 2
// @param offset2 starting offset, must be less than offset 1, default is current candle
// @returns performance in percentage between the 2 offset. A value greater than 0 indicates outperformance, less than 0 indicates underperformance
export atOffset(float symbol, float benchmark, int offset1, int offset2 = 0) =>
	symbolRatio = symbol[offset2]/symbol[offset1]	
	benchmarkRatio = benchmark[offset2]/benchmark[offset1]
	(symbolRatio / benchmarkRatio) - 1

// @function Equilibrium price - The price as per today's symbol and benchmark close, that will make the symbol to perform equal to benchmark
// @param symbol to be compared
// @param benchmark to be compared
// @param offset to measure under/out performance
// @returns Price - This is the price if achieved will make the symbol perform equally w.r.t. benchmark on the offset bar
export equilibriumPrice(float symbol, float benchmark, int offset) =>
    (benchmark * symbol[offset])/benchmark[offset]

// @function Relative Strength performance crossed over/under specified threshold
// @param symbol to be compared
// @param benchmark to be compared
// @param offset to measure under/out performance
// @param threshold threshold set for alert
// @returns 1/0/-1. 1 - crossover, 0 - no signal, -1 crossunder
export thresholdCrossed(float symbol, float benchmark, int offset, float threshold) =>
	r = atOffset(symbol, benchmark, offset)
	xover = ta.crossover(r, threshold)
	xunder = ta.crossunder(r, threshold)
	val = xover ? 1 : xunder ? -1 : 0
	val
	
// @function Return the percentage outperformance of the ratio from offset1 to offset2 along with percentage symbol price change
// @param symbol to be compared
// @param benchmark to be compared
// @param offset1 starting offset, must be greater than offset 2
// @param offset2 starting offset, must be less than offset 1, default is current candle
// @returns deltacrs deltaprice as a 2 element array
export deltaCRSAndPrice(float symbol, float benchmark, int offset1, int offset2) =>
    r1 = ratio(symbol[offset1], benchmark[offset1])
    r2 = ratio(symbol[offset2], benchmark[offset2])
    deltaCRS = (r2 - r1)/r1 * 100
    deltaPrice = (symbol[offset2] - symbol[offset1]) / symbol[offset1] * 100
    [deltaCRS, deltaPrice]

//Usage
benchmark = request.security("NSE:NIFTY", "",close)
symbol = close
hline(0, color=color.fuchsia)
plot(atOffset(symbol, benchmark, 157))

