绘制具有矩形背景的分形



我是Tradingview中Pine Script的新手。

我正在尝试将威廉姆斯分形指标绘制为一个阴影框,从向下分形到下一个向上分形,然后向下到下一个向下分形,依此类推。有一个链接到我正在尝试做的事情的图像。

示例框

经过 30+ 次尝试,如果可以的话,我会伸出援手以获得一些帮助。

这是分形的代码

//@version=4
study("Williams Fractals", shorttitle="Fractals", format=format.price, precision=0, overlay=true)
// Define "n" as the number of periods and keep a minimum value of 2 for error handling.
n = input(title="Periods", defval=2, minval=2, type=input.integer)
upFractal = (                                                                                                          (high[n+2]  < high[n]) and (high[n+1]  < high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
or (                                                                               (high[n+3]  < high[n]) and (high[n+2]  < high[n]) and (high[n+1] == high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
or (                                                    (high[n+4]  < high[n]) and (high[n+3]  < high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
or (                          (high[n+5] < high[n]) and (high[n+4]  < high[n]) and (high[n+3] == high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
or ((high[n+6] < high[n]) and (high[n+5] < high[n]) and (high[n+4] == high[n]) and (high[n+3] <= high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n]))
dnFractal = (                                                                                                  (low[n+2]  > low[n]) and (low[n+1]  > low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
or (                                                                         (low[n+3]  > low[n]) and (low[n+2]  > low[n]) and (low[n+1] == low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
or (                                                (low[n+4]  > low[n]) and (low[n+3]  > low[n]) and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
or (                        (low[n+5] > low[n]) and (low[n+4]  > low[n]) and (low[n+3] == low[n]) and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
or ((low[n+6] > low[n]) and (low[n+5] > low[n]) and (low[n+4] == low[n]) and (low[n+3] >= low[n]) and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n]))
// Plot the fractals as shapes on the chart.
plotshape(dnFractal, style=shape.triangledown, location=location.belowbar, offset=-2, color=color.maroon, transp=0)
plotshape(upFractal, style=shape.triangleup,   location=location.abovebar, offset=-2, color=color.olive,  transp=0)

如果你能给我任何指示,我将不胜感激。

谢谢

不太确定您对阴影框的想法,但这可能会给您带来想法。将其添加到代码末尾:

var float hiBase = na
var float loBase = na
// Remember hi/lo of new fractal as it is identified, 
// and ignore new opposite fractal when dual fractals are identified on same bar.
if upFractal and not dnFractal
hiBase := high[n]
loBase := na
if dnFractal and not upFractal
hiBase := na
loBase := low[n]
// Use the fact that we are plotting hiBase=na or loBase=na when we don't need one of the fills.
// Make special cases at new fractal to continue plotting previous fractal fill for another bar.
hi1 = plot(na(loBase[1]) and not na(loBase) ? hiBase[1] : hiBase, color = color.white, transp = 100, offset=-n)
hi2 = plot(low[n], color = color.white, transp = 100, offset=-n)
fill( hi1, hi2, color = color.red)
lo1 = plot(na(hiBase[1]) and not na(hiBase) ? loBase[1] : loBase, color = color.white, transp = 100, offset=-n)
lo2 = plot(high[n], color = color.white, transp = 100, offset=-n)
fill( lo1, lo2, color = color.green)

[编辑2019.08.21 21:32 — LucF]

plotchar(dnFractal, "", "⌄", location=location.belowbar, offset=-n, color=color.maroon, transp=0, size=size.small)
plotchar(upFractal, "", "⌃", location=location.abovebar, offset=-n, color=color.olive, transp=0, size=size.small)
var float hiBase = na
var float loBase = na
if upFractal and not dnFractal
hiBase := high[n]
if dnFractal and not upFractal
loBase := low[n]
hi = plot(hiBase, color=color.white, transp=100, offset=-n, style=plot.style_linebr)
lo = plot(loBase, color=color.white, transp=100, offset=-n, style=plot.style_linebr)
fill(hi, lo, color=color.gray)

最新更新