简体   繁体   English

Pine-Script normalize() 和 rescale() 函数有计算或输入错误?

[英]Pine-Script normalize() and rescale()-function with calculation or input errors?

(Sorry, I wrote this question as an answer. So, I ask a new question.) (对不起,我把这个问题写成了答案。所以,我问了一个新问题。)

I have two problems with the normalize()-function: normalize() 函数有两个问题:

1) Is there a tiny calculation error? 1)是否有微小的计算错误? Compared to the standard MACD with same parameters there is a small difference of the MACD-Histogram values.与具有相同参数的标准 MACD 相比,MACD 直方图值存在微小差异。 Especially with small values.特别是对于小值。 I did a lot adjustments onto the function formula, but I haven't fixed it so far.我对 function 公式做了很多调整,但到目前为止我还没有修复它。 I lifted the histbase (the zero-line of histogram) to 50 and the normalize spread between min to max is 0 to 100.我将 histbase(直方图的零线)提升到 50,最小值到最大值之间的标准化分布为 0 到 100。

2) Pretty rare, I have stocks where the normalized MACD Histogram is showing completely red (throughout negative values). 2) 非常罕见,我有股票的标准化 MACD 直方图显示完全红色(整个负值)。 It seems there is a zero-line error in the formular.公式中似乎存在零线错误。

Check the pics I've added.检查我添加的图片。 They should explane it pretty good.他们应该很好地解释它。

Here the 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) => 
    // 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>=50? (ma_color[1] < ma_color ? col_grow_above : col_fall_above) : (ma_color[1] < ma_color ? col_grow_below : col_fall_below)),transp=50)


//END RSI/MACD/MFI ####################################################

1) Calculation Errors 1)计算错误

2) Negative bars only 2) 仅负条

Just wanted to add to the answer, to continue from what LucF said.只是想补充一下答案,继续 LucF 所说的内容。 I didn't have the permission to make comments on his answer.我无权对他的回答发表评论。

I used your code and it worked great, I managed to get it to sit on the zero line everytime by doing this: _historicMax:= _historicMin - (_historicMin * 2)我使用了您的代码并且效果很好,我设法让它每次都位于零线上:_historicMax:= _historicMin - (_historicMin * 2)

So what I am doing is completely not bothering to calculate the historicMax and instead use the historicMin value and convert it into a negative.所以我所做的是完全不费心计算 historicMax 而是使用 historicMin 值并将其转换为负值。

Its because I noticed that on the chart the predicted max and min were at random ranges apart.这是因为我注意到图表上预测的最大值和最小值在随机范围内分开。 So if you lock them so they mirror each other, then that means the center will always be zero.所以如果你锁定它们使它们相互镜像,那么这意味着中心将始终为零。

I guess doing this could cut off one of the ranges?我想这样做可能会切断其中一个范围? so maybe first check which is bigger, like if min was -45 and max was +40, then you could lock them both to min's range, eg -45 and +45, which means the middle is 0.所以也许首先检查哪个更大,比如如果最小值是 -45 而最大值是 +40,那么你可以将它们都锁定在最小值的范围内,例如 -45 和 +45,这意味着中间值是 0。

You are normalizing an unbounded signal into new coordinates, so nothing guarantees that a negative value will be above or below the centerline once normalized.您正在将无界信号标准化为新坐标,因此无法保证标准化后负值会高于或低于中心线。 The best way to visualize it is that the normalized centerline is moving, as bars progress.可视化它的最佳方式是标准化的中心线随着柱的前进而移动。 Different original signal values can map to 100 in your normalized value, as new original signal historical highs are discovered with the progressive discovery of history.不同的原始信号值可以在您的标准化值中从 map 到 100,因为新的原始信号历史高点是随着历史的逐步发现而发现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM