威廉姆斯分形图与策略结果不一致
Williams Fractals plot not consistent with strategy results
我正在使用我直接从 Tradingview 中的标准指标“按原样”复制的 Williams 分形指标进行非常简单的测试。
我正在尝试在 downFractal 为真时入场,在 upFractal 为真时退出。
策略的结果完全不符合情节的预期,看在我的份上,老实说,我无法理解为什么。也许我没有使用布尔变量吧?
strategy("1d fractals", overlay=true, default_qty_type=strategy.cash, default_qty_value=100)
i_startTime = input.time(defval = timestamp("01 Jan 2021 00:00 +0000"), title = "Start Time")
inDateRange = time >= i_startTime
////////////////////////////////// FRACTALS ///////////////////////////////////////////
n = input.int(title="Periods", defval=2, minval=2)
// UpFractal
bool upflagDownFrontier = true
bool upflagUpFrontier0 = true
bool upflagUpFrontier1 = true
bool upflagUpFrontier2 = true
bool upflagUpFrontier3 = true
bool upflagUpFrontier4 = true
for i = 1 to n
upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n])
upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n])
upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4
upFractal = (upflagDownFrontier and flagUpFrontier)
// downFractal
bool downflagDownFrontier = true
bool downflagUpFrontier0 = true
bool downflagUpFrontier1 = true
bool downflagUpFrontier2 = true
bool downflagUpFrontier3 = true
bool downflagUpFrontier4 = true
for i = 1 to n
downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n])
downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n])
downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4
downFractal = (downflagDownFrontier and flagDownFrontier)
//////////////////////////////////////////////// STRATEGY CONDITIONS ////////////////////////////////////////////////
longEntry = downFractal
exitLong = upFractal
inTradeLong = strategy.position_size > 0
notInTradeLong = strategy.position_size <= 0
if (longEntry and inDateRange)
strategy.entry("Long", strategy.long)
if ((exitLong) and inTradeLong)
strategy.close("Long")
//////////////////////////////////////////////// PLOTTING ////////////////////////////////////////////////
bgcolor(notInTradeLong ? color.new(color.red, 80) : color.new(color.green, 80))
plotshape(downFractal, "downFractal", style=shape.triangledown, location=location.belowbar, offset=-n, color=#F44336, size = size.small)
plotshape(upFractal, "upFractal", style=shape.triangleup, location=location.abovebar, offset=-n, color=#009688, size = size.small)
plotshape(downFractal, "downFractal", style=shape.triangledown, location=location.belowbar, offset=-n, color=#F44336, size = size.small)
plotshape(upFractal, "upFractal", style=shape.triangleup, location=location.abovebar, offset=-n, color=#009688, size = size.small)
那是因为这些分形是用偏移量绘制的。您无法准确输入分形发生的时间,因为它等待 n 根柱线确认,当它绘制时,它返回 n 根柱线并在那里绘制形状。所以,当你想入场的时候,你已经晚了n根柱子。
查看此内容的一种方法是,从那些 plotshape()
函数中删除偏移参数。然后你会看到分形何时被确认,你的买卖订单实际上是在确认之后。
我正在使用我直接从 Tradingview 中的标准指标“按原样”复制的 Williams 分形指标进行非常简单的测试。
我正在尝试在 downFractal 为真时入场,在 upFractal 为真时退出。
策略的结果完全不符合情节的预期,看在我的份上,老实说,我无法理解为什么。也许我没有使用布尔变量吧?
strategy("1d fractals", overlay=true, default_qty_type=strategy.cash, default_qty_value=100)
i_startTime = input.time(defval = timestamp("01 Jan 2021 00:00 +0000"), title = "Start Time")
inDateRange = time >= i_startTime
////////////////////////////////// FRACTALS ///////////////////////////////////////////
n = input.int(title="Periods", defval=2, minval=2)
// UpFractal
bool upflagDownFrontier = true
bool upflagUpFrontier0 = true
bool upflagUpFrontier1 = true
bool upflagUpFrontier2 = true
bool upflagUpFrontier3 = true
bool upflagUpFrontier4 = true
for i = 1 to n
upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n])
upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n])
upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4
upFractal = (upflagDownFrontier and flagUpFrontier)
// downFractal
bool downflagDownFrontier = true
bool downflagUpFrontier0 = true
bool downflagUpFrontier1 = true
bool downflagUpFrontier2 = true
bool downflagUpFrontier3 = true
bool downflagUpFrontier4 = true
for i = 1 to n
downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n])
downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n])
downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4
downFractal = (downflagDownFrontier and flagDownFrontier)
//////////////////////////////////////////////// STRATEGY CONDITIONS ////////////////////////////////////////////////
longEntry = downFractal
exitLong = upFractal
inTradeLong = strategy.position_size > 0
notInTradeLong = strategy.position_size <= 0
if (longEntry and inDateRange)
strategy.entry("Long", strategy.long)
if ((exitLong) and inTradeLong)
strategy.close("Long")
//////////////////////////////////////////////// PLOTTING ////////////////////////////////////////////////
bgcolor(notInTradeLong ? color.new(color.red, 80) : color.new(color.green, 80))
plotshape(downFractal, "downFractal", style=shape.triangledown, location=location.belowbar, offset=-n, color=#F44336, size = size.small)
plotshape(upFractal, "upFractal", style=shape.triangleup, location=location.abovebar, offset=-n, color=#009688, size = size.small)
plotshape(downFractal, "downFractal", style=shape.triangledown, location=location.belowbar, offset=-n, color=#F44336, size = size.small)
plotshape(upFractal, "upFractal", style=shape.triangleup, location=location.abovebar, offset=-n, color=#009688, size = size.small)
那是因为这些分形是用偏移量绘制的。您无法准确输入分形发生的时间,因为它等待 n 根柱线确认,当它绘制时,它返回 n 根柱线并在那里绘制形状。所以,当你想入场的时候,你已经晚了n根柱子。
查看此内容的一种方法是,从那些 plotshape()
函数中删除偏移参数。然后你会看到分形何时被确认,你的买卖订单实际上是在确认之后。