【问题标题】:How would you translate this Pine Script nested loop into Python?你会如何将这个 Pine Script 嵌套循环翻译成 Python?
【发布时间】:2020-12-20 13:00:23
【问题描述】:

我是 Pine Script 的新手,我一直在努力理解 if/else Pine Script 运算符背后的逻辑。我可以理解所有可以在网上找到的简单示例,但是当涉及到我想要翻译成 Python 的特定代码时,就变得更加困难了。这是我很难理解的嵌套循环:

hi:=hi[1]?high[1]>=stop[1]?false:true:low[1]<=stop[1]?true:false
    stop:=hi?max1:min1
    stop:=hi?hi[1]==false?stop:stop>stop[1]?stop[1]:stop:hi[1]?stop:stop<stop[1]?stop[1]:stop

正如我之前提到的,我的目标是将这段 sn-p 代码翻译成 Python。谢谢大家帮忙!

附:这是 Pine Script v4

【问题讨论】:

  • 您应该尝试扩展答案,以便 ?: 在不同的行上——这会给您一个更好的主意。一个 ? b : c 表示“如果 a 为真,则返回 b,否则返回 c”

标签: python pine-script


【解决方案1】:

您可以在this website 上将任何ternary (?:) 构造转换为if-then-else
[1] 不表示数组,而是history referencing operator
有了它,您就可以访问以前柱上的值。

第一个例子

hi:=hi[1]?high[1]>=stop[1]?false:true:low[1]<=stop[1]?true:false

变成

hi :=
if (hi[1]) {
    if (high[1] >=stop[1]) {
        false
    } else {
        true
    }
} else {
    if (low[1] <=stop[1]) {
        true
    } else {
        false
    }
}

第二个例子

stop:=hi?max1:min1

变成

stop:=
if (hi) {
    max1
} else {
    min1
}

第三个例子

stop:=hi?hi[1]==false?stop:stop>stop[1]?stop[1]:stop:hi[1]?stop:stop<stop[1]?stop[1]:stop

变成

stop := 
if (hi) {
    if (hi[1]==false) {
        stop
    } else {
        if (stop>stop[1]) {
            stop[1]
        } else {
            stop
        }
    }
} else {
    if (hi[1]) {
        stop
    } else {
        if (stop<stop[1]) {
            stop[1]
        } else {
            stop
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2020-02-24
    • 2020-04-17
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多