es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Condiciones de Pine script / Sentencias condicionales

Tengo 6 condiciones:

c1 = ...
c2 = ...

Luego, si se cumplen 4 de ellas (lo que da lugar a 15 combinaciones), ejecutaré algún comando. ¿Cómo hacer esto?

Por ejemplo:

cb1 = c1 and c2 and c3 and c4
cb2 = c1 and c2 and c3 and c5
cb3 = c1 and c2 and c3 and c6
cb4 = c1 and c2 and c4 and c5
cb5 = c1 and c2 and c4 and c6
cb6 = c1 and c2 and c5 and c6
cb7 = c1 and c3 and c4 and c5
cb8 = c1 and c3 and c4 and c6
cb9 = c1 and c3 and c5 and c6
cb10 = c1 and c4 and c5 and c6
cb11 = c2 and c3 and c4 and c5
cb12 = c2 and c3 and c4 and c6
cb13 = c2 and c3 and c5 and c6
cb14 = c2 and c4 and c5 and c6
cb15 = c3 and c4 and c5 and c6

// Configurar la alerta
alertcondition(condition=cb1 or cb2 or cb3 or cb4 or cb5 or cb6 or cb7 or cb8 or cb9 or cb10 or cb11 or cb12 or cb13 or cb14 or cb15,
     message="cb")
Tags:  , ,

Answer

  1. Avatar for davy.ai

    Para ejecutar un comando cuando se cumplan 4 de 6 condiciones, necesitamos crear todas las combinaciones posibles de 4 condiciones de las 6 disponibles. Esto se puede hacer utilizando if anidados o operadores booleanos.

    Aquí tienes un ejemplo utilizando operadores booleanos:

    cb1 = c1 y c2 y c3 y c4
    cb2 = c1 y c2 y c3 y c5
    cb3 = c1 y c2 y c3 y c6
    cb4 = c1 y c2 y c4 y c5
    cb5 = c1 y c2 y c4 y c6
    cb6 = c1 y c2 y c5 y c6
    cb7 = c1 y c3 y c4 y c5
    cb8 = c1 y c3 y c4 y c6
    cb9 = c1 y c3 y c5 y c6
    cb10 = c1 y c4 y c5 y c6
    cb11 = c2 y c3 y c4 y c5
    cb12 = c2 y c3 y c4 y c6
    cb13 = c2 y c3 y c5 y c6
    cb14 = c2 y c4 y c5 y c6
    cb15 = c3 y c4 y c5 y c6
    
    // comprobamos si al menos 4 condiciones son verdaderas
    if (sum([cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10, cb11, cb12, cb13, cb14, cb15]) >= 4)
        // ejecutamos el comando
    

    En el ejemplo anterior, creamos 15 variables booleanas cb1 a cb15 correspondientes a todas las combinaciones posibles de cumplir 4 condiciones.

    Luego utilizamos la función sum() para contar el número de valores “verdadero” entre estas condiciones. Si al menos 4 de ellas son verdaderas (lo cual corresponde a una suma de 4 o más), ejecutamos el comando.

    Nota: El comando en sí no se muestra en el ejemplo anterior, ya que depende de la aplicación específica.

Comments are closed.