繁体   English   中英

在 pine 脚本中通过具有多个“if”条件的 for 循环时出错

[英]Error when going through a for loop with multiple "if" conditions in pine script

我告诉你,我有一个以前的版本可以使用多个条件来执行一个条目,我给你举个例子:

lo0 := deltaCloseMa0>=lo0CloseMa0Thresold and rsi>=lo0RsiThreshold
lo1 := deltaCloseMa0>=lo1CloseMa0Thresold and rsi>=lo1RsiThreshold and deltaCloseOpen>=lo1CloseOpenThreshold 
lo2 := ....
lo3 := ....
lo4 := ....
....
lo25 := ....

问题来自于代码已经大幅增长,现在不可能继续将条件添加到无穷大,因为它不可扩展。

我想到了一个可能的解决方案,它处理一个一个地评估条件,然后如果满足所有启用的条件,则执行输入。

我放了我写的代码片段,但我无法让它编译脚本。

var entryOrderConditions = false, constructorMa0Cond = false, constructorRsiCond = false
var orderBuilder = array.new_bool(0)
if orderType    // If orderType is enable, then evaluate the conditions for long .. if disable, for short
    // LONG
    if barstate.isconfirmed and strategy.position_size == 0
        // ## Setup constructor
        if enableCloseToMa0
            if deltaCloseMa0 >= ma0CloseThreshold
                array.push(orderBuilder, true)
            else
                array.push(orderBuilder, false)
        if enableRsi
            if rsi >= rsiThreshold
                array.push(orderBuilder, true)
            else
                array.push(orderBuilder, false)
        
        for signal in orderBuilder
            if signal
                entryOrderConditions := true
            else
                entryOrderConditions := false
                break

问题是相同的条件并不总是启用,容差阈值也不总是相同的,所以它们在上面用输入动态化(bool,int,float ...)

所以我必须寻找一个灵活的系统,它允许我评估尽可能多的条件,并且具有足够的可扩展性,不会在代码中定义 100 种输入类型。

非常感谢您的帮助。

肮脏的解决方案:

我试过各种类型的 arrays,用各种方式遍历它们,用不同的检查,我什至寻找一个“开关”来过滤......但没有任何效果。

最后我找到了一个可行的解决方案,它不是世界上最优雅和最干净的代码,但它是功能性的,我认为我无法改进它。

如果有人读到这篇文章并认为有更好的方法,我愿意接受建议。

自己创作的一点哲学,

有时,最好的解决方案并不是最令人愉快的

即便如此,它仍然比开始时更具可扩展性。

float constructorMa0Cond = na
float constructorRsiCond = na
if orderType
    // LONG
    if barstate.isconfirmed and strategy.position_size == 0
        // ## Setup constructor
        if enableCloseToMa0
            if deltaCloseMa0 >= ma0CloseThreshold
                constructorMa0Cond := 1
            else
                constructorMa0Cond := 0
        if enableRsi
            if rsi >= rsiThreshold
                constructorRsiCond := 1
            else
                constructorRsiCond := 0

        for i = 0 to 1
            if i == 0
                if not na(constructorMa0Cond)
                    if constructorMa0Cond
                        entryOrderConditions := true
                    else
                        entryOrderConditions := false
                        break
            else if i == 1
                if not na(constructorRsiCond)
                    if constructorRsiCond
                        entryOrderConditions := true
                    else
                        entryOrderConditions := false
                        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM