3

I'm trying to merge a couple of indicators together, the problem is some of these have different scales, so while an oscillator might range from 0 to 100 another might have a variable range which can sometimes be very small say from -5 to +5. of course plotting these together will make one almost impossible to see.

how can I scale one up so its stays consistent across different stocks/datasets? I've tried to multiply the value of the indicator that gets plotted by a constant which works to an extent however depending on the data in some cases this can become too big and out of range. Ideally I want to keep the plot uniform between a certain range of values. any input would be much appreciated!

Jimmy
  • 33
  • 1
  • 7

2 Answers2

4

You can use the normalize function

normalize(_src, _min, _max) =>
// Normalizes series with unknown min/max using historical min/max.
// _src      : series to rescale.
// _min, _min: min/max values of rescaled series.
var _historicMin =  10e10
var _historicMax = -10e10
_historicMin := min(nz(_src, _historicMin), _historicMin)
_historicMax := max(nz(_src, _historicMax), _historicMax)
_min + (_max - _min) * (_src - _historicMin) / max(_historicMax - _historicMin, 10e-10)

// ————— Normalized volume in the same region as the rescaled RSI.
plot(normalize(volume, -100, 100), "Normalized volume", color.black)
hline( 100)
hline(-100)
Super
  • 43
  • 3
1

Technically normalize outputs 0 to 1 values. To scale it up you will need to adjust. Using series you'll need its range over a period of time. To facilitate I use a 500 bars period. It works even if range goes from negative to positive numbers.

I use to relate result to one hundred so it will look as percentage like RSI and many other indicators do but you can multiply for other values as it fits your scale.

// For a series x
xMax = ta.highest(x, 500)
xMin = ta.lowest(x, 500)
range = xMax - xMin
y = 100 * x / range
plot (y)
Braconnot_P
  • 181
  • 3
  • 8