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

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

Administrative
Staff member
Staff
VIP
Lifetime
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.
 

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.
 

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:

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.
 

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
 

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.
 

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.
 

henry1224

Active 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
 

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Top