NexusFi: Find Your Edge


Home Menu

 





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 3,176 views
    2. thumb_up 9 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 1 attachments




 
Search this Thread

Conditional Nested Boolean Statements

  #1 (permalink)
 
serac's Avatar
 serac 
Arizona, USA
 
Experience: Intermediate
Platform: NinjaTrader
Broker: MB Trading
Trading: Cello
Posts: 116 since Jan 2011
Thanks Given: 321
Thanks Received: 138

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

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
Increase in trading performance by 75%
The Elite Circle
REcommedations for programming help
Sierra Chart
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
How to apply profiles
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
31 thanks
Spoo-nalysis ES e-mini futures S&P 500
28 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
17 thanks
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


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
Elite Membership required to download: SuperTrendCode.zip
Reply With Quote
  #4 (permalink)
 
ktrader's Avatar
 ktrader 
glostrup, denmark
 
Experience: Advanced
Platform: Custom platform
Broker: CQG
Trading: Futures, Options, Stocks
Posts: 249 since Aug 2011
Thanks Given: 152
Thanks Received: 275


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
  #5 (permalink)
 
serac's Avatar
 serac 
Arizona, USA
 
Experience: Intermediate
Platform: NinjaTrader
Broker: MB Trading
Trading: Cello
Posts: 116 since Jan 2011
Thanks Given: 321
Thanks Received: 138

@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




Last Updated on November 27, 2011


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts