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")
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:
En el ejemplo anterior, creamos 15 variables booleanas
cb1
acb15
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.