How to say if 3 out 5 conditions are true show signal?

S

strategynode

New member
Hi,
I am having trouble figuring out whats the most efficient way to code the following.

For example:
we have defined 4 conditions now I want to show a signal when 3 out of 4 conditions are true. I was thinking if statements but couldn't figure how the logic will look like because the order of the conditions being true in not linear.

Thank You.
 
BenTen

BenTen

Administrative
Staff
VIP
You can use and / or condition statement to filter them out.

Example 1: cond1 and cond2 and cond3
Example 2: cond1 and cond2 and cond4
Example 3: cond1 and cond2 and cond3 or cond1 and cond2 and cond4

Something like that should work.
 
S

shanc

New member
Hi, I'm stuck with something similar. Any helping hand will be much appreciated.
I have 5 Conditions. If (Cond1 or Cond2) then Cond3, Cond4, Cond5 need to fulfill.
Cond1 or Cond2 will trigger the starting point but all the other 3 conditions will be coming from different bars.
How would the code be for a signal to fire once the final condition is fulfilled? Much thanks.
 
BenTen

BenTen

Administrative
Staff
VIP
@shanc Would be much better if you already have a script in the work. If you do, post it and we'll take a look.
 
C

codydog

Active member
I do something similar, but use assignvaluecolor to show me - eg, if cond1 + cond2 then color.yellow etc
 
L

Len20

Member
Something like this?
Code:
def cond1 = if (condition met) then 1 else 0;
def cond2 = if (condition met) then 1 else 0;
def cond3 = if (condition met) then 1 else 0;
def cond4 = if (condition met) then 1 else 0;
def cond5 = if (condition met) then 1 else 0;

def totalCond = cond1 + cond2 + cond3 + cond4 + cond5;

def signal = totalCond >= 3;
 
Last edited:
S

strategynode

New member
@Len20 This makes it look a lot cleaner. Thank you I will try this logic if I add anything to it I will post.
 
S

shanc

New member
Thanks to everyone replying. I'll explain my request due to restrictions of me not being able to post the codes. I have 4 Signals. Each buy Signal is from 4 different indicators that I've combined. 3 of them are proprietary and I have no permission to post them. Sorry about that everyone and I understand if help is not provided due to lack of information. Here goes.

Each signal are Buy Signal from respective indicators;
Signal1= Indicator A
Signal2 = Indicator B
Signal3 = Indicator C
Signal4 = Indicator D

If Signal1 then Signal2, Signal3, Signal4 need to fulfill for a Final Signal. The condition must start with Signal1, Signal2-4 doesn't need to be in sequence.
Once Signal1 triggers, all the other 3 Signals might be coming from the same bar or different upcoming bars. This is where I'm stuck.

Example:
Indicator A(Signal1) triggers at bar X1,
Signal2 at bar X3,
Signal3 at bar X5,
Signal4 at bar X5,
The Final Signal will trigger at the close of bar X5 since it has now fulfilled all the conditions. The reset will be triggered by Signal1 if it appears anywhere in between and it will be treated as the new starting point.

My unsuccessful attempt:
plot finalsignal = if signal1 and signal2 and signal3 and signal4 then 1 else 0;

This ends up on finding a signal if all condition is met on the first bar X1. 😅. Thank you so much knowledgeable Thinkscripters and helping me learn to code. If answered this can help us combine a few indicators for solid final confirmation. Good day, all.
 
C

codydog

Active member
@shanc - try using assignpricecolor(if signal1 then color.white if signal1 and signal2 then color.green etcetc );

using colors on the bars will tell you exactly where you are in your triggering process

But I do lolol at "we have a secret sauce", I can almost guarantee you don't. Try googling renaissance tech, virtu etc
 
S

shanc

New member
Thanks @codydog, I'll try with that. Hehe, and I agree with the secret sauce. I'm learning to code so I thought of combining my everyday scripts. Easier on the eyes. Have a nice day @codydog.
 
L

Len20

Member
@shanc
How many bars in the future or past are the signals for signal2, signal3, and signal 4?
 
S

shanc

New member
After Signal1, most of the time all the other Signals will come into place within 3-10bars. If it doesn't, very likely a new Signal1 will trigger. Thank you @Len20 .
 
L

Len20

Member
That sounds a little beyond what I can do, but I think this might be the general logic to give you a starting point
Code:
def finalsignal = if signal1 then if signal2 and signal3 and signal4 then 1 else 0 else 0;
If your code determines how may bars in the future to look for signal2, in the code above your would place that number as a negative in brackets. For example signal2[-5] will pull the value of signal 2 from 5 bars in the future. I imagine you would have a variable determine how far ahead to grab the value from, so would multiply the variable by -1 and put that in the brackets.
 
S

shanc

New member
Hi @Len20, I decided to just combine the last 3signals with the help of your Post#7 and treat the Signal1 from Indicator A separately. Thanks to you, it works as I wanted. Hopefully, I'll be able to try out the Assignpricecolor suggested by @codydog one of these days. I will update if I succeed. A great day to you both.
 
H

henry1224

Member
VIP
Hi, I'm stuck with something similar. Any helping hand will be much appreciated.
I have 5 Conditions. If (Cond1 or Cond2) then Cond3, Cond4, Cond5 need to fulfill.
Cond1 or Cond2 will trigger the starting point but all the other 3 conditions will be coming from different bars.
How would the code be for a signal to fire once the final condition is fulfilled? Much thanks.
add the conditions

(Cond1 + Cond2 + Cond3 + Cond4 + Cond5) >3

shows when at least 4 of those conditions are true
 

Similar threads

Top