'how to normalize values in pinescript
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!
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Super |
