简体   繁体   中英

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...)

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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