title: Pine-Script normalize() and rescale()-function with calculation or input errors?
body: <p>(Sorry, I wrote this question as an answer. So, I ask a new question.)</p>
  
  <p>I have two problems with the normalize()-function:</p>
  
  <p>1) Is there a tiny calculation error? Compared to the standard MACD with same parameters there is a small difference of the MACD-Histogram values. Especially with small values. I did a lot adjustments onto the function formula, but I haven't fixed it so far. I lifted the histbase (the zero-line of histogram) to 50 and the normalize spread between min to max is 0 to 100.</p>
  
  <p>2) Pretty rare, I have stocks where the normalized MACD Histogram is showing completely red (throughout negative values). It seems there is a zero-line error in the formular.</p>
  
  <p>Check the pics I've added. They should explane it pretty good.</p>
  
  <p>Here the code:</p>
  
  <pre><code>// MACD ################################################
  
  //Inputs MACD
  fast_length     = input(title="MACD Fast Length", type=input.integer, defval=10)
  slow_length     = input(title="MACD Slow Length", type=input.integer, defval=35)
  src             = input(type=input.source, defval=close)
  signal_length   = input(title="MACD Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 5)
  sma_source      = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
  sma_signal      = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
  
  
  //Calculating MACD, Signal + Histogram
  fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
  slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
  macd    = fast_ma - slow_ma
  signal  = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
  
  
  //Reference Declaration
  hist    = (macd-signal) // true reference historgram
  
  
  //Normalize Function when min/max unknown
  normalize(_src, _min, _max) =&gt; 
      // Normalizes series with unknown min/max using historical min/max.
      // _src: series to rescale.
      // _min: minimum value of rescaled series.
      // _max: maximum value 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)
  
  
  //Histogram Colors
  col_grow_above  = #26A69A
  col_grow_below  = #FFCDD2
  col_fall_above  = #B2DFDB
  col_fall_below  = #EF5350
  col_macd        = #0094ff
  col_signal      = #ff6a00
  ma_color = normalize(hist, 0,100) // Coloring of histo-bars
  
  
  //Plot MACD
  plot(normalize(hist, 0,100), title="Histogram", style=plot.style_columns, color=color.red, histbase=50.0, color=(ma_color&gt;=50? (ma_color[1] &lt; ma_color ? col_grow_above : col_fall_above) : (ma_color[1] &lt; ma_color ? col_grow_below : col_fall_below)),transp=50)
  
  
  //END RSI/MACD/MFI ####################################################
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/U9EGH.jpg" rel="nofollow noreferrer">1) Calculation Errors</a></p>
  
  <p><a href="https://i.stack.imgur.com/L18Yw.jpg" rel="nofollow noreferrer">2) Negative bars only </a></p>