De Morgans Law and the ! operation in ThinkOrSwim

useThinkScript

Administrative
Staff member
Staff
VIP
Lifetime
This mistake has become much more prevalent with AI-generated scripts.
It seems AI bots skipped 8th-grade algebra!

De Morgan’s Laws are essential rules in logic, set theory, and Boolean algebra that explain how negation interacts with AND and OR operations. Simply put, when you negate a combined statement, De Morgan warns us that the negation applies to each individual component, converting ANDs to ORs:

In Thinkscript, using the ToS not operation ! on a multi-condition statement flips the operations underneath in a way you might not intend.

The Problem
Consider this example:

# bear = Current candle is red AND current close is lower than previous close.
def bear = close < open and close < close[1];
plot scan = !bear;

You might think !bear means "find bars where NEITHER condition is true" (a green candle that closed higher).
But because of De Morgan's Law, !(A and B) actually evaluates as (NOT A) OR (NOT B).

As a result, !bear triggers if either single condition fails:
▸ Red candle, but higher than previous close → Triggers scan​
▸ Green candle, but lower than previous close → Triggers scan​
▸ Green candle and higher than previous close Both are False → Triggers scan​
Because it uses OR, it matches almost every bar on your chart!

The Solutions
To negate a compound statement correctly in Thinkscript, you have two options:

1. Do not use ! operation. Create a 2nd def statement with explicit conditions or
2. Evaluate the combined condition first using a binary signal which will force Thinkscript to evaluate both conditions together before applying your filter:
def bear = if close < open and close < close[1] then 1 else -1;
plot scan = bear == -1;

Clear as mud?
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
672 Online
Create Post

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.
Back
Top