Pine script 如何在Pine脚本策略中使用条件语句?

Pine script 如何在Pine脚本策略中使用条件语句?,pine-script,Pine Script,我正在尝试学习Pine脚本,对于我的第一个项目,我希望基于以下代码构建一个简单的Fisher指标策略(用户HPotter在TradingView上公开提供): 我试图通过以下方式实施: if (nFish > nz(nFish[1])) if (nz(nFish[1]) < nz(nFish[2])) strategy.entry("Long", strategy.long) if(nFish>nz(nFish[1])) 如果(nz(nF

我正在尝试学习Pine脚本,对于我的第一个项目,我希望基于以下代码构建一个简单的Fisher指标策略(用户HPotter在TradingView上公开提供):

我试图通过以下方式实施:

if (nFish > nz(nFish[1]))
    if (nz(nFish[1]) < nz(nFish[2]))
        strategy.entry("Long", strategy.long)
if(nFish>nz(nFish[1]))
如果(nz(nFish[1])

但是,此代码无法生成任何位置条目,对if语句中的值的任何修改也无法生成任何输出。如何修复我的条件以实现我想要测试的想法?

首先,我不建议使用那么旧的源代码进行学习。您可以从任何版本开始,但我建议不要使用任何版本,除非您在源代码顶部附近看到
/@version=4
。我刚开始的时候犯了这个错误,修改了一个旧脚本,并在这个过程中学习了一个过时的版本

所以这里我修改了V4的脚本,请注意更改。一些语法,并用var关键字声明变量以初始化某些变量。在V4中,在声明中使用变量时不能声明变量。此外,我用三元替换了iff的
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")
注:我还在
下做了一个
交叉,只是为了退出策略

此外,我还制作了一个变量来绘制一个形状,这在不使用
overlay=true
时是定位所必需的。Sp发生交叉时,plotshapePosition返回na或nFish值。
location.absolute
参数使用此级别来定位形状,而如果在主图表上绘图,则不必严格执行此级别<代码>绘图形状
在测试条件是否正常时非常方便

此外,如果您确实有需要手动构建的条件,而不是使用像
交叉
这样的函数,则可以使用布尔逻辑来构建它

longCondition = nFish > nFish[1] and nFish[1] < nFish[2]
strategy.entry("Long", strategy.long, when = longCondition)
longCondition=nFish>nFish[1]和nFish[1]
//@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")
//@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")
longCondition = nFish > nFish[1] and nFish[1] < nFish[2]
strategy.entry("Long", strategy.long, when = longCondition)