How to combine two if statements that require separate "else"

rottentrade

Member
From what I understand, you would use "if...if else...else" statement to combine more than one IF statement.

However, in my case, for testing purposes, I need to use 2 IF statements in a chart bubble and each of them clearly needs to state A or B . So essentially there will be 2 pairs of TRUE/FALSE. But I can't use "AND" to combine the two IFs in one line.

Here's my code:

input order = {default buy, buynhold, sell, sellnhold};
def order1 = order == order.buy or order == order.buynhold;
def order2= order == order.buynhold or order == order.sellnhold;

AddLabel(1, if order1 then “buy” else “sell” AND if order2 then “& Hold” else “”, Color.LIME);

----------------------------------
Error message:
Expected double
Wrong type cast: different types after then and else: double vs class java.lang.String
----------------------------------

However, there's no problem when I write out two separate if statements.

input order = {default buy, buynhold, sell, sellnhold};
def order1 = order == order.buy or order == order.buynhold;
def order2= order == order.buynhold or order == order.sellnhold;

AddLabel(1, if order1 then “buy” else “sell”, Color.LIME);
AddLabel(1, if order2 then “& Hold” else “”, Color.LIME);


How would I bypass this problem without using the "AND" function?
 
Last edited:
From what I understand, you would use "if...if else...else" statement to combine more than one IF statement.

However, in my case, for testing purposes, I need to use 2 IF statements in a chart bubble and each of them clearly needs to state A or B . So essentially there will be 2 pairs of TRUE/FALSE. But I can't use "AND" to combine the two IFs in one line.

Here's my code:

input order = {default buy, buynhold, sell, sellnhold};
def order1 = order == order.buy or order == order.buynhold;
def order2= order == order.buynhold or order == order.sellnhold;

AddLabel(1, if order1 then “buy” else “sell” AND if order2 then “& Hold” else “”, Color.LIME);

----------------------------------
Error message:
Expected double
Wrong type cast: different types after then and else: double vs class java.lang.String
----------------------------------

However, there's no problem when I write out two separate if statements.

input order = {default buy, buynhold, sell, sellnhold};
def order1 = order == order.buy or order == order.buynhold;
def order2= order == order.buynhold or order == order.sellnhold;

AddLabel(1, if order1 then “buy” else “sell”, Color.LIME);
AddLabel(1, if order2 then “& Hold” else “”, Color.LIME);


How would I bypass this problem without using the "AND" function?
Try using this
Ruby:
AddLabel(1, (if order1 then “buy” else “sell”) + (if order2 then “& Hold” else “”), Color.LIME);
 

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

That was too easy! :D

On the other hand, how would I use the same concept for "def" or "plot". I don't think you can use "+" other than label or chart bubble, right? Thank you.
 
something like this:

Code:
def condition_1 = if close > close[1] then 1 else 0;
def condition_2 = if close[1] > close[2] then 1 else 0;

plot buy = if condition_1 == 1 AND condition_2 == 1 then low else double.nan;

notice that there is no additional "if" after the AND. It's part of the same condition
e.g.
Code:
if (condition_1 AND condition_2) then ...

We may read it in English as two if statements, but here it's really just one with a more complicated test.

perhaps that helps, perhaps not so much.

-mashume
 
That was too easy! :D

On the other hand, how would I use the same concept for "def" or "plot". I don't think you can use "+" other than label or chart bubble, right? Thank you.

You cannot use text as output in a def or plot statement. The "+" works as a connector for text in things where text can be displayed, for example, labels, bubbles and verticallines.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
412 Online
Create Post

Similar threads

Similar threads

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