Pine 脚本:IF 与 IFF
Pine Script: IF vs IFF
对我来说回到基础知识.. 似乎无法弄清楚“if”和“iff”语句之间的真正区别,而且我没有找到太多详细解释的信息。 “Iff”和“?:”具有我收集到的相同功能。
下面是一些示例代码,根据我使用的 if/iff 函数,它们的行为会有所不同。
'''
longStop = input(title="Stop", type=input.bool, defval=false)
//following works fine when passed to plot funtion
longStpTransp = iff(longStop, 0, 100)
//following does not work when passed to plot function
longStpTransp = if (longStop == true)
0
else
100
//also does not work when passed to plot function
longStpTransp = 100
if (longStop == true)
longStpTransp := 0
else
longStpTransp := 100
plot(series=(strategy.position_size > 0) ? longStopPrice : na, color=color.red,
style=plot.style_linebr, transp=longStpTransp, linewidth=4, title="Long Stop")
'''
正确。所有这些在功能上都是等价的。
if then else
iff
?:
iff
做与三元条件运算符 ?:
完全相同的事情,但采用函数式风格。
此外 iff
的效率略低于运算符 ?:
你的两个例子都正确绘制。
示例 1
//@version=4
study("IF", "IF", false)
longStop = input(title="Stop", type=input.bool, defval=false)
//following does not work when passed to plot function
longStpTransp = if (longStop == true)
0
else
100
plot(longStpTransp)
示例 2
//@version=4
study("IF", "IF", false)
longStop = input(title="Stop", type=input.bool, defval=false)
//also does not work when passed to plot function
longStpTransp = 100
if (longStop == true)
longStpTransp := 0
else
longStpTransp := 100
plot(longStpTransp)
编辑 1 以响应
这是一个有趣的观察。
plot()
函数中的transp=
参数需要为固定整数。
所以它不能是可变变量。
iff()
返回的值不被视为可变变量,但是使用if
时,它似乎被视为可变变量并被拒绝。
我自己也不知道。
我假设 iff()
和 if-then-else
是相同的。
显然不是这样。
transp (input integer) Transparency of the plot. Possible values are from 0 (not transparent) to 100 (invisible). Optional argument.
对我来说回到基础知识.. 似乎无法弄清楚“if”和“iff”语句之间的真正区别,而且我没有找到太多详细解释的信息。 “Iff”和“?:”具有我收集到的相同功能。
下面是一些示例代码,根据我使用的 if/iff 函数,它们的行为会有所不同。
'''
longStop = input(title="Stop", type=input.bool, defval=false)
//following works fine when passed to plot funtion
longStpTransp = iff(longStop, 0, 100)
//following does not work when passed to plot function
longStpTransp = if (longStop == true)
0
else
100
//also does not work when passed to plot function
longStpTransp = 100
if (longStop == true)
longStpTransp := 0
else
longStpTransp := 100
plot(series=(strategy.position_size > 0) ? longStopPrice : na, color=color.red,
style=plot.style_linebr, transp=longStpTransp, linewidth=4, title="Long Stop")
'''
正确。所有这些在功能上都是等价的。
if then else
iff
?:
iff
做与三元条件运算符 ?:
完全相同的事情,但采用函数式风格。
此外 iff
的效率略低于运算符 ?:
你的两个例子都正确绘制。
示例 1
//@version=4
study("IF", "IF", false)
longStop = input(title="Stop", type=input.bool, defval=false)
//following does not work when passed to plot function
longStpTransp = if (longStop == true)
0
else
100
plot(longStpTransp)
示例 2
//@version=4
study("IF", "IF", false)
longStop = input(title="Stop", type=input.bool, defval=false)
//also does not work when passed to plot function
longStpTransp = 100
if (longStop == true)
longStpTransp := 0
else
longStpTransp := 100
plot(longStpTransp)
编辑 1 以响应
这是一个有趣的观察。
plot()
函数中的transp=
参数需要为固定整数。
所以它不能是可变变量。
iff()
返回的值不被视为可变变量,但是使用if
时,它似乎被视为可变变量并被拒绝。
我自己也不知道。
我假设 iff()
和 if-then-else
是相同的。
显然不是这样。
transp (input integer) Transparency of the plot. Possible values are from 0 (not transparent) to 100 (invisible). Optional argument.