Conditional Nested Boolean Statements - futures io
futures io



Conditional Nested Boolean Statements


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one serac with 2 posts (0 thanks)
    2. looks_two Fat Tails with 1 posts (4 thanks)
    3. looks_3 ktrader with 1 posts (5 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 2,587 views
    2. thumb_up 9 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 1 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Conditional Nested Boolean Statements

(login for full post details)
  #1 (permalink)
 serac 
Arizona, USA
 
Experience: Intermediate
Platform: NinjaTrader
Broker: MB Trading
Trading: Cello
 
serac's Avatar
 
Posts: 116 since Jan 2011
Thanks: 321 given, 138 received

Hi all,

This is less of a NinjaTrader specific question and more of a question on how the C# compiler works. It is a pretty nit-picky question best suited for programming experts. Let me explain my question:

I'm trying to make my strategy efficient. As with any programming language, there a lots of if/then statements. My question arises from what I can do in other languages. C# is new to me. My native tongue is Pascal and Ada. In the latter, I can do this:
 
Code
if A and B and C then
DoThis;
end if;
In this case, the compiler always evaluates A, B, and C --- even if A is false. On the other hand, one can write:
 
Code
if A and if B and if C then
DoThis;
end if;
The extra ifs added above function the same. But, if A is false, Boolean statements B and C are never evaluated. The result is the same, but is (very slightly) less computational intensive. The "and if" statement is an elegant way to write the ugly:
 
Code
if A then
if B then
if C then
DoThis;
end if;
end if;
end if;
[The "or" statement equivalent is "or else" (e.g. if A and B or else C then...). ]

Question: Can I do the same sort of thing in C# without resorting to ugly nested if/then statements?

Thanks!

Started this thread Reply With Quote

Journal Challenge April 2021 results (now extended!):
Competing for $1800 in prizes from Jigsaw
looks_oneMaking a Living with the Microsby sstheo
(107 thanks from 17 posts)
looks_twoSalao's Journalby Salao
(33 thanks from 8 posts)
looks_3Deetee’s DAX Trading Journal (time based)by Deetee
(28 thanks from 11 posts)
looks_4Learning to Profit - A journey in algorithms and optionsby Syntax
(14 thanks from 9 posts)
looks_5Maybe a little bit different journalby Malykubo
(9 thanks from 8 posts)
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Would You Sell Your System?
73 thanks
The Crude Dude Oil Trading System
44 thanks
Big Mike in Ecuador
42 thanks
The New Micro Contract - MICRO BITCOIN coming May 2021
25 thanks
futures io site changelog and issues/problem reporting
24 thanks
 
(login for full post details)
  #3 (permalink)
 Fat Tails 
Market Wizard
Berlin, Europe
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,855 since Mar 2010
Thanks: 4,238 given, 26,743 received


Good question. I think that you can do it.

I have attached two indicators in the zip file below. Both are identical versions of the SuperTrend, but there is a small difference in lines 74 and 75 of the code

The first indicator, SuperTrendErrorCode uses

 
Code
if (ChartControl.ChartStyleType == ChartStyleType.CandleStick)
	candles = true;
If you call this indicator from another indicator, you will get an error, because there is no chart and ChartControl will throw an exception in OnStartUp().

The second indicator, SuperTrendCorrectedCode uses

 
Code
if (ChartControl != null && ChartControl.ChartStyleType == ChartStyleType.CandleStick)
              candles = true;
In this case NinjaTrader will not throw an exception, because the first statement is false and the second statement is not executed.

I would conclude from this behavior that you do not need to use nested statements.

But I am clearly no expert for C#, so you may want to ask somebody else.

Attached Files
Register to download File Type: zip SuperTrendCode.zip (10.5 KB, 24 views)
Reply With Quote
The following 4 users say Thank You to Fat Tails for this post:
 
(login for full post details)
  #4 (permalink)
 ktrader 
glostrup, denmark
 
Experience: Advanced
Platform: Custom platform
Broker: CQG
Trading: Futures, Options, Stocks
 
ktrader's Avatar
 
Posts: 249 since Aug 2011
Thanks: 152 given, 275 received


serac View Post
Hi all,

This is less of a NinjaTrader specific question and more of a question on how the C# compiler works. It is a pretty nit-picky question best suited for programming experts. Let me explain my question:

I'm trying to make my strategy efficient. As with any programming language, there a lots of if/then statements. My question arises from what I can do in other languages. C# is new to me. My native tongue is Pascal and Ada. In the latter, I can do this:
 
Code
if A and B and C then
DoThis;
end if;
In this case, the compiler always evaluates A, B, and C --- even if A is false. On the other hand, one can write:
 
Code
if A and if B and if C then
DoThis;
end if;
The extra ifs added above function the same. But, if A is false, Boolean statements B and C are never evaluated. The result is the same, but is (very slightly) less computational intensive. The "and if" statement is an elegant way to write the ugly:
 
Code
if A then
if B then
if C then
DoThis;
end if;
end if;
end if;
[The "or" statement equivalent is "or else" (e.g. if A and B or else C then...). ]

Question: Can I do the same sort of thing in C# without resorting to ugly nested if/then statements?

Thanks!


C# uses short-circuit evaluation for && and ||, so it will only evaluate as much as needed to have the full result. so false && false will only evaluate the first false as it is enough to conclude the entire expression is false. true || true will only evaluate the first true as it is enough.

C# does have binary operators for and/or that evaluates all arguments, & and | these will evaluate both arguments.

Reply With Quote
The following 5 users say Thank You to ktrader for this post:
 
(login for full post details)
  #5 (permalink)
 serac 
Arizona, USA
 
Experience: Intermediate
Platform: NinjaTrader
Broker: MB Trading
Trading: Cello
 
serac's Avatar
 
Posts: 116 since Jan 2011
Thanks: 321 given, 138 received

@FatTails and @ktrader: Thanks for the reply! Looks like && is built-in to function as "and if" as I described. Makes sense, I guess. Thanks, again!

Started this thread Reply With Quote


futures io Trading Community Platforms and Indicators NinjaTrader > Conditional Nested Boolean Statements


Last Updated on November 27, 2011


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Journal Challenge w/$1,800 in prizes!

April

Seven Trading Mistakes Solved With Smart Trading Tools w/Brannigan Barrett

Elite only
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts