简体   繁体   English

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

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

I'm telling you, I had a previous version that worked with multiple conditions to execute an entry, I'll give you an example:我告诉你,我有一个以前的版本可以使用多个条件来执行一个条目,我给你举个例子:

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

The problem comes from the fact that the code has grown substantially and now it is impossible to continue adding conditions to infinity, because it is not scalable.问题来自于代码已经大幅增长,现在不可能继续将条件添加到无穷大,因为它不可扩展。

A possible solution has occurred to me, which deals with evaluating the conditions one by one and then if all the ones that are enabled are met, executing the input.我想到了一个可能的解决方案,它处理一个一个地评估条件,然后如果满足所有启用的条件,则执行输入。

I put the code fragment that I have written but I am not able to make it compile the script.我放了我写的代码片段,但我无法让它编译脚本。

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

The question is that the same conditions are not always enabled, nor is the tolerance threshold always the same, so they are dynamized above with inputs (bool, int, float...)问题是相同的条件并不总是启用,容差阈值也不总是相同的,所以它们在上面用输入动态化(bool,int,float ...)

So I have to look for a flexible system that allows me to evaluate as many conditions as I want and is scalable enough not to have 100 input types defined in the code.所以我必须寻找一个灵活的系统,它允许我评估尽可能多的条件,并且具有足够的可扩展性,不会在代码中定义 100 种输入类型。

Thank you very much for your help.非常感谢您的帮助。

Dirty solution:肮脏的解决方案:

I have tried with various types of arrays, with various ways of traversing them, with different checks, and I have even looked for a "switch" to filter... But nothing worked.我试过各种类型的 arrays,用各种方式遍历它们,用不同的检查,我什至寻找一个“开关”来过滤......但没有任何效果。

In the end I have found a solution that works, it is not the most elegant and cleanest code in the world but, it is functional, and I think I can't improve it.最后我找到了一个可行的解决方案,它不是世界上最优雅和最干净的代码,但它是功能性的,我认为我无法改进它。

I'm open to suggestions if anyone reads this and thinks there's a better way to do it.如果有人读到这篇文章并认为有更好的方法,我愿意接受建议。

A little philosophy of own creation,自己创作的一点哲学,

Sometimes, the best solution is not the most pleasant有时,最好的解决方案并不是最令人愉快的

Even so, it is still more scalable than at the beginning.即便如此,它仍然比开始时更具可扩展性。

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