首先,我不建议使用旧的源代码进行学习。您可以从任何版本开始,但我建议您不要使用任何版本,除非您在源代码顶部附近看到 //@version=4。我刚开始的时候就犯了这个错误,改编了一个旧脚本并在此过程中学习了一个过时的版本。
所以我在这里修改了 V4 的脚本,注意更改。一些语法,并用 var 关键字声明变量来初始化某些变量。在 V4 中,您不能在声明中使用变量时声明变量。此外,我将iff 替换为三元组,您会看到它的使用频率更高。
//@version=4
study(title="Fisher Transform Indicator by Ehlers - modified for V4", shorttitle="Fisher Transform Indicator by Ehlers")
length = input(title='Length', type=input.integer, defval=10, minval=1)
MaxHL2 = highest(hl2, length)
MinHL2 = lowest(hl2, length)
var float nValue1 = na
var float nValue2 = na
var float nFish = na
nValue1 := 0.33 * 2 * ((hl2 - MinHL2) / (MaxHL2 - MinHL2) - 0.5) + 0.67 * nz(nValue1[1])
nValue2 := nValue1 > 0.99 ? 0.999 : nValue1 < -0.99 ? -0.999 : nValue1
nFish := 0.5 * log((1 + nValue2) / (1 - nValue2)) + 0.5 * nz(nFish[1])
plot(nFish, color=color.green, title="Fisher")
plot(nFish[1], color=color.red, title="Trigger")
接下来,您所描述的逻辑称为crossover。这意味着第一个参数超过了第二个。熟悉cross 和crossunder。因此,为了简单起见,我们可以将其调整为策略:
//@version=4
strategy(title="Fisher Transform Indicator by Ehlers - modified for V4", shorttitle="Fisher Transform Indicator by Ehlers")
length = input(title='Length', type=input.integer, defval=10, minval=1)
MaxHL2 = highest(hl2, length)
MinHL2 = lowest(hl2, length)
var float nValue1 = na
var float nValue2 = na
var float nFish = na
nValue1 := 0.33 * 2 * ((hl2 - MinHL2) / (MaxHL2 - MinHL2) - 0.5) + 0.67 * nz(nValue1[1])
nValue2 := nValue1 > 0.99 ? 0.999 : nValue1 < -0.99 ? -0.999 : nValue1
nFish := 0.5 * log((1 + nValue2) / (1 - nValue2)) + 0.5 * nz(nFish[1])
longCross = crossover(nFish,nFish[1])
shortCross = crossunder(nFish, nFish[1])
plotshapePosition = longCross ? nFish : na
strategy.entry('long', strategy.long, when=longCross)
strategy.close('long', when=shortCross)
plotshape(plotshapePosition, style=shape.circle, color=color.green, size=size.small, location=location.absolute)
plot(nFish, color=color.green, title="Fisher")
plot(nFish[1], color=color.red, title="Trigger")
注意我还发了crossunder,只是为了退出策略。
另外我创建了一个变量来绘制一个形状,当不使用overlay=true时,这是定位所必需的。当crossovers 发生时,sp plotshapePosition 返回 na 或 nFish 值。 location.absolute 参数使用此级别来定位形状,而如果您在主图表上绘图,则不是绝对必要的。 plotshape 可以在测试您的条件是否正常工作时派上用场。
此外,如果您确实有一个条件需要手动构建,而不是使用像 crossover 这样的函数,您可以使用布尔逻辑来构建它。
longCondition = nFish > nFish[1] and nFish[1] < nFish[2]
strategy.entry("Long", strategy.long, when = longCondition)