我是Tradingview中的Pine Script的新手。
我试图将Williams Fractal指标绘制为阴影框,从向下的分形到下一个向上的分形,然后再向下到下一个向下的分形,依此类推。有一个指向我要执行的操作的图像的链接。
经过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)
如果您能给我任何指导,我将不胜感激。
谢谢
答案 0 :(得分: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)
[EDIT 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)