(对不起,我写这个问题是为了回答。因此,我提出了一个新问题。)
我对normalize()-function有两个问题:
1)是否有微小的计算误差?与相同参数的标准MACD相比,MACD直方图值略有不同。特别是对于小的值。我对函数公式做了很多调整,但到目前为止我还没有修复它。我将直方图的基数(直方图的零线)提升到50,min到max之间的归一化分布是0到100。
2)非常罕见的是,我的股票的归一化MACD直方图显示为完全红色(整个负值)。似乎在公式中有一个零线错误。
检查我添加的图片。他们应该会做得很好。
代码如下:
// 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 ####################################################发布于 2020-11-10 23:57:43
只是想补充一下答案,从LucF说的继续。我没有被允许对他的回答发表评论。
我使用了你的代码,它工作得很好,我设法让它每次都处于零线上:_historicMax := _historicMin - (_historicMin * 2)
因此,我所做的完全不需要计算historicMax,而是使用historicMin值并将其转换为负值。
这是因为我注意到在图表上,预测的最大值和最小值在随机范围内。因此,如果你锁定它们,使它们相互镜像,那么这意味着中心将始终为零。
我猜这么做可能会切断其中一个范围?所以可能首先检查哪个更大,比如如果min是-45,max是+40,那么你可以将它们都锁定在min的范围内,例如-45和+45,这意味着中间是0。
发布于 2020-06-12 22:22:59
您正在将一个无界信号归一化为新的坐标,因此没有任何东西可以保证一旦归一化,负值将位于中心线的上方或下方。可视化它的最好方法是,随着条的进展,规格化的中心线正在移动。不同的原始信号值可以映射到归一化值中的100,因为随着历史的逐步发现,新的原始信号历史高点被发现。
https://stackoverflow.com/questions/62306070
复制相似问题