How to code whenever any 3 conditions out of 5 are met?

I have 5 buy conditions and I want whenever any three conditions met buy should happen i.e:

buy= whenever any3 conditions out of the above 5 met

so, please help me achieve it.

@santy try this if you want a "state" signal

Buy = (buycondition1 + buycondition2 + buycondition3 + buycondition4 + buycondition5) >=3;

Or if you want an "impulse" signal,

SumOfSignals = buycondition1 + buycondition2 + buycondition3 + buycondition4 + buycondition5;
Buy = Cross(SumOfSignals,2);

Thanks a lot for your reply.

I just posted this query again in error. moderator please do not allow it .

could you please briefly explain what do you mean by state and impulse signal?

Secondly, should the "2" in the impulse code be 3 if I want any 3 conditions to be met for signals or what else it is?

Please guide.

No, it is 2.
When crossed 2 , you get 3 or more ( when the increment is by 1++ ,of course) .

1 Like

@santy the BUY signal you are attempting to create can be coded so that you have a BUY only on the first bar where your signal is triggered (an IMPULSE signal), or you can have your signal show up on every bar that it remains true (a STATE signal).

Or graphically,

As for your second question about crossing above your trigger threshold you should familiarize yourself with the functions used in the code.

As @NowToLook wrote, your buyconditions will add up in increments of one, from 1-5. To reach your trigger (3) from below your trigger you need the sum of your buyconditions to equal 3 or greater, which means to CROSS above 2.

P.S. IMHO it is a good idea to create an Exploration and/or a chart to help illustrate what your variables are calculating.


Thanks for your beautiful explanation!!!


I am using a combination of Impulse and State signals in one of my AFL but I've run into some issues. Can you please look into it?

Buy = (buycondition1 + buycondition2 + buycondition3 + buycondition4 + buycondition5) >=3;

I applied the above format , but I am not getting the signals correctly. should I apply the 2nd format or any thing else to be done?

Please advise.

please give a start or redirect me to generate signals(buy / sell) when any 3 out of 5 conditions met.


buy cond1=.....;
buy cond2=.....;
buy cond3=.....;
buy cond4=.....;
buy cond5=.....;

Buy= whenever any 3 out of the above 5 conditions are met;

I posted for this earlier some 3/4 days ago but that post is not there at all....any clues please?

Please help.

Buy = sum(cond1 + cond2 + cond3 + cond4 + cond5) >= 3 ;

I can see your previous post in the link:

Your code above was very helpful thank you.
Please can you let me know how to code for 3 out of 5 averages above a fixed value,.
For example you are using 5 EMA's on RSI and you want to code a buy condition to be valid only if 3 out of these 5 EMA's are above 45.

@Vikas.sood your question seem identical to the original post on this thread. So the answer is exactly the same too,

If your question is really about coding a moving average above a certain threshold, show us your attempted code and let us know where you are having difficulty.