【问题标题】:How can I use conditionals in Pine Script strategies?如何在 Pine Script 策略中使用条件?
【发布时间】:2021-03-25 23:34:21
【问题描述】:

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

Length = input(10, minval=1)
xHL2 = hl2
xMaxH = highest(xHL2, Length)
xMinL = lowest(xHL2,Length)
nValue1 = 0.33 * 2 * ((xHL2 - xMinL) / (xMaxH - xMinL) - 0.5) + 0.67 * nz(nValue1[1])
nValue2 = iff(nValue1 > .99,  .999,
            iff(nValue1 < -.99, -.999, nValue1))
nFish = 0.5 * log((1 + nValue2) / (1 - nValue2)) + 0.5 * nz(nFish[1])
plot(nFish, color=green, title="Fisher")
plot(nz(nFish[1]), color=red, title="Trigger")

据我了解转换,您对一个值执行操作,绘制该值 (nFish),然后得到您的 Fisher Line。从一根柱线 nz(nFish[1]) 中取值,绘制它,你就得到了你的触发线。当您在费希尔线和触发线之间获得交叉时,您可能有兴趣在附近进行交易。我当时的想法是,在制定策略时要实施以下内容:

"If the Fisher Line is above the Trigger Line now AND IF the Fisher Line was below the Trigger Line on the last bar, enter a long position"

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

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

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

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    首先,我不建议使用旧的源代码进行学习。您可以从任何版本开始,但我建议您不要使用任何版本,除非您在源代码顶部附近看到 //@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。这意味着第一个参数超过了第二个。熟悉crosscrossunder。因此,为了简单起见,我们可以将其调整为策略:

    //@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)
    

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2023-02-06
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2019-01-16
      • 2020-08-03
      相关资源
      最近更新 更多